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

main
ts 77 lines 2.94 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { describe, 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 { MinimumLogLevel } from "effect/References";
7 import * as Schedule from "effect/Schedule";
8 import { INTERCEPT_HOST } from "./fixtures/intercept/object.ts";
9 import InterceptStack, { state } from "./fixtures/intercept/stack.ts";
10
11 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
12 providers: Cloudflare.providers(),
13 state,
14 dev: true,
15 });
16
17 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
18
19 // First request has to wait for the local runtime to `docker build` the image
20 // and boot the container, so give it plenty of room.
21 const HOOK_TIMEOUT = 300_000;
22 const TEST_TIMEOUT = 240_000;
23
24 const readinessSchedule = Schedule.min([
25 Schedule.exponential("500 millis"),
26 Schedule.spaced("3 seconds"),
27 ]);
28
29 /**
30 * `interceptOutboundHttp` / `interceptAllOutboundHttp` on a container
31 * handle must register the caller's Fetcher with workerd, so the container's
32 * outbound HTTP reaches the Durable Object that owns it.
33 */
34 describe(
35 "local container outbound interception",
36 { tags: ["provider:cloudflare", "provider:cloudflare:container", "provider:cloudflare:worker"] },
37 () => {
38 const stack = beforeAll(deploy(InterceptStack), { timeout: HOOK_TIMEOUT });
39 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(InterceptStack), { timeout: HOOK_TIMEOUT });
40
41 const probe = (path: string) =>
42 Effect.gen(function* () {
43 const { url } = yield* stack;
44 const client = yield* HttpClient.HttpClient;
45 const text = yield* client.get(new URL(path, url)).pipe(
46 Effect.flatMap((r) =>
47 r.status !== 200 ? Effect.fail(new Error(`not ready: ${r.status}`)) : r.text,
48 ),
49 Effect.timeout("30 seconds"),
50 Effect.retry({ schedule: readinessSchedule, times: 30 }),
51 );
52 return JSON.parse(text) as { status?: number; body?: string; error?: string };
53 });
54
55 test(
56 "interceptOutboundHttp routes the container's requests for a host to the object",
57 Effect.gen(function* () {
58 const result = yield* probe("/probe/host");
59 expect(result.error).toBeUndefined();
60 expect(result.status).toBe(200);
61 expect(result.body).toBe(`intercepted ${INTERCEPT_HOST}/hello`);
62 }).pipe(logLevel),
63 { tags: ["local"], timeout: TEST_TIMEOUT },
64 );
65
66 test(
67 "interceptAllOutboundHttp routes all of the container's requests to the object",
68 Effect.gen(function* () {
69 const result = yield* probe("/probe/all");
70 expect(result.error).toBeUndefined();
71 expect(result.status).toBe(200);
72 expect(result.body).toBe("intercepted any.example/hello");
73 }).pipe(logLevel),
74 { tags: ["local"], timeout: TEST_TIMEOUT },
75 );
76 },
77 );