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

// Mock fs/promises
vi.mock('fs/promises', () => ({
  access: vi.fn().mockResolvedValue(undefined),
  stat: vi.fn(),
  rename: vi.fn().mockResolvedValue(undefined),
  mkdir: vi.fn().mockResolvedValue(undefined),
}));

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

describe('moveFileTool - 文件移动工具', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

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

    it('有正确的元数据', () => {
      expect(moveFileTool.metadata.category).toBe('filesystem');
      expect(moveFileTool.metadata.keywords).toContain('move');
      expect(moveFileTool.metadata.keywords).toContain('rename');
      expect(moveFileTool.metadata.keywords).toContain('mv');
    });

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

  describe('execute - 执行', () => {
    it('成功移动文件', async () => {
      vi.mocked(fs.stat).mockRejectedValue(new Error('ENOENT')); // 目标不存在

      const result = await moveFileTool.execute({
        source: 'old.txt',
        destination: 'new.txt',
      });

      expect(result.success).toBe(true);
      expect(result.output).toContain('已移动');
      expect(fs.rename).toHaveBeenCalled();
    });

    it('移动到已存在的目录', async () => {
      vi.mocked(fs.stat).mockResolvedValue({ isDirectory: () => true } as any);

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

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

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

      const result = await moveFileTool.execute({
        source: '/protected/file',
        destination: 'dest.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 moveFileTool.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 moveFileTool.execute({
        source: 'file.txt',
        destination: 'new.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.access).mockRejectedValue(new Error('ENOENT'));

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

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

    it('创建目标目录', async () => {
      // 确保权限检查通过
      vi.mocked(getPermissionManager).mockReturnValue({
        checkFilePermission: vi.fn().mockResolvedValue({ allowed: true }),
      } as any);
      // 源文件存在
      vi.mocked(fs.access).mockResolvedValue(undefined);
      // 目标不存在
      vi.mocked(fs.stat).mockRejectedValue(new Error('ENOENT'));

      const result = await moveFileTool.execute({
        source: 'file.txt',
        destination: '/new/path/file.txt',
      });

      expect(result.success).toBe(true);
      expect(fs.mkdir).toHaveBeenCalledWith(
        expect.any(String),
        { recursive: true }
      );
    });
  });
});
