import { readdirSync } from 'fs'; import { spawn } from 'child_process'; import path from 'path'; const testsDir = path.resolve('tests'); const files = readdirSync(testsDir) .filter(f => f.endsWith('.test.ts')) .sort(); const STALL_TIMEOUT = 20_000; // kill if no output for 20s const MAX_RETRIES = 3; // retry logic needed because tests get randomly get stuck on startup with bun // strace shows bun completely spinning in futex and not doing anything else. // Only a stall-kill is retried — a genuine assertion failure returns // {ok: false, stalled: false} and must NOT be retried, or a real product bug // that fails intermittently would be laundered into a pass. function runTest(filePath: string): Promise<{ ok: boolean; stalled: boolean }> { return new Promise((resolve) => { const child = spawn('bun', ['test', '--bail=1', '--timeout', '30000', filePath], { stdio: ['ignore', 'pipe', 'pipe'], }); let stalled = false; let timer = setTimeout(onStall, STALL_TIMEOUT); function onStall() { stalled = true; console.error(`\n[test-runner] stall detected, killing ${path.basename(filePath)} (no output for ${STALL_TIMEOUT / 1000}s)`); child.kill('SIGKILL'); } function resetTimer() { clearTimeout(timer); timer = setTimeout(onStall, STALL_TIMEOUT); } child.stdout!.on('data', (chunk: Buffer) => { process.stdout.write(chunk); resetTimer(); }); child.stderr!.on('data', (chunk: Buffer) => { process.stderr.write(chunk); resetTimer(); }); child.on('close', (code) => { clearTimeout(timer); resolve({ ok: code === 0, stalled }); }); }); } let passed = 0; let failed = 0; for (const file of files) { const filePath = path.join(testsDir, file); let ok = false; for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) { if (attempt > 1) { console.log(`[test-runner] retrying ${file} (attempt ${attempt}/${MAX_RETRIES})`); } const result = await runTest(filePath); ok = result.ok; // Retry only the futex stall — a genuine failure is final. if (ok || !result.stalled) break; } if (ok) passed++; else failed++; } console.log(`\n${passed + failed} test files: ${passed} passed${failed ? `, ${failed} failed` : ''}`); if (failed > 0) process.exit(1);