"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 mcp_config_1 = require("../core/mcp-config");
const fs = __importStar(require("fs"));
const os = __importStar(require("os"));
const path = __importStar(require("path"));
// mcp-config field-name regression tests (Pre-Launch bug found in Uos fresh test)
// All 5 IDEs must use the correct field name AND end up with exactly one cell entry.
const TMP_HOME = path.join(os.tmpdir(), 'fivo-cell-mcpconfig-' + Date.now() + '-' + Math.random().toString(36).slice(2));
const REAL_HOME = process.env.USERPROFILE || process.env.HOME || '';
process.env.USERPROFILE = TMP_HOME;
process.env.HOME = TMP_HOME;
beforeAll(() => {
    fs.mkdirSync(TMP_HOME, { recursive: true });
});
afterAll(() => {
    try {
        fs.rmSync(TMP_HOME, { recursive: true, force: true });
    }
    catch { }
    process.env.USERPROFILE = REAL_HOME;
    process.env.HOME = REAL_HOME;
});
function readJson(p) {
    return JSON.parse(fs.readFileSync(p, 'utf8'));
}
function runMcpConfig() {
    (0, mcp_config_1.writeMcpConfig)(TMP_HOME);
}
describe('Pre-Launch fix v4.1.7: mcp-config field names', () => {
    it('Cursor: writes { url } under mcpServers.fivo-cell', () => {
        runMcpConfig();
        const cfg = readJson(path.join(TMP_HOME, '.cursor', 'mcp.json'));
        const mcpServers = cfg.mcpServers;
        expect(mcpServers['fivo-cell']).toEqual({ url: 'http://localhost:9876/mcp' });
    });
    it('Gemini CLI: writes { url } under mcpServers.fivo-cell', () => {
        runMcpConfig();
        const cfg = readJson(path.join(TMP_HOME, '.gemini', 'settings.json'));
        const mcpServers = cfg.mcpServers;
        expect(mcpServers['fivo-cell']).toEqual({ url: 'http://localhost:9876/mcp' });
    });
    it('Antigravity: writes { serverUrl } under mcpServers.fivo-cell (NOT url)', () => {
        runMcpConfig();
        const cfg = readJson(path.join(TMP_HOME, '.gemini', 'config', 'mcp_config.json'));
        const mcpServers = cfg.mcpServers;
        const entry = mcpServers['fivo-cell'];
        expect(entry).toEqual({ serverUrl: 'http://localhost:9876/mcp' });
        expect(entry.url).toBeUndefined();
    });
    it('Codex: writes { url } under mcp.servers.fivo-cell (NOT mcpServers)', () => {
        runMcpConfig();
        const cfg = readJson(path.join(TMP_HOME, '.codex', 'config.json'));
        const mcp = cfg.mcp;
        const servers = mcp.servers;
        expect(servers['fivo-cell']).toEqual({ url: 'http://localhost:9876/mcp' });
        expect(cfg.mcpServers).toBeUndefined();
    });
    it('OpenCode: writes { type:remote,url,enabled:true } under mcp.cell', () => {
        runMcpConfig();
        const cfg = readJson(path.join(TMP_HOME, '.config', 'opencode', 'opencode.jsonc'));
        const mcp = cfg.mcp;
        const cell = mcp.cell;
        expect(cell).toEqual({ type: 'remote', url: 'http://localhost:9876/mcp', enabled: true });
    });
});
describe('Pre-Launch fix v4.1.7: mcp-config legacy cleanup', () => {
    it('Antigravity: removes leftover "cell" key from v4.1.3 install', () => {
        const file = path.join(TMP_HOME, '.gemini', 'config', 'mcp_config.json');
        fs.mkdirSync(path.dirname(file), { recursive: true });
        fs.writeFileSync(file, JSON.stringify({
            mcpServers: {
                cell: { serverUrl: 'http://localhost:9876/mcp' },
                other: { url: 'http://other.example/mcp' },
            },
        }, null, 2));
        runMcpConfig();
        const cfg = readJson(file);
        const mcpServers = cfg.mcpServers;
        expect(mcpServers.cell).toBeUndefined();
        expect(mcpServers['fivo-cell']).toEqual({ serverUrl: 'http://localhost:9876/mcp' });
        expect(mcpServers.other).toEqual({ url: 'http://other.example/mcp' });
    });
    it('Codex: removes flat mcpServers leftover, keeps nested mcp.servers', () => {
        const file = path.join(TMP_HOME, '.codex', 'config.json');
        fs.mkdirSync(path.dirname(file), { recursive: true });
        fs.writeFileSync(file, JSON.stringify({
            mcpServers: { cell: { url: 'http://old' } },
            mcp: { servers: { cell: { type: 'url', url: 'http://old' } } },
        }, null, 2));
        runMcpConfig();
        const cfg = readJson(file);
        expect(cfg.mcpServers).toBeUndefined();
        const mcp = cfg.mcp;
        const servers = mcp.servers;
        expect(servers.cell).toBeUndefined();
        expect(servers['fivo-cell']).toEqual({ url: 'http://localhost:9876/mcp' });
    });
    it('Cursor: removes multiple legacy cell keys (cell, fivo-cell, FIVO-Cell)', () => {
        const file = path.join(TMP_HOME, '.cursor', 'mcp.json');
        fs.mkdirSync(path.dirname(file), { recursive: true });
        fs.writeFileSync(file, JSON.stringify({
            mcpServers: {
                cell: { url: 'http://old1' },
                'fivo-cell': { url: 'http://old2' },
                'FIVO-Cell': { url: 'http://old3' },
                other: { url: 'http://keep' },
            },
        }, null, 2));
        runMcpConfig();
        const cfg = readJson(file);
        const mcpServers = cfg.mcpServers;
        expect(Object.keys(mcpServers).sort()).toEqual(['fivo-cell', 'other']);
        expect(mcpServers['fivo-cell']).toEqual({ url: 'http://localhost:9876/mcp' });
        expect(mcpServers.other).toEqual({ url: 'http://keep' });
    });
    it('OpenCode: only one cell entry survives under mcp.cell', () => {
        const file = path.join(TMP_HOME, '.config', 'opencode', 'opencode.jsonc');
        fs.mkdirSync(path.dirname(file), { recursive: true });
        fs.writeFileSync(file, JSON.stringify({
            mcp: { cell: { type: 'remote', url: 'http://old', enabled: true } },
        }, null, 2));
        runMcpConfig();
        const cfg = readJson(file);
        const mcp = cfg.mcp;
        expect(Object.keys(mcp)).toEqual(['cell']);
    });
});
describe('Pre-Launch fix v4.1.7: mcp-config output', () => {
    it('reports 5/5 written when home is empty', () => {
        const result = (0, mcp_config_1.writeMcpConfig)(TMP_HOME);
        expect(result.written.length).toBe(5);
        expect(result.skipped.length).toBe(0);
    });
});
//# sourceMappingURL=mcp-config-fields.test.js.map