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