Open History to see the last commit
1/**
2 * True `alchemy dev` end-to-end for the TanStack Start site: spawns the
3 * REAL CLI (mirroring examples/aws-dev/test/dev.test.ts), which runs
4 * TanStack Start's own Vite dev server as the local `Website.Server`
5 * provider — no Fly Machine, no Fly, no S3; the only cloud touch is the
6 * state store.
7 *
8 * Coverage:
9 * - stack output → `url` is a local dev-server address (port is
10 * whatever the framework bound — never hard-coded)
11 * - SSR env parity → `/` renders the GREETING declared in alchemy.run.ts
12 * (read via a server function from `process.env`)
13 * - HOT RELOAD → editing src/routes/index.tsx is served by Vite's
14 * HMR without a redeploy
15 */
16import { afterAll, expect, test } from "bun:test";
17import { spawn, spawnSync } from "node:child_process";
18import * as fs from "node:fs";
19import * as path from "node:path";
20
21const root = path.resolve(import.meta.dirname, "..");
22const alchemyBin = path.join(root, "node_modules", "alchemy", "bin", "alchemy.js");
23const STAGE = "dev-cli-test";
24
25const pagePath = path.join(root, "src", "routes", "index.tsx");
26const pageSource = fs.readFileSync(pagePath, "utf8");
27const MARKER = "Styled with Tailwind CSS";
28const MARKER_V2 = "Styled with Tailwind CSS [dev-v2]";
29
30let proc: ReturnType<typeof spawn> | undefined;
31let output = "";
32
33const pump = (stream: NodeJS.ReadableStream) => {
34 stream.on("data", (chunk: Buffer) => {
35 const text = chunk.toString();
36 output += text;
37 if (process.env.DEBUG) process.stderr.write(text);
38 });
39};
40
41/** Bounded poll for a (possibly async) producer to yield a value. */
42const pollUntil = async <T>(
43 what: string,
44 f: () => T | undefined | Promise<T | undefined>,
45 { tries = 30, delayMs = 1000 }: { tries?: number; delayMs?: number } = {},
46): Promise<T> => {
47 for (let i = 0; i < tries; i++) {
48 const value = await f();
49 if (value !== undefined) return value;
50 await Bun.sleep(delayMs);
51 }
52 throw new Error(
53 `Timed out waiting for ${what}.\n--- alchemy dev output (tail) ---\n${output.slice(-4000)}`,
54 );
55};
56
57/** Fetch with retries — the dev server takes a moment to start serving. */
58const fetchOk = async (
59 url: string | URL,
60 { tries = 30, delayMs = 1000 }: { tries?: number; delayMs?: number } = {},
61) => {
62 let last: Response | undefined;
63 for (let i = 0; i < tries; i++) {
64 try {
65 last = await fetch(url);
66 if (last.ok) return last;
67 } catch {
68 // dev server not listening yet
69 }
70 await Bun.sleep(delayMs);
71 }
72 throw new Error(`GET ${url} never returned 2xx (last status: ${last?.status})`);
73};
74
75/** Extract the stack-output URL the CLI prints on stdout. */
76const outputUrl = () => output.match(/\burl:\s*['"]?(http[^\s'",]+)/)?.[1];
77
78afterAll(async () => {
79 fs.writeFileSync(pagePath, pageSource);
80
81 if (proc?.pid) {
82 const killGroup = (signal: NodeJS.Signals) => {
83 try {
84 process.kill(-proc!.pid!, signal);
85 } catch {
86 // group already gone
87 }
88 };
89 const exited = new Promise((resolve) => proc!.once("exit", resolve));
90 killGroup("SIGINT");
91 await Promise.race([exited, Bun.sleep(15_000)]);
92 if (proc.exitCode === null && proc.signalCode === null) {
93 killGroup("SIGKILL");
94 await Promise.race([exited, Bun.sleep(5_000)]);
95 }
96 }
97 if (!process.env.NO_DESTROY) {
98 spawnSync("bun", [alchemyBin, "destroy", "--stage", STAGE, "--yes"], {
99 cwd: root,
100 stdio: "inherit",
101 timeout: 120_000,
102 });
103 }
104}, 180_000);
105
106test(
107 "alchemy dev serves the TanStack Start site locally with hot reload",
108 async () => {
109 proc = spawn("bun", [alchemyBin, "dev", "--stage", STAGE], {
110 cwd: root,
111 detached: true,
112 stdio: ["ignore", "pipe", "pipe"],
113 });
114 pump(proc.stdout!);
115 pump(proc.stderr!);
116
117 const url = await pollUntil("url in stack outputs", outputUrl, {
118 tries: 180,
119 delayMs: 1000,
120 });
121
122 expect(new URL(url).hostname).toBe("localhost");
123 expect(url).not.toContain("example.invalid");
124
125 const home = await (await fetchOk(url)).text();
126 expect(home).toContain("Hello from TanStack Start on Fly!");
127 expect(home).toContain(MARKER);
128
129 fs.writeFileSync(pagePath, pageSource.replace(MARKER, MARKER_V2));
130 await pollUntil(
131 "hot-reloaded page (v2 marker)",
132 async () => {
133 try {
134 const res = await fetch(url);
135 if (!res.ok) return undefined;
136 const html = await res.text();
137 return html.includes(MARKER_V2) ? true : undefined;
138 } catch {
139 return undefined;
140 }
141 },
142 { tries: 120, delayMs: 500 },
143 );
144
145 fs.writeFileSync(pagePath, pageSource);
146 await pollUntil(
147 "restored page (v2 marker gone)",
148 async () => {
149 try {
150 const res = await fetch(url);
151 if (!res.ok) return undefined;
152 const html = await res.text();
153 return html.includes(MARKER_V2) ? undefined : true;
154 } catch {
155 return undefined;
156 }
157 },
158 { tries: 120, delayMs: 500 },
159 );
160 },
161 { timeout: 600_000 },
162);