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

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

describe('createDirectoryTool - 创建目录工具', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    // 默认目录不存在
    vi.mocked(fs.stat).mockRejectedValue(new Error('ENOENT'));
  });

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

    it('有正确的元数据', () => {
      expect(createDirectoryTool.metadata.category).toBe('filesystem');
      expect(createDirectoryTool.metadata.keywords).toContain('create');
      expect(createDirectoryTool.metadata.keywords).toContain('directory');
      expect(createDirectoryTool.metadata.keywords).toContain('mkdir');
    });

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

  describe('execute - 执行', () => {
    it('成功创建目录', async () => {
      const result = await createDirectoryTool.execute({ path: 'new_dir' });

      expect(result.success).toBe(true);
      expect(result.output).toContain('已创建目录');
      expect(fs.mkdir).toHaveBeenCalledWith(
        expect.any(String),
        { recursive: true }
      );
    });

    it('目录已存在返回成功', async () => {
      vi.mocked(fs.stat).mockResolvedValue({
        isDirectory: () => true,
      } as any);

      const result = await createDirectoryTool.execute({ path: 'existing_dir' });

      expect(result.success).toBe(true);
      expect(result.output).toContain('目录已存在');
      expect(fs.mkdir).not.toHaveBeenCalled();
    });

    it('路径是文件返回错误', async () => {
      vi.mocked(fs.stat).mockResolvedValue({
        isDirectory: () => false,
      } as any);

      const result = await createDirectoryTool.execute({ path: 'file.txt' });

      expect(result.success).toBe(false);
      expect(result.error).toContain('路径已存在且不是目录');
    });

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

      const result = await createDirectoryTool.execute({ path: '/protected/dir' });

      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 createDirectoryTool.execute({ path: 'new_dir' });

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

    it('创建嵌套目录', async () => {
      // 确保权限检查通过
      vi.mocked(getPermissionManager).mockReturnValue({
        checkFilePermission: vi.fn().mockResolvedValue({ allowed: true }),
      } as any);

      const result = await createDirectoryTool.execute({ path: 'a/b/c/d' });

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

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

      await createDirectoryTool.execute({ path: 'test_dir' });

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

    it('处理创建错误', async () => {
      vi.mocked(fs.mkdir).mockRejectedValue(new Error('Permission denied'));

      const result = await createDirectoryTool.execute({ path: 'new_dir' });

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