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

// Mock fs/promises
vi.mock('fs/promises', () => ({
  readdir: vi.fn(),
  readFile: 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 { grepContentTool } from '../../../../src/tools/filesystem/grep_content.js';
import * as fs from 'fs/promises';
import { getPermissionManager } from '../../../../src/permission/index.js';

describe('grepContentTool - 内容搜索工具', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

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

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

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

    it('定义了可选参数', () => {
      expect(grepContentTool.parameters.file_pattern.required).toBe(false);
      expect(grepContentTool.parameters.max_results.required).toBe(false);
    });
  });

  describe('execute - 执行', () => {
    it('成功搜索并返回匹配结果', async () => {
      vi.mocked(fs.readdir).mockResolvedValue([
        { name: 'test.ts', isDirectory: () => false, isFile: () => true },
      ] as any);
      vi.mocked(fs.readFile).mockResolvedValue('const hello = "world";\nconst foo = "bar";');

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

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

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

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

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

    it('按文件模式过滤', async () => {
      vi.mocked(fs.readdir).mockResolvedValue([
        { name: 'test.ts', isDirectory: () => false, isFile: () => true },
        { name: 'test.js', isDirectory: () => false, isFile: () => true },
      ] as any);
      vi.mocked(fs.readFile).mockResolvedValue('const hello = "world";');

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

      expect(result.success).toBe(true);
      // 只搜索 .ts 文件
      expect(fs.readFile).toHaveBeenCalledTimes(1);
    });

    it('限制最大结果数', async () => {
      vi.mocked(fs.readdir).mockResolvedValue([
        { name: 'test.ts', isDirectory: () => false, isFile: () => true },
      ] as any);
      // 多行匹配内容
      vi.mocked(fs.readFile).mockResolvedValue(
        'hello1\nhello2\nhello3\nhello4\nhello5'
      );

      const result = await grepContentTool.execute({
        directory: '.',
        pattern: 'hello',
        max_results: 2,
      });

      expect(result.success).toBe(true);
      expect(result.output).toContain('已达上限');
    });

    it('跳过隐藏文件和 node_modules', async () => {
      // 第一次调用返回根目录内容，第二次调用返回 src 目录内容
      vi.mocked(fs.readdir)
        .mockResolvedValueOnce([
          { name: '.hidden', 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);
      vi.mocked(fs.readFile).mockResolvedValue('const test = 1;');

      await grepContentTool.execute({
        directory: '.',
        pattern: 'test',
      });

      // 不应该进入隐藏目录或 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 grepContentTool.execute({
        directory: '/protected',
        pattern: 'test',
      });

      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 grepContentTool.execute({
        directory: '.',
        pattern: 'test',
      });

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

    it('递归搜索子目录', async () => {
      // 恢复权限检查
      vi.mocked(getPermissionManager).mockReturnValue({
        checkFilePermission: vi.fn().mockResolvedValue({ allowed: true }),
      } as any);
      vi.mocked(fs.readdir)
        .mockResolvedValueOnce([
          { name: 'src', isDirectory: () => true, isFile: () => false },
        ] as any)
        .mockResolvedValueOnce([
          { name: 'index.ts', isDirectory: () => false, isFile: () => true },
        ] as any);
      vi.mocked(fs.readFile).mockResolvedValue('const test = 1;');

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

      expect(result.success).toBe(true);
      expect(fs.readdir).toHaveBeenCalledTimes(2);
    });

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

      await grepContentTool.execute({
        directory: 'src',
        pattern: 'test',
      });

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