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

// 定义一个可控的 mock
let mockExecAsyncResult: { stdout: string; stderr: string } | Error = {
  stdout: 'Already up to date.',
  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(() => '从远程仓库拉取更新'),
}));

import { gitPullTool } from '../../../../src/tools/git/git_pull.js';
import { getPermissionManager } from '../../../../src/permission/index.js';

describe('gitPullTool - Git Pull 工具', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mockExecAsyncResult = {
      stdout: 'Already up to date.',
      stderr: '',
    };
  });

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

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

    it('所有参数都是可选的', () => {
      expect(gitPullTool.parameters.remote.required).toBe(false);
      expect(gitPullTool.parameters.branch.required).toBe(false);
      expect(gitPullTool.parameters.rebase.required).toBe(false);
    });
  });

  describe('execute - 执行', () => {
    it('成功拉取更新', async () => {
      const result = await gitPullTool.execute({});

      expect(result.success).toBe(true);
      expect(result.output).toContain('Already up to date');
    });

    it('指定远程仓库和分支', async () => {
      const result = await gitPullTool.execute({
        remote: 'upstream',
        branch: 'develop',
      });

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

    it('使用 rebase 模式', async () => {
      const result = await gitPullTool.execute({ rebase: true });

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

    it('合并冲突时返回友好错误', async () => {
      mockExecAsyncResult = Object.assign(
        new Error('Command failed'),
        { stdout: '', stderr: 'CONFLICT (content): Merge conflict in file.txt' }
      );

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

      expect(result.success).toBe(false);
      expect(result.error).toContain('合并冲突');
      expect(result.error).toContain('手动解决');
    });

    it('本地变更时返回友好错误', async () => {
      mockExecAsyncResult = Object.assign(
        new Error('Command failed'),
        { stdout: '', stderr: 'error: Your local changes would be overwritten by merge' }
      );

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

      expect(result.success).toBe(false);
      expect(result.error).toContain('未提交的变更');
    });

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

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

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

    it('Git 命令失败返回错误', async () => {
      // 恢复权限检查（因为之前的测试可能修改了 mock）
      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 gitPullTool.execute({});

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