"use strict";
// src/test_chat_tui.ts
// Tests for the ChatTui non-TTY surface area:
// - buildInventoryStats reads .catalog counts correctly
// - reading models.json returns the active model
// - session log appends to .openlife/chat-sessions/<id>.jsonl
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 fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const ChatTui_1 = require("./cli/ChatTui");
let failed = 0;
function check(label, condition, detail) {
    if (condition) {
        console.log(`✅ ${label}`);
    }
    else {
        console.error(`❌ ${label}${detail ? ` — ${detail}` : ''}`);
        failed++;
    }
}
function makeTempRoot() {
    const root = fs.mkdtempSync(path.join(os.tmpdir(), 'openlife-chat-tui-'));
    // .catalog skeleton with a couple of fake entries
    fs.mkdirSync(path.join(root, '.catalog', 'agents', 'agent-a'), { recursive: true });
    fs.mkdirSync(path.join(root, '.catalog', 'agents', 'agent-b'), { recursive: true });
    fs.mkdirSync(path.join(root, '.catalog', 'squads', 'squad-a'), { recursive: true });
    fs.mkdirSync(path.join(root, '.catalog', 'skills', 'skill-a'), { recursive: true });
    fs.mkdirSync(path.join(root, '.catalog', 'mcps'), { recursive: true });
    // package.json with version
    fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify({ name: 'test', version: '9.9.9' }), 'utf-8');
    // models.json with a primary
    fs.writeFileSync(path.join(root, 'models.json'), JSON.stringify({ primary: { provider: 'p', name: 'm', raw: 'p/m' }, fallbacks: [] }), 'utf-8');
    return root;
}
// 1. buildInventoryStats reads .catalog and models.json
{
    const root = makeTempRoot();
    try {
        const stats = (0, ChatTui_1.buildInventoryStats)(root, 'sess-1');
        check('version reads from package.json', stats.version === 'v9.9.9', `got=${stats.version}`);
        check('model reads from models.json', stats.model === 'p/m', `got=${stats.model}`);
        check('agents counted', stats.agents === 2, `got=${stats.agents}`);
        check('squads counted', stats.squads === 1, `got=${stats.squads}`);
        check('skills counted', stats.skills === 1, `got=${stats.skills}`);
        check('mcps counted (0 ok)', stats.mcps === 0, `got=${stats.mcps}`);
        check('sessionId echoed', stats.sessionId === 'sess-1');
        check('cwd reflects root', stats.cwd === root);
    }
    finally {
        fs.rmSync(root, { recursive: true, force: true });
    }
}
// 2. test-* directories are ignored (matches clean-test-pollution semantics)
{
    const root = makeTempRoot();
    try {
        fs.mkdirSync(path.join(root, '.catalog', 'agents', 'test-pollution-1'), { recursive: true });
        const stats = (0, ChatTui_1.buildInventoryStats)(root, 's');
        check('test-* agents excluded from counts', stats.agents === 2, `got=${stats.agents} (should ignore test-pollution-1)`);
    }
    finally {
        fs.rmSync(root, { recursive: true, force: true });
    }
}
// 3. Missing .catalog returns zeros, doesn't throw
{
    const root = fs.mkdtempSync(path.join(os.tmpdir(), 'openlife-chat-tui-empty-'));
    fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify({ name: 'x', version: '1.0.0' }), 'utf-8');
    try {
        const stats = (0, ChatTui_1.buildInventoryStats)(root, 's');
        check('empty .catalog yields zero counts', stats.agents === 0 && stats.squads === 0 && stats.skills === 0 && stats.mcps === 0);
        check('missing models.json yields unknown', stats.model === 'unknown');
    }
    finally {
        fs.rmSync(root, { recursive: true, force: true });
    }
}
if (failed > 0) {
    console.error(`\n${failed} check(s) failed.`);
    process.exit(1);
}
console.log('\nTEST_CHAT_TUI_OK');
