// engine/teammate-router/green-phase-verifier.ts — SPEC-750
// Verifies green phase: tests pass AND none of the red tests were deleted.
import { execFile as execFileCb } from 'node:child_process';
import { promisify } from 'node:util';
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
const execFile = promisify(execFileCb);
/**
 * Verify that:
 * 1. The tests from the red handoff now pass (green).
 * 2. None of the expected red tests were deleted by the Implementer.
 *
 * If tests were deleted: status='tampered'.
 * If tests still fail: status='red'.
 * If all pass and none deleted: status='green'.
 */
export async function verifyGreenPhase(redHandoff, projectRoot) {
    const ranAt = new Date().toISOString();
    // Step 1: Check that none of the expected failures were deleted
    const removedTests = await detectRemovedTests(redHandoff, projectRoot);
    if (removedTests.length > 0) {
        return { status: 'tampered', removedTests, ranAt };
    }
    // Step 2: Run the tests and expect them to pass now
    try {
        const testArgs = ['--pool=forks', ...redHandoff.testFiles];
        await execFile('npx', ['vitest', 'run', ...testArgs], {
            cwd: projectRoot,
            timeout: 120_000,
        });
        // Exit 0 = tests passed = green
        return { status: 'green', removedTests: [], ranAt };
    }
    catch {
        // Tests still failing — not green yet
        return { status: 'red', removedTests: [], ranAt };
    }
}
/**
 * Detect if any expectedFailure test names are no longer present in the test files.
 */
async function detectRemovedTests(handoff, projectRoot) {
    const removed = [];
    for (const testFile of handoff.testFiles) {
        let content;
        try {
            content = await readFile(join(projectRoot, testFile), 'utf-8');
        }
        catch {
            // Test file itself was deleted — flag all expected failures as removed
            removed.push(...handoff.expectedFailures.map((f) => `[deleted] ${f} (from ${testFile})`));
            continue;
        }
        // Check that each expected failure still appears in the file
        for (const failure of handoff.expectedFailures) {
            if (!content.includes(failure)) {
                removed.push(failure);
            }
        }
    }
    return removed;
}
//# sourceMappingURL=green-phase-verifier.js.map