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

// 定义一个可控的 mock
let mockExecAsyncResult: { stdout: string; stderr: string } | Error = {
  stdout: '[main abc1234] test commit\n1 file changed',
  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 { gitCommitTool } from '../../../../src/tools/git/git_commit.js';
import { getPermissionManager } from '../../../../src/permission/index.js';

describe('gitCommitTool - Git 提交工具', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mockExecAsyncResult = {
      stdout: '[main abc1234] test commit\n1 file changed',
      stderr: '',
    };
  });

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

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

    it('定义了必需的 message 参数', () => {
      expect(gitCommitTool.parameters.message).toBeDefined();
      expect(gitCommitTool.parameters.message.required).toBe(true);
    });

    it('定义了可选参数', () => {
      expect(gitCommitTool.parameters.amend).toBeDefined();
      expect(gitCommitTool.parameters.amend.required).toBe(false);
      expect(gitCommitTool.parameters.all).toBeDefined();
      expect(gitCommitTool.parameters.all.required).toBe(false);
    });
  });

  describe('execute - 执行', () => {
    it('成功提交', async () => {
      const result = await gitCommitTool.execute({ message: 'test commit' });

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

    it('无消息且无 amend 返回错误', async () => {
      const result = await gitCommitTool.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 gitCommitTool.execute({ message: 'test' });

      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 gitCommitTool.execute({ message: 'test' });

      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('nothing to commit'),
        { stderr: 'nothing to commit, working tree clean', stdout: '', message: 'nothing to commit' }
      );

      const result = await gitCommitTool.execute({ message: 'test' });

      expect(result.success).toBe(false);
      expect(result.error).toContain('没有变更需要提交');
    });

    it('传递操作类型给权限检查', async () => {
      const mockCheck = vi.fn().mockResolvedValue({
        allowed: true,
        action: 'allow',
      });
      vi.mocked(getPermissionManager).mockReturnValue({
        checkGitPermission: mockCheck,
      } as any);

      await gitCommitTool.execute({ message: 'test message' });

      expect(mockCheck).toHaveBeenCalledWith(
        expect.objectContaining({
          operation: 'commit',
          message: 'test message',
        })
      );
    });
  });
});
