feat: 添加完整的单元测试套件
- 新增 vitest 测试框架配置 - 添加 54 个测试文件,共 951 个测试用例 - 覆盖核心模块: - Agent: executor, registry, config-loader, permission-merger - Context: manager, compaction, prune, token-counter - Permission: manager, bash/file/git/web checkers, wildcard - Session: manager, storage - Tools: filesystem (12个), git (10个), web, shell, todo, task - LSP: client, server, language - Utils: config, diff - UI: terminal
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
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',
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user