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

// Mock fs/promises
vi.mock('fs/promises', () => ({
  stat: vi.fn(),
  copyFile: vi.fn().mockResolvedValue(undefined),
  mkdir: vi.fn().mockResolvedValue(undefined),
  readdir: vi.fn().mockResolvedValue([]),
}));

// 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 { copyFileTool } from '../../../../src/tools/filesystem/copy_file.js';
import * as fs from 'fs/promises';
import { getPermissionManager } from '../../../../src/permission/index.js';

describe('copyFileTool - 文件复制工具', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    vi.mocked(fs.stat).mockResolvedValue({
      isDirectory: () => false,
      isFile: () => true,
    } as any);
  });

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

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

    it('定义了必需参数', () => {
      expect(copyFileTool.parameters.source.required).toBe(true);
      expect(copyFileTool.parameters.destination.required).toBe(true);
    });
  });

  describe('execute - 执行', () => {
    it('成功复制文件', async () => {
      // 第一次调用检查源文件，第二次调用检查目标是否是目录
      vi.mocked(fs.stat)
        .mockResolvedValueOnce({ isDirectory: () => false } as any)
        .mockRejectedValueOnce(new Error('ENOENT')); // 目标不存在

      const result = await copyFileTool.execute({
        source: 'src.txt',
        destination: 'dest.txt',
      });

      expect(result.success).toBe(true);
      expect(result.output).toContain('已复制');
      expect(fs.copyFile).toHaveBeenCalled();
    });

    it('复制到已存在的目录', async () => {
      vi.mocked(fs.stat)
        .mockResolvedValueOnce({ isDirectory: () => false } as any) // 源文件
        .mockResolvedValueOnce({ isDirectory: () => true } as any); // 目标是目录

      const result = await copyFileTool.execute({
        source: 'file.txt',
        destination: '/target/dir',
      });

      expect(result.success).toBe(true);
      expect(result.output).toContain('file.txt');
    });

    it('递归复制目录', async () => {
      vi.mocked(fs.stat)
        .mockResolvedValueOnce({ isDirectory: () => true } as any) // 源是目录
        .mockRejectedValueOnce(new Error('ENOENT')); // 目标不存在

      vi.mocked(fs.readdir).mockResolvedValueOnce([]);

      const result = await copyFileTool.execute({
        source: 'src_dir',
        destination: 'dest_dir',
      });

      expect(result.success).toBe(true);
      expect(fs.mkdir).toHaveBeenCalled();
    });

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

      const result = await copyFileTool.execute({
        source: '/etc/passwd',
        destination: 'copy.txt',
      });

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

    it('目标位置写入权限被拒绝', async () => {
      const mockCheck = vi.fn()
        .mockResolvedValueOnce({ allowed: true }) // 读取权限
        .mockResolvedValueOnce({ allowed: false, action: 'deny', reason: '不允许写入' }); // 复制权限

      vi.mocked(getPermissionManager).mockReturnValue({
        checkFilePermission: mockCheck,
      } as any);

      const result = await copyFileTool.execute({
        source: 'src.txt',
        destination: '/protected/dest.txt',
      });

      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 copyFileTool.execute({
        source: '/sensitive/file',
        destination: 'dest.txt',
      });

      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.stat).mockRejectedValue(new Error('ENOENT: no such file'));

      const result = await copyFileTool.execute({
        source: 'nonexistent.txt',
        destination: 'dest.txt',
      });

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