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 Lambda, no
5 * CloudFront, 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 { DevCli, fetchOk } from "alchemy-test/DevCli";
19import * as fs from "node:fs";
20import * as path from "node:path";
21
22const root = path.resolve(import.meta.dirname, "..");
23// Isolated stage so this suite never fights integ.test.ts (same stack
24// name) over state rows.
25const STAGE = "dev-cli-test";
26const cli = new DevCli({ root, stage: STAGE });
27
28// Hot-reload surface: the SSR index page. The test rewrites it in place
29// with the CLI running, then restores it.
30const pagePath = path.join(root, "src", "routes", "+page.svelte");
31const pageSource = fs.readFileSync(pagePath, "utf8");
32const MARKER = "SvelteKit on AWS";
33const MARKER_V2 = "SvelteKit on AWS [dev-v2]";
34
35afterAll(async () => {
36 // Always leave the repo tree clean, even on a mid-reload failure.
37 fs.writeFileSync(pagePath, pageSource);
38
39 await cli.stop();
40 if (!process.env.NO_DESTROY) cli.destroy();
41}, 180_000);
42
43test(
44 "alchemy dev serves the SvelteKit site locally with hot reload",
45 async () => {
46 cli.start();
47
48 const url = await cli.outputUrlWhenReady("url", {
49 tries: 180,
50 delayMs: 1000,
51 });
52
53 // Dev identity: the framework dev server, not CloudFront. The port is
54 // whatever the framework bound — only the URL captured from the CLI's
55 // stdout is authoritative.
56 expect(new URL(url).hostname).toBe("localhost");
57 expect(url).not.toContain("cloudfront.net");
58
59 // SSR env parity: GREETING from alchemy.run.ts reaches the dev server
60 // (read via `process.env` in +page.server.ts's load).
61 const home = await (await fetchOk(url)).text();
62 expect(home).toContain("Hello from SvelteKit on AWS!");
63 expect(home).toContain(MARKER);
64
65 // Prerendered route serves through the dev server.
66 const about = await fetchOk(new URL("/about", url));
67 expect(about.status).toBe(200);
68
69 // Static asset from static/.
70 const robots = await (await fetchOk(new URL("/robots.txt", url))).text();
71 expect(robots).toContain("User-agent:");
72
73 // ── HOT RELOAD: rewrite the index page with the CLI still running —
74 // the framework dev server serves the new markup without a deploy ──
75 fs.writeFileSync(pagePath, pageSource.replace(MARKER, MARKER_V2));
76 await cli.pollUntil(
77 "hot-reloaded page (v2 marker)",
78 async () => {
79 try {
80 const res = await fetch(url);
81 if (!res.ok) return undefined;
82 const html = await res.text();
83 return html.includes(MARKER_V2) ? true : undefined;
84 } catch {
85 return undefined; // mid-reload
86 }
87 },
88 { tries: 120, delayMs: 500 },
89 );
90
91 // Restore — the swap back is itself a second hot reload and leaves
92 // the checked-in tree clean.
93 fs.writeFileSync(pagePath, pageSource);
94 await cli.pollUntil(
95 "restored page (v2 marker gone)",
96 async () => {
97 try {
98 const res = await fetch(url);
99 if (!res.ok) return undefined;
100 const html = await res.text();
101 return html.includes(MARKER_V2) ? undefined : true;
102 } catch {
103 return undefined; // mid-reload
104 }
105 },
106 { tries: 120, delayMs: 500 },
107 );
108 },
109 { timeout: 600_000 },
110);