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

main
ts 119 lines 3.81 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Data from "effect/Data";
5 import * as Effect from "effect/Effect";
6 import * as HttpClient from "effect/http/HttpClient";
7 import * as HttpClientRequest from "effect/http/HttpClientRequest";
8 import * as Schedule from "effect/Schedule";
9 import Stack from "./fixtures/stack.ts";
10
11 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
12 providers: Cloudflare.providers(),
13 });
14
15 class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{
16 status: number;
17 body: string;
18 }> {}
19
20 /**
21 * 1x1 red PNG — a known-good minimal image that Cloudflare Images accepts and
22 * reports as `image/png`, 1x1. The test uploads this to each worker, which
23 * forwards the request stream straight into `images.info()`.
24 */
25 const TINY_PNG = new Uint8Array(
26 Buffer.from(
27 "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==",
28 "base64",
29 ),
30 );
31
32 // Cloudflare's edge takes a few seconds to start serving a fresh workers.dev
33 // URL — initial requests can return Cloudflare's "There is nothing here yet"
34 // 404 page or, while the version is still propagating to the subdomain, the
35 // blue "Error 1104 / Script not found" page (served with a 5xx status). Both
36 // are transient propagation states, so retry through them until the worker
37 // answers 200 (and surface its body if it doesn't, so a real failure isn't
38 // hidden by the retry loop).
39 const looksLikeCloudflarePlaceholder = (body: string) =>
40 body.includes("There is nothing here yet") ||
41 // The "Error 1104 / Script not found" page renders the word "Error" and
42 // the code in separate HTML tags, so match the page's own markers rather
43 // than a contiguous "Error NNNN" string.
44 body.includes("Script not found") ||
45 body.includes("cf-error-code") ||
46 /Error\s+\d{3,4}/i.test(body);
47
48 const postImage = (url: string) =>
49 HttpClient.execute(
50 HttpClientRequest.post(url).pipe(HttpClientRequest.bodyUint8Array(TINY_PNG)),
51 ).pipe(
52 Effect.flatMap((res) =>
53 res.status === 200
54 ? res.json
55 : res.text.pipe(
56 Effect.flatMap((body) => Effect.fail(new WorkerNotReady({ status: res.status, body }))),
57 ),
58 ),
59 Effect.retry({
60 while: (e): e is WorkerNotReady =>
61 e instanceof WorkerNotReady &&
62 ((e.status >= 400 && e.status < 500) || looksLikeCloudflarePlaceholder(e.body)),
63 // Cap each backoff at 5s (otherwise the exponential blows past a minute
64 // per sleep and looks like a hang) and stop after 30 attempts.
65 schedule: Schedule.max([
66 Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("5 seconds")]),
67 Schedule.recurs(30),
68 ]),
69 }),
70 );
71
72 const stack = beforeAll(deploy(Stack));
73 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));
74
75 test(
76 "async worker reads image info via env Images binding",
77 Effect.gen(function* () {
78 const { asyncWorkerUrl } = yield* stack;
79 const info = yield* postImage(asyncWorkerUrl);
80 expect(info).toMatchObject({
81 mode: "async",
82 format: "image/png",
83 width: 1,
84 height: 1,
85 });
86 }),
87 {
88 tags: [
89 "provider:cloudflare",
90 "provider:cloudflare:images",
91 "provider:cloudflare:worker",
92 "live",
93 ],
94 timeout: 240_000,
95 },
96 );
97
98 test(
99 "effect worker reads image info via yield* Images",
100 Effect.gen(function* () {
101 const { effectWorkerUrl } = yield* stack;
102 const info = yield* postImage(effectWorkerUrl);
103 expect(info).toMatchObject({
104 mode: "effect",
105 format: "image/png",
106 width: 1,
107 height: 1,
108 });
109 }),
110 {
111 tags: [
112 "provider:cloudflare",
113 "provider:cloudflare:images",
114 "provider:cloudflare:worker",
115 "live",
116 ],
117 timeout: 240_000,
118 },
119 );