import { describe, it, expect, beforeEach, vi } from 'vitest';

// Mock fs/promises
vi.mock('fs/promises', () => ({
  readdir: vi.fn(),
}));

// Mock permission manager
vi.mock('../../../../src/permission/index.js', () => ({
  getPermissionManager: vi.fn(() => ({
    checkFilePermission: vi.fn().mockResolvedValue({
      allowed: true,
      action: 'allow',
    }),
  })),
}));

// Mock loadDescription
vi.mock('../../../../src/tools/load_description.js', () => ({
  loadDescription: vi.fn(() => '按文件名搜索文件'),
}));

import { searchFilesTool } from '../../../../src/tools/filesystem/search_files.js';
import * as fs from 'fs/promises';
import { getPermissionManager } from '../../../../src/permission/index.js';

describe('searchFilesTool - 文件搜索工具', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

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

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

    it('定义了必需参数', () => {
      expect(searchFilesTool.parameters.directory.required).toBe(true);
      expect(searchFilesTool.parameters.pattern.required).toBe(true);
    });
  });

  describe('execute - 执行', () => {
    it('成功搜索并返回匹配文件', async () => {
      vi.mocked(fs.readdir).mockResolvedValue([
        { name: 'test.ts', isDirectory: () => false, isFile: () => true },
        { name: 'test.js', isDirectory: () => false, isFile: () => true },
      ] as any);

      const result = await searchFilesTool.execute({
        directory: '.',
        pattern: '*.ts',
      });

      expect(result.success).toBe(true);
      expect(result.output).toContain('test.ts');
      expect(result.output).not.toContain('test.js');
    });

    it('没有匹配时返回提示', async () => {
      vi.mocked(fs.readdir).mockResolvedValue([
        { name: 'test.js', isDirectory: () => false, isFile: () => true },
      ] as any);

      const result = await searchFilesTool.execute({
        directory: '.',
        pattern: '*.tsx',
      });

      expect(result.success).toBe(true);
      expect(result.output).toContain('没有找到匹配的文件');
    });

    it('递归搜索子目录', async () => {
      vi.mocked(fs.readdir)
        .mockResolvedValueOnce([
          { name: 'src', isDirectory: () => true, isFile: () => false },
          { name: 'index.ts', isDirectory: () => false, isFile: () => true },
        ] as any)
        .mockResolvedValueOnce([
          { name: 'app.ts', isDirectory: () => false, isFile: () => true },
        ] as any);

      const result = await searchFilesTool.execute({
        directory: '.',
        pattern: '*.ts',
      });

      expect(result.success).toBe(true);
      expect(result.output).toContain('index.ts');
      expect(result.output).toContain('app.ts');
    });

    it('跳过隐藏文件和 node_modules', async () => {
      // 第一次调用返回根目录内容，第二次调用返回 src 目录内容
      vi.mocked(fs.readdir)
        .mockResolvedValueOnce([
          { name: '.git', isDirectory: () => true, isFile: () => false },
          { name: 'node_modules', isDirectory: () => true, isFile: () => false },
          { name: 'src', isDirectory: () => true, isFile: () => false },
        ] as any)
        .mockResolvedValueOnce([
          { name: 'index.ts', isDirectory: () => false, isFile: () => true },
        ] as any);

      await searchFilesTool.execute({
        directory: '.',
        pattern: '*',
      });

      // 不应该进入隐藏目录或 node_modules，只进入 src
      expect(fs.readdir).toHaveBeenCalledTimes(2); // 根目录 + src
    });

    it('权限被拒绝时返回错误', async () => {
      vi.mocked(getPermissionManager).mockReturnValue({
        checkFilePermission: vi.fn().mockResolvedValue({
          allowed: false,
          action: 'deny',
          reason: '不允许搜索',
        }),
      } as any);

      const result = await searchFilesTool.execute({
        directory: '/protected',
        pattern: '*',
      });

      expect(result.success).toBe(false);
      expect(result.error).toContain('权限被拒绝');
    });

    it('需要确认时返回提示', async () => {
      vi.mocked(getPermissionManager).mockReturnValue({
        checkFilePermission: vi.fn().mockResolvedValue({
          allowed: false,
          action: 'ask',
          needsConfirmation: true,
        }),
      } as any);

      const result = await searchFilesTool.execute({
        directory: '.',
        pattern: '*',
      });

      expect(result.success).toBe(false);
      expect(result.error).toContain('需要用户确认');
    });

    it('支持 glob 模式匹配', async () => {
      // 恢复权限检查
      vi.mocked(getPermissionManager).mockReturnValue({
        checkFilePermission: vi.fn().mockResolvedValue({ allowed: true }),
      } as any);
      vi.mocked(fs.readdir).mockResolvedValue([
        { name: 'component.tsx', isDirectory: () => false, isFile: () => true },
        { name: 'helper.ts', isDirectory: () => false, isFile: () => true },
        { name: 'style.css', isDirectory: () => false, isFile: () => true },
      ] as any);

      // *.tsx 模式会匹配 component.tsx
      const result = await searchFilesTool.execute({
        directory: '.',
        pattern: '*.tsx',
      });

      expect(result.success).toBe(true);
      expect(result.output).toContain('component.tsx');
      expect(result.output).not.toContain('style.css');
    });

    it('传递正确参数给权限检查', async () => {
      const mockCheck = vi.fn().mockResolvedValue({ allowed: true });
      vi.mocked(getPermissionManager).mockReturnValue({
        checkFilePermission: mockCheck,
      } as any);
      vi.mocked(fs.readdir).mockResolvedValue([]);

      await searchFilesTool.execute({
        directory: 'src',
        pattern: '*.ts',
      });

      expect(mockCheck).toHaveBeenCalledWith(
        expect.objectContaining({
          operation: 'search',
        })
      );
    });
  });
});
