(root: string, runBuild: BuildRunner = build)
| 59 | |
| 60 | /** Exported for isolated filesystem fixtures; Vitest calls the default wrapper. */ |
| 61 | export function ensureTestDist(root: string, runBuild: BuildRunner = build): void { |
| 62 | for (const part of ['engine', 'viewer'] as const) { |
| 63 | const stamp = path.join(root, 'dist', `.test-build-${part}.json`); |
| 64 | const inputs = inputFingerprint(root, part); |
| 65 | const outputs = outputSnapshot(root, part); |
| 66 | let current = false; |
| 67 | try { |
| 68 | const previous = JSON.parse(fs.readFileSync(stamp, 'utf8')); |
| 69 | current = previous.inputs === inputs && Object.keys(outputs).length > 0 |
| 70 | && JSON.stringify(previous.outputs) === JSON.stringify(outputs); |
| 71 | } catch { /* No valid successful-build record: rebuild. */ } |
| 72 | if (current) continue; |
| 73 | |
| 74 | process.stderr.write(`[test setup] ${part} build is missing or stale; rebuilding\n`); |
| 75 | // Never leave a successful stamp behind after a partially failed rebuild. |
| 76 | fs.rmSync(stamp, { force: true }); |
| 77 | try { |
| 78 | runBuild(part, root); |
| 79 | const built = outputSnapshot(root, part); |
| 80 | const sentinel = part === 'engine' ? 'dist/bin/codegraph.js' : 'dist/viewer/index.html'; |
| 81 | if (!fs.existsSync(path.join(root, sentinel))) throw new Error(`Build did not produce ${sentinel}`); |
| 82 | fs.writeFileSync(stamp, JSON.stringify({ inputs, outputs: built })); |
| 83 | } catch (error) { |
| 84 | throw new Error(`[test setup] ${part} build failed: ${error instanceof Error ? error.message : String(error)}`, { cause: error }); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | export default function setup(): void { |
| 90 | ensureTestDist(ROOT); |
no test coverage detected