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

main
ts 131 lines 5.27 KB
Raw
1 import * as NodeHttp from "node:http";
2 import * as Cloudflare from "@alchemy.run/cloudflare";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
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 {
10 HOST_PROBE_PORT,
11 NEON_URL,
12 PLANETSCALE_MYSQL_URL,
13 PLANETSCALE_PG_URL,
14 PPG_URL,
15 } from "./fixtures/hostreach/object.ts";
16 import HostReachStack, { state } from "./fixtures/hostreach/stack.ts";
17
18 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
19 providers: Cloudflare.providers(),
20 state,
21 dev: true,
22 });
23
24 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
25
26 // First request has to wait for the local runtime to `docker build` the image
27 // and boot the container, so give it plenty of room.
28 const HOOK_TIMEOUT = 300_000;
29 const TEST_TIMEOUT = 240_000;
30
31 const readinessSchedule = Schedule.min([
32 Schedule.exponential("500 millis"),
33 Schedule.spaced("3 seconds"),
34 ]);
35
36 /**
37 * A host-side stand-in for a local dev database: an HTTP server bound to the
38 * developer machine's loopback, exactly like `@prisma/dev`'s direct Postgres
39 * endpoint. The container's env points at it via `http://localhost:<port>`.
40 */
41 const hostServer = Effect.acquireRelease(
42 Effect.callback<NodeHttp.Server>((resume) => {
43 const server = NodeHttp.createServer((_req, res) => {
44 res.setHeader("content-type", "text/plain");
45 res.end("hello-from-host");
46 });
47 // Bind loopback only, like `@prisma/dev`. Docker Desktop's
48 // host-gateway reaches 127.0.0.1. Native Linux cannot SYN the
49 // bridge IP (UFW INPUT); the sidecar unix-socket-tunnels this
50 // port into the container netns instead.
51 server.listen(HOST_PROBE_PORT, "127.0.0.1", () => resume(Effect.succeed(server)));
52 server.on("error", (err) => resume(Effect.die(err)));
53 }),
54 (server) =>
55 Effect.callback<void>((resume) => {
56 server.close(() => resume(Effect.void));
57 }),
58 );
59
60 /**
61 * Regression test for alchemy-run/alchemy#1334: under `alchemy dev`, a
62 * container's env values that reference the developer machine's loopback
63 * (`localhost` / `127.0.0.1` — every local dev database emulator) must be
64 * reachable from inside the Docker container, where `localhost` is the
65 * container itself.
66 */
67 describe(
68 "local container reaches host services",
69 { tags: ["provider:cloudflare", "provider:cloudflare:container", "provider:cloudflare:worker"] },
70 () => {
71 const stack = beforeAll(deploy(HostReachStack), { timeout: HOOK_TIMEOUT });
72 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(HostReachStack), { timeout: HOOK_TIMEOUT });
73
74 test(
75 "container env loopback URLs are rewritten and reach the host",
76 Effect.gen(function* () {
77 const { url } = yield* stack;
78 const client = yield* HttpClient.HttpClient;
79 yield* hostServer;
80
81 const get = (path: string) =>
82 client.get(new URL(path, url)).pipe(
83 Effect.flatMap((r) =>
84 r.status !== 200 ? Effect.fail(new Error(`not ready: ${r.status}`)) : r.text,
85 ),
86 Effect.timeout("30 seconds"),
87 Effect.retry({ schedule: readinessSchedule, times: 30 }),
88 );
89
90 // The env the container received: loopback hosts must be rewritten to
91 // a name that resolves to the host machine — and that name must still
92 // contain "localhost", because Prisma's client only speaks plain HTTP
93 // to `prisma+postgres://` hosts that look local (anything else flips
94 // it to TLS against a plain-HTTP local dev server).
95 const env = JSON.parse(yield* get("/env")) as {
96 TARGET_URL: string;
97 PPG_URL: string;
98 NEON_URL: string;
99 PLANETSCALE_PG_URL: string;
100 PLANETSCALE_MYSQL_URL: string;
101 };
102 expect(new URL(env.TARGET_URL).hostname).not.toBe("localhost");
103 expect(new URL(env.TARGET_URL).hostname).toContain("localhost");
104 expect(new URL(env.PPG_URL).hostname).not.toBe("localhost");
105 expect(new URL(env.PPG_URL).hostname).toContain("localhost");
106 // Everything else about the URLs is preserved.
107 expect(new URL(env.TARGET_URL).port).toBe(String(HOST_PROBE_PORT));
108 expect(new URL(env.PPG_URL).searchParams.get("api_key")).toBe(
109 new URL(PPG_URL).searchParams.get("api_key"),
110 );
111 // Cloud SQL URLs (Neon, PlanetScale) must not be rewritten — they are
112 // not loopback, and the container talks to them over the internet.
113 expect(env.NEON_URL).toBe(NEON_URL);
114 expect(env.PLANETSCALE_PG_URL).toBe(PLANETSCALE_PG_URL);
115 expect(env.PLANETSCALE_MYSQL_URL).toBe(PLANETSCALE_MYSQL_URL);
116
117 // The proof: the container fetches TARGET_URL (the host-side server)
118 // and reports what it got back.
119 const probe = JSON.parse(yield* get("/probe")) as {
120 status?: number;
121 body?: string;
122 error?: string;
123 };
124 expect(probe.error).toBeUndefined();
125 expect(probe.status).toBe(200);
126 expect(probe.body).toBe("hello-from-host");
127 }).pipe(Effect.scoped, logLevel),
128 { tags: ["local"], timeout: TEST_TIMEOUT },
129 );
130 },
131 );