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

// 定义一个可控的 mock
let mockExecAsyncResult: { stdout: string; stderr: string } | Error = {
  stdout: 'abc123 Fix bug (John, 2 days ago)\ndef456 Add feature (Jane, 3 days ago)',
  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 提交历史'),
}));

import { gitLogTool } from '../../../../src/tools/git/git_log.js';
import { getPermissionManager } from '../../../../src/permission/index.js';

describe('gitLogTool - Git Log 工具', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mockExecAsyncResult = {
      stdout: 'abc123 Fix bug (John, 2 days ago)\ndef456 Add feature (Jane, 3 days ago)',
      stderr: '',
    };
  });

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

    it('有正确的元数据', () => {
      expect(gitLogTool.metadata.category).toBe('git');
      expect(gitLogTool.metadata.keywords).toContain('log');
      expect(gitLogTool.metadata.keywords).toContain('history');
      expect(gitLogTool.metadata.keywords).toContain('commit');
    });

    it('所有参数都是可选的', () => {
      expect(gitLogTool.parameters.limit.required).toBe(false);
      expect(gitLogTool.parameters.oneline.required).toBe(false);
      expect(gitLogTool.parameters.file.required).toBe(false);
      expect(gitLogTool.parameters.author.required).toBe(false);
      expect(gitLogTool.parameters.since.required).toBe(false);
      expect(gitLogTool.parameters.graph.required).toBe(false);
    });
  });

  describe('execute - 执行', () => {
    it('成功获取提交历史', async () => {
      const result = await gitLogTool.execute({});

      expect(result.success).toBe(true);
      expect(result.output).toContain('abc123');
      expect(result.output).toContain('Fix bug');
    });

    it('使用自定义 limit', async () => {
      const result = await gitLogTool.execute({ limit: 5 });

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

    it('使用 oneline 格式', async () => {
      const result = await gitLogTool.execute({ oneline: true });

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

    it('显示分支图', async () => {
      const result = await gitLogTool.execute({ graph: true });

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

    it('按作者筛选', async () => {
      const result = await gitLogTool.execute({ author: 'John' });

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

    it('按日期筛选', async () => {
      const result = await gitLogTool.execute({ since: '2024-01-01' });

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

    it('查看指定文件的历史', async () => {
      const result = await gitLogTool.execute({ file: 'src/index.ts' });

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

    it('没有提交记录时返回提示', async () => {
      mockExecAsyncResult = { stdout: '', stderr: '' };

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

      expect(result.success).toBe(true);
      expect(result.output).toContain('无提交记录');
    });

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

      const result = await gitLogTool.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 gitLogTool.execute({});

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

    it('Git 命令失败返回错误', async () => {
      // 恢复权限检查
      vi.mocked(getPermissionManager).mockReturnValue({
        checkGitPermission: vi.fn().mockResolvedValue({ allowed: true }),
      } as any);
      mockExecAsyncResult = Object.assign(
        new Error('Command failed'),
        { stdout: '', stderr: 'fatal: not a git repository' }
      );

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

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