// engine/spec-coverage/test-matchers.ts — Test-to-criterion matching strategies.
// Extracted from criteria-mapper.ts to keep file sizes within limits.
import { extractTestName, extractAllTestNames } from './keyword-extractor.js';
/** Strategy 1: look for AC-ID or AC ID (with space or no dash) in test content. */
export function findDirectReferences(acId, testFiles, testContents) {
    const matches = [];
    // Build regex variants: AC-01, AC01, #AC-01, AC 01
    const acNum = acId.replace('AC-', '');
    const pattern = new RegExp(`\\b(?:AC[-\\s]?${acNum}|#AC-?${acNum})\\b`, 'i');
    for (const tf of testFiles) {
        const content = testContents.get(tf.file) ?? '';
        if (!pattern.test(content)) {
            continue;
        }
        // Extract the test name from the first matching line
        const testName = extractTestName(content, pattern);
        matches.push({ file: tf.file, testName, strategy: 'direct-reference' });
    }
    return matches;
}
/** Strategy 2: look for SPEC-ID in file path or content. */
export function findSpecReferences(specId, testFiles, testContents) {
    const matches = [];
    const specPattern = new RegExp(`\\b${specId.replace('-', '[-_]?')}\\b`, 'i');
    for (const tf of testFiles) {
        const inPath = specPattern.test(tf.file);
        const content = testContents.get(tf.file) ?? '';
        const inContent = specPattern.test(content);
        if (!inPath && !inContent) {
            continue;
        }
        const testName = inContent ? extractTestName(content, specPattern) : tf.file;
        matches.push({ file: tf.file, testName, strategy: 'spec-reference' });
    }
    return matches;
}
/** Strategy 3: match keywords from criterion text against test names in files. */
export function findKeywordMatches(keywords, testFiles, testContents) {
    const matches = [];
    for (const tf of testFiles) {
        const content = testContents.get(tf.file) ?? '';
        // Extract all describe/it/test block names
        const testNames = extractAllTestNames(content);
        const combinedText = [...testNames, tf.file].join(' ').toLowerCase();
        const matchCount = keywords.filter((kw) => combinedText.includes(kw.toLowerCase())).length;
        if (matchCount >= 2) {
            matches.push({
                file: tf.file,
                /* v8 ignore next 1 */
                testName: testNames[0] ?? tf.file,
                strategy: 'heuristic',
            });
        }
    }
    return matches;
}
// Re-export extractKeywords for backward compatibility
export { extractKeywords } from './keyword-extractor.js';
//# sourceMappingURL=test-matchers.js.map