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

main
ts 151 lines 5.05 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 { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import * as pathe from "pathe";
10
11 const { test } = Test.make({
12 providers: Cloudflare.providers(),
13 dev: true,
14 });
15
16 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
17
18 class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{
19 status: number;
20 }> {}
21
22 const getJsonReady = (url: string) =>
23 Effect.gen(function* () {
24 const client = yield* HttpClient.HttpClient;
25 const res = yield* client.get(url).pipe(
26 Effect.flatMap((res) =>
27 res.status === 200
28 ? Effect.succeed(res)
29 : Effect.fail(new WorkerNotReady({ status: res.status })),
30 ),
31 Effect.retry({
32 while: (e): e is WorkerNotReady => e instanceof WorkerNotReady,
33 // Cap the backoff: an uncapped exponential over 10 recurs sums to
34 // ~8.5 minutes and turns a persistent non-200 into an apparent hang.
35 schedule: Schedule.max([
36 Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("2 seconds")]),
37 Schedule.recurs(10),
38 ]),
39 }),
40 );
41 return yield* res.json;
42 }).pipe(Effect.orDie);
43
44 /**
45 * The local runtime ships an always-on Cache API simulator
46 * (`caches.default` persisted under `.alchemy/local/cache`) and builds
47 * `request.cf` from a fallback blob in the entry middleware. Neither needs
48 * any alchemy-side wiring — this pins that both actually work for a worker
49 * deployed by the local provider.
50 */
51 test.provider(
52 "Cache API and request.cf work under the local runtime",
53 (stack) =>
54 Effect.gen(function* () {
55 yield* stack.destroy();
56
57 const deployed = yield* stack.deploy(
58 Effect.gen(function* () {
59 const worker = yield* Cloudflare.Worker("cache-cf-worker", {
60 main: pathe.resolve(import.meta.dirname, "fixtures/cache-cf/worker.ts"),
61 });
62 return { worker };
63 }),
64 );
65 const url = deployed.worker.url!;
66
67 // The local provider serves from the dev proxy — proof no cloud call
68 // ran.
69 expect(url).toMatch(/^http:\/\/localhost:\d+$/);
70
71 // request.cf: the middleware injects Miniflare's static fallback blob,
72 // so `colo`/`country` are present without any configuration.
73 const cf = (yield* getJsonReady(`${url}/cf`)) as {
74 colo: string | null;
75 country: string | null;
76 clientAcceptEncoding: string | null;
77 };
78 expect(cf.colo).toBeTruthy();
79 expect(cf.country).toBeTruthy();
80
81 // Cache API: first request misses and populates, second hits. The
82 // simulator's storage persists across runs, so use a per-run key.
83 const cacheKey = crypto.randomUUID();
84 const first = (yield* getJsonReady(`${url}/cache?key=${cacheKey}`)) as {
85 hit: boolean;
86 body: string;
87 };
88 expect(first.hit).toBe(false);
89 expect(first.body).toBe("cached-body");
90
91 const second = (yield* getJsonReady(`${url}/cache?key=${cacheKey}`)) as {
92 hit: boolean;
93 body: string;
94 };
95 expect(second.hit).toBe(true);
96 expect(second.body).toBe("cached-body");
97
98 yield* stack.destroy();
99 }).pipe(logLevel),
100 {
101 tags: ["provider:cloudflare", "provider:cloudflare:worker", "local"],
102 timeout: 120_000,
103 },
104 );
105
106 /**
107 * `dev: { cache: false }` turns every Cache API operation into a no-op
108 * (matching production workers.dev), and `dev: { cf }` overrides the
109 * `request.cf` blob served to this Worker.
110 */
111 test.provider(
112 "dev.cache=false disables the Cache API and dev.cf overrides request.cf",
113 (stack) =>
114 Effect.gen(function* () {
115 yield* stack.destroy();
116
117 const deployed = yield* stack.deploy(
118 Effect.gen(function* () {
119 const worker = yield* Cloudflare.Worker("cache-off-worker", {
120 main: pathe.resolve(import.meta.dirname, "fixtures/cache-cf/worker.ts"),
121 dev: {
122 cache: false,
123 cf: { colo: "TST", country: "XX" },
124 },
125 });
126 return { worker };
127 }),
128 );
129 const url = deployed.worker.url!;
130
131 // The overridden blob is served verbatim.
132 const cf = (yield* getJsonReady(`${url}/cf`)) as {
133 colo: string | null;
134 country: string | null;
135 };
136 expect(cf.colo).toBe("TST");
137 expect(cf.country).toBe("XX");
138
139 // With the cache disabled, every request misses — the put is a no-op.
140 const first = (yield* getJsonReady(`${url}/cache`)) as { hit: boolean };
141 const second = (yield* getJsonReady(`${url}/cache`)) as { hit: boolean };
142 expect(first.hit).toBe(false);
143 expect(second.hit).toBe(false);
144
145 yield* stack.destroy();
146 }).pipe(logLevel),
147 {
148 tags: ["provider:cloudflare", "provider:cloudflare:worker", "local"],
149 timeout: 120_000,
150 },
151 );