import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import fs from 'fs';
import path from 'path';
import os from 'os';
import { writeMcpConfig, removeMcpConfig, readMcpJson } from '../setup.js';
let tmpDir;
beforeEach(() => {
    tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'mcp-setup-test-'));
});
afterEach(() => {
    fs.rmSync(tmpDir, { recursive: true, force: true });
});
function makeDef(id) {
    return {
        id,
        name: id,
        description: '',
        category: 'test',
        detection: { method: 'npm', npm: { package: `@test/${id}` } },
        server: { command: 'npx', args: ['-y', `@test/${id}`], env: { TOKEN: '${TOKEN}' } },
        auth: [{ key: 'TOKEN', label: 'Token', hint: 'hint', required: true }],
        tools: [],
        relevant_sectors: [],
    };
}
describe('writeMcpConfig', () => {
    it('creates .mcp.json with server entry', () => {
        writeMcpConfig(tmpDir, makeDef('github'), { TOKEN: 'abc123' });
        const json = readMcpJson(tmpDir);
        expect(json.mcpServers.github).toBeDefined();
        expect(json.mcpServers.github.command).toBe('npx');
        expect(json.mcpServers.github.args).toContain('-y');
    });
    it('merges into existing .mcp.json without overwriting', () => {
        const existing = { mcpServers: { pencil: { command: '/bin/pencil', args: [], env: {} } } };
        fs.writeFileSync(path.join(tmpDir, '.mcp.json'), JSON.stringify(existing, null, 2));
        writeMcpConfig(tmpDir, makeDef('github'), { TOKEN: 'abc' });
        const json = readMcpJson(tmpDir);
        expect(json.mcpServers.pencil).toBeDefined();
        expect(json.mcpServers.github).toBeDefined();
    });
    it('adds env vars to .env file', () => {
        writeMcpConfig(tmpDir, makeDef('github'), { TOKEN: 'mytoken' });
        const env = fs.readFileSync(path.join(tmpDir, '.env'), 'utf-8');
        expect(env).toContain('TOKEN=mytoken');
    });
    it('adds placeholders to .env.example', () => {
        writeMcpConfig(tmpDir, makeDef('github'), { TOKEN: 'mytoken' });
        const example = fs.readFileSync(path.join(tmpDir, '.env.example'), 'utf-8');
        expect(example).toContain('TOKEN=');
        expect(example).not.toContain('mytoken');
    });
    it('handles extension-based MCP with resolved command', () => {
        const def = makeDef('pencil');
        def.server.command = null;
        writeMcpConfig(tmpDir, def, {}, '/resolved/path/to/binary');
        const json = readMcpJson(tmpDir);
        expect(json.mcpServers.pencil.command).toBe('/resolved/path/to/binary');
    });
});
describe('removeMcpConfig', () => {
    it('removes MCP entry from .mcp.json', () => {
        const existing = {
            mcpServers: {
                github: { command: 'npx', args: [], env: {} },
                slack: { command: 'npx', args: [], env: {} },
            },
        };
        fs.writeFileSync(path.join(tmpDir, '.mcp.json'), JSON.stringify(existing, null, 2));
        removeMcpConfig(tmpDir, 'github');
        const json = readMcpJson(tmpDir);
        expect(json.mcpServers.github).toBeUndefined();
        expect(json.mcpServers.slack).toBeDefined();
    });
});
