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