"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * E2E smoke: build FlowV3 → runtime → run to completion.
 *
 * Uses a mock injector that simulates LLM behaviour by writing predetermined
 * values into train.json after prompt injection. Covers the research-loop
 * example from spec §2:
 *
 *   user_input → llm(research) → llm(check) → if has_error → loop / end
 *
 * check node returns has_error=true on 1st call (loop), false on 2nd (end).
 * user input (area=逆合成) is submitted asynchronously via setTimeout.
 *
 * 5 checks:
 *   1. run completed
 *   2. check node called 2 times
 *   3. flow_node_active emitted >= 4 times
 *   4. flow_done emitted
 *   5. has_error final value = false
 */
const fs = __importStar(require("fs"));
const os = __importStar(require("os"));
const path = __importStar(require("path"));
const runtime_1 = require("../runtime");
const run_registry_1 = require("../run-registry");
const results = [];
function check(name, ok, detail) {
    results.push({ name, ok, detail });
    const mark = ok ? '✓' : '✗';
    console.log(`${mark} ${name}${detail ? `  — ${detail}` : ''}`);
}
async function main() {
    const testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'verify-flow-v3-'));
    let checkCallCount = 0;
    /**
     * Mock injector: reads current train.json written by dispatchLlmCall,
     * sets the appropriate fields, then atomically writes it back.
     *
     * A 10ms sleep ensures the mtime of the re-written file is strictly
     * greater than the mtime recorded by dispatchLlmCall right after its
     * own copyToProjectCwd write.
     */
    const mockInjector = async (prompt) => {
        await new Promise((r) => setTimeout(r, 10));
        const trainJsonPath = path.join(testDir, 'train.json');
        let current = {};
        try {
            current = JSON.parse(fs.readFileSync(trainJsonPath, 'utf8'));
        }
        catch {
            /* file may not exist yet; start from empty */
        }
        if (prompt.includes('请调研')) {
            // research node: provide ref_fp
            current.ref_fp = './test.bibtex';
        }
        else if (prompt.includes('请检查')) {
            // check node: first call → has_error=true (loop); second → false (end)
            checkCallCount += 1;
            current.has_error = checkCallCount === 1 ? true : false;
        }
        // Atomic write so mtime is updated and file is valid JSON
        const tmp = `${trainJsonPath}.tmp.${process.pid}.${Date.now()}`;
        fs.writeFileSync(tmp, JSON.stringify(current), 'utf8');
        fs.renameSync(tmp, trainJsonPath);
    };
    // Research-loop FlowV3 definition
    const flow = {
        version: 3,
        trackName: 'research-loop',
        adapter: 'claude-code',
        variables: [
            { key: 'area', description: '研究领域', initialValue: null },
            { key: 'ref_fp', description: '文献存储 bibtex 格式文件的路径', initialValue: null },
            { key: 'has_error', description: '文献存在错误', initialValue: null },
        ],
        nodes: [
            {
                id: 'n_input', type: 'user_input', position: { x: 0, y: 0 },
                fields: [{ varKey: 'area', uiHint: 'text' }],
            },
            {
                id: 'n_research', type: 'llm', position: { x: 0, y: 100 },
                promptTemplate: '请调研@{area}的科研论文，结果填写到${ref_fp}中',
                inputs: ['area'], outputs: ['ref_fp'],
            },
            {
                id: 'n_check', type: 'llm', position: { x: 0, y: 200 },
                promptTemplate: '请检查@{ref_fp}中的论文，相关性@{area}，结果${has_error}',
                inputs: ['area', 'ref_fp'], outputs: ['has_error'],
            },
            {
                id: 'n_if', type: 'if', position: { x: 0, y: 300 },
                conditionExpr: 'has_error == true',
            },
        ],
        edges: [
            { id: 'e1', source: 'n_input', target: 'n_research' },
            { id: 'e2', source: 'n_research', target: 'n_check' },
            { id: 'e3', source: 'n_check', target: 'n_if' },
            { id: 'e4', source: 'n_if', sourceHandle: 'true', target: 'n_check' }, // retry loop
            { id: 'e5', source: 'n_if', sourceHandle: 'false', target: null }, // end
        ],
    };
    const runId = 'verify_run_v3_1';
    const events = [];
    run_registry_1.flowRunRegistry.start({ runId, projectId: 'p_verify', basename: 'research-loop' });
    // Submit user input asynchronously (simulates frontend interaction)
    setTimeout(() => {
        (0, runtime_1.submitUserInputForRun)(runId, { area: '逆合成' });
    }, 100);
    await (0, runtime_1.runFlow)(flow, {}, {
        projectFolder: testDir,
        basename: 'research-loop',
        runId,
        injector: mockInjector,
        broadcast: (event, payload) => events.push({ type: event, payload }),
    });
    // ── Assertions ──────────────────────────────────────────────────────────
    const info = run_registry_1.flowRunRegistry.get(runId);
    check('run completed', info?.status === 'completed', `status=${info?.status}`);
    check('check node called 2 times (loop once then end)', checkCallCount === 2, `checkCallCount=${checkCallCount}`);
    const activeCount = events.filter((e) => e.type === 'flow_node_active').length;
    check('flow_node_active emitted >= 4 times', activeCount >= 4, `count=${activeCount}`);
    check('flow_done emitted', events.some((e) => e.type === 'flow_done'));
    const hasErrorEvents = events.filter((e) => e.type === 'flow_var_changed' && e.payload.key === 'has_error');
    const lastHasError = hasErrorEvents.at(-1)?.payload.value;
    check('has_error final value = false', lastHasError === false, `last has_error=${JSON.stringify(lastHasError)}`);
    // ── Summary ─────────────────────────────────────────────────────────────
    fs.rmSync(testDir, { recursive: true, force: true });
    const failures = results.filter((r) => !r.ok);
    console.log(`\n${results.length - failures.length}/${results.length} checks passed`);
    if (failures.length > 0) {
        console.log('FAILED:');
        failures.forEach((r) => console.log(`  ✗ ${r.name}${r.detail ? ` — ${r.detail}` : ''}`));
        process.exit(1);
    }
}
main().catch((e) => {
    console.error('verify-flow-v3 failed with exception:', e);
    process.exit(1);
});
//# sourceMappingURL=verify-flow-v3.js.map