1/**
2 * True `alchemy dev` end-to-end for the Next.js site: spawns the REAL CLI
3 * (mirroring examples/aws-dev/test/dev.test.ts), which runs `next dev` as
4 * the local `Website.Server` provider — no Lambda, no CloudFront, no S3;
5 * 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 * - API route → `/api/hello` serves the route handler
12 * - static assets → `/robots.txt` from public/
13 * - HOT RELOAD → editing app/page.jsx is served by Next's HMR
14 * without a redeploy
15 */
16import { afterAll, expect, test } from "bun:test";
17import { DevCli, fetchOk } from "alchemy-test/DevCli";
18import * as fs from "node:fs";
19import * as path from "node:path";
20
21const root = path.resolve(import.meta.dirname, "..");
22// Isolated stage so this suite never fights integ.test.ts (same stack
23// name) over state rows.
24const STAGE = "dev-cli-test";
25const cli = new DevCli({ root, stage: STAGE });
26
27// Hot-reload surface: the SSR index page. The test rewrites it in place
28// with the CLI running, then restores it.
29const pagePath = path.join(root, "app", "page.jsx");
30const pageSource = fs.readFileSync(pagePath, "utf8");
31const MARKER = "Next.js on AWS";
32const MARKER_V2 = "Next.js on AWS [dev-v2]";
33
34afterAll(async () => {
35 // Always leave the repo tree clean, even on a mid-reload failure.
36 fs.writeFileSync(pagePath, pageSource);
37
38 await cli.stop();
39 if (!process.env.NO_DESTROY) cli.destroy();
40}, 180_000);
41
42test(
43 "alchemy dev serves the Next.js site locally with hot reload",
44 async () => {
45 cli.start();
46
47 const url = await cli.outputUrlWhenReady("url", {
48 tries: 180,
49 delayMs: 1000,
50 });
51
52 // Dev identity: the framework dev server, not CloudFront. The port is
53 // whatever the framework bound — only the URL captured from the CLI's
54 // stdout is authoritative.
55 expect(new URL(url).hostname).toBe("localhost");
56 expect(url).not.toContain("cloudfront.net");
57
58 // SSR env parity: GREETING from alchemy.run.ts reaches the dev server.
59 const home = await (await fetchOk(url)).text();
60 expect(home).toContain("Hello from Next.js on AWS!");
61 expect(home).toContain(MARKER);
62
63 // App Router API route serves through the dev server.
64 const hello = (await (
65 await fetchOk(new URL("/api/hello", url))
66 ).json()) as { hello: string };
67 expect(hello).toEqual({ hello: "world" });
68
69 // Static asset from public/.
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);