Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

smoke-test.mjs

84 lines2.8 KB

1/**
2 * Smoke test: builds desktop + renderer, launches Electron against the
3 * production bundle, waits for the renderer to confirm it loaded, then exits.
4 *
5 * Catches the two categories of regression we've hit:
6 * 1. Module resolution failures (preload can't find @acme/contracts, etc.)
7 * 2. CSP / script blocking (React fails to mount)
8 *
9 * The test works by injecting a tiny check via ELECTRON_ENABLE_LOGGING —
10 * Electron forwards renderer console.log to the main process stdout when
11 * that env var is set. We look for React's "Download the React DevTools"
12 * message as proof that React successfully mounted (it only fires after
13 * the first render). For production mode (no React DevTools message), we
14 * instead check that no fatal errors appeared.
15 */
16import { spawn, execSync } from "node:child_process";
17import { dirname, resolve } from "node:path";
18import { fileURLToPath } from "node:url";
19​
20const __dirname = dirname(fileURLToPath(import.meta.url));
21const root = resolve(__dirname, "../../..");
22const desktopDir = resolve(__dirname, "..");
23const electronBin = resolve(desktopDir, "node_modules/.bin/electron");
24const mainJs = resolve(desktopDir, "dist-electron/main.js");
25​
26// ── Build first ──────────────────────────────────────────────────────
27console.log("Building contracts + renderer + desktop...");
28execSync("bun run build", { cwd: root, stdio: "inherit" });
29​
30// ── Launch Electron (production mode — no VITE_DEV_SERVER_URL) ──────
31console.log("\nLaunching Electron (production mode)...");
32​
33const child = spawn(electronBin, [mainJs], {
34 stdio: ["pipe", "pipe", "pipe"],
35 env: {
36 ...process.env,
37 VITE_DEV_SERVER_URL: "", // ensure production path
38 ELECTRON_ENABLE_LOGGING: "1",
39 },
40});
41​
42let output = "";
43​
44child.stdout.on("data", (d) => {
45 output += d.toString();
46});
47child.stderr.on("data", (d) => {
48 output += d.toString();
49});
50​
51const TIMEOUT_MS = 8_000;
52​
53const timer = setTimeout(() => {
54 child.kill();
55}, TIMEOUT_MS);
56​
57child.on("exit", () => {
58 clearTimeout(timer);
59​
60 // Fatal patterns that indicate broken builds
61 const fatalPatterns = [
62 "Cannot find module",
63 "MODULE_NOT_FOUND",
64 "Refused to execute", // CSP blocking scripts
65 "can't detect preamble", // @vitejs/plugin-react failure
66 "Uncaught Error",
67 "Uncaught TypeError",
68 "Uncaught ReferenceError",
69 ];
70​
71 const failures = fatalPatterns.filter((p) => output.includes(p));
72​
73 if (failures.length > 0) {
74 console.error("\n❌ Smoke test FAILED. Matched fatal patterns:");
75 for (const f of failures) {
76 console.error(` • ${f}`);
77 }
78 console.error("\nFull output:\n" + output);
79 process.exit(1);
80 }
81​
82 console.log("✅ Smoke test passed — no fatal errors detected");
83 process.exit(0);
84});