// engine/teammate-router/red-phase-verifier.ts — SPEC-750
// Runs the test suite and confirms that all expected tests are failing (RED phase).
import { execFile as execFileCb } from 'node:child_process';
import { promisify } from 'node:util';
const execFile = promisify(execFileCb);
/**
 * Verify that the tests listed in the handoff are failing (status: 'red').
 * If all expected failures pass, returns status: 'green' (bad — handoff was not red).
 */
export async function verifyRedPhase(handoff, projectRoot) {
    const ranAt = new Date().toISOString();
    try {
        // Run just the test files from the handoff
        const testArgs = ['--pool=forks', ...handoff.testFiles];
        await execFile('npx', ['vitest', 'run', ...testArgs], {
            cwd: projectRoot,
            timeout: 120_000,
        });
        // If execution succeeded (exit 0), tests passed — NOT red
        return {
            status: 'green',
            failures: [],
            ranAt,
        };
    }
    catch (err) {
        // Non-zero exit means tests failed — extract failure names
        const stderr = err instanceof Error && 'stderr' in err
            ? (err.stderr ?? '')
            : '';
        const failures = extractFailureNames(stderr, handoff.expectedFailures);
        return {
            status: 'red',
            failures,
            ranAt,
        };
    }
}
/**
 * Extract test failure names from stderr output.
 * Falls back to the expected failures list if parsing yields nothing.
 */
function extractFailureNames(stderr, expectedFailures) {
    const lines = stderr.split('\n');
    const found = [];
    for (const line of lines) {
        const trimmed = line.trim();
        if (trimmed.startsWith('✗') || trimmed.startsWith('×') || trimmed.includes('FAIL')) {
            found.push(trimmed);
        }
    }
    return found.length > 0 ? found : expectedFailures;
}
//# sourceMappingURL=red-phase-verifier.js.map