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

// 定义一个可控的 mock
let mockExecAsyncResult: { stdout: string; stderr: string } | Error = {
  stdout: '',
  stderr: '',
};

// Mock child_process
vi.mock('child_process', () => ({
  exec: vi.fn(),
}));

// Mock util
vi.mock('util', () => ({
  promisify: vi.fn(() => vi.fn(async () => {
    if (mockExecAsyncResult instanceof Error) {
      throw mockExecAsyncResult;
    }
    return mockExecAsyncResult;
  })),
}));

// Mock permission manager
vi.mock('../../../../src/permission/index.js', () => ({
  getPermissionManager: vi.fn(() => ({
    checkGitPermission: vi.fn().mockResolvedValue({
      allowed: true,
      action: 'allow',
    }),
  })),
}));

// Mock loadDescription
vi.mock('../../../../src/tools/load_description.js', () => ({
  loadDescription: vi.fn(() => 'Git diff 命令'),
}));

import { gitDiffTool } from '../../../../src/tools/git/git_diff.js';
import { getPermissionManager } from '../../../../src/permission/index.js';

describe('gitDiffTool - Git Diff 工具', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mockExecAsyncResult = {
      stdout: 'diff --git a/file.txt b/file.txt\n-old\n+new',
      stderr: '',
    };
  });

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

    it('有正确的元数据', () => {
      expect(gitDiffTool.metadata.category).toBe('git');
      expect(gitDiffTool.metadata.keywords).toContain('diff');
      expect(gitDiffTool.metadata.keywords).toContain('compare');
    });

    it('定义了可选参数', () => {
      expect(gitDiffTool.parameters.path.required).toBe(false);
      expect(gitDiffTool.parameters.staged.required).toBe(false);
      expect(gitDiffTool.parameters.commit.required).toBe(false);
      expect(gitDiffTool.parameters.stat.required).toBe(false);
    });
  });

  describe('execute - 执行', () => {
    it('成功获取差异', async () => {
      const result = await gitDiffTool.execute({});

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

    it('无差异时显示提示', async () => {
      mockExecAsyncResult = { stdout: '', stderr: '' };

      const result = await gitDiffTool.execute({});

      expect(result.success).toBe(true);
      expect(result.output).toContain('无差异');
    });

    it('显示暂存区差异', async () => {
      mockExecAsyncResult = { stdout: 'staged changes', stderr: '' };

      const result = await gitDiffTool.execute({ staged: true });

      expect(result.success).toBe(true);
      expect(result.output).toContain('staged changes');
    });

    it('与指定提交对比', async () => {
      mockExecAsyncResult = { stdout: 'commit diff', stderr: '' };

      const result = await gitDiffTool.execute({ commit: 'HEAD~1' });

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

    it('指定文件路径', async () => {
      mockExecAsyncResult = { stdout: 'file diff', stderr: '' };

      const result = await gitDiffTool.execute({ path: 'src/file.ts' });

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

    it('仅显示统计信息', async () => {
      mockExecAsyncResult = {
        stdout: ' file.txt | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)',
        stderr: '',
      };

      const result = await gitDiffTool.execute({ stat: true });

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

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

      const result = await gitDiffTool.execute({});

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

    it('需要确认时返回提示', async () => {
      vi.mocked(getPermissionManager).mockReturnValue({
        checkGitPermission: vi.fn().mockResolvedValue({
          allowed: false,
          action: 'ask',
          needsConfirmation: true,
        }),
      } as any);

      const result = await gitDiffTool.execute({});

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

    it('命令执行失败返回错误', async () => {
      vi.mocked(getPermissionManager).mockReturnValue({
        checkGitPermission: vi.fn().mockResolvedValue({
          allowed: true,
          action: 'allow',
        }),
      } as any);
      mockExecAsyncResult = Object.assign(
        new Error('Command failed'),
        { stdout: '', stderr: 'fatal: not a git repository', message: 'Command failed' }
      );

      const result = await gitDiffTool.execute({});

      expect(result.success).toBe(false);
      expect(result.error).toContain('not a git repository');
    });
  });
});
