"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 });
const vitest_1 = require("vitest");
const fs = __importStar(require("fs"));
const os = __importStar(require("os"));
const path = __importStar(require("path"));
const run_state_file_1 = require("../run-state-file");
let folder;
(0, vitest_1.beforeEach)(() => {
    folder = fs.mkdtempSync(path.join(os.tmpdir(), 'run-state-'));
});
(0, vitest_1.afterEach)(() => {
    fs.rmSync(folder, { recursive: true, force: true });
});
(0, vitest_1.describe)('run-state-file', () => {
    (0, vitest_1.it)('initialRunState 含所有节点 idle + iter=0', () => {
        const s = (0, run_state_file_1.initialRunState)('r1', 'flow1', [
            { id: 'n_a', type: 'user_input', label: '入口' },
            { id: 'n_b', type: 'llm' },
        ]);
        (0, vitest_1.expect)(s.runId).toBe('r1');
        (0, vitest_1.expect)(s.status).toBe('pending');
        (0, vitest_1.expect)(s.nodes.n_a.status).toBe('idle');
        (0, vitest_1.expect)(s.nodes.n_a.iter).toBe(0);
        (0, vitest_1.expect)(s.nodes.n_a.label).toBe('入口');
        (0, vitest_1.expect)(s.nodes.n_b.label).toBeUndefined();
    });
    (0, vitest_1.it)('writeRunState + readRunState 往返', () => {
        const init = (0, run_state_file_1.initialRunState)('r1', 'flow1', [{ id: 'n_a', type: 'llm' }]);
        (0, run_state_file_1.writeRunState)(folder, 'flow1', init);
        const read = (0, run_state_file_1.readRunState)(folder, 'flow1');
        (0, vitest_1.expect)(read?.runId).toBe('r1');
        (0, vitest_1.expect)(read?.nodes.n_a.type).toBe('llm');
    });
    (0, vitest_1.it)('文件不存在时 readRunState 返 null', () => {
        (0, vitest_1.expect)((0, run_state_file_1.readRunState)(folder, 'nope')).toBeNull();
    });
    (0, vitest_1.it)('版本号不为 1 时 readRunState 拒绝', () => {
        const target = path.join(folder, '.ccweb', 'tracks', 'flow1.run-state.json');
        fs.mkdirSync(path.dirname(target), { recursive: true });
        fs.writeFileSync(target, JSON.stringify({ version: 99, runId: 'x' }));
        (0, vitest_1.expect)((0, run_state_file_1.readRunState)(folder, 'flow1')).toBeNull();
    });
    (0, vitest_1.it)('patchRunState read-modify-write', () => {
        (0, run_state_file_1.writeRunState)(folder, 'flow1', (0, run_state_file_1.initialRunState)('r1', 'flow1', [{ id: 'n_a', type: 'llm' }]));
        (0, run_state_file_1.patchRunState)(folder, 'flow1', (cur) => ({
            ...cur,
            status: 'running',
            nodes: { ...cur.nodes, n_a: { ...cur.nodes.n_a, status: 'active', iter: 1 } },
        }));
        const read = (0, run_state_file_1.readRunState)(folder, 'flow1');
        (0, vitest_1.expect)(read?.status).toBe('running');
        (0, vitest_1.expect)(read?.nodes.n_a.status).toBe('active');
        (0, vitest_1.expect)(read?.nodes.n_a.iter).toBe(1);
    });
    (0, vitest_1.it)('patchRunState 在文件不存在时返 null（不创建）', () => {
        const r = (0, run_state_file_1.patchRunState)(folder, 'absent', (cur) => cur);
        (0, vitest_1.expect)(r).toBeNull();
    });
    (0, vitest_1.it)('getRunStateMtime 文件不存在返 0', () => {
        (0, vitest_1.expect)((0, run_state_file_1.getRunStateMtime)(folder, 'nope')).toBe(0);
    });
    (0, vitest_1.it)('getRunStateMtime 在 write 后递增', () => {
        (0, run_state_file_1.writeRunState)(folder, 'f', (0, run_state_file_1.initialRunState)('r', 'f', []));
        (0, vitest_1.expect)((0, run_state_file_1.getRunStateMtime)(folder, 'f')).toBeGreaterThan(0);
    });
    (0, vitest_1.it)('runStateRelativePath 返相对路径（给 LLM prompt 用）', () => {
        (0, vitest_1.expect)((0, run_state_file_1.runStateRelativePath)('research-loop')).toBe('.ccweb/tracks/research-loop.run-state.json');
    });
    (0, vitest_1.it)('LLM 模拟修改 done flag → readRunState 能读出', () => {
        (0, run_state_file_1.writeRunState)(folder, 'f', (0, run_state_file_1.initialRunState)('r', 'f', [{ id: 'n_a', type: 'llm' }]));
        // 模拟 LLM Edit 文件加 done=true
        (0, run_state_file_1.patchRunState)(folder, 'f', (cur) => ({
            ...cur,
            nodes: { ...cur.nodes, n_a: { ...cur.nodes.n_a, done: true } },
        }));
        const s = (0, run_state_file_1.readRunState)(folder, 'f');
        (0, vitest_1.expect)(s?.nodes.n_a.done).toBe(true);
        (0, vitest_1.expect)(s?.nodes.n_a.failed).toBeUndefined();
    });
    (0, vitest_1.it)('LLM 自报 failed=true + reason → readRunState 能读出', () => {
        (0, run_state_file_1.writeRunState)(folder, 'f', (0, run_state_file_1.initialRunState)('r', 'f', [{ id: 'n_a', type: 'llm' }]));
        (0, run_state_file_1.patchRunState)(folder, 'f', (cur) => ({
            ...cur,
            nodes: { ...cur.nodes, n_a: { ...cur.nodes.n_a, failed: true, reason: '用户输入不合理' } },
        }));
        const s = (0, run_state_file_1.readRunState)(folder, 'f');
        (0, vitest_1.expect)(s?.nodes.n_a.failed).toBe(true);
        (0, vitest_1.expect)(s?.nodes.n_a.reason).toBe('用户输入不合理');
    });
});
//# sourceMappingURL=run-state-file.test.js.map