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

main
ts 91 lines 3.09 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 Alchemy from "alchemy";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10 import InitIOWorker from "./fixtures/init-io/worker.ts";
11
12 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
13 providers: Cloudflare.providers(),
14 });
15
16 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
17
18 const Stack = Alchemy.Stack(
19 "InitIOTestStack",
20 { providers: Cloudflare.providers(), state: Cloudflare.state() },
21 Effect.gen(function* () {
22 const worker = yield* InitIOWorker;
23 return { url: worker.url.as<string>() };
24 }),
25 );
26
27 const stack = beforeAll(deploy(Stack));
28 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));
29
30 interface InitIOBody {
31 nonce: string;
32 traceLength: number;
33 hasUag: boolean;
34 initFetches: number;
35 }
36
37 class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{
38 status: number;
39 }> {}
40
41 /**
42 * Pins the init-phase I/O contract: one-shot async I/O in a Layer build
43 * (here a real `fetch` of cloudflare.com/cdn-cgi/trace) runs exactly once
44 * per isolate, and the cached plain value serves every event — sequential
45 * and concurrent. A regression to per-event builds would report
46 * `initFetches > 1` and a fresh `nonce` per request; a broken cross-request
47 * build would fail the concurrent batch outright.
48 */
49 test(
50 "async I/O in a layer build runs once per isolate and serves every event",
51 Effect.gen(function* () {
52 const { url } = yield* stack;
53 const client = yield* HttpClient.HttpClient;
54
55 const get = Effect.gen(function* () {
56 const res = yield* client.get(url).pipe(
57 Effect.flatMap((res) =>
58 res.status === 200
59 ? Effect.succeed(res)
60 : Effect.fail(new WorkerNotReady({ status: res.status })),
61 ),
62 Effect.retry({
63 while: (e): e is WorkerNotReady => e instanceof WorkerNotReady,
64 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
65 }),
66 );
67 return (yield* res.json) as unknown as InitIOBody;
68 }).pipe(Effect.orDie);
69
70 const first = yield* get;
71 const second = yield* get;
72 const concurrent = yield* Effect.all(
73 Array.from({ length: 6 }, () => get),
74 { concurrency: "unbounded" },
75 );
76
77 for (const body of [first, second, ...concurrent]) {
78 // The layer's fetch really ran and produced a usable value...
79 expect(body.hasUag).toBe(true);
80 expect(body.traceLength).toBeGreaterThan(0);
81 expect(body.nonce).toMatch(/^[0-9a-f-]{36}$/);
82 // ...exactly once on whichever isolate served this event, no matter
83 // how many events that isolate has handled.
84 expect(body.initFetches).toBe(1);
85 }
86 }).pipe(logLevel),
87 {
88 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
89 timeout: 180_000,
90 },
91 );