import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { AcodeMcpAdapter } from './mcp-acode.js';
let tempDir;
let configPath;
function buildServer(name = 'agents-work-studio') {
    return {
        id: 'mcp_test',
        name,
        server: {
            type: 'stdio',
            command: 'cmd',
            args: ['/c', 'node', 'D:\\code\\AgentsWorkStudio\\aws-runtime-bridge\\package\\aws-client-agent-mcp\\dist\\index.js'],
            env: {
                AWS_SERVER_URL: 'ws://localhost:8080/ws/agent',
            },
        },
        apps: {
            acode: true,
            claude: false,
            claudecode: false,
            codex: false,
            gemini: false,
            opencode: false,
            openclaw: false,
        },
    };
}
beforeEach(async () => {
    tempDir = await mkdtemp(path.join(os.tmpdir(), 'acode-mcp-adapter-'));
    configPath = path.join(tempDir, '.acode', 'config.json');
});
afterEach(async () => {
    await rm(tempDir, { recursive: true, force: true });
});
describe('AcodeMcpAdapter', () => {
    it('syncs MCP servers into ACode config mcpServers while preserving provider config', async () => {
        const adapter = new AcodeMcpAdapter(configPath);
        await mkdir(path.dirname(configPath), { recursive: true });
        await writeFile(configPath, JSON.stringify({ provider: { default: { model: 'test-model' } } }), 'utf-8');
        await adapter.syncServer(buildServer());
        const written = JSON.parse(await readFile(configPath, 'utf-8'));
        expect(written.provider?.default).toEqual({ model: 'test-model' });
        expect(written.mcpServers?.['agents-work-studio']).toMatchObject({
            type: 'stdio',
            command: 'cmd',
            args: ['/c', 'node', 'D:\\code\\AgentsWorkStudio\\aws-runtime-bridge\\package\\aws-client-agent-mcp\\dist\\index.js'],
            env: {
                AWS_SERVER_URL: 'ws://localhost:8080/ws/agent',
            },
        });
    });
    it('imports and removes ACode MCP servers', async () => {
        const adapter = new AcodeMcpAdapter(configPath);
        await mkdir(path.dirname(configPath), { recursive: true });
        await writeFile(configPath, JSON.stringify({
            mcpServers: {
                'agents-work-studio': {
                    type: 'stdio',
                    command: 'cmd',
                    args: ['/c', 'node', 'old.js'],
                },
                playwright: {
                    type: 'stdio',
                    command: 'npx',
                    args: ['@playwright/mcp@latest'],
                },
            },
        }), 'utf-8');
        const imported = await adapter.importServers();
        await adapter.removeServer(buildServer());
        const written = JSON.parse(await readFile(configPath, 'utf-8'));
        expect(imported).toHaveLength(2);
        expect(imported[0]?.apps.acode).toBe(true);
        expect(written.mcpServers?.['agents-work-studio']).toBeUndefined();
        expect(written.mcpServers?.playwright).toBeDefined();
    });
});
