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

main
ts 246 lines 10.3 KB
Raw
1 /**
2 * True `alchemy dev` end-to-end: spawns the REAL CLI and drives every local
3 * binding over HTTP.
4 *
5 * This is a different code path from `integ.test.ts`, which deploys through
6 * the test harness (`Test.make({ dev: true })`). The harness mirrors the RPC
7 * sidecar topology, but only this test covers the CLI itself: arg parsing,
8 * the `bin/exec` child under `--watch`, the `ALCHEMY_RPC_SPAWNER_URL`
9 * handshake, and provider sidecars whose lifetime is tied to the CLI
10 * process. #1007 (local D1 migrations crashing under `alchemy dev` but not
11 * under the harness) is the class of bug this suite exists to catch.
12 *
13 * Coverage per binding, all against the local simulators (no cloud
14 * resources beyond the state store):
15 * - assets → `/` serves the static index
16 * - vars + secrets → `/env` echoes MY_VARIABLE / MY_SECRET
17 * - self_url → `/env`.PUBLIC_URL and EffectWorker `/url`
18 * - Durable Object → `/counter` increments across requests
19 * - R2 → `/r2` put/get/list roundtrip
20 * - D1 + migrations → `/d1` inserts into the `greetings` table, which
21 * exists ONLY if `./migrations` applied through the
22 * real dev path
23 * - Queue + Consumer → `/queue/send` produces, the local broker delivers
24 * to the `queue()` handler, `/queue/messages` reads
25 * - wasm modules → `/wasm` on both entrypoint styles
26 * - KV (Effect-style) → EffectWorker `/` lists the namespace
27 * - Workflow → `/workflow/start` + status poll to completion
28 * - Container → `/sandbox` fetches through the local container
29 * - Cache API → `/cache` misses then hits
30 * - Rate limit → `/ratelimit` throttles the third call
31 * - versionMetadata → `/version` returns a stubbed id
32 * - Service binding → `/service` calls EffectWorker worker-to-worker
33 * - Secrets Store → MediaWorker `/secret` echoes the seeded value
34 *
35 * The heavier local simulators (Browser, Images, Stream, email, tail
36 * consumers, cron, secret_key, Analytics Engine) are covered end-to-end by
37 * integ.test.ts — the binding lowering is identical under both topologies,
38 * so this suite pins only the cheap ones on the CLI path.
39 */
40 import { afterAll, expect, test } from "bun:test";
41 import * as path from "node:path";
42 import { DevCli, fetchOk } from "@alchemy.run/test/DevCli";
43 import { WORKFLOW_SECRET_VALUE } from "../src/NotifyWorkflow.ts";
44
45 const root = path.resolve(import.meta.dirname, "..");
46 const STAGE = "dev-cli-test";
47 const cli = new DevCli({ root, stage: STAGE });
48
49 afterAll(async () => {
50 await cli.stop();
51 if (!process.env.NO_DESTROY) {
52 cli.destroy();
53 }
54 }, 180_000);
55
56 test(
57 "alchemy dev serves every local binding end-to-end",
58 async () => {
59 // Start from an empty stage. The local D1 provider trusts its state row
60 // for "migrations applied", so a stage left deployed by an interrupted
61 // run (remote state store) combined with a wiped `.alchemy/local` (e.g.
62 // `git clean -fdx`) would skip the migrations and fail `/d1` with
63 // `no such table: greetings` — a stale-environment failure, not the
64 // #1007 regression this route exists to pin.
65 cli.destroy();
66 cli.start();
67
68 // The first dev deploy applies D1 migrations through the sidecar,
69 // prepares the sandbox image, boots workerd, and then prints the stack
70 // outputs.
71 const asyncWorker = await cli.pollUntil(
72 "asyncWorker url in stack outputs",
73 () => cli.outputUrl("asyncWorker"),
74 { tries: 300, delayMs: 1000 },
75 );
76 const effectWorker = await cli.pollUntil(
77 "effectWorker url in stack outputs",
78 () => cli.outputUrl("effectWorker"),
79 { tries: 30, delayMs: 1000 },
80 );
81 const mediaWorker = await cli.pollUntil(
82 "mediaWorker url in stack outputs",
83 () => cli.outputUrl("mediaWorker"),
84 { tries: 30, delayMs: 1000 },
85 );
86
87 // Assets: the fallthrough route serves ./assets/index.html.
88 const index = await (await fetchOk(asyncWorker)).text();
89 expect(index).toContain("<h1>Hello, world!</h1>");
90
91 // Plain-text vars, secrets, and the self_url binding.
92 const env = (await (await fetchOk(new URL("/env", asyncWorker))).json()) as Record<
93 string,
94 unknown
95 >;
96 expect(env.MY_VARIABLE).toBe("my-variable-abc123");
97 expect(env.MY_SECRET).toBe("my-secret-abc123");
98 expect(env.PUBLIC_URL).toBe(asyncWorker.replace(/\/$/, ""));
99
100 // Durable Object: state persists across requests.
101 const count = (body: string) => Number(body.match(/^Hello, world! (\d+)$/)?.[1]);
102 const first = await (await fetchOk(new URL("/counter", asyncWorker))).text();
103 const second = await (await fetchOk(new URL("/counter", asyncWorker))).text();
104 expect(count(second)).toBe(count(first) + 1);
105
106 // R2: put/get/list against the local simulator.
107 const r2 = (await (await fetchOk(new URL("/r2", asyncWorker))).json()) as {
108 text: string;
109 keys: string[];
110 };
111 expect(r2.text).toBe("hello from r2");
112 expect(r2.keys).toContain("hello.txt");
113
114 // D1: the `greetings` table exists ONLY if ./migrations applied through
115 // the real `alchemy dev` path — the #1007 repro.
116 const d1 = (await (await fetchOk(new URL("/d1", asyncWorker))).json()) as {
117 text: string | null;
118 };
119 expect(d1.text).toBe("hello from d1");
120
121 // wasm modules ship through the bundler for both entrypoint styles.
122 for (const worker of [asyncWorker, effectWorker]) {
123 const wasm = (await (await fetchOk(new URL("/wasm", worker))).json()) as {
124 result: number;
125 };
126 expect(wasm.result).toBe(7);
127 }
128
129 // Queue: produce over the binding; the local broker delivers to the
130 // `queue()` handler, which records into the QueueMessages DO.
131 const message = { text: "hello from alchemy dev", sentAt: Date.now() };
132 await fetchOk(new URL("/queue/send", asyncWorker), {
133 method: "POST",
134 headers: { "content-type": "application/json" },
135 body: JSON.stringify(message),
136 });
137 const received = await cli.pollUntil(
138 "queue message to be consumed",
139 async () => {
140 const res = await fetch(new URL("/queue/messages", asyncWorker));
141 if (!res.ok) return undefined;
142 const messages = (await res.json()) as Array<{
143 body: { sentAt: number };
144 }>;
145 return messages.find((m) => m.body.sentAt === message.sentAt);
146 },
147 { tries: 50, delayMs: 500 },
148 );
149 expect(received).toMatchObject({ body: message });
150
151 // Cache API: per-run key — first miss, second hit.
152 const cacheKey = crypto.randomUUID();
153 const cacheOnce = async () =>
154 (await (await fetchOk(new URL(`/cache?key=${cacheKey}`, asyncWorker))).json()) as {
155 hit: boolean;
156 };
157 expect((await cacheOnce()).hit).toBe(false);
158 expect((await cacheOnce()).hit).toBe(true);
159
160 // Rate limit: 2 per 10s per key — the third call is throttled.
161 const rlKey = crypto.randomUUID();
162 const limitOnce = async () =>
163 (await (await fetchOk(new URL(`/ratelimit?key=${rlKey}`, asyncWorker))).json()) as {
164 success: boolean;
165 };
166 expect((await limitOnce()).success).toBe(true);
167 expect((await limitOnce()).success).toBe(true);
168 expect((await limitOnce()).success).toBe(false);
169
170 // Version metadata: locally stubbed with a random id.
171 const version = (await (await fetchOk(new URL("/version", asyncWorker))).json()) as {
172 id: string;
173 };
174 expect(typeof version.id).toBe("string");
175 expect(version.id.length).toBeGreaterThan(0);
176
177 // Service binding: AsyncWorker calls EffectWorker worker-to-worker. The
178 // dev registry may pick the peer up asynchronously, so allow retries.
179 const service = (await (
180 await fetchOk(new URL("/service", asyncWorker), undefined, {
181 tries: 30,
182 delayMs: 1000,
183 })
184 ).json()) as { url: string };
185 expect(service.url).toBe(effectWorker.replace(/\/$/, ""));
186
187 // Secrets Store: the seeded value round-trips through the binding.
188 const secret = (await (await fetchOk(new URL("/secret", mediaWorker))).json()) as {
189 value: string;
190 };
191 expect(secret.value).toBe("store-secret-abc123");
192
193 // KV via the Effect-style binding: EffectWorker's root route lists the
194 // namespace.
195 const kv = (await (await fetchOk(effectWorker)).json()) as {
196 keys: unknown[];
197 list_complete: boolean;
198 };
199 expect(Array.isArray(kv.keys)).toBe(true);
200 expect(typeof kv.list_complete).toBe("boolean");
201
202 // self_url, Effect-style: `yield* Worker.URL` at init.
203 const self = (await (await fetchOk(new URL("/url", effectWorker))).json()) as { url: string };
204 expect(self.url).toBe(effectWorker.replace(/\/$/, ""));
205
206 // Workflow: start an instance and poll it to completion — pins the
207 // workflows binding, the KV roundtrip task inside it, and plantime
208 // secret resolution.
209 const roomId = `dev-cli-${Math.random().toString(36).slice(2, 10)}`;
210 const started = (await (
211 await fetchOk(new URL(`/workflow/start/${roomId}`, effectWorker), {
212 method: "POST",
213 })
214 ).json()) as { instanceId: string };
215 const status = await cli.pollUntil(
216 "workflow to settle",
217 async () => {
218 const res = await fetch(new URL(`/workflow/status/${started.instanceId}`, effectWorker));
219 if (!res.ok) return undefined;
220 const s = (await res.json()) as {
221 status: string;
222 output?: { text?: string; secret?: string };
223 error?: unknown;
224 };
225 return s.status === "complete" || s.status === "errored" ? s : undefined;
226 },
227 { tries: 60, delayMs: 2000 },
228 );
229 expect(status.error).toBeFalsy();
230 expect(status.status).toBe("complete");
231 expect(status.output?.text).toBe("Processed: hello from workflow");
232 expect(status.output?.secret).toBe(WORKFLOW_SECRET_VALUE);
233
234 // Container: the sandbox route proxies into the locally-running
235 // container (docker), env vars included. Generous retry budget — the
236 // container may still be pulling/starting on first request.
237 const sandbox = await (
238 await fetchOk(new URL("/sandbox", effectWorker), undefined, {
239 tries: 120,
240 delayMs: 1000,
241 })
242 ).text();
243 expect(sandbox).toBe("Hello from Sandbox container! GREETING=hello-from-env");
244 },
245 { timeout: 600_000 },
246 );