import { describe, it, expect, beforeEach, vi } from 'vitest';
import { readFileTool } from '../../../../src/tools/filesystem/read_file.js';

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

describe('readFileTool - 读取文件工具', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

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

    it('有正确的元数据', () => {
      expect(readFileTool.metadata.category).toBe('filesystem');
      expect(readFileTool.metadata.keywords).toContain('read');
      expect(readFileTool.metadata.keywords).toContain('file');
    });

    it('定义了必需的 path 参数', () => {
      expect(readFileTool.parameters.path).toBeDefined();
      expect(readFileTool.parameters.path.required).toBe(true);
      expect(readFileTool.parameters.path.type).toBe('string');
    });
  });

  describe('execute - 执行', () => {
    it('成功读取文件', async () => {
      const mockContent = 'Hello, World!';
      vi.mocked(fs.readFile).mockResolvedValue(mockContent);

      const result = await readFileTool.execute({ path: './test.txt' });

      expect(result.success).toBe(true);
      expect(result.output).toBe(mockContent);
    });

    it('处理绝对路径', async () => {
      vi.mocked(fs.readFile).mockResolvedValue('content');

      await readFileTool.execute({ path: '/absolute/path/file.txt' });

      expect(fs.readFile).toHaveBeenCalledWith('/absolute/path/file.txt', 'utf-8');
    });

    it('处理相对路径', async () => {
      vi.mocked(fs.readFile).mockResolvedValue('content');

      await readFileTool.execute({ path: './relative/file.txt' });

      // 应该解析为绝对路径
      expect(fs.readFile).toHaveBeenCalled();
      const calledPath = vi.mocked(fs.readFile).mock.calls[0][0] as string;
      expect(calledPath.endsWith('relative/file.txt')).toBe(true);
    });

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

      const result = await readFileTool.execute({ path: '/etc/passwd' });

      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,
          reason: '需要确认',
        }),
      } as any);

      const result = await readFileTool.execute({ path: './sensitive.txt' });

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

    it('文件不存在时返回错误', async () => {
      vi.mocked(getPermissionManager).mockReturnValue({
        checkFilePermission: vi.fn().mockResolvedValue({
          allowed: true,
          action: 'allow',
        }),
      } as any);
      vi.mocked(fs.readFile).mockRejectedValue(new Error('ENOENT: no such file'));

      const result = await readFileTool.execute({ path: './nonexistent.txt' });

      expect(result.success).toBe(false);
      expect(result.error).toContain('ENOENT');
    });

    it('读取大文件', async () => {
      const largeContent = 'x'.repeat(10000);
      vi.mocked(getPermissionManager).mockReturnValue({
        checkFilePermission: vi.fn().mockResolvedValue({
          allowed: true,
          action: 'allow',
        }),
      } as any);
      vi.mocked(fs.readFile).mockResolvedValue(largeContent);

      const result = await readFileTool.execute({ path: './large.txt' });

      expect(result.success).toBe(true);
      expect(result.output.length).toBe(10000);
    });
  });
});
