@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 503 lines 19 KB
Raw
1 import { createHash } from "node:crypto";
2 import * as Cloudflare from "@alchemy.run/cloudflare";
3 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
4 import { CloudflareApiLive } from "@alchemy.run/cloudflare/Providers";
5 import { expect } from "@alchemy.run/test";
6 import * as Test from "@alchemy.run/test/Alchemy";
7 import * as r2 from "@distilled.cloud/cloudflare/r2";
8 import * as workers from "@distilled.cloud/cloudflare/workers";
9 import * as Data from "effect/Data";
10 import * as Effect from "effect/Effect";
11 import * as Exit from "effect/Exit";
12 import * as HttpClient from "effect/http/HttpClient";
13 import * as HttpClientRequest from "effect/http/HttpClientRequest";
14 import type * as HttpClientResponse from "effect/http/HttpClientResponse";
15 import { MinimumLogLevel } from "effect/References";
16 import * as Schedule from "effect/Schedule";
17 import Stack from "./fixtures/stack.ts";
18
19 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
20 providers: Cloudflare.providers(),
21 state: Cloudflare.state(),
22 });
23
24 const HOOK_TIMEOUT = 120_000;
25 const TEST_TIMEOUT = 120_000;
26
27 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
28
29 class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{
30 status: number;
31 body: string;
32 }> {}
33
34 // Bounded spaced schedule — caps total wait so a genuine failure
35 // surfaces fast instead of an uncapped exponential blowing past the
36 // test timeout while riding out cold-start propagation.
37 const ready = Schedule.max([Schedule.spaced("5 seconds"), Schedule.recurs(10)]);
38
39 /** Retry an HTTP call until it returns 200 (rides out cold-start 404s). */
40 const untilOk = <E, R>(eff: Effect.Effect<HttpClientResponse.HttpClientResponse, E, R>) =>
41 eff.pipe(
42 Effect.flatMap((res) =>
43 res.status === 200
44 ? Effect.succeed(res)
45 : res.text.pipe(
46 Effect.flatMap((body) => Effect.fail(new WorkerNotReady({ status: res.status, body }))),
47 ),
48 ),
49 Effect.retry({
50 while: (e): e is WorkerNotReady => e instanceof WorkerNotReady,
51 schedule: ready,
52 }),
53 );
54
55 class ValueMismatch extends Data.TaggedError("ValueMismatch")<{
56 expected: string;
57 actual: string | null;
58 }> {}
59
60 const retryMismatch = <A, E, R>(eff: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>
61 eff.pipe(
62 Effect.retry({
63 while: (e: E) => e instanceof ValueMismatch,
64 schedule: ready,
65 }),
66 );
67
68 /** GET `${base}/get?key=` and retry until the value matches (read-after-write propagation). */
69 const expectValue = (base: string, key: string, expected: string) =>
70 untilOk(HttpClient.get(`${base}/get?key=${encodeURIComponent(key)}`)).pipe(
71 Effect.flatMap((res) => res.json),
72 Effect.flatMap((body) => {
73 const actual = (body as { value: string | null }).value;
74 return actual === expected
75 ? Effect.succeed(actual)
76 : Effect.fail(new ValueMismatch({ expected, actual }));
77 }),
78 retryMismatch,
79 );
80
81 /** GET `/get` and retry until the object is gone (`value === null`). */
82 const expectMissing = (base: string, key: string) =>
83 untilOk(HttpClient.get(`${base}/get?key=${encodeURIComponent(key)}`)).pipe(
84 Effect.flatMap((res) => res.json),
85 Effect.flatMap((body) => {
86 const actual = (body as { value: string | null }).value;
87 return actual === null
88 ? Effect.succeed(null)
89 : Effect.fail(new ValueMismatch({ expected: "<missing>", actual }));
90 }),
91 retryMismatch,
92 );
93
94 /** HEAD-equivalent: `/head` returns `{ exists, size }` (metadata only, no body). */
95 const headObject = (base: string, key: string) =>
96 untilOk(HttpClient.get(`${base}/head?key=${encodeURIComponent(key)}`)).pipe(
97 Effect.flatMap((res) => res.json),
98 Effect.map((body) => body as { exists: boolean; size: number | null }),
99 );
100
101 /** `/list?prefix=` and retry until `key` appears (list is eventually consistent). */
102 const expectListed = (base: string, prefix: string, key: string) =>
103 untilOk(HttpClient.get(`${base}/list?prefix=${encodeURIComponent(prefix)}`)).pipe(
104 Effect.flatMap((res) => res.json),
105 Effect.flatMap((body) => {
106 const keys = (body as { keys: string[] }).keys;
107 return keys.includes(key)
108 ? Effect.succeed(keys)
109 : Effect.fail(new ValueMismatch({ expected: key, actual: keys.join(",") }));
110 }),
111 retryMismatch,
112 );
113
114 const put = (base: string, key: string, value: string) =>
115 untilOk(
116 HttpClient.execute(
117 HttpClientRequest.put(`${base}/put?key=${encodeURIComponent(key)}`).pipe(
118 HttpClientRequest.bodyText(value),
119 ),
120 ),
121 );
122
123 /** PUT `/put-stream?key=&sha256=`: the body is streamed into R2 under a declared hash. */
124 const putStream = (base: string, key: string, body: string, sha256: string) =>
125 HttpClient.execute(
126 HttpClientRequest.put(
127 `${base}/put-stream?key=${encodeURIComponent(key)}&sha256=${sha256}`,
128 ).pipe(HttpClientRequest.bodyText(body)),
129 );
130
131 const del = (base: string, key: string) =>
132 untilOk(
133 HttpClient.execute(
134 HttpClientRequest.make("DELETE")(`${base}/del?key=${encodeURIComponent(key)}`),
135 ),
136 );
137
138 const delMany = (base: string, keys: string[]) =>
139 untilOk(
140 HttpClient.execute(
141 HttpClientRequest.make("DELETE")(
142 `${base}/del-many?keys=${encodeURIComponent(keys.join(","))}`,
143 ),
144 ),
145 );
146
147 const putCopySource = (base: string, key: string, value: string) =>
148 untilOk(
149 HttpClient.execute(
150 HttpClientRequest.put(`${base}/copy-source?key=${encodeURIComponent(key)}`).pipe(
151 HttpClientRequest.bodyText(value),
152 ),
153 ),
154 );
155
156 const copyStream = (
157 base: string,
158 source: string,
159 key: string,
160 options: Record<string, string> = {},
161 ) =>
162 HttpClient.execute(
163 HttpClientRequest.post(
164 `${base}/copy-stream?${new URLSearchParams({ source, key, ...options })}`,
165 ),
166 );
167
168 const readCopy = (base: string, key: string) =>
169 untilOk(HttpClient.get(`${base}/copy-readback?key=${encodeURIComponent(key)}`)).pipe(
170 Effect.flatMap((response) => response.json),
171 Effect.map(
172 (body) =>
173 body as {
174 exists: boolean;
175 size: number;
176 bytes: number[];
177 httpMetadata: { contentType: string; cacheControl: string };
178 customMetadata: { purpose: string; label: string };
179 },
180 ),
181 );
182
183 const expectCopy = (base: string, key: string, bytes: Uint8Array, source: string) =>
184 Effect.gen(function* () {
185 const object = yield* readCopy(base, key);
186 expect(object.exists).toBe(true);
187 expect(object.size).toBe(bytes.byteLength);
188 expect(object.bytes).toEqual(Array.from(bytes));
189 expect(object.httpMetadata).toMatchObject({
190 contentType: "application/octet-stream",
191 cacheControl: "max-age=60",
192 });
193 expect(object.customMetadata).toEqual({ purpose: "stream-copy", label: source });
194 });
195
196 /**
197 * Drive every client method through `fetch`: `put` → `get` → `head` →
198 * `list` → `delete` (single) → `delete` (batch), reading back through
199 * `readBase` and writing through `writeBase`. All workers share one bucket,
200 * so keys are namespaced by `label` to keep the four runs independent.
201 */
202 const post = (base: string, path: string, key: string) =>
203 untilOk(
204 HttpClient.execute(HttpClientRequest.post(`${base}/${path}?key=${encodeURIComponent(key)}`)),
205 );
206
207 const exercise = (
208 label: string,
209 writeBase: string,
210 readBase: string,
211 /** Multipart upload is only supported over the native binding. */
212 multipart: boolean,
213 ) =>
214 Effect.gen(function* () {
215 const prefix = `${label}/`;
216 const k1 = `${prefix}k1`;
217 const v1 = `${label}-value`;
218
219 // put + get (read-after-write)
220 expect((yield* put(writeBase, k1, v1)).status).toBe(200);
221 expect(yield* expectValue(readBase, k1, v1)).toBe(v1);
222
223 // head — metadata reflects the written object
224 const meta = yield* headObject(readBase, k1);
225 expect(meta.exists).toBe(true);
226 expect(meta.size).toBe(new TextEncoder().encode(v1).length);
227
228 // list — the key shows up under its prefix
229 expect(yield* expectListed(readBase, prefix, k1)).toContain(k1);
230
231 // delete (single) — head/get then report it gone
232 yield* del(writeBase, k1);
233 yield* expectMissing(readBase, k1);
234 expect((yield* headObject(readBase, k1)).exists).toBe(false);
235
236 // delete (missing key) — idempotent in BOTH implementations: the native
237 // binding resolves on a key that was never there, so the HTTP client must
238 // not surface R2's `NoSuchKey` either.
239 expect((yield* del(writeBase, `${prefix}never-written`)).status).toBe(200);
240
241 // delete (batch) — write two, delete both in one call
242 const k2 = `${prefix}k2`;
243 const k3 = `${prefix}k3`;
244 yield* put(writeBase, k2, "v2");
245 yield* put(writeBase, k3, "v3");
246 expect(yield* expectValue(readBase, k2, "v2")).toBe("v2");
247 yield* delMany(writeBase, [k2, k3]);
248 yield* expectMissing(readBase, k2);
249 yield* expectMissing(readBase, k3);
250
251 if (multipart) {
252 // 5 MiB first part + 4-byte tail. `head` reads back the size without
253 // downloading the payload.
254 const expectedSize = 5 * 1024 * 1024 + 4;
255
256 // create → uploadPart → complete
257 const mk = `${prefix}mpu`;
258 const created = yield* (yield* post(writeBase, "mpu", mk)).json;
259 expect((created as { size: number }).size).toBe(expectedSize);
260 expect((yield* headObject(readBase, mk)).size).toBe(expectedSize);
261
262 // create → uploadPart → abort (object never materializes)
263 const ak = `${prefix}mpu-abort`;
264 yield* post(writeBase, "mpu-abort", ak);
265 expect((yield* headObject(readBase, ak)).exists).toBe(false);
266
267 // create → resumeMultipartUpload → uploadPart → complete
268 const rk = `${prefix}mpu-resume`;
269 const resumed = yield* (yield* post(writeBase, "mpu-resume", rk)).json;
270 expect((resumed as { size: number }).size).toBe(expectedSize);
271 expect((yield* headObject(readBase, rk)).size).toBe(expectedSize);
272 }
273 });
274
275 /**
276 * Deploys six Workers that all bind one shared R2 bucket — read /
277 * write / read-write, each over the native Worker binding
278 * (`*BucketBinding`) and over a scoped HTTP API token (`*BucketHttp`)
279 * — via {@link Stack}, then drives each binding flavor over `fetch` in
280 * its own test:
281 *
282 * - write through the Write worker, read it back through the Read
283 * worker (cross-worker, proving both halves agree on the bucket);
284 * - round-trip a key through the ReadWrite worker by itself.
285 *
286 * The stack lives in `fixtures/stack.ts` so it can also be inspected
287 * directly, e.g. `alchemy logs --tail --stage test --config ./test/Cloudflare/R2/fixtures/stack.ts`.
288 */
289 const stack = beforeAll(deploy(Stack), { timeout: HOOK_TIMEOUT });
290 afterAll.skipIf(!!process.env.NO_DESTROY)(
291 Effect.gen(function* () {
292 const output = yield* Effect.exit(stack);
293 yield* destroy(Stack);
294 if (Exit.isSuccess(output)) {
295 yield* Effect.gen(function* () {
296 const { accountId } = yield* yield* CloudflareEnvironment;
297 const gone = yield* r2.getBucket({ accountId, bucketName: output.value.bucketName }).pipe(
298 Effect.as(false),
299 Effect.catchTag("NoSuchBucket", () => Effect.succeed(true)),
300 );
301 expect(gone).toBe(true);
302 for (const scriptName of output.value.workerNames) {
303 const absent = yield* workers.getScriptSetting({ accountId, scriptName }).pipe(
304 Effect.as(false),
305 Effect.catchTag("WorkerNotFound", () => Effect.succeed(true)),
306 );
307 expect(absent).toBe(true);
308 }
309 }).pipe(Effect.provide(CloudflareApiLive()), Effect.scoped);
310 }
311 }),
312 { timeout: HOOK_TIMEOUT },
313 );
314
315 // ── Native Worker binding ── write through the Write worker, read back through
316 // the Read worker (cross-worker, proving both halves agree on the bucket).
317 test(
318 "native binding: write + read across separate workers",
319 Effect.gen(function* () {
320 const out = yield* stack;
321 yield* exercise("bind", out.writeBinding, out.readBinding, true);
322 }).pipe(logLevel),
323 {
324 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
325 timeout: TEST_TIMEOUT,
326 },
327 );
328
329 // The ReadWrite worker round-trips a key by itself over the native binding.
330 test(
331 "native binding: read-write round-trip in one worker",
332 Effect.gen(function* () {
333 const out = yield* stack;
334 yield* exercise("rw-bind", out.readWriteBinding, out.readWriteBinding, true);
335 }).pipe(logLevel),
336 {
337 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
338 timeout: TEST_TIMEOUT,
339 },
340 );
341
342 // A `put` given an Effect `Stream` goes through a `FixedLengthStream`, and
343 // the options passed alongside it must still reach R2. Without them R2 has
344 // nothing to verify and stores whatever bytes arrive under the declared
345 // hash, which lets a caller plant content under a hash it does not have.
346 test(
347 "native binding: a streamed put is verified against its declared sha256",
348 Effect.gen(function* () {
349 const out = yield* stack;
350 const value = "stream-value";
351 const sha256 = yield* Effect.sync(() => createHash("sha256").update(value).digest("hex"));
352
353 // Matching hash: stored and readable.
354 const ok = yield* untilOk(putStream(out.writeBinding, "stream/ok", value, sha256));
355 expect(((yield* ok.json) as { stored: boolean }).stored).toBe(true);
356 expect(yield* expectValue(out.readBinding, "stream/ok", value)).toBe(value);
357
358 // Mismatched hash: R2 rejects the body and nothing is stored. The key is
359 // cleared first so a previous run cannot satisfy the final check.
360 yield* del(out.writeBinding, "stream/bad");
361 yield* expectMissing(out.readBinding, "stream/bad");
362 const bad = yield* putStream(out.writeBinding, "stream/bad", value, "0".repeat(64));
363 expect(bad.status).toBe(400);
364 expect(((yield* bad.json) as { stored: boolean }).stored).toBe(false);
365 expect((yield* headObject(out.readBinding, "stream/bad")).exists).toBe(false);
366 }).pipe(logLevel),
367 {
368 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
369 timeout: TEST_TIMEOUT,
370 },
371 );
372
373 test(
374 "native binding: copies real object bodies, ranges and empty bodies with metadata",
375 Effect.gen(function* () {
376 const { readWriteBinding: base } = yield* stack;
377 const source = "copy/native-source";
378 const value = "native R2 copy ☃\u0000".repeat(1024);
379 const bytes = new TextEncoder().encode(value);
380 yield* putCopySource(base, source, value);
381 for (const [label, options, expected] of [
382 ["implicit", {}, bytes],
383 ["explicit", { contentLength: String(bytes.byteLength) }, bytes],
384 ["range", { offset: "17", rangeLength: "257", contentLength: "257" }, bytes.slice(17, 274)],
385 ] as const) {
386 const key = `copy/${label}`;
387 yield* del(base, key);
388 const copied = yield* untilOk(copyStream(base, source, key, options));
389 expect((yield* copied.json) as unknown).toMatchObject({
390 stored: true,
391 size: expected.byteLength,
392 });
393 yield* expectCopy(base, key, expected, source);
394 }
395 const emptySource = "copy/empty-source";
396 yield* putCopySource(base, emptySource, "");
397 for (const [label, options] of [
398 ["implicit", {}],
399 ["explicit", { contentLength: "0" }],
400 ] as const) {
401 const key = `copy/empty-${label}`;
402 yield* del(base, key);
403 expect((yield* untilOk(copyStream(base, emptySource, key, options))).status).toBe(200);
404 yield* expectCopy(base, key, new Uint8Array(), emptySource);
405 }
406 }).pipe(logLevel),
407 {
408 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
409 timeout: TEST_TIMEOUT,
410 },
411 );
412
413 test(
414 "native binding: transformed object streams store their transformed bytes",
415 Effect.gen(function* () {
416 const { readWriteBinding: base } = yield* stack;
417 const source = "copy/transform-source";
418 const key = "copy/transformed";
419 const value = "transform native bytes ☃".repeat(256);
420 const bytes = new TextEncoder().encode(value);
421 const transformed = bytes.map((byte) => byte ^ 0xff);
422 const sha256 = createHash("sha256").update(transformed).digest("hex");
423 yield* putCopySource(base, source, value);
424 yield* del(base, key);
425 const copied = yield* untilOk(
426 copyStream(base, source, key, {
427 transform: "invert",
428 contentLength: String(bytes.byteLength),
429 sha256,
430 }),
431 );
432 expect((yield* copied.json) as unknown).toMatchObject({ stored: true, size: bytes.byteLength });
433 yield* expectCopy(base, key, transformed, source);
434 }).pipe(logLevel),
435 {
436 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
437 timeout: TEST_TIMEOUT,
438 },
439 );
440
441 test(
442 "native binding: forwarded bodies enforce lengths, checksums and conditional writes",
443 Effect.gen(function* () {
444 const { readWriteBinding: base } = yield* stack;
445 const source = "copy/validation-source";
446 const value = "verified native body";
447 const bytes = new TextEncoder().encode(value);
448 yield* putCopySource(base, source, value);
449 for (const [label, options] of [
450 ["short", { contentLength: String(bytes.byteLength - 1) }],
451 ["long", { contentLength: String(bytes.byteLength + 1) }],
452 ["checksum", { sha256: "0".repeat(64) }],
453 ] as const) {
454 const key = `copy/invalid-${label}`;
455 yield* del(base, key);
456 const rejected = yield* copyStream(base, source, key, options);
457 expect(rejected.status).toBe(400);
458 expect((yield* rejected.json) as unknown).toMatchObject({ stored: false });
459 expect((yield* readCopy(base, key)).exists).toBe(false);
460 }
461 const checked = "copy/checksum-ok";
462 yield* del(base, checked);
463 const sha256 = createHash("sha256").update(bytes).digest("hex");
464 expect((yield* untilOk(copyStream(base, source, checked, { sha256 }))).status).toBe(200);
465 yield* expectCopy(base, checked, bytes, source);
466
467 const existing = "copy/existing";
468 yield* putCopySource(base, existing, "keep this value");
469 const conditional = yield* untilOk(copyStream(base, source, existing));
470 expect((yield* conditional.json) as unknown).toMatchObject({ stored: false, size: null });
471 yield* expectCopy(base, existing, new TextEncoder().encode("keep this value"), existing);
472 }).pipe(logLevel),
473 {
474 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
475 timeout: TEST_TIMEOUT,
476 },
477 );
478
479 // ── Scoped HTTP API token ── same matrix over the `*BucketHttp` clients
480 // (multipart is unsupported over the HTTP API, so it is skipped here).
481 test(
482 "http token: write + read across separate workers",
483 Effect.gen(function* () {
484 const out = yield* stack;
485 yield* exercise("http", out.writeHttp, out.readHttp, false);
486 }).pipe(logLevel),
487 {
488 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
489 timeout: TEST_TIMEOUT,
490 },
491 );
492
493 test(
494 "http token: read-write round-trip in one worker",
495 Effect.gen(function* () {
496 const out = yield* stack;
497 yield* exercise("rw-http", out.readWriteHttp, out.readWriteHttp, false);
498 }).pipe(logLevel),
499 {
500 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
501 timeout: TEST_TIMEOUT,
502 },
503 );