/**
 * Adapter 类型定义测试
 */
import { describe, expect, it } from 'vitest';
import { getToolActionInfo } from '../adapter/types.js';
describe('Adapter Types', () => {
    it('should define valid SessionStatus values', () => {
        const statuses = [
            'starting',
            'thinking',
            'tool_using',
            'waiting_input',
            'completed',
            'terminated',
            'error',
        ];
        expect(statuses).toHaveLength(7);
    });
    it('should define valid ProviderEventType values', () => {
        const eventTypes = [
            'text_delta',
            'thinking',
            'tool_use_start',
            'tool_use_end',
            'permission_request',
            'ask_user_question',
            'exit_plan_mode',
            'turn_complete',
            'session_complete',
            'error',
        ];
        expect(eventTypes).toHaveLength(10);
    });
    it('should create valid AdapterSessionConfig', () => {
        const config = {
            command: 'claude',
            workingDirectory: '/tmp/test',
            autoAccept: true,
        };
        expect(config.command).toBe('claude');
        expect(config.autoAccept).toBe(true);
    });
    it('should create valid ConversationMessage', () => {
        const message = {
            id: 'msg-1',
            sessionId: 'session-1',
            role: 'user',
            content: 'Hello',
            timestamp: new Date().toISOString(),
        };
        expect(message.role).toBe('user');
        expect(message.content).toBe('Hello');
    });
    it('classifies direct skill tool calls as skill actions', () => {
        expect(getToolActionInfo('Skill', { skill: 'brainstorming' })).toEqual({
            actionType: 'skill',
            actionLabel: '使用技能',
            actionDetail: 'brainstorming',
        });
    });
    it('classifies task tool calls with load_skills as skill actions', () => {
        expect(getToolActionInfo('Task', { load_skills: ['brainstorming', 'writing-plans'] })).toEqual({
            actionType: 'skill',
            actionLabel: '加载技能',
            actionDetail: 'brainstorming, writing-plans',
        });
    });
    it('prefers explicit skill name fields for skill tools', () => {
        expect(getToolActionInfo('load_skill', { name: 'frontend-design' })).toEqual({
            actionType: 'skill',
            actionLabel: '使用技能',
            actionDetail: 'frontend-design',
        });
    });
    it('classifies SDK tool_use_start aliases as skill actions', () => {
        expect(getToolActionInfo('Task', { load_skills: ['requesting-code-review'] })).toEqual({
            actionType: 'skill',
            actionLabel: '加载技能',
            actionDetail: 'requesting-code-review',
        });
    });
    it('keeps plain task tool calls as generic tool actions', () => {
        expect(getToolActionInfo('Task', { prompt: 'search the repo' })).toEqual({
            actionType: 'other',
            actionLabel: '调用工具',
            actionDetail: 'Task',
        });
    });
    // MCP 工具分类测试
    it('classifies semantic file MCP tools as file actions', () => {
        // 标准格式: mcp__server__tool；文件类工具优先按语义展示，而不是泛化为 MCP
        expect(getToolActionInfo('mcp__filesystem__read_file', { path: '/tmp/test.txt' })).toEqual({
            actionType: 'read_file',
            actionLabel: '读取文件',
            actionDetail: '/tmp/test.txt',
        });
    });
    it('classifies semantic shell MCP tools as bash actions', () => {
        expect(getToolActionInfo('mcp__shell__execute_command', { command: 'npm test' })).toEqual({
            actionType: 'bash',
            actionLabel: '执行命令',
            actionDetail: 'npm test',
        });
    });
    it('classifies custom MCP tools with server name in detail', () => {
        // 自定义格式: xxx-mcp__tool (如 aws-mcp__get_profile)
        expect(getToolActionInfo('aws-mcp__get_profile', {})).toEqual({
            actionType: 'mcp',
            actionLabel: '调用MCP',
            actionDetail: 'aws-mcp/get_profile',
        });
    });
    it('classifies MCP tools with complex server names', () => {
        // 复杂 MCP server 名称
        expect(getToolActionInfo('my-custom-mcp__do_something', {})).toEqual({
            actionType: 'mcp',
            actionLabel: '调用MCP',
            actionDetail: 'my-custom-mcp/do_something',
        });
    });
    it('handles MCP tools with multiple underscores in tool name', () => {
        // 工具名本身包含下划线
        expect(getToolActionInfo('mcp__aws__get_colleague_list', {})).toEqual({
            actionType: 'mcp',
            actionLabel: '调用MCP',
            actionDetail: 'aws/get_colleague_list',
        });
    });
    // metadata 中包含 MCP server 信息的测试
    it('uses metadata.mcpServer for MCP tool classification', () => {
        // 当 metadata 中有 mcpServer 时，优先使用它
        expect(getToolActionInfo('get_profile', {}, { mcpServer: 'aws-mcp' })).toEqual({
            actionType: 'mcp',
            actionLabel: '调用MCP',
            actionDetail: 'aws-mcp/get_profile',
        });
    });
    it('uses metadata.mcpServer even for simple tool names', () => {
        // 简单工具名 + metadata 也能识别为 MCP 工具
        expect(getToolActionInfo('get_colleague', {}, { mcpServer: 'aws-mcp' })).toEqual({
            actionType: 'mcp',
            actionLabel: '调用MCP',
            actionDetail: 'aws-mcp/get_colleague',
        });
    });
    it('prefers metadata over tool name pattern for MCP detection', () => {
        // metadata 优先级高于工具名格式
        expect(getToolActionInfo('custom_tool', {}, { mcpServer: 'my-server' })).toEqual({
            actionType: 'mcp',
            actionLabel: '调用MCP',
            actionDetail: 'my-server/custom_tool',
        });
    });
    // 已知 MCP 工具列表测试
    it('recognizes known MCP tools from whitelist', () => {
        // 已知的 aws-mcp 工具，即使没有 metadata 也能识别
        expect(getToolActionInfo('get_profile', {})).toEqual({
            actionType: 'mcp',
            actionLabel: '调用MCP',
            actionDetail: 'aws-mcp/get_profile',
        });
    });
    it('recognizes all aws-mcp tools', () => {
        expect(getToolActionInfo('get_colleague', {})).toEqual({
            actionType: 'mcp',
            actionLabel: '调用MCP',
            actionDetail: 'aws-mcp/get_colleague',
        });
        expect(getToolActionInfo('send_group', {})).toEqual({
            actionType: 'mcp',
            actionLabel: '调用MCP',
            actionDetail: 'aws-mcp/send_group',
        });
        expect(getToolActionInfo('poll_message', {})).toEqual({
            actionType: 'mcp',
            actionLabel: '调用MCP',
            actionDetail: 'aws-mcp/poll_message',
        });
    });
});
