"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 train_json_sync_1 = require("../train-json-sync");
const CWD_FILE = '.ccweb-flow-train.json';
let testDir;
(0, vitest_1.beforeEach)(() => {
    testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'sync-test-'));
});
(0, vitest_1.afterEach)(() => {
    fs.rmSync(testDir, { recursive: true, force: true });
});
(0, vitest_1.describe)('train-json-sync', () => {
    (0, vitest_1.it)('copyToProjectCwd 原子写 .ccweb-flow-train.json', () => {
        const snapshot = { a: 1, b: 'hello' };
        const ok = (0, train_json_sync_1.copyToProjectCwd)(testDir, snapshot);
        (0, vitest_1.expect)(ok).toBe(true);
        const trainJson = JSON.parse(fs.readFileSync(path.join(testDir, CWD_FILE), 'utf8'));
        (0, vitest_1.expect)(trainJson).toEqual(snapshot);
    });
    (0, vitest_1.it)('copyToProjectCwd 不写 train.json / workflow_data.json（避开用户业务文件）', () => {
        (0, train_json_sync_1.copyToProjectCwd)(testDir, { x: 1 });
        (0, vitest_1.expect)(fs.existsSync(path.join(testDir, 'train.json'))).toBe(false);
        (0, vitest_1.expect)(fs.existsSync(path.join(testDir, 'workflow_data.json'))).toBe(false);
    });
    (0, vitest_1.it)('reloadFromProjectCwd 读现有 .ccweb-flow-train.json', async () => {
        fs.writeFileSync(path.join(testDir, CWD_FILE), JSON.stringify({ x: 42 }), 'utf8');
        const r = await (0, train_json_sync_1.reloadFromProjectCwd)(testDir);
        (0, vitest_1.expect)(r.ok).toBe(true);
        (0, vitest_1.expect)(r.data).toEqual({ x: 42 });
    });
    (0, vitest_1.it)('reloadFromProjectCwd 找不到文件返 ok=false', async () => {
        const r = await (0, train_json_sync_1.reloadFromProjectCwd)(testDir);
        (0, vitest_1.expect)(r.ok).toBe(false);
    });
    (0, vitest_1.it)('reloadFromProjectCwd 非法 JSON：重试一次后报错', async () => {
        fs.writeFileSync(path.join(testDir, CWD_FILE), 'not json', 'utf8');
        const r = await (0, train_json_sync_1.reloadFromProjectCwd)(testDir);
        (0, vitest_1.expect)(r.ok).toBe(false);
        (0, vitest_1.expect)(r.error).toMatch(/JSON|parse/);
    });
    (0, vitest_1.it)('cleanupProjectCwd 删除 .ccweb-flow-train.json', () => {
        fs.writeFileSync(path.join(testDir, CWD_FILE), '{}', 'utf8');
        (0, train_json_sync_1.cleanupProjectCwd)(testDir);
        (0, vitest_1.expect)(fs.existsSync(path.join(testDir, CWD_FILE))).toBe(false);
    });
    (0, vitest_1.it)('cleanupProjectCwd 不碰用户项目自有的 train.json / workflow_data.json', () => {
        // 用户业务文件 — daemon 启动的 cleanupStaleCwdFiles 绝对不应抹掉这些
        fs.writeFileSync(path.join(testDir, 'train.json'), '{"user":"data"}', 'utf8');
        fs.writeFileSync(path.join(testDir, 'workflow_data.json'), '{"user":"data"}', 'utf8');
        (0, train_json_sync_1.cleanupProjectCwd)(testDir);
        (0, vitest_1.expect)(fs.existsSync(path.join(testDir, 'train.json'))).toBe(true);
        (0, vitest_1.expect)(fs.existsSync(path.join(testDir, 'workflow_data.json'))).toBe(true);
    });
    (0, vitest_1.it)('cleanupProjectCwd 文件不存在不报错', () => {
        (0, vitest_1.expect)(() => (0, train_json_sync_1.cleanupProjectCwd)(testDir)).not.toThrow();
    });
    (0, vitest_1.it)('filterByWhitelist 仅保留声明 key', () => {
        const r = (0, train_json_sync_1.filterByWhitelist)({ a: 1, b: 2, ghost: 3 }, ['a', 'b']);
        (0, vitest_1.expect)(r).toEqual({ a: 1, b: 2 });
    });
    (0, vitest_1.it)('filterByWhitelist 缺字段填 null', () => {
        const r = (0, train_json_sync_1.filterByWhitelist)({ a: 1 }, ['a', 'b']);
        (0, vitest_1.expect)(r).toEqual({ a: 1, b: null });
    });
});
//# sourceMappingURL=train-json-sync.test.js.map