import { loadFixtures } from '@mmnto/totem';
import { loadCompiledHooks } from './loader.js';
import { evaluateHook } from './runtime.js';
export function runHookTests(options) {
    const { hooks, warnings, errors } = loadCompiledHooks({
        manifestPath: options.manifestPath,
        installedPackVersions: options.installedPackVersions,
    });
    const fixtures = loadFixtures(options.testsDir).filter((f) => f.surface === 'hooks');
    const hooksById = new Map(hooks.map((h) => [h.id, h]));
    const results = [];
    const unknownHooks = [];
    for (const fixture of fixtures) {
        const hook = hooksById.get(fixture.ruleHash);
        if (!hook) {
            unknownHooks.push({ fixturePath: fixture.fixturePath, hookId: fixture.ruleHash });
            continue;
        }
        results.push(testHook(hook, fixture));
    }
    const passed = results.filter((r) => r.passed).length;
    return {
        total: results.length,
        passed,
        failed: results.length - passed,
        unknownHooks,
        results,
        loadWarnings: warnings,
        loadErrors: errors.map((e) => ({ code: e.code, message: e.message })),
    };
}
function testHook(hook, fixture) {
    const failures = [];
    const tool = hook.trigger.tool;
    for (const args of fixture.failLines) {
        const payload = { tool, args };
        const decision = evaluateHook(hook, payload).decision;
        if (decision !== 'reject') {
            failures.push({ line: args, expected: 'reject', actual: decision });
        }
    }
    for (const args of fixture.passLines) {
        const payload = { tool, args };
        const decision = evaluateHook(hook, payload).decision;
        if (decision !== 'allow') {
            failures.push({ line: args, expected: 'allow', actual: decision });
        }
    }
    return {
        hookId: hook.id,
        packId: hook.packId,
        fixturePath: fixture.fixturePath,
        failures,
        passed: failures.length === 0,
    };
}
//# sourceMappingURL=test-runner.js.map