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

// Mock child_process
const mockExec = vi.fn();
vi.mock('child_process', () => ({
  exec: (cmd: string, opts: any, cb?: Function) => {
    if (typeof opts === 'function') {
      cb = opts;
    }
    setImmediate(() => {
      const result = mockExec(cmd);
      if (result.error) {
        const err = result.error;
        err.stdout = result.stdout || '';
        err.stderr = result.stderr || '';
        cb?.(err, result.stdout || '', result.stderr || '');
      } else {
        cb?.(null, result.stdout || '', result.stderr || '');
      }
    });
  },
}));

// 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 { gitCheckoutTool } from '../../../../src/tools/git/git_checkout.js';
import { getPermissionManager } from '../../../../src/permission/index.js';

describe('gitCheckoutTool - Git Checkout 工具', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mockExec.mockReturnValue({ stdout: '', stderr: "Switched to branch 'develop'" });
  });

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

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

    it('定义了必需和可选参数', () => {
      expect(gitCheckoutTool.parameters.target.required).toBe(true);
      expect(gitCheckoutTool.parameters.create.required).toBe(false);
      expect(gitCheckoutTool.parameters.force.required).toBe(false);
      expect(gitCheckoutTool.parameters.file.required).toBe(false);
    });
  });

  describe('execute - 执行', () => {
    it('成功切换分支', async () => {
      const result = await gitCheckoutTool.execute({
        target: 'develop',
      });

      expect(result.success).toBe(true);
      expect(result.output).toContain('develop');
      expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('git checkout develop'));
    });

    it('创建并切换到新分支', async () => {
      mockExec.mockReturnValue({ stdout: '', stderr: "Switched to a new branch 'feature/test'" });

      const result = await gitCheckoutTool.execute({
        target: 'feature/test',
        create: true,
      });

      expect(result.success).toBe(true);
      expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('git checkout -b feature/test'));
    });

    it('强制切换分支', async () => {
      const result = await gitCheckoutTool.execute({
        target: 'develop',
        force: true,
      });

      expect(result.success).toBe(true);
      expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('git checkout -f develop'));
    });

    it('恢复文件', async () => {
      mockExec.mockReturnValue({ stdout: '', stderr: '' });

      const result = await gitCheckoutTool.execute({
        target: 'src/index.ts',
        file: true,
      });

      expect(result.success).toBe(true);
      expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('git checkout -- src/index.ts'));
    });

    it('缺少 target 返回错误', async () => {
      const result = await gitCheckoutTool.execute({});

      expect(result.success).toBe(false);
      expect(result.error).toContain('请指定目标');
    });

    it('本地变更冲突时返回友好错误', async () => {
      // 确保权限通过
      vi.mocked(getPermissionManager).mockReturnValue({
        checkGitPermission: vi.fn().mockResolvedValue({ allowed: true }),
      } as any);
      mockExec.mockReturnValue({
        error: new Error('Command failed'),
        stdout: '',
        stderr: 'error: Your local changes would be overwritten by checkout',
      });

      const result = await gitCheckoutTool.execute({
        target: 'main',
      });

      expect(result.success).toBe(false);
      expect(result.error).toContain('本地有未提交的变更');
      expect(result.error).toContain('force: true');
    });

    it('分支不存在时返回友好错误', async () => {
      // 确保权限通过
      vi.mocked(getPermissionManager).mockReturnValue({
        checkGitPermission: vi.fn().mockResolvedValue({ allowed: true }),
      } as any);
      mockExec.mockReturnValue({
        error: new Error('Command failed'),
        stdout: '',
        stderr: "error: pathspec 'nonexistent' did not match",
      });

      const result = await gitCheckoutTool.execute({
        target: 'nonexistent',
      });

      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 gitCheckoutTool.execute({
        target: 'main',
      });

      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 gitCheckoutTool.execute({
        target: 'develop',
      });

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