"use strict";
/**
 * test_host_installers_gemini_codex.ts — Stories 9.3 + 9.4 + 9.5
 *
 * Asserts that installForGeminiCli + installForCodex are no longer stubs,
 * mirror the claude-code pattern, and produce reversible uninstall.
 */
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 os = __importStar(require("os"));
const path = __importStar(require("path"));
const HostInstaller_1 = require("./cli/HostInstaller");
function assertTrue(cond, label) {
    if (!cond)
        throw new Error(`ASSERT_FAILED[${label}]`);
}
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'host-install-gc-'));
try {
    const installer = new HostInstaller_1.HostInstaller();
    // ── 1. gemini-cli install populates .gemini/ ────────────────────────
    const target1 = path.join(tmp, 'gemini-host');
    fs.mkdirSync(target1, { recursive: true });
    const r1 = installer.installForGeminiCli({ targetRoot: target1 });
    assertTrue(r1.host === 'gemini-cli', '[9.3] result host');
    assertTrue(fs.existsSync(path.join(target1, '.gemini', 'agents')), '[9.3] .gemini/agents created');
    const agents = fs.readdirSync(path.join(target1, '.gemini', 'agents')).filter((f) => f.startsWith('openlife-'));
    assertTrue(agents.length >= 5, `[9.3] >=5 agents installed (got ${agents.length})`);
    assertTrue(fs.existsSync(path.join(target1, '.gemini', 'commands', 'openlife')), '[9.3] commands namespace created');
    const cmds = fs.readdirSync(path.join(target1, '.gemini', 'commands', 'openlife'));
    assertTrue(cmds.length >= 4, `[9.3] >=4 commands installed (got ${cmds.length})`);
    assertTrue(fs.existsSync(path.join(target1, '.openlife', 'install-mcp-snippet-gemini-cli.json')), '[9.3] mcp snippet staged');
    console.log('[9.3] installForGeminiCli produced .gemini/{agents,commands,mcp} ✓');
    // ── 2. codex install populates .codex/ ─────────────────────────────
    const target2 = path.join(tmp, 'codex-host');
    fs.mkdirSync(target2, { recursive: true });
    const r2 = installer.installForCodex({ targetRoot: target2 });
    assertTrue(r2.host === 'codex', '[9.4] result host');
    assertTrue(fs.existsSync(path.join(target2, '.codex', 'agents')), '[9.4] .codex/agents created');
    assertTrue(fs.existsSync(path.join(target2, '.openlife', 'install-mcp-snippet-codex.json')), '[9.4] mcp snippet staged');
    console.log('[9.4] installForCodex produced .codex/{agents,commands,mcp} ✓');
    // ── 3. uninstall is reversible for gemini-cli ──────────────────────
    const u1 = installer.uninstallForGeminiCli({ targetRoot: target1 });
    assertTrue(u1.host === 'gemini-cli', '[9.3] uninstall result');
    const remainingAgents = fs.existsSync(path.join(target1, '.gemini', 'agents'))
        ? fs.readdirSync(path.join(target1, '.gemini', 'agents')).filter((f) => f.startsWith('openlife-'))
        : [];
    assertTrue(remainingAgents.length === 0, `[9.3] uninstall removes openlife-* agents (got ${remainingAgents.length})`);
    assertTrue(!fs.existsSync(path.join(target1, '.openlife', 'install-mcp-snippet-gemini-cli.json')), '[9.3] uninstall removes mcp snippet');
    console.log('[9.3] uninstallForGeminiCli reversible ✓');
    // ── 4. uninstall is reversible for codex ───────────────────────────
    const u2 = installer.uninstallForCodex({ targetRoot: target2 });
    assertTrue(u2.host === 'codex', '[9.4] uninstall result');
    console.log('[9.4] uninstallForCodex reversible ✓');
    // ── 5. dist-templates layout was scaffolded by 9.5 ─────────────────
    assertTrue(fs.existsSync(path.join(process.cwd(), 'dist-templates', 'gemini-cli', 'agents')), '[9.5] dist-templates/gemini-cli/agents exists');
    assertTrue(fs.existsSync(path.join(process.cwd(), 'dist-templates', 'codex', 'agents')), '[9.5] dist-templates/codex/agents exists');
    console.log('[9.5] dist-templates scaffolding present ✓');
}
finally {
    fs.rmSync(tmp, { recursive: true, force: true });
}
console.log('TEST_HOST_INSTALLERS_GEMINI_CODEX_OK');
