/**
 * MCP 路由单元测试
 */
import { describe, it, expect } from 'vitest';
describe('mcp route validation', () => {
    it('requires agentId for list operation', () => {
        const validateListRequest = (body) => {
            if (!body.agentId)
                return { valid: false, error: 'agentId is required' };
            return { valid: true };
        };
        expect(validateListRequest({}).valid).toBe(false);
        expect(validateListRequest({ agentId: 'agent-123' }).valid).toBe(true);
    });
    it('requires agentId and mcpId for get operation', () => {
        const validateGetRequest = (body) => {
            if (!body.agentId || !body.mcpId)
                return { valid: false, error: 'agentId and mcpId are required' };
            return { valid: true };
        };
        expect(validateGetRequest({}).valid).toBe(false);
        expect(validateGetRequest({ agentId: 'agent-1' }).valid).toBe(false);
        expect(validateGetRequest({ agentId: 'agent-1', mcpId: 'mcp-1' }).valid).toBe(true);
    });
    it('requires mcp.name for create', () => {
        const validateCreateMcp = (mcp) => {
            const name = String(mcp?.name || '').trim();
            if (!name)
                return { valid: false, error: 'mcp.name is required' };
            return { valid: true, name };
        };
        expect(validateCreateMcp(undefined).valid).toBe(false);
        expect(validateCreateMcp({ name: 'filesystem' }).valid).toBe(true);
    });
});
describe('mcp server spec normalization', () => {
    it('normalizes stdio transport', () => {
        const normalizeMcpSpec = (mcp) => {
            if (mcp.command) {
                return {
                    type: 'stdio',
                    command: String(mcp.command),
                    args: Array.isArray(mcp.args) ? mcp.args : [],
                };
            }
            return null;
        };
        const result = normalizeMcpSpec({
            command: 'npx',
            args: ['-y', '@modelcontextprotocol/server-filesystem'],
        });
        expect(result).toEqual({
            type: 'stdio',
            command: 'npx',
            args: ['-y', '@modelcontextprotocol/server-filesystem'],
        });
    });
    it('normalizes sse transport', () => {
        const normalizeMcpSpec = (mcp) => {
            if (mcp.url && !mcp.command) {
                return { type: 'sse', url: String(mcp.url) };
            }
            return null;
        };
        const result = normalizeMcpSpec({ url: 'http://localhost:3000/sse' });
        expect(result).toEqual({ type: 'sse', url: 'http://localhost:3000/sse' });
    });
});
describe('app flags normalization', () => {
    it('normalizes app flags from various formats', () => {
        const normalizeAppFlags = (apps) => {
            if (!apps)
                return [];
            if (typeof apps === 'string')
                return apps.split(',').map(s => s.trim()).filter(Boolean);
            if (Array.isArray(apps))
                return apps.filter(s => typeof s === 'string').map(s => String(s).trim()).filter(Boolean);
            return [];
        };
        expect(normalizeAppFlags('claude,opencode')).toEqual(['claude', 'opencode']);
        expect(normalizeAppFlags(['claude', 'opencode'])).toEqual(['claude', 'opencode']);
        expect(normalizeAppFlags(null)).toEqual([]);
    });
    it('allows supported per-tool toggle app ids', () => {
        const isSupportedToggleAppId = (appId) => ['claudecode', 'opencode', 'codex'].includes(appId);
        expect(isSupportedToggleAppId('claudecode')).toBe(true);
        expect(isSupportedToggleAppId('opencode')).toBe(true);
        expect(isSupportedToggleAppId('codex')).toBe(true);
        expect(isSupportedToggleAppId('gemini')).toBe(false);
    });
});
