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

main
ts 107 lines 3.9 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 Effect from "effect/Effect";
5 import * as HttpClient from "effect/http/HttpClient";
6 import * as HttpClientRequest from "effect/http/HttpClientRequest";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import { materializeIsolatedProject, removeIsolatedProject } from "../../IsolatedProject.ts";
10 import { project } from "./fixtures/isolated/container.ts";
11 import IsolatedStack from "./fixtures/isolated/stack.ts";
12
13 // Live proof that the Cloudflare Container bootstrap boots when the
14 // container's `main` lives in an isolated project (see
15 // test/IsolatedProject.ts) — the bundle `cwd` resolves none of alchemy's
16 // dependencies, so the bootstrap's `@effect/platform-bun` / `alchemy/*`
17 // imports must be bundled by the virtual-entry plugin rather than found from
18 // the project root. With them left external bun dies at module load and the
19 // Durable Object's RPC / TCP requests never get a reply.
20 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
21 providers: Cloudflare.providers(),
22 state: Cloudflare.state(),
23 });
24
25 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
26
27 // Container image build + push + worker/DO deploy comfortably exceeds the
28 // default 120s hook budget.
29 const HOOK_TIMEOUT = 600_000;
30
31 // The project must exist before the deploy bundles it; written in its own
32 // hook so a failed deploy still runs the cleanup.
33 beforeAll(materializeIsolatedProject(project));
34 const stack = beforeAll(deploy(IsolatedStack), { timeout: HOOK_TIMEOUT });
35 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(IsolatedStack), {
36 timeout: HOOK_TIMEOUT,
37 });
38 afterAll(removeIsolatedProject(project));
39
40 // Force `Connection: close` so each readiness attempt opens a fresh
41 // connection and can land on an edge that already has the new deploy.
42 const freshConn = HttpClient.HttpClient.pipe(
43 Effect.map(HttpClient.mapRequest(HttpClientRequest.setHeader("connection", "close"))),
44 );
45
46 // While a freshly pre-created worker propagates, the edge serves Alchemy's
47 // pre-create stub; any poll that sees it retries.
48 const DEPLOY_PLACEHOLDER = "Alchemy worker is being deployed...";
49
50 const fetchReady = (url: URL, expected: string) =>
51 Effect.gen(function* () {
52 const client = yield* freshConn;
53 return yield* client.get(url).pipe(
54 Effect.flatMap((r) =>
55 r.text.pipe(
56 Effect.flatMap((body) =>
57 r.status !== 200
58 ? Effect.fail(new Error(`Worker not ready: ${r.status} ${body}`))
59 : body.includes(DEPLOY_PLACEHOLDER) || !body.includes(expected)
60 ? Effect.fail(new Error(`not ready: got ${body}`))
61 : Effect.succeed(body),
62 ),
63 ),
64 ),
65 Effect.timeout("10 seconds"),
66 Effect.retry({
67 schedule: Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("3 seconds")]),
68 times: 60,
69 }),
70 );
71 });
72
73 test(
74 "RPC: ping round-trips into the container",
75 Effect.gen(function* () {
76 const { url } = yield* stack;
77 const pong = yield* fetchReady(new URL("/ping", url), "pong");
78 expect(pong).toContain("pong");
79 }).pipe(logLevel),
80 {
81 tags: [
82 "provider:cloudflare",
83 "provider:cloudflare:container",
84 "provider:cloudflare:worker",
85 "live",
86 ],
87 timeout: 300_000,
88 },
89 );
90
91 test(
92 "fetch: the container's HTTP server answers over its TCP port",
93 Effect.gen(function* () {
94 const { url } = yield* stack;
95 const hello = yield* fetchReady(new URL("/hello", url), "hello from isolated project");
96 expect(hello).toContain("hello from isolated project");
97 }).pipe(logLevel),
98 {
99 tags: [
100 "provider:cloudflare",
101 "provider:cloudflare:container",
102 "provider:cloudflare:worker",
103 "live",
104 ],
105 timeout: 300_000,
106 },
107 );