@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 221 lines 7.41 KB
Raw
1 import * as Stripe from "@alchemy.run/stripe";
2 import { isMissingStripeResource } from "@alchemy.run/stripe/missing";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import { Credentials } from "@distilled.cloud/stripe";
6 import { GetFileLink, GetFiles, CreateFile } from "@distilled.cloud/stripe/stripe";
7 import * as Provider from "alchemy/Provider";
8 import * as Effect from "effect/Effect";
9 import { MinimumLogLevel } from "effect/References";
10 import * as Result from "effect/Result";
11 import * as Schedule from "effect/Schedule";
12
13 const { test } = Test.make({ providers: Stripe.providers() });
14
15 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
16
17 const FILES_API_BASE_URL = "https://files.stripe.com";
18
19 /** 1×1 PNG — Stripe accepts PNG for `dispute_evidence`. */
20 const PNG_1X1 = new Uint8Array([
21 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
22 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53,
23 0xde, 0x00, 0x00, 0x00, 0x0c, 0x49, 0x44, 0x41, 0x54, 0x08, 0xd7, 0x63, 0xf8, 0xcf, 0xc0, 0x00,
24 0x00, 0x00, 0x03, 0x00, 0x01, 0x18, 0xdd, 0x8d, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e,
25 0x44, 0xae, 0x42, 0x60, 0x82,
26 ]);
27
28 /** 2030-03-17 — Stripe rejects expiries more than five years out. */
29 const EXPIRES_AT = 1_900_000_000;
30 /** 2030-11-01 */
31 const EXPIRES_AT_UPDATED = 1_920_000_000;
32
33 const isMissing = isMissingStripeResource;
34
35 const waitUntilExpired = (id: string) =>
36 GetFileLink({ link: id }).pipe(
37 Effect.map((link) => (link.expired ? ("expired" as const) : ("active" as const))),
38 Effect.catchIf(isMissing, () => Effect.succeed("gone" as const)),
39 Effect.repeat({
40 schedule: Schedule.spaced("1 second"),
41 until: (status) => status === "expired" || status === "gone",
42 times: 10,
43 }),
44 );
45
46 const FILE_PURPOSE = "dispute_evidence" as const;
47 const FILE_NAME = "alchemy-filelink.png";
48
49 const postFile = (file: File) =>
50 Effect.gen(function* () {
51 const resolve = yield* Credentials;
52 const creds = yield* resolve;
53 return yield* CreateFile({
54 file: file as unknown as string,
55 purpose: FILE_PURPOSE,
56 }).pipe(
57 Effect.provideService(
58 Credentials,
59 Effect.succeed({
60 apiKey: creds.apiKey,
61 apiBaseUrl: FILES_API_BASE_URL,
62 }),
63 ),
64 );
65 });
66
67 /**
68 * Files cannot be deleted. Reuse a prior Alchemy-uploaded PNG when present;
69 * otherwise upload a 1×1 PNG via distilled. Do not reuse arbitrary account
70 * files — Stripe-generated files often reject `file_links` creates.
71 */
72 const ensureFile = Effect.gen(function* () {
73 const listed = yield* GetFiles({
74 purpose: FILE_PURPOSE,
75 limit: 100,
76 });
77 const existing = listed.data.find((file) => file.filename === FILE_NAME);
78 if (existing !== undefined) return existing;
79 const png = yield* Effect.sync(() => new File([PNG_1X1], FILE_NAME, { type: "image/png" }));
80 return yield* postFile(png);
81 });
82
83 test.provider(
84 "file upload probe",
85 (stack) =>
86 Effect.gen(function* () {
87 yield* stack.destroy();
88
89 const result = yield* ensureFile.pipe(Effect.result);
90 if (Result.isSuccess(result)) {
91 expect(result.success.id).toMatch(/^file_/);
92 } else {
93 expect(["InvalidRequestError", "Forbidden", "Unauthorized", "BadRequest"]).toContain(
94 result.failure._tag,
95 );
96 }
97
98 yield* stack.destroy();
99 }).pipe(logLevel),
100 {
101 tags: ["provider:stripe", "provider:stripe:filelink", "live"],
102 timeout: 60_000,
103 },
104 );
105
106 test.provider(
107 "create, update, and expire a file link",
108 (stack) =>
109 Effect.gen(function* () {
110 yield* stack.destroy();
111
112 const file = yield* ensureFile;
113
114 const created = yield* stack.deploy(
115 Effect.gen(function* () {
116 return yield* Stripe.FileLink("ReportLink", {
117 file: file.id,
118 metadata: { kind: "report" },
119 });
120 }),
121 );
122
123 expect(created.id).toMatch(/^link_/);
124 expect(created.file).toEqual(file.id);
125 expect(created.expired).toEqual(false);
126 expect(created.expiresAt).toBeUndefined();
127 expect(created.url).toEqual(expect.any(String));
128 expect(created.metadata).toMatchObject({ kind: "report" });
129 expect(created.created).toEqual(expect.any(Number));
130 expect(created.livemode).toEqual(false);
131
132 const fetched = yield* GetFileLink({ link: created.id });
133 expect(fetched.id).toEqual(created.id);
134 expect(typeof fetched.file === "string" ? fetched.file : fetched.file.id).toEqual(file.id);
135 expect(fetched.expired).toEqual(false);
136 expect(fetched.metadata?.kind).toEqual("report");
137 expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stack]).toBeDefined();
138 expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.stage]).toBeDefined();
139 expect(fetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined();
140
141 const updated = yield* stack.deploy(
142 Effect.gen(function* () {
143 return yield* Stripe.FileLink("ReportLink", {
144 file: file.id,
145 expiresAt: EXPIRES_AT,
146 metadata: { kind: "report", env: "test" },
147 });
148 }),
149 );
150
151 expect(updated.id).toEqual(created.id);
152 expect(updated.file).toEqual(file.id);
153 expect(updated.expired).toEqual(false);
154 expect(updated.expiresAt).toEqual(EXPIRES_AT);
155 expect(updated.metadata).toEqual({ kind: "report", env: "test" });
156
157 const refetched = yield* GetFileLink({ link: updated.id });
158 expect(refetched.expires_at).toEqual(EXPIRES_AT);
159 expect(refetched.metadata?.kind).toEqual("report");
160 expect(refetched.metadata?.env).toEqual("test");
161 expect(refetched.metadata?.[Stripe.alchemyMetadataKeys.id]).toBeDefined();
162
163 const extended = yield* stack.deploy(
164 Effect.gen(function* () {
165 return yield* Stripe.FileLink("ReportLink", {
166 file: file.id,
167 expiresAt: EXPIRES_AT_UPDATED,
168 metadata: { kind: "invoice" },
169 });
170 }),
171 );
172
173 expect(extended.id).toEqual(created.id);
174 expect(extended.expiresAt).toEqual(EXPIRES_AT_UPDATED);
175 expect(extended.metadata).toEqual({ kind: "invoice" });
176
177 yield* stack.destroy();
178
179 const expired = yield* waitUntilExpired(created.id);
180 expect(expired).toEqual("expired");
181 }).pipe(logLevel),
182 {
183 tags: ["provider:stripe", "provider:stripe:filelink", "live"],
184 timeout: 120_000,
185 },
186 );
187
188 test.provider(
189 "list enumerates the deployed file link",
190 (stack) =>
191 Effect.gen(function* () {
192 yield* stack.destroy();
193
194 const file = yield* ensureFile;
195
196 const deployed = yield* stack.deploy(
197 Effect.gen(function* () {
198 return yield* Stripe.FileLink("ListLink", {
199 file: file.id,
200 metadata: { kind: "list" },
201 });
202 }),
203 );
204
205 const provider = yield* Provider.findProvider(Stripe.FileLink);
206 const all = yield* provider.list();
207 const found = all.find((link) => link.id === deployed.id);
208 expect(found).toBeDefined();
209 expect(found?.file).toEqual(file.id);
210 expect(found?.metadata).toMatchObject({ kind: "list" });
211
212 yield* stack.destroy();
213
214 const expired = yield* waitUntilExpired(deployed.id);
215 expect(expired).toEqual("expired");
216 }).pipe(logLevel),
217 {
218 tags: ["provider:stripe", "provider:stripe:filelink", "live"],
219 timeout: 120_000,
220 },
221 );