"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 });
/*
 * Story 3.1 — Host enum + validator regression test.
 *
 * Asserts:
 * 1. validateHost accepts the 3 canonical hosts (claude-code, gemini-cli, codex)
 * 2. validateHost rejects unknown values with clear error message
 * 3. validateHost falls back to DEFAULT_HOST on empty/null/undefined
 * 4. detectHostFromEnv picks the right host based on env signals
 * 5. InstallFlow.run() with valid --host runs without throwing
 * 6. InstallFlow.run() with invalid --host throws clear error
 * 7. buildNextCommands no longer references the deprecated `mode set` command
 */
const InstallFlow_1 = require("./cli/InstallFlow");
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
function assert(cond, msg) {
    if (!cond)
        throw new Error(`ASSERTION_FAILED: ${msg}`);
}
function testValidateHostAcceptsCanonical() {
    for (const h of InstallFlow_1.VALID_HOSTS) {
        const result = (0, InstallFlow_1.validateHost)(h);
        assert(result === h, `validateHost('${h}') should return '${h}', got '${result}'`);
    }
}
function testValidateHostRejectsUnknown() {
    const cases = ['cursor', 'vscode', 'random', 'CLAUDE-CODE-TYPO', ''];
    for (const bad of cases) {
        if (bad === '')
            continue; // empty falls to default — tested separately
        let threw = false;
        try {
            (0, InstallFlow_1.validateHost)(bad);
        }
        catch (err) {
            threw = true;
            assert(err.message.includes('INVALID_HOST') && err.message.includes('claude-code'), `Error should mention INVALID_HOST and list valid hosts; got: ${err.message}`);
        }
        assert(threw, `validateHost('${bad}') should have thrown but didn't`);
    }
}
function testValidateHostFallsBackToDefault() {
    assert((0, InstallFlow_1.validateHost)(undefined) === InstallFlow_1.DEFAULT_HOST, 'undefined should fall to DEFAULT_HOST');
    assert((0, InstallFlow_1.validateHost)(null) === InstallFlow_1.DEFAULT_HOST, 'null should fall to DEFAULT_HOST');
    assert((0, InstallFlow_1.validateHost)('') === InstallFlow_1.DEFAULT_HOST, 'empty string should fall to DEFAULT_HOST');
}
function testValidateHostCaseInsensitive() {
    assert((0, InstallFlow_1.validateHost)('CLAUDE-CODE') === 'claude-code', 'uppercase should normalize');
    assert((0, InstallFlow_1.validateHost)('  Codex  ') === 'codex', 'whitespace + caps should normalize');
}
function testDetectHostFromEnv() {
    assert((0, InstallFlow_1.detectHostFromEnv)({ CLAUDECODE: '1' }) === 'claude-code', 'CLAUDECODE → claude-code');
    assert((0, InstallFlow_1.detectHostFromEnv)({ CLAUDE_PROJECT_DIR: '/x' }) === 'claude-code', 'CLAUDE_PROJECT_DIR → claude-code');
    assert((0, InstallFlow_1.detectHostFromEnv)({ GEMINI_CONFIG_DIR: '/x' }) === 'gemini-cli', 'GEMINI_CONFIG_DIR → gemini-cli');
    assert((0, InstallFlow_1.detectHostFromEnv)({ CODEX_HOME: '/x' }) === 'codex', 'CODEX_HOME → codex');
    assert((0, InstallFlow_1.detectHostFromEnv)({}) === null, 'no env signals → null');
}
function testInstallFlowAcceptsValidHosts() {
    // Run InstallFlow in a temp dir to avoid clobbering project state.
    const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'openlife-installflow-test-'));
    try {
        const flow = new InstallFlow_1.InstallFlow(tempRoot);
        for (const h of InstallFlow_1.VALID_HOSTS) {
            const result = flow.run({ profile: 'framework', host: h, skipDoctor: true });
            assert(result.host === h, `InstallFlow with host ${h} should keep host in result`);
            assert(result.nextCommands.some((c) => c.includes(`--host ${h}`)), `nextCommands should include --host ${h}: got ${JSON.stringify(result.nextCommands)}`);
            // Story 3.1 fix: no `mode set` in nextCommands
            assert(!result.nextCommands.some((c) => c.includes('mode set')), `nextCommands should NOT reference deprecated 'mode set'; got: ${JSON.stringify(result.nextCommands)}`);
            assert(result.nextCommands.some((c) => c.includes('system setup')), `nextCommands should use 'system setup' instead; got: ${JSON.stringify(result.nextCommands)}`);
        }
    }
    finally {
        fs.rmSync(tempRoot, { recursive: true, force: true });
    }
}
function testInstallFlowRejectsInvalidHost() {
    const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'openlife-installflow-test-'));
    try {
        const flow = new InstallFlow_1.InstallFlow(tempRoot);
        let threw = false;
        try {
            flow.run({ profile: 'framework', host: 'cursor', skipDoctor: true });
        }
        catch (err) {
            threw = true;
            assert(err.message.includes('INVALID_HOST'), `should throw INVALID_HOST; got: ${err.message}`);
        }
        assert(threw, 'InstallFlow.run with invalid host should throw');
    }
    finally {
        fs.rmSync(tempRoot, { recursive: true, force: true });
    }
}
async function main() {
    testValidateHostAcceptsCanonical();
    testValidateHostRejectsUnknown();
    testValidateHostFallsBackToDefault();
    testValidateHostCaseInsensitive();
    testDetectHostFromEnv();
    testInstallFlowAcceptsValidHosts();
    testInstallFlowRejectsInvalidHost();
    console.log('TEST_INSTALL_FLOW_HOST_VALIDATION_OK');
}
main().catch((err) => {
    console.error('TEST_INSTALL_FLOW_HOST_VALIDATION_FAIL:', err?.message || err);
    process.exit(1);
});
