| ... | ... | @@ -1,58 +1,84 @@ |
| 1 | 1 | /** |
| 2 | | * Smoke test: launches Electron with the production build, |
| 3 | | * waits for the page to render, checks for console errors, and exits. |
| 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. |
| 4 | 15 | */ |
| 5 | | import { spawn } from "node:child_process"; |
| 6 | | import { resolve, dirname } from "node:path"; |
| 16 | import { spawn, execSync } from "node:child_process"; |
| 17 | import { dirname, resolve } from "node:path"; |
| 7 | 18 | import { fileURLToPath } from "node:url"; |
| 8 | 19 | |
| 9 | 20 | const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 10 | | const electronPath = resolve( |
| 11 | | __dirname, |
| 12 | | "../node_modules/.bin/electron" |
| 13 | | ); |
| 14 | | const mainPath = resolve(__dirname, "../dist-electron/main.js"); |
| 21 | const root = resolve(__dirname, "../../.."); |
| 22 | const desktopDir = resolve(__dirname, ".."); |
| 23 | const electronBin = resolve(desktopDir, "node_modules/.bin/electron"); |
| 24 | const mainJs = resolve(desktopDir, "dist-electron/main.js"); |
| 15 | 25 | |
| 16 | | const child = spawn(electronPath, [mainPath], { |
| 26 | // ── Build first ────────────────────────────────────────────────────── |
| 27 | console.log("Building contracts + renderer + desktop..."); |
| 28 | execSync("bun run build", { cwd: root, stdio: "inherit" }); |
| 29 | |
| 30 | // ── Launch Electron (production mode — no VITE_DEV_SERVER_URL) ────── |
| 31 | console.log("\nLaunching Electron (production mode)..."); |
| 32 | |
| 33 | const child = spawn(electronBin, [mainJs], { |
| 17 | 34 | stdio: ["pipe", "pipe", "pipe"], |
| 18 | 35 | env: { |
| 19 | 36 | ...process.env, |
| 20 | | // Force production mode (no VITE_DEV_SERVER_URL) |
| 37 | VITE_DEV_SERVER_URL: "", // ensure production path |
| 21 | 38 | ELECTRON_ENABLE_LOGGING: "1", |
| 22 | | SMOKE_TEST: "1", |
| 23 | 39 | }, |
| 24 | 40 | }); |
| 25 | 41 | |
| 26 | | let stdout = ""; |
| 27 | | let stderr = ""; |
| 42 | let output = ""; |
| 28 | 43 | |
| 29 | | child.stdout.on("data", (data) => { |
| 30 | | stdout += data.toString(); |
| 31 | | process.stdout.write(data); |
| 44 | child.stdout.on("data", (d) => { |
| 45 | output += d.toString(); |
| 32 | 46 | }); |
| 33 | | |
| 34 | | child.stderr.on("data", (data) => { |
| 35 | | stderr += data.toString(); |
| 36 | | process.stderr.write(data); |
| 47 | child.stderr.on("data", (d) => { |
| 48 | output += d.toString(); |
| 37 | 49 | }); |
| 38 | 50 | |
| 39 | | // Give the app 5 seconds to start then kill it |
| 40 | | const timeout = setTimeout(() => { |
| 51 | const TIMEOUT_MS = 8_000; |
| 52 | |
| 53 | const timer = setTimeout(() => { |
| 41 | 54 | child.kill(); |
| 42 | | }, 5000); |
| 55 | }, TIMEOUT_MS); |
| 56 | |
| 57 | child.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 | ]; |
| 43 | 70 | |
| 44 | | child.on("exit", (code) => { |
| 45 | | clearTimeout(timeout); |
| 46 | | const hasErrors = |
| 47 | | stderr.includes("Error") || |
| 48 | | stderr.includes("Cannot find module") || |
| 49 | | stderr.includes("MODULE_NOT_FOUND"); |
| 71 | const failures = fatalPatterns.filter((p) => output.includes(p)); |
| 50 | 72 | |
| 51 | | if (hasErrors) { |
| 52 | | console.error("\nā Smoke test FAILED — errors detected in stderr"); |
| 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); |
| 53 | 79 | process.exit(1); |
| 54 | 80 | } |
| 55 | 81 | |
| 56 | | console.log("\nā
Smoke test passed — no fatal errors detected"); |
| 82 | console.log("ā
Smoke test passed — no fatal errors detected"); |
| 57 | 83 | process.exit(0); |
| 58 | 84 | }); |