// SPEC-452 — TDD gates for status transitions
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
const TDD_STUB_MARKER = "expect.fail('TDD stub";
export async function checkTddReadiness(specId, projectPath) {
    const specDir = join(projectPath, 'planu', 'specs');
    const { glob } = await import('glob');
    // Check for test-stubs.ts in spec dir
    const stubMatches = await glob(`${specDir}/${specId}-*/test-stubs.ts`);
    const hasStubs = stubMatches.length > 0;
    let stubCount = 0;
    if (hasStubs && stubMatches[0]) {
        const content = await readFile(stubMatches[0], 'utf-8');
        stubCount = (content.match(/it\(/g) ?? []).length;
    }
    // Check for test files referencing this spec
    const testsDir = join(projectPath, 'tests');
    const testFiles = await glob(`${testsDir}/**/*.test.ts`);
    let hasTestFiles = false;
    for (const tf of testFiles) {
        try {
            const content = await readFile(tf, 'utf-8');
            if (content.includes(specId)) {
                hasTestFiles = true;
                break;
            }
        }
        catch {
            // skip unreadable files
        }
    }
    const recommendation = !hasStubs && !hasTestFiles
        ? `No test stubs or test files found for ${specId}. Generate stubs first with generate_edge_tests or create tests manually.`
        : hasStubs && !hasTestFiles
            ? `Test stubs exist (${stubCount}) but no test files reference ${specId} yet. Copy stubs to tests/ and implement them.`
            : 'TDD readiness OK.';
    return { hasStubs, stubCount, hasTestFiles, recommendation };
}
export async function checkTddCompletion(specId, projectPath) {
    const specDir = join(projectPath, 'planu', 'specs');
    const { glob } = await import('glob');
    const stubMatches = await glob(`${specDir}/${specId}-*/test-stubs.ts`);
    if (stubMatches.length === 0 || !stubMatches[0]) {
        return { unresolvedStubs: 0, totalStubs: 0, resolvedPercentage: 100, pendingCriteria: [] };
    }
    const content = await readFile(stubMatches[0], 'utf-8');
    const allStubs = content.match(/it\(/g) ?? [];
    const unresolvedMatches = content.match(new RegExp(TDD_STUB_MARKER.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g')) ?? [];
    const totalStubs = allStubs.length;
    const unresolvedStubs = unresolvedMatches.length;
    const resolvedPercentage = totalStubs > 0 ? Math.round(((totalStubs - unresolvedStubs) / totalStubs) * 100) : 100;
    // Extract pending criteria text
    const pendingCriteria = [];
    const lines = content.split('\n');
    for (let i = 0; i < lines.length; i++) {
        const line = lines[i] ?? '';
        if (line.includes(TDD_STUB_MARKER)) {
            // Look at the it() line above
            const itLine = lines[i - 2] ?? lines[i - 1] ?? '';
            const match = /it\('([^']+)'/.exec(itLine);
            if (match?.[1]) {
                pendingCriteria.push(match[1]);
            }
        }
    }
    return { unresolvedStubs, totalStubs, resolvedPercentage, pendingCriteria };
}
//# sourceMappingURL=tdd-gate.js.map