import { describe, it, expect, beforeEach, vi } from 'vitest';
import { skillSearchTool } from '../../../../src/tools/skill/skill_search.js';
import { getSkillRegistry, resetSkillRegistry } from '../../../../src/skills/registry.js';

// Mock loader to prevent file system access
vi.mock('../../../../src/skills/loader.js', () => ({
  skillLoader: {
    loadFromDirectory: vi.fn().mockResolvedValue([]),
    getUserSkillsDir: vi.fn().mockReturnValue('/mock/user/skills'),
    getProjectSkillsDir: vi.fn().mockReturnValue('/mock/project/skills'),
  },
}));

// Mock builtin skills
vi.mock('../../../../src/skills/builtin/index.js', () => ({
  builtinSkills: [
    {
      name: 'code-review',
      description: '代码审查',
      promptTemplate: '审查代码',
      keywords: ['review', 'quality'],
      category: 'development',
      source: 'builtin',
      enabled: true,
    },
    {
      name: 'generate-tests',
      description: '生成测试',
      promptTemplate: '生成测试',
      keywords: ['test', 'unit'],
      category: 'testing',
      source: 'builtin',
      enabled: true,
    },
    {
      name: 'explain-code',
      description: '解释代码功能',
      promptTemplate: '解释代码',
      category: 'development',
      source: 'builtin',
      enabled: true,
    },
  ],
}));

describe('skillSearchTool - Skill 搜索工具', () => {
  beforeEach(async () => {
    vi.clearAllMocks();
    resetSkillRegistry();
    const registry = getSkillRegistry();
    await registry.initialize();
  });

  describe('工具定义', () => {
    it('有正确的名称', () => {
      expect(skillSearchTool.name).toBe('skill_search');
    });

    it('有正确的元数据', () => {
      expect(skillSearchTool.metadata.category).toBe('agent');
      expect(skillSearchTool.metadata.keywords).toContain('search');
    });

    it('所有参数都是可选的', () => {
      expect(skillSearchTool.parameters.query.required).toBe(false);
      expect(skillSearchTool.parameters.category.required).toBe(false);
      expect(skillSearchTool.parameters.list_all.required).toBe(false);
    });
  });

  describe('execute - 执行', () => {
    it('list_all 列出所有 Skills', async () => {
      const result = await skillSearchTool.execute({ list_all: true });

      expect(result.success).toBe(true);
      expect(result.output).toContain('code-review');
      expect(result.output).toContain('generate-tests');
      expect(result.output).toContain('explain-code');
      expect(result.output).toContain('development');
      expect(result.output).toContain('testing');
    });

    it('按关键词搜索', async () => {
      const result = await skillSearchTool.execute({ query: 'review' });

      expect(result.success).toBe(true);
      expect(result.output).toContain('code-review');
      expect(result.metadata?.query).toBe('review');
    });

    it('按分类筛选', async () => {
      const result = await skillSearchTool.execute({ category: 'testing' });

      expect(result.success).toBe(true);
      expect(result.output).toContain('generate-tests');
      expect(result.output).not.toContain('code-review');
    });

    it('分类不存在时显示可用分类', async () => {
      const result = await skillSearchTool.execute({ category: 'non-existent' });

      expect(result.success).toBe(true);
      expect(result.output).toContain('没有 Skills');
      expect(result.output).toContain('可用分类');
    });

    it('搜索无结果时提示', async () => {
      const result = await skillSearchTool.execute({ query: 'xxxxxxxxx' });

      expect(result.success).toBe(true);
      expect(result.output).toContain('没有找到');
    });

    it('无参数时显示概览', async () => {
      const result = await skillSearchTool.execute({});

      expect(result.success).toBe(true);
      expect(result.output).toContain('概览');
      expect(result.output).toContain('使用方法');
    });
  });
});
