@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

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