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(() => '推送 Git 变更到远程仓库'),
}));

import { gitPushTool } from '../../../../src/tools/git/git_push.js';
import { getPermissionManager } from '../../../../src/permission/index.js';

describe('gitPushTool - Git Push 工具', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mockExec.mockReturnValue({
      stdout: '',
      stderr: 'Everything up-to-date',
    });
  });

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

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

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

  describe('execute - 执行', () => {
    it('成功推送', async () => {
      const result = await gitPushTool.execute({});

      expect(result.success).toBe(true);
      // 源代码: stdout || stderr || '推送成功'
      expect(result.output).toContain('推送成功');
      expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('git push origin'));
    });

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

      expect(result.success).toBe(true);
      expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('git push upstream develop'));
    });

    it('设置上游分支', async () => {
      const result = await gitPushTool.execute({ set_upstream: true });

      expect(result.success).toBe(true);
      expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('git push -u'));
    });

    it('强制推送', async () => {
      const result = await gitPushTool.execute({ force: true });

      expect(result.success).toBe(true);
      expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('git push --force'));
    });

    it('推送标签', async () => {
      const result = await gitPushTool.execute({ tags: true });

      expect(result.success).toBe(true);
      expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('git push --tags'));
    });

    it('推送被拒绝时返回友好错误', async () => {
      // 确保权限通过
      vi.mocked(getPermissionManager).mockReturnValue({
        checkGitPermission: vi.fn().mockResolvedValue({ allowed: true }),
      } as any);
      mockExec.mockReturnValue({
        error: new Error('Command failed'),
        stdout: '',
        stderr: '! [rejected]        main -> main (fetch first)',
      });

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

      expect(result.success).toBe(false);
      expect(result.error).toContain('推送被拒绝');
      expect(result.error).toContain('git_pull');
    });

    it('没有上游分支时返回友好错误', async () => {
      // 确保权限通过
      vi.mocked(getPermissionManager).mockReturnValue({
        checkGitPermission: vi.fn().mockResolvedValue({ allowed: true }),
      } as any);
      mockExec.mockReturnValue({
        error: new Error('Command failed'),
        stdout: '',
        stderr: 'fatal: The current branch has no upstream branch',
      });

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

      expect(result.success).toBe(false);
      expect(result.error).toContain('没有设置上游分支');
      expect(result.error).toContain('set_upstream: true');
    });

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

      const result = await gitPushTool.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 gitPushTool.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);
      mockExec.mockReturnValue({
        error: new Error('Command failed'),
        stdout: '',
        stderr: 'fatal: not a git repository',
      });

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

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