"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const RuntimePolicy_1 = require("./orchestrator/RuntimePolicy");
function assert(condition, message) {
    if (!condition)
        throw new Error(message);
}
async function main() {
    const runtime = new RuntimePolicy_1.RuntimePolicy();
    runtime.recordResult('gemini', false, 'quota exhausted');
    runtime.recordResult('claude', false, 'auth expired');
    runtime.recordResult('codex', true, 'ok');
    // Pass explicit executors so the test is deterministic regardless of the
    // repo's models.json (otherwise getStrictModelExecutors() reads cwd's
    // models.json and biases `base` to a single executor when the chain has
    // length 1 — which is the case in this repo today). The test exercises
    // the health/cooldown filter, not chain-detection from disk.
    //
    // Also unset GEMINI_API_KEY in this process so the runtime's gemini-api
    // bypass (which keeps gemini preferred when the key is exported even if
    // cooling down) doesn't mask the cooldown assertion. CI/local env may
    // export it for other tests; we restore it after.
    const previousGeminiKey = process.env.GEMINI_API_KEY;
    delete process.env.GEMINI_API_KEY;
    try {
        const baseExecutors = ['gemini', 'claude', 'codex'];
        const research = runtime.decide('RESEARCH_ANALYSIS', [...baseExecutors]);
        assert(!research.preferred.includes('gemini'), 'gemini should enter cooldown after quota failure');
        assert(research.preferred.includes('codex'), 'research should fallback to codex when gemini is unhealthy');
        const engineering = runtime.decide('ENGINEERING_BUILD', [...baseExecutors]);
        assert(!engineering.preferred.includes('claude'), 'claude should enter cooldown after auth failure');
        assert(engineering.preferred[0] === 'codex', 'codex should become preferred when secondary executors cool down');
    }
    finally {
        if (previousGeminiKey !== undefined)
            process.env.GEMINI_API_KEY = previousGeminiKey;
    }
    const snapshot = runtime.saveSnapshot();
    assert(!!snapshot, 'runtime policy snapshot should be saved');
    console.log('TEST_RUNTIME_POLICY_OK');
}
main().catch((error) => {
    console.error(error.message || error);
    process.exit(1);
});
