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

main
ts 132 lines 5.1 KB
Raw
1 /**
2 * True `alchemy dev` end-to-end for the orders app: spawns the REAL CLI
3 * (mirroring examples/aws-dev/test/dev.test.ts) and drives the WHOLE stack
4 * against the floci emulator — no cloud credentials, no cloud resources:
5 *
6 * - VPC / subnets / routing / security groups → floci EC2
7 * - shared ALB + listener + path rules → floci ELBv2 (the ALB's
8 * DNS resolves to 127.0.0.1 and is host-routed on the gateway port)
9 * - OrdersTable → floci DynamoDB
10 * - Api (bundled main:) + Web (registry image) → real containers run by
11 * floci's ECS engine, registered behind the emulated ALB
12 * - SeedTask (one-shot, launched via the RunTask binding from
13 * POST /api/seed) + HeartbeatSchedule → floci ECS + Scheduler
14 * - HOT RELOAD → editing src/Api.ts swaps the running Api container's
15 * image without a redeploy
16 */
17 import { afterAll, expect, test } from "bun:test";
18 import * as fs from "node:fs";
19 import * as path from "node:path";
20 import { DevCli, fetchOk } from "@alchemy.run/test/DevCli";
21
22 const root = path.resolve(import.meta.dirname, "..");
23 const STAGE = "dev-cli-test";
24 const cli = new DevCli({ root, stage: STAGE });
25
26 // Hot-reload surface: the Api service source. The test rewrites it in
27 // place with the CLI running, then restores it.
28 const apiPath = path.join(root, "src", "Api.ts");
29 const apiSource = fs.readFileSync(apiPath, "utf8");
30 const MARKER = 'service: "orders-api"';
31 const MARKER_V2 = 'service: "orders-api-v2"';
32
33 afterAll(async () => {
34 // Always leave the repo tree clean, even on a mid-reload failure.
35 fs.writeFileSync(apiPath, apiSource);
36
37 await cli.stop();
38 if (!process.env.NO_DESTROY) {
39 cli.destroy({ timeout: 300_000 });
40 }
41 }, 400_000);
42
43 test(
44 "alchemy dev runs the orders app fully locally with hot reload",
45 async () => {
46 cli.start();
47
48 // The first dev deploy builds the Api/SeedTask/Report images and pulls
49 // the Web image before printing stack outputs.
50 const url = await cli.pollUntil("url in stack outputs", () => cli.outputUrl("url"), {
51 tries: 600,
52 delayMs: 1000,
53 });
54 const apiUrl = await cli.pollUntil("apiUrl in stack outputs", () => cli.outputUrl("apiUrl"));
55 const seedUrl = await cli.pollUntil("seedUrl in stack outputs", () => cli.outputUrl("seedUrl"));
56
57 // Dev identity: the emulated ALB's DNS resolves to 127.0.0.1 — no real
58 // AWS. The port is whatever the emulator gateway bound; only the URLs
59 // captured from the CLI's stdout are authoritative.
60 expect(url).not.toContain("amazonaws.com");
61 expect(cli.output).not.toContain("apply failed");
62
63 // Api service behind the shared ALB's `/api/*` rule.
64 const orders = (await (await fetchOk(apiUrl)).json()) as {
65 count: number;
66 orders: unknown[];
67 };
68 expect(orders.count).toBeNumber();
69
70 // Web service behind the catch-all `/*` rule (external nginx image).
71 const home = await (await fetchOk(url)).text();
72 expect(home).toContain("Server");
73
74 // POST /api/seed launches the one-shot SeedTask on the local cluster
75 // via the RunTask binding; it seeds three orders into the local table.
76 await fetchOk(seedUrl, { method: "POST" });
77 const seeded = await cli.pollUntil(
78 "seeded orders to appear",
79 async () => {
80 const res = await fetch(apiUrl);
81 if (!res.ok) return undefined;
82 const body = (await res.json()) as { count: number };
83 return body.count >= 3 ? body.count : undefined;
84 },
85 { tries: 120, delayMs: 1000 },
86 );
87 expect(seeded).toBeGreaterThanOrEqual(3);
88
89 // ── HOT RELOAD: rewrite src/Api.ts with the CLI still running — the
90 // dev watcher rebuilds the image and rolls the service's container ──
91 fs.writeFileSync(apiPath, apiSource.replace(MARKER, MARKER_V2));
92 const fallbackUrl = new URL(apiUrl);
93 fallbackUrl.pathname = "/api/ping"; // unmatched /api/* → fallback JSON
94 await cli.pollUntil(
95 "hot-swapped api (v2 marker)",
96 async () => {
97 try {
98 const res = await fetch(fallbackUrl);
99 if (!res.ok) return undefined;
100 const body = (await res.json()) as { service?: string };
101 return body.service === "orders-api-v2" ? true : undefined;
102 } catch {
103 return undefined; // mid-roll
104 }
105 },
106 { tries: 240, delayMs: 1000 },
107 );
108
109 // Restore — the swap back is itself a second hot reload and leaves
110 // the checked-in tree clean. The seeded data survives the roll.
111 fs.writeFileSync(apiPath, apiSource);
112 await cli.pollUntil(
113 "restored api (v1 marker)",
114 async () => {
115 try {
116 const res = await fetch(fallbackUrl);
117 if (!res.ok) return undefined;
118 const body = (await res.json()) as { service?: string };
119 return body.service === "orders-api" ? true : undefined;
120 } catch {
121 return undefined; // mid-roll
122 }
123 },
124 { tries: 240, delayMs: 1000 },
125 );
126 const afterReload = (await (await fetchOk(apiUrl)).json()) as {
127 count: number;
128 };
129 expect(afterReload.count).toBeGreaterThanOrEqual(3);
130 },
131 { timeout: 1_200_000 },
132 );