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

// 定义一个可控的 mock
let mockExecAsyncResult: { stdout: string; stderr: string } | Error = {
  stdout: 'command output',
  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(() => ({
    checkBashPermission: vi.fn().mockResolvedValue({
      allowed: true,
      action: 'allow',
    }),
  })),
}));

// Mock loadDescription
vi.mock('../../../../src/tools/load_description.js', () => ({
  loadDescription: vi.fn(() => '执行 shell 命令'),
}));

import { bashTool } from '../../../../src/tools/shell/bash.js';
import { getPermissionManager } from '../../../../src/permission/index.js';

describe('bashTool - Bash 命令工具', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mockExecAsyncResult = {
      stdout: 'command output',
      stderr: '',
    };
  });

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

    it('有正确的元数据', () => {
      expect(bashTool.metadata.category).toBe('shell');
      expect(bashTool.metadata.keywords).toContain('bash');
      expect(bashTool.metadata.keywords).toContain('command');
      expect(bashTool.metadata.keywords).toContain('terminal');
    });

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

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

  describe('execute - 执行', () => {
    it('成功执行命令', async () => {
      const result = await bashTool.execute({ command: 'ls -la' });

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

    it('包含 stderr 输出', async () => {
      mockExecAsyncResult = {
        stdout: 'output',
        stderr: 'warning message',
      };

      const result = await bashTool.execute({ command: 'some_cmd' });

      expect(result.success).toBe(true);
      expect(result.output).toContain('output');
      expect(result.output).toContain('STDERR');
      expect(result.output).toContain('warning message');
    });

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

      const result = await bashTool.execute({ command: 'rm -rf /' });

      expect(result.success).toBe(false);
      expect(result.error).toContain('权限被拒绝');
    });

    it('需要确认时返回提示', async () => {
      vi.mocked(getPermissionManager).mockReturnValue({
        checkBashPermission: vi.fn().mockResolvedValue({
          allowed: false,
          action: 'ask',
          needsConfirmation: true,
          reason: '需要确认',
          patterns: ['rm *'],
        }),
      } as any);

      const result = await bashTool.execute({ command: 'rm file.txt' });

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

    it('命令执行失败时返回错误', async () => {
      vi.mocked(getPermissionManager).mockReturnValue({
        checkBashPermission: vi.fn().mockResolvedValue({
          allowed: true,
          action: 'allow',
        }),
      } as any);
      mockExecAsyncResult = Object.assign(
        new Error('Command failed'),
        { stdout: '', stderr: 'command not found', message: 'Command failed' }
      );

      const result = await bashTool.execute({ command: 'nonexistent_cmd' });

      expect(result.success).toBe(false);
      expect(result.error).toContain('command not found');
    });

    it('保留失败命令的 stdout', async () => {
      vi.mocked(getPermissionManager).mockReturnValue({
        checkBashPermission: vi.fn().mockResolvedValue({
          allowed: true,
          action: 'allow',
        }),
      } as any);
      mockExecAsyncResult = Object.assign(
        new Error('Command failed'),
        { stdout: 'partial output', stderr: 'error occurred', message: 'Command failed' }
      );

      const result = await bashTool.execute({ command: 'failing_cmd' });

      expect(result.success).toBe(false);
      expect(result.output).toBe('partial output');
      expect(result.error).toContain('error occurred');
    });

    it('传递正确的参数给权限检查', async () => {
      const mockCheck = vi.fn().mockResolvedValue({
        allowed: true,
        action: 'allow',
      });
      vi.mocked(getPermissionManager).mockReturnValue({
        checkBashPermission: mockCheck,
      } as any);

      await bashTool.execute({ command: 'ls -la', cwd: '/home/user' });

      expect(mockCheck).toHaveBeenCalledWith({
        command: 'ls -la',
        workdir: '/home/user',
      });
    });
  });
});
