"use strict";
// src/test_catalog_step_executor.ts
// Phase 5: CatalogStepExecutor runs workflow steps through real catalog/method
// agents (via CatalogAgentExecutor), with graceful degradation.
Object.defineProperty(exports, "__esModule", { value: true });
const CatalogStepExecutor_1 = require("./orchestrator/workflow/CatalogStepExecutor");
const CatalogAgentExecutor_1 = require("./orchestrator/CatalogAgentExecutor");
let passed = 0;
let failed = 0;
function check(label, ok, detail) {
    if (ok) {
        console.log(`  ✓ ${label}`);
        passed++;
    }
    else {
        console.error(`  ✗ ${label}${detail ? ` — ${detail}` : ''}`);
        failed++;
    }
}
class MockBrain {
    calls = 0;
    async think(_sys, user) {
        this.calls++;
        return `DID: ${user.slice(0, 30)}`;
    }
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function step(s) { return s; }
async function main() {
    console.log('test_catalog_step_executor: starting\n');
    const brain = new MockBrain();
    const agentExec = new CatalogAgentExecutor_1.CatalogAgentExecutor(brain);
    const stepExec = new CatalogStepExecutor_1.CatalogStepExecutor(agentExec);
    console.log('[1] elicit step → awaiting_input');
    const r1 = await stepExec.execute({ runId: 'r', step: step({ id: 's1', elicit: true }), context: {} });
    check('elicit returns awaiting_input', r1.status === 'awaiting_input');
    check('elicit did not call brain', brain.calls === 0);
    console.log('\n[2] structural step (no agent) → completed');
    const r2 = await stepExec.execute({ runId: 'r', step: step({ id: 's2', creates: ['spec.md'] }), context: {} });
    check('structural step completed', r2.status === 'completed');
    check('artifacts carried through', (r2.artifacts || []).includes('spec.md'));
    check('marked structural', !!r2.result && r2.result.includes('structural'));
    console.log('\n[3] step naming a method agent → runs it');
    // openlife-builder resolves to .openlife/method/agents/builder.md
    const r3 = await stepExec.execute({
        runId: 'r',
        step: step({ id: 's3', agent: 'openlife-builder', action: 'implement the login form', creates: ['login.tsx'] }),
        context: { storyId: '2.1' },
    });
    check('method agent step completed', r3.status === 'completed', r3.error);
    check('brain was invoked for the agent step', brain.calls === 1);
    check('result records the agent', !!r3.result && r3.result.includes('openlife-builder'));
    check('artifacts carried through', (r3.artifacts || []).includes('login.tsx'));
    console.log('\n[4] unresolvable agent → completed-as-structural (no crash)');
    const r4 = await stepExec.execute({
        runId: 'r',
        step: step({ id: 's4', agent: '__ghost_agent__', action: 'do x' }),
        context: {},
    });
    check('unresolvable agent does not fail the run', r4.status === 'completed');
    check('skip reason recorded', !!r4.result && r4.result.includes('agent_not_found'));
    check('brain not called for ghost agent', brain.calls === 1);
    console.log('\n[5] agent execution failure → step failed');
    // Force failure: an executor whose agent loads but brain throws
    class ThrowBrain {
        async think() { throw new Error('llm exploded'); }
    }
    const failExec = new CatalogStepExecutor_1.CatalogStepExecutor(new CatalogAgentExecutor_1.CatalogAgentExecutor(new ThrowBrain()));
    const r5 = await failExec.execute({
        runId: 'r',
        step: step({ id: 's5', agent: 'openlife-builder', action: 'x' }),
        context: {},
    });
    check('agent failure surfaces as failed step', r5.status === 'failed');
    check('error is populated', !!r5.error);
    console.log('');
    console.log(`Results: ${passed} passed, ${failed} failed`);
    if (failed > 0) {
        console.error('TEST_CATALOG_STEP_EXECUTOR_FAIL');
        process.exit(1);
    }
    console.log('✅ test_catalog_step_executor: all checks passed');
}
main().catch(err => { console.error(err); process.exit(1); });
