1/**
2 * True `alchemy dev` end-to-end for the SvelteKit site: spawns the REAL
3 * CLI (mirroring examples/aws-dev/test/dev.test.ts), which runs the Vite
4 * dev server as the local `Website.Server` provider — no Railway Service, no
5 * Railway, no S3; the only cloud touch is the state store.
6 *
7 * Coverage:
8 * - stack output → `url` is a local dev-server address (port is
9 * whatever the framework bound — never hard-coded)
10 * - SSR env parity → `/` renders the GREETING declared in alchemy.run.ts
11 * (read via `process.env` in +page.server.ts)
12 * - routing → `/about` (prerendered route) serves
13 * - static assets → `/robots.txt` from static/
14 * - HOT RELOAD → editing src/routes/+page.svelte is served by
15 * Vite's HMR without a redeploy
16 */
17import { afterAll, expect, test } from "bun:test";
18import { spawn, spawnSync } from "node:child_process";
19import * as fs from "node:fs";
20import * as path from "node:path";
21
22const root = path.resolve(import.meta.dirname, "..");
23// Spawn the CLI entry directly (not through `bun run` / the cli.js
24// launcher) so signals hit the actual CLI process, whose scope teardown
25// kills the dev server and the provider sidecars.
26const alchemyBin = path.join(
27 root,
28 "node_modules",
29 "alchemy",
30 "bin",
31 "alchemy.js",
32);
33// Isolated stage so this suite never fights integ.test.ts (same stack
34// name) over state rows.
35const STAGE = "dev-cli-test";
36
37// Hot-reload surface: the SSR index page. The test rewrites it in place
38// with the CLI running, then restores it.
39const pagePath = path.join(root, "src", "routes", "+page.svelte");
40const pageSource = fs.readFileSync(pagePath, "utf8");
41const MARKER = "SvelteKit on Railway";
42const MARKER_V2 = "SvelteKit on Railway [dev-v2]";
43
44let proc: ReturnType<typeof spawn> | undefined;
45let output = "";
46
47const pump = (stream: NodeJS.ReadableStream) => {
48 stream.on("data", (chunk: Buffer) => {
49 const text = chunk.toString();
50 output += text;
51 if (process.env.DEBUG) process.stderr.write(text);
52 });
53};
54
55/** Bounded poll for a (possibly async) producer to yield a value. */
56const pollUntil = async <T>(
57 what: string,
58 f: () => T | undefined | Promise<T | undefined>,
59 { tries = 30, delayMs = 1000 }: { tries?: number; delayMs?: number } = {},
60): Promise<T> => {
61 for (let i = 0; i < tries; i++) {
62 const value = await f();
63 if (value !== undefined) return value;
64 await Bun.sleep(delayMs);
65 }
66 throw new Error(
67 `Timed out waiting for ${what}.\n--- alchemy dev output (tail) ---\n${output.slice(-4000)}`,
68 );
69};
70
71/** Fetch with retries — the dev server takes a moment to start serving. */
72const fetchOk = async (
73 url: string | URL,
74 { tries = 30, delayMs = 1000 }: { tries?: number; delayMs?: number } = {},
75) => {
76 let last: Response | undefined;
77 for (let i = 0; i < tries; i++) {
78 try {
79 last = await fetch(url);
80 if (last.ok) return last;
81 } catch {
82 // dev server not listening yet
83 }
84 await Bun.sleep(delayMs);
85 }
86 throw new Error(
87 `GET ${url} never returned 2xx (last status: ${last?.status})`,
88 );
89};
90
91/** Extract the stack-output URL the CLI prints on stdout. */
92const outputUrl = () => output.match(/\burl:\s*['"]?(http[^\s'",]+)/)?.[1];
93
94afterAll(async () => {
95 // Always leave the repo tree clean, even on a mid-reload failure.
96 fs.writeFileSync(pagePath, pageSource);
97
98 if (proc?.pid) {
99 // Ctrl-C semantics: signal the whole PROCESS GROUP (the CLI, its
100 // `--watch` exec child, and the provider sidecars).
101 const killGroup = (signal: NodeJS.Signals) => {
102 try {
103 process.kill(-proc!.pid!, signal);
104 } catch {
105 // group already gone
106 }
107 };
108 const exited = new Promise((resolve) => proc!.once("exit", resolve));
109 killGroup("SIGINT");
110 await Promise.race([exited, Bun.sleep(15_000)]);
111 if (proc.exitCode === null && proc.signalCode === null) {
112 killGroup("SIGKILL");
113 await Promise.race([exited, Bun.sleep(5_000)]);
114 }
115 }
116 if (!process.env.NO_DESTROY) {
117 spawnSync("bun", [alchemyBin, "destroy", "--stage", STAGE, "--yes"], {
118 cwd: root,
119 stdio: "inherit",
120 timeout: 120_000,
121 });
122 }
123}, 180_000);
124
125test(
126 "alchemy dev serves the SvelteKit site locally with hot reload",
127 async () => {
128 proc = spawn("bun", [alchemyBin, "dev", "--stage", STAGE], {
129 cwd: root,
130 // Own process group, so teardown can deliver Ctrl-C to the whole
131 // tree the way a terminal would.
132 detached: true,
133 stdio: ["ignore", "pipe", "pipe"],
134 });
135 pump(proc.stdout!);
136 pump(proc.stderr!);
137
138 const url = await pollUntil("url in stack outputs", outputUrl, {
139 tries: 180,
140 delayMs: 1000,
141 });
142
143 // Dev identity: the framework dev server, not Railway. The port is
144 // whatever the framework bound — only the URL captured from the CLI's
145 // stdout is authoritative.
146 expect(new URL(url).hostname).toBe("localhost");
147 expect(url).not.toContain("example.invalid");
148
149 // SSR env parity: GREETING from alchemy.run.ts reaches the dev server
150 // (read via `process.env` in +page.server.ts's load).
151 const home = await (await fetchOk(url)).text();
152 expect(home).toContain("Hello from SvelteKit on Railway!");
153 expect(home).toContain(MARKER);
154
155 // Prerendered route serves through the dev server.
156 const about = await fetchOk(new URL("/about", url));
157 expect(about.status).toBe(200);
158
159 // Static asset from static/.
160 const robots = await (await fetchOk(new URL("/robots.txt", url))).text();
161 expect(robots).toContain("User-agent:");
162
163 // ── HOT RELOAD: rewrite the index page with the CLI still running —
164 // the framework dev server serves the new markup without a deploy ──
165 fs.writeFileSync(pagePath, pageSource.replace(MARKER, MARKER_V2));
166 await pollUntil(
167 "hot-reloaded page (v2 marker)",
168 async () => {
169 try {
170 const res = await fetch(url);
171 if (!res.ok) return undefined;
172 const html = await res.text();
173 return html.includes(MARKER_V2) ? true : undefined;
174 } catch {
175 return undefined; // mid-reload
176 }
177 },
178 { tries: 120, delayMs: 500 },
179 );
180
181 // Restore — the swap back is itself a second hot reload and leaves
182 // the checked-in tree clean.
183 fs.writeFileSync(pagePath, pageSource);
184 await pollUntil(
185 "restored page (v2 marker gone)",
186 async () => {
187 try {
188 const res = await fetch(url);
189 if (!res.ok) return undefined;
190 const html = await res.text();
191 return html.includes(MARKER_V2) ? undefined : true;
192 } catch {
193 return undefined; // mid-reload
194 }
195 },
196 { tries: 120, delayMs: 500 },
197 );
198 },
199 { timeout: 600_000 },
200);