feat: 重构为 Monorepo 架构并实现 HTTP Server
架构变更: - 采用 pnpm workspaces 实现 Monorepo 结构 - 将现有代码迁移到 packages/core - 新增 packages/server HTTP 服务层 Server 功能: - REST API: 会话管理、工具管理、配置管理 - WebSocket: 实时双向通信支持 - SSE: 服务端事件推送 - Hono + Bun 作为运行时 API 端点: - GET/POST /api/sessions - 会话 CRUD - GET/POST /api/sessions/:id/messages - 消息管理 - GET /api/sessions/:id/events - SSE 事件流 - WS /api/ws/:sessionId - WebSocket 连接 - GET/POST /api/tools - 工具管理 - GET/PUT /api/config - 配置管理
This commit is contained in:
@@ -0,0 +1,288 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import {
|
||||
loadAgentConfig,
|
||||
saveAgentConfig,
|
||||
getConfigTemplate,
|
||||
} from '../../../src/agent/config-loader.js';
|
||||
import type { AgentConfigFile } from '../../../src/agent/types.js';
|
||||
|
||||
// Mock fs
|
||||
vi.mock('fs', () => ({
|
||||
existsSync: vi.fn(),
|
||||
promises: {
|
||||
readFile: vi.fn(),
|
||||
writeFile: vi.fn(),
|
||||
mkdir: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
// Mock js-yaml
|
||||
vi.mock('js-yaml', () => ({
|
||||
load: vi.fn((content: string) => JSON.parse(content)),
|
||||
dump: vi.fn((obj: unknown) => JSON.stringify(obj, null, 2)),
|
||||
}));
|
||||
|
||||
import * as fs from 'fs';
|
||||
|
||||
describe('loadAgentConfig - 加载 Agent 配置', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('无配置文件时返回 null', async () => {
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
const config = await loadAgentConfig('/test/project');
|
||||
|
||||
expect(config).toBeNull();
|
||||
});
|
||||
|
||||
it('加载 JSON 配置文件', async () => {
|
||||
const mockConfig: AgentConfigFile = {
|
||||
defaults: {
|
||||
maxSteps: 20,
|
||||
},
|
||||
agents: {
|
||||
'custom-agent': {
|
||||
description: '自定义 Agent',
|
||||
mode: 'subagent',
|
||||
prompt: '你是助手',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
vi.mocked(fs.existsSync).mockImplementation((path: unknown) =>
|
||||
String(path).endsWith('.json')
|
||||
);
|
||||
vi.mocked(fs.promises.readFile).mockResolvedValue(JSON.stringify(mockConfig));
|
||||
|
||||
const config = await loadAgentConfig('/test/project');
|
||||
|
||||
expect(config).not.toBeNull();
|
||||
expect(config?.defaults?.maxSteps).toBe(20);
|
||||
expect(config?.agents?.['custom-agent']).toBeDefined();
|
||||
});
|
||||
|
||||
it('加载 YAML 配置文件', async () => {
|
||||
const mockConfig: AgentConfigFile = {
|
||||
defaults: {
|
||||
maxSteps: 15,
|
||||
},
|
||||
agents: {},
|
||||
};
|
||||
|
||||
vi.mocked(fs.existsSync).mockImplementation((path: unknown) =>
|
||||
String(path).endsWith('.yaml')
|
||||
);
|
||||
vi.mocked(fs.promises.readFile).mockResolvedValue(JSON.stringify(mockConfig));
|
||||
|
||||
const config = await loadAgentConfig('/test/project');
|
||||
|
||||
expect(config).not.toBeNull();
|
||||
});
|
||||
|
||||
it('无效配置格式返回 null', async () => {
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.promises.readFile).mockResolvedValue('invalid json');
|
||||
|
||||
// 会在解析时失败
|
||||
const config = await loadAgentConfig('/test/project');
|
||||
|
||||
// 取决于实现,可能是 null 或抛出错误后 null
|
||||
expect(config).toBeNull();
|
||||
});
|
||||
|
||||
it('配置搜索顺序', async () => {
|
||||
// 测试搜索多个路径
|
||||
const calls: string[] = [];
|
||||
vi.mocked(fs.existsSync).mockImplementation((path: unknown) => {
|
||||
calls.push(String(path));
|
||||
return false;
|
||||
});
|
||||
|
||||
await loadAgentConfig('/test/project');
|
||||
|
||||
// 应该搜索多个配置路径
|
||||
expect(calls.length).toBeGreaterThan(0);
|
||||
expect(calls.some(p => p.includes('.ai-assist'))).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('saveAgentConfig - 保存 Agent 配置', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
});
|
||||
|
||||
it('保存 JSON 格式配置', async () => {
|
||||
const config: AgentConfigFile = {
|
||||
defaults: {
|
||||
maxSteps: 10,
|
||||
},
|
||||
agents: {},
|
||||
};
|
||||
|
||||
await saveAgentConfig('/test/project', config, 'json');
|
||||
|
||||
expect(fs.promises.mkdir).toHaveBeenCalled();
|
||||
expect(fs.promises.writeFile).toHaveBeenCalledWith(
|
||||
expect.stringContaining('agents.json'),
|
||||
expect.any(String),
|
||||
'utf-8'
|
||||
);
|
||||
});
|
||||
|
||||
it('保存 YAML 格式配置', async () => {
|
||||
const config: AgentConfigFile = {
|
||||
defaults: {
|
||||
maxSteps: 10,
|
||||
},
|
||||
agents: {},
|
||||
};
|
||||
|
||||
await saveAgentConfig('/test/project', config, 'yaml');
|
||||
|
||||
expect(fs.promises.writeFile).toHaveBeenCalledWith(
|
||||
expect.stringContaining('agents.yaml'),
|
||||
expect.any(String),
|
||||
'utf-8'
|
||||
);
|
||||
});
|
||||
|
||||
it('创建配置目录如果不存在', async () => {
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
await saveAgentConfig('/test/project', { agents: {} }, 'json');
|
||||
|
||||
expect(fs.promises.mkdir).toHaveBeenCalledWith(
|
||||
expect.stringContaining('.ai-assist'),
|
||||
{ recursive: true }
|
||||
);
|
||||
});
|
||||
|
||||
it('目录已存在时不重复创建', async () => {
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
|
||||
await saveAgentConfig('/test/project', { agents: {} }, 'json');
|
||||
|
||||
expect(fs.promises.mkdir).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getConfigTemplate - 获取配置模板', () => {
|
||||
it('返回有效的配置模板', () => {
|
||||
const template = getConfigTemplate();
|
||||
|
||||
expect(template).toBeDefined();
|
||||
expect(template.defaults).toBeDefined();
|
||||
expect(template.agents).toBeDefined();
|
||||
});
|
||||
|
||||
it('模板包含默认配置', () => {
|
||||
const template = getConfigTemplate();
|
||||
|
||||
expect(template.defaults?.maxSteps).toBeDefined();
|
||||
expect(template.defaults?.model).toBeDefined();
|
||||
});
|
||||
|
||||
it('模板包含示例 Agent', () => {
|
||||
const template = getConfigTemplate();
|
||||
|
||||
expect(template.agents).toBeDefined();
|
||||
expect(Object.keys(template.agents || {}).length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('示例 Agent 包含必要字段', () => {
|
||||
const template = getConfigTemplate();
|
||||
const agents = template.agents || {};
|
||||
const firstAgent = Object.values(agents)[0];
|
||||
|
||||
expect(firstAgent).toBeDefined();
|
||||
expect(firstAgent.description).toBeDefined();
|
||||
expect(firstAgent.mode).toBeDefined();
|
||||
});
|
||||
|
||||
it('模板包含权限配置示例', () => {
|
||||
const template = getConfigTemplate();
|
||||
|
||||
expect(template.defaults?.permission).toBeDefined();
|
||||
expect(template.defaults?.permission?.bash).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('配置验证', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('空对象是有效配置', async () => {
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.promises.readFile).mockResolvedValue(JSON.stringify({}));
|
||||
|
||||
const config = await loadAgentConfig('/test/project');
|
||||
|
||||
expect(config).not.toBeNull();
|
||||
});
|
||||
|
||||
it('只有 defaults 的配置有效', async () => {
|
||||
const mockConfig = {
|
||||
defaults: {
|
||||
maxSteps: 10,
|
||||
},
|
||||
};
|
||||
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.promises.readFile).mockResolvedValue(JSON.stringify(mockConfig));
|
||||
|
||||
const config = await loadAgentConfig('/test/project');
|
||||
|
||||
expect(config).not.toBeNull();
|
||||
expect(config?.defaults?.maxSteps).toBe(10);
|
||||
});
|
||||
|
||||
it('只有 agents 的配置有效', async () => {
|
||||
const mockConfig = {
|
||||
agents: {
|
||||
'test-agent': {
|
||||
description: 'Test',
|
||||
mode: 'subagent',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.promises.readFile).mockResolvedValue(JSON.stringify(mockConfig));
|
||||
|
||||
const config = await loadAgentConfig('/test/project');
|
||||
|
||||
expect(config).not.toBeNull();
|
||||
expect(config?.agents?.['test-agent']).toBeDefined();
|
||||
});
|
||||
|
||||
it('defaults 为非对象时配置无效', async () => {
|
||||
const mockConfig = {
|
||||
defaults: 'invalid',
|
||||
};
|
||||
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.promises.readFile).mockResolvedValue(JSON.stringify(mockConfig));
|
||||
|
||||
const config = await loadAgentConfig('/test/project');
|
||||
|
||||
// 应该返回 null(无效配置)
|
||||
expect(config).toBeNull();
|
||||
});
|
||||
|
||||
it('agents 为非对象时配置无效', async () => {
|
||||
const mockConfig = {
|
||||
agents: 'invalid',
|
||||
};
|
||||
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.promises.readFile).mockResolvedValue(JSON.stringify(mockConfig));
|
||||
|
||||
const config = await loadAgentConfig('/test/project');
|
||||
|
||||
expect(config).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,429 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import type { LanguageModel } from 'ai';
|
||||
import type { Tool, AgentConfig } from '../../../src/types/index.js';
|
||||
import type { AgentInfo, AgentExecutionContext } from '../../../src/agent/types.js';
|
||||
|
||||
// Mock ai module
|
||||
const mockGenerateText = vi.fn();
|
||||
const mockStreamText = vi.fn();
|
||||
vi.mock('ai', () => ({
|
||||
generateText: (...args: unknown[]) => mockGenerateText(...args),
|
||||
streamText: (...args: unknown[]) => mockStreamText(...args),
|
||||
stepCountIs: vi.fn((n) => n),
|
||||
}));
|
||||
|
||||
// Mock buildZodSchema
|
||||
vi.mock('../../../src/types/index.js', async (importOriginal) => {
|
||||
const actual = await importOriginal() as Record<string, unknown>;
|
||||
return {
|
||||
...actual,
|
||||
buildZodSchema: vi.fn(() => ({})),
|
||||
};
|
||||
});
|
||||
|
||||
// Mock ToolRegistry
|
||||
class MockToolRegistry {
|
||||
getAllTools = vi.fn(() => []);
|
||||
}
|
||||
vi.mock('../../../src/tools/registry.js', () => ({
|
||||
ToolRegistry: MockToolRegistry,
|
||||
}));
|
||||
|
||||
// Mock permission merger
|
||||
const mockCheckBashPermission = vi.fn();
|
||||
vi.mock('../../../src/agent/permission-merger.js', () => ({
|
||||
checkBashPermission: (...args: unknown[]) => mockCheckBashPermission(...args),
|
||||
}));
|
||||
|
||||
// Mock providers
|
||||
const mockGetModelFactory = vi.fn();
|
||||
vi.mock('../../../src/core/providers.js', () => ({
|
||||
getModelFactory: (...args: unknown[]) => mockGetModelFactory(...args),
|
||||
}));
|
||||
|
||||
import { AgentExecutor } from '../../../src/agent/executor.js';
|
||||
|
||||
describe('AgentExecutor - Agent 执行器扩展测试', () => {
|
||||
let executor: AgentExecutor;
|
||||
let mockToolRegistry: MockToolRegistry;
|
||||
let mockModel: LanguageModel;
|
||||
|
||||
const baseConfig: AgentConfig = {
|
||||
provider: 'anthropic',
|
||||
apiKey: 'test-key',
|
||||
model: 'claude-sonnet-4-20250514',
|
||||
maxTokens: 4096,
|
||||
systemPrompt: 'You are a helpful assistant',
|
||||
};
|
||||
|
||||
const basicAgentInfo: AgentInfo = {
|
||||
name: 'test-agent',
|
||||
description: 'Test agent',
|
||||
mode: 'subagent',
|
||||
};
|
||||
|
||||
const createMockTool = (name: string): Tool => ({
|
||||
name,
|
||||
description: `${name} tool`,
|
||||
parameters: {},
|
||||
execute: vi.fn().mockResolvedValue({ success: true, output: 'ok' }),
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
|
||||
mockToolRegistry = new MockToolRegistry();
|
||||
mockModel = {} as LanguageModel;
|
||||
|
||||
mockGetModelFactory.mockReturnValue(() => mockModel);
|
||||
|
||||
mockGenerateText.mockResolvedValue({
|
||||
text: 'Response text',
|
||||
steps: [{ text: 'step1' }],
|
||||
});
|
||||
|
||||
mockStreamText.mockReturnValue({
|
||||
textStream: (async function* () {
|
||||
yield 'chunk1';
|
||||
yield 'chunk2';
|
||||
})(),
|
||||
response: Promise.resolve({}),
|
||||
});
|
||||
|
||||
mockCheckBashPermission.mockReturnValue('allow');
|
||||
|
||||
executor = new AgentExecutor(basicAgentInfo, baseConfig, mockToolRegistry as any);
|
||||
});
|
||||
|
||||
describe('constructor - 构造函数', () => {
|
||||
it('初始化执行器', () => {
|
||||
expect(executor).toBeDefined();
|
||||
expect(mockGetModelFactory).toHaveBeenCalledWith('anthropic', expect.any(Object));
|
||||
});
|
||||
|
||||
it('使用 Agent 指定的 provider', () => {
|
||||
const agentWithProvider: AgentInfo = {
|
||||
...basicAgentInfo,
|
||||
model: { provider: 'openai', model: 'gpt-4' },
|
||||
};
|
||||
|
||||
new AgentExecutor(agentWithProvider, baseConfig, mockToolRegistry as any);
|
||||
|
||||
expect(mockGetModelFactory).toHaveBeenCalledWith('openai', expect.any(Object));
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行任务', () => {
|
||||
it('非流式模式返回结果', async () => {
|
||||
const context: AgentExecutionContext = {};
|
||||
|
||||
const result = await executor.execute('test prompt', context);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.text).toBe('Response text');
|
||||
expect(result.steps).toBe(1);
|
||||
expect(mockGenerateText).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('流式模式调用回调', async () => {
|
||||
const onStream = vi.fn();
|
||||
const context: AgentExecutionContext = { onStream };
|
||||
|
||||
const result = await executor.execute('test prompt', context);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(onStream).toHaveBeenCalledWith('chunk1');
|
||||
expect(onStream).toHaveBeenCalledWith('chunk2');
|
||||
expect(mockStreamText).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('使用 Agent 的系统提示词', async () => {
|
||||
const agentWithPrompt: AgentInfo = {
|
||||
...basicAgentInfo,
|
||||
prompt: 'Custom system prompt',
|
||||
};
|
||||
const customExecutor = new AgentExecutor(agentWithPrompt, baseConfig, mockToolRegistry as any);
|
||||
|
||||
await customExecutor.execute('test', {});
|
||||
|
||||
expect(mockGenerateText).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
system: 'Custom system prompt',
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('使用基础配置的系统提示词', async () => {
|
||||
await executor.execute('test', {});
|
||||
|
||||
expect(mockGenerateText).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
system: 'You are a helpful assistant',
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('处理执行错误', async () => {
|
||||
mockGenerateText.mockRejectedValue(new Error('API Error'));
|
||||
|
||||
const result = await executor.execute('test', {});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('API Error');
|
||||
});
|
||||
|
||||
it('包含图片时构建多模态消息', async () => {
|
||||
const context: AgentExecutionContext = {
|
||||
images: [{ data: 'base64data', mimeType: 'image/png' }],
|
||||
};
|
||||
|
||||
await executor.execute('describe this', context);
|
||||
|
||||
expect(mockGenerateText).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
messages: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
content: expect.arrayContaining([
|
||||
expect.objectContaining({ type: 'image' }),
|
||||
expect.objectContaining({ type: 'text' }),
|
||||
]),
|
||||
}),
|
||||
]),
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('使用 Agent 的 maxSteps 配置', async () => {
|
||||
const agentWithSteps: AgentInfo = {
|
||||
...basicAgentInfo,
|
||||
maxSteps: 20,
|
||||
};
|
||||
const customExecutor = new AgentExecutor(agentWithSteps, baseConfig, mockToolRegistry as any);
|
||||
|
||||
await customExecutor.execute('test', {});
|
||||
|
||||
expect(mockGenerateText).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
stopWhen: 20,
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('使用 Agent 的 maxTokens 配置', async () => {
|
||||
const agentWithTokens: AgentInfo = {
|
||||
...basicAgentInfo,
|
||||
model: { model: 'claude-sonnet', maxTokens: 8000 },
|
||||
};
|
||||
const customExecutor = new AgentExecutor(agentWithTokens, baseConfig, mockToolRegistry as any);
|
||||
|
||||
await customExecutor.execute('test', {});
|
||||
|
||||
expect(mockGenerateText).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
maxOutputTokens: 8000,
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getFilteredTools - 工具过滤', () => {
|
||||
it('无配置返回所有工具', async () => {
|
||||
const tools = [createMockTool('read'), createMockTool('write')];
|
||||
mockToolRegistry.getAllTools.mockReturnValue(tools);
|
||||
|
||||
await executor.execute('test', {});
|
||||
|
||||
// 验证所有工具都被使用
|
||||
expect(mockGenerateText).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
tools: expect.objectContaining({
|
||||
read: expect.any(Object),
|
||||
write: expect.any(Object),
|
||||
}),
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('enabled 只保留指定工具', async () => {
|
||||
const agentWithEnabled: AgentInfo = {
|
||||
...basicAgentInfo,
|
||||
tools: { enabled: ['read'] },
|
||||
};
|
||||
const tools = [createMockTool('read'), createMockTool('write')];
|
||||
mockToolRegistry.getAllTools.mockReturnValue(tools);
|
||||
|
||||
const customExecutor = new AgentExecutor(agentWithEnabled, baseConfig, mockToolRegistry as any);
|
||||
await customExecutor.execute('test', {});
|
||||
|
||||
expect(mockGenerateText).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
tools: expect.objectContaining({
|
||||
read: expect.any(Object),
|
||||
}),
|
||||
})
|
||||
);
|
||||
// write 不应该存在
|
||||
const call = mockGenerateText.mock.calls[0][0];
|
||||
expect(call.tools.write).toBeUndefined();
|
||||
});
|
||||
|
||||
it('disabled 移除指定工具', async () => {
|
||||
const agentWithDisabled: AgentInfo = {
|
||||
...basicAgentInfo,
|
||||
tools: { disabled: ['write'] },
|
||||
};
|
||||
const tools = [createMockTool('read'), createMockTool('write')];
|
||||
mockToolRegistry.getAllTools.mockReturnValue(tools);
|
||||
|
||||
const customExecutor = new AgentExecutor(agentWithDisabled, baseConfig, mockToolRegistry as any);
|
||||
await customExecutor.execute('test', {});
|
||||
|
||||
const call = mockGenerateText.mock.calls[0][0];
|
||||
expect(call.tools.read).toBeDefined();
|
||||
expect(call.tools.write).toBeUndefined();
|
||||
});
|
||||
|
||||
it('noTask 移除 task 工具', async () => {
|
||||
const agentWithNoTask: AgentInfo = {
|
||||
...basicAgentInfo,
|
||||
tools: { noTask: true },
|
||||
};
|
||||
const tools = [createMockTool('read'), createMockTool('task')];
|
||||
mockToolRegistry.getAllTools.mockReturnValue(tools);
|
||||
|
||||
const customExecutor = new AgentExecutor(agentWithNoTask, baseConfig, mockToolRegistry as any);
|
||||
await customExecutor.execute('test', {});
|
||||
|
||||
const call = mockGenerateText.mock.calls[0][0];
|
||||
expect(call.tools.task).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('checkToolPermission - 权限检查', () => {
|
||||
it('无权限配置时允许所有', async () => {
|
||||
const tools = [createMockTool('bash')];
|
||||
mockToolRegistry.getAllTools.mockReturnValue(tools);
|
||||
|
||||
await executor.execute('test', {});
|
||||
|
||||
// 工具应该被包含
|
||||
const call = mockGenerateText.mock.calls[0][0];
|
||||
expect(call.tools.bash).toBeDefined();
|
||||
});
|
||||
|
||||
it('bash 命令被禁止时拒绝', async () => {
|
||||
const agentWithBashDeny: AgentInfo = {
|
||||
...basicAgentInfo,
|
||||
permission: {
|
||||
bash: { allow: [], deny: ['rm'], ask: [] },
|
||||
},
|
||||
};
|
||||
mockCheckBashPermission.mockReturnValue('deny');
|
||||
|
||||
const bashTool = createMockTool('bash');
|
||||
mockToolRegistry.getAllTools.mockReturnValue([bashTool]);
|
||||
|
||||
const customExecutor = new AgentExecutor(agentWithBashDeny, baseConfig, mockToolRegistry as any);
|
||||
await customExecutor.execute('test', {});
|
||||
|
||||
// 模拟工具执行
|
||||
const call = mockGenerateText.mock.calls[0][0];
|
||||
const toolResult = await call.tools.bash.execute({ command: 'rm -rf /' });
|
||||
|
||||
expect(toolResult.success).toBe(false);
|
||||
expect(toolResult.error).toContain('权限拒绝');
|
||||
});
|
||||
|
||||
it('文件写入被禁止时拒绝', async () => {
|
||||
const agentWithFileDeny: AgentInfo = {
|
||||
...basicAgentInfo,
|
||||
permission: {
|
||||
file: { write: 'deny', edit: 'allow', delete: 'allow' },
|
||||
},
|
||||
};
|
||||
|
||||
const writeTool = createMockTool('write_file');
|
||||
mockToolRegistry.getAllTools.mockReturnValue([writeTool]);
|
||||
|
||||
const customExecutor = new AgentExecutor(agentWithFileDeny, baseConfig, mockToolRegistry as any);
|
||||
await customExecutor.execute('test', {});
|
||||
|
||||
const call = mockGenerateText.mock.calls[0][0];
|
||||
const toolResult = await call.tools.write_file.execute({ path: '/test.txt' });
|
||||
|
||||
expect(toolResult.success).toBe(false);
|
||||
expect(toolResult.error).toContain('权限拒绝');
|
||||
});
|
||||
|
||||
it('Git 写操作被禁止时拒绝', async () => {
|
||||
const agentWithGitDeny: AgentInfo = {
|
||||
...basicAgentInfo,
|
||||
permission: {
|
||||
git: { write: 'deny' },
|
||||
},
|
||||
};
|
||||
|
||||
const gitCommitTool = createMockTool('git_commit');
|
||||
mockToolRegistry.getAllTools.mockReturnValue([gitCommitTool]);
|
||||
|
||||
const customExecutor = new AgentExecutor(agentWithGitDeny, baseConfig, mockToolRegistry as any);
|
||||
await customExecutor.execute('test', {});
|
||||
|
||||
const call = mockGenerateText.mock.calls[0][0];
|
||||
const toolResult = await call.tools.git_commit.execute({ message: 'test' });
|
||||
|
||||
expect(toolResult.success).toBe(false);
|
||||
expect(toolResult.error).toContain('Git 写操作被禁止');
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildMessageContent - 构建消息内容', () => {
|
||||
it('无图片时返回纯文本', async () => {
|
||||
await executor.execute('simple text', {});
|
||||
|
||||
expect(mockGenerateText).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
messages: [{ role: 'user', content: 'simple text' }],
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('有图片时返回多模态内容', async () => {
|
||||
const context: AgentExecutionContext = {
|
||||
images: [
|
||||
{ data: 'img1', mimeType: 'image/png' },
|
||||
{ data: 'img2', mimeType: 'image/jpeg' },
|
||||
],
|
||||
};
|
||||
|
||||
await executor.execute('describe images', context);
|
||||
|
||||
const call = mockGenerateText.mock.calls[0][0];
|
||||
const content = call.messages[0].content;
|
||||
|
||||
expect(Array.isArray(content)).toBe(true);
|
||||
expect(content.length).toBe(3); // 2 images + 1 text
|
||||
expect(content[0].type).toBe('image');
|
||||
expect(content[1].type).toBe('image');
|
||||
expect(content[2].type).toBe('text');
|
||||
});
|
||||
});
|
||||
|
||||
describe('sessionId 处理', () => {
|
||||
it('有 parentSessionId 时使用它', async () => {
|
||||
const context: AgentExecutionContext = {
|
||||
parentSessionId: 'parent-123',
|
||||
};
|
||||
|
||||
const result = await executor.execute('test', context);
|
||||
|
||||
expect(result.sessionId).toBe('parent-123');
|
||||
});
|
||||
|
||||
it('无 parentSessionId 时使用 standalone', async () => {
|
||||
const result = await executor.execute('test', {});
|
||||
|
||||
expect(result.sessionId).toBe('standalone');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,363 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// Mock state
|
||||
const mockState = {
|
||||
generateTextResult: {
|
||||
text: '任务完成',
|
||||
steps: [{ toolCalls: [] }],
|
||||
},
|
||||
streamTextResult: {
|
||||
textStream: (async function* () {
|
||||
yield '流式';
|
||||
yield '输出';
|
||||
})(),
|
||||
response: Promise.resolve({}),
|
||||
},
|
||||
};
|
||||
|
||||
// Mock @ai-sdk/anthropic
|
||||
vi.mock('@ai-sdk/anthropic', () => ({
|
||||
createAnthropic: vi.fn(() => vi.fn(() => ({ modelId: 'claude-3' }))),
|
||||
}));
|
||||
|
||||
// Mock @ai-sdk/deepseek
|
||||
vi.mock('@ai-sdk/deepseek', () => ({
|
||||
createDeepSeek: vi.fn(() => vi.fn(() => ({ modelId: 'deepseek' }))),
|
||||
}));
|
||||
|
||||
// Mock ai package
|
||||
vi.mock('ai', () => ({
|
||||
generateText: vi.fn(async () => mockState.generateTextResult),
|
||||
streamText: vi.fn(() => mockState.streamTextResult),
|
||||
stepCountIs: vi.fn(() => () => false),
|
||||
}));
|
||||
|
||||
// Mock permission-merger
|
||||
vi.mock('../../../src/agent/permission-merger.js', () => ({
|
||||
checkBashPermission: vi.fn(() => 'allow'),
|
||||
}));
|
||||
|
||||
// Mock types
|
||||
vi.mock('../../../src/types/index.js', () => ({
|
||||
buildZodSchema: vi.fn(() => ({})),
|
||||
}));
|
||||
|
||||
import { AgentExecutor } from '../../../src/agent/executor.js';
|
||||
import { generateText, streamText } from 'ai';
|
||||
import { checkBashPermission } from '../../../src/agent/permission-merger.js';
|
||||
|
||||
describe('AgentExecutor - Agent 执行器', () => {
|
||||
let executor: AgentExecutor;
|
||||
let mockToolRegistry: any;
|
||||
let mockAgentInfo: any;
|
||||
let mockBaseConfig: any;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
|
||||
mockToolRegistry = {
|
||||
getAllTools: vi.fn(() => [
|
||||
{
|
||||
name: 'bash',
|
||||
description: '执行命令',
|
||||
parameters: { command: { type: 'string', required: true } },
|
||||
execute: vi.fn().mockResolvedValue({ success: true, output: 'ok' }),
|
||||
},
|
||||
{
|
||||
name: 'read_file',
|
||||
description: '读取文件',
|
||||
parameters: { path: { type: 'string', required: true } },
|
||||
execute: vi.fn().mockResolvedValue({ success: true, output: 'content' }),
|
||||
},
|
||||
{
|
||||
name: 'task',
|
||||
description: '子任务',
|
||||
parameters: { prompt: { type: 'string', required: true } },
|
||||
execute: vi.fn().mockResolvedValue({ success: true, output: 'done' }),
|
||||
},
|
||||
]),
|
||||
};
|
||||
|
||||
mockAgentInfo = {
|
||||
name: 'test-agent',
|
||||
description: '测试 Agent',
|
||||
mode: 'subagent',
|
||||
prompt: '你是测试助手',
|
||||
};
|
||||
|
||||
mockBaseConfig = {
|
||||
provider: 'anthropic',
|
||||
model: 'claude-3-sonnet',
|
||||
apiKey: 'test-api-key',
|
||||
maxTokens: 4096,
|
||||
systemPrompt: '默认系统提示词',
|
||||
};
|
||||
|
||||
// 重置 mock 结果
|
||||
mockState.generateTextResult = {
|
||||
text: '任务完成',
|
||||
steps: [{ toolCalls: [] }],
|
||||
};
|
||||
|
||||
executor = new AgentExecutor(mockAgentInfo, mockBaseConfig, mockToolRegistry);
|
||||
});
|
||||
|
||||
describe('构造函数', () => {
|
||||
it('成功创建 Anthropic provider', () => {
|
||||
const exec = new AgentExecutor(mockAgentInfo, mockBaseConfig, mockToolRegistry);
|
||||
expect(exec).toBeDefined();
|
||||
});
|
||||
|
||||
it('成功创建 DeepSeek provider', () => {
|
||||
const config = { ...mockBaseConfig, provider: 'deepseek' as const };
|
||||
const exec = new AgentExecutor(mockAgentInfo, config, mockToolRegistry);
|
||||
expect(exec).toBeDefined();
|
||||
});
|
||||
|
||||
it('不支持的 provider 抛出错误', () => {
|
||||
const config = { ...mockBaseConfig, provider: 'unknown' as any };
|
||||
expect(() => new AgentExecutor(mockAgentInfo, config, mockToolRegistry)).toThrow('不支持的 provider');
|
||||
});
|
||||
|
||||
it('使用 Agent 指定的 provider', () => {
|
||||
const agentInfo = {
|
||||
...mockAgentInfo,
|
||||
model: { provider: 'deepseek' as const, model: 'deepseek-chat' },
|
||||
};
|
||||
const exec = new AgentExecutor(agentInfo, mockBaseConfig, mockToolRegistry);
|
||||
expect(exec).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('非流式模式成功执行', async () => {
|
||||
const result = await executor.execute('测试任务', {});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.text).toBe('任务完成');
|
||||
expect(generateText).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('流式模式成功执行', async () => {
|
||||
const onStream = vi.fn();
|
||||
|
||||
// 重置流式结果
|
||||
mockState.streamTextResult = {
|
||||
textStream: (async function* () {
|
||||
yield '流式';
|
||||
yield '输出';
|
||||
})(),
|
||||
response: Promise.resolve({}),
|
||||
};
|
||||
|
||||
const result = await executor.execute('测试任务', { onStream });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.text).toBe('流式输出');
|
||||
expect(streamText).toHaveBeenCalled();
|
||||
expect(onStream).toHaveBeenCalledWith('流式');
|
||||
expect(onStream).toHaveBeenCalledWith('输出');
|
||||
});
|
||||
|
||||
it('执行失败返回错误', async () => {
|
||||
vi.mocked(generateText).mockRejectedValueOnce(new Error('API 错误'));
|
||||
|
||||
const result = await executor.execute('测试任务', {});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('API 错误');
|
||||
});
|
||||
|
||||
it('传递父会话 ID', async () => {
|
||||
const result = await executor.execute('测试任务', {
|
||||
parentSessionId: 'parent-123',
|
||||
});
|
||||
|
||||
expect(result.sessionId).toBe('parent-123');
|
||||
});
|
||||
|
||||
it('无父会话 ID 使用 standalone', async () => {
|
||||
const result = await executor.execute('测试任务', {});
|
||||
|
||||
expect(result.sessionId).toBe('standalone');
|
||||
});
|
||||
});
|
||||
|
||||
describe('工具过滤', () => {
|
||||
it('无配置返回所有工具', async () => {
|
||||
await executor.execute('测试', {});
|
||||
|
||||
const call = vi.mocked(generateText).mock.calls[0][0];
|
||||
expect(Object.keys(call.tools || {})).toHaveLength(3);
|
||||
});
|
||||
|
||||
it('enabled 配置只保留指定工具', async () => {
|
||||
mockAgentInfo.tools = { enabled: ['bash'] };
|
||||
executor = new AgentExecutor(mockAgentInfo, mockBaseConfig, mockToolRegistry);
|
||||
|
||||
await executor.execute('测试', {});
|
||||
|
||||
const call = vi.mocked(generateText).mock.calls[0][0];
|
||||
expect(Object.keys(call.tools || {})).toContain('bash');
|
||||
expect(Object.keys(call.tools || {})).not.toContain('read_file');
|
||||
});
|
||||
|
||||
it('disabled 配置移除指定工具', async () => {
|
||||
mockAgentInfo.tools = { disabled: ['task'] };
|
||||
executor = new AgentExecutor(mockAgentInfo, mockBaseConfig, mockToolRegistry);
|
||||
|
||||
await executor.execute('测试', {});
|
||||
|
||||
const call = vi.mocked(generateText).mock.calls[0][0];
|
||||
expect(Object.keys(call.tools || {})).not.toContain('task');
|
||||
});
|
||||
|
||||
it('noTask 配置移除 task 工具', async () => {
|
||||
mockAgentInfo.tools = { noTask: true };
|
||||
executor = new AgentExecutor(mockAgentInfo, mockBaseConfig, mockToolRegistry);
|
||||
|
||||
await executor.execute('测试', {});
|
||||
|
||||
const call = vi.mocked(generateText).mock.calls[0][0];
|
||||
expect(Object.keys(call.tools || {})).not.toContain('task');
|
||||
});
|
||||
});
|
||||
|
||||
describe('权限检查', () => {
|
||||
it('bash 命令被拒绝', async () => {
|
||||
vi.mocked(checkBashPermission).mockReturnValue('deny');
|
||||
mockAgentInfo.permission = { bash: { deny: ['rm *'] } };
|
||||
executor = new AgentExecutor(mockAgentInfo, mockBaseConfig, mockToolRegistry);
|
||||
|
||||
// 获取工具并执行
|
||||
await executor.execute('测试', {});
|
||||
|
||||
const call = vi.mocked(generateText).mock.calls[0][0];
|
||||
const bashTool = call.tools?.bash;
|
||||
|
||||
if (bashTool && 'execute' in bashTool) {
|
||||
const result = await bashTool.execute({ command: 'rm -rf /' });
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('权限拒绝');
|
||||
}
|
||||
});
|
||||
|
||||
it('文件写入被拒绝', async () => {
|
||||
mockAgentInfo.permission = { file: { write: 'deny' } };
|
||||
|
||||
// 添加 write_file 工具
|
||||
mockToolRegistry.getAllTools.mockReturnValue([
|
||||
{
|
||||
name: 'write_file',
|
||||
description: '写文件',
|
||||
parameters: { path: { type: 'string', required: true } },
|
||||
execute: vi.fn().mockResolvedValue({ success: true, output: 'ok' }),
|
||||
},
|
||||
]);
|
||||
|
||||
executor = new AgentExecutor(mockAgentInfo, mockBaseConfig, mockToolRegistry);
|
||||
await executor.execute('测试', {});
|
||||
|
||||
const call = vi.mocked(generateText).mock.calls[0][0];
|
||||
const writeTool = call.tools?.write_file;
|
||||
|
||||
if (writeTool && 'execute' in writeTool) {
|
||||
const result = await writeTool.execute({ path: '/test.txt', content: 'test' });
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('权限拒绝');
|
||||
}
|
||||
});
|
||||
|
||||
it('Git 写操作被拒绝', async () => {
|
||||
mockAgentInfo.permission = { git: { write: 'deny' } };
|
||||
|
||||
mockToolRegistry.getAllTools.mockReturnValue([
|
||||
{
|
||||
name: 'git_push',
|
||||
description: 'Git push',
|
||||
parameters: {},
|
||||
execute: vi.fn().mockResolvedValue({ success: true, output: 'ok' }),
|
||||
},
|
||||
]);
|
||||
|
||||
executor = new AgentExecutor(mockAgentInfo, mockBaseConfig, mockToolRegistry);
|
||||
await executor.execute('测试', {});
|
||||
|
||||
const call = vi.mocked(generateText).mock.calls[0][0];
|
||||
const gitTool = call.tools?.git_push;
|
||||
|
||||
if (gitTool && 'execute' in gitTool) {
|
||||
const result = await gitTool.execute({});
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('Git 写操作被禁止');
|
||||
}
|
||||
});
|
||||
|
||||
it('无权限配置允许所有操作', async () => {
|
||||
// 无 permission 配置
|
||||
delete mockAgentInfo.permission;
|
||||
executor = new AgentExecutor(mockAgentInfo, mockBaseConfig, mockToolRegistry);
|
||||
await executor.execute('测试', {});
|
||||
|
||||
const call = vi.mocked(generateText).mock.calls[0][0];
|
||||
const bashTool = call.tools?.bash;
|
||||
|
||||
if (bashTool && 'execute' in bashTool) {
|
||||
const result = await bashTool.execute({ command: 'ls' });
|
||||
expect(result.success).toBe(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('系统提示词', () => {
|
||||
it('使用 Agent 自定义提示词', async () => {
|
||||
mockAgentInfo.prompt = '自定义提示词';
|
||||
executor = new AgentExecutor(mockAgentInfo, mockBaseConfig, mockToolRegistry);
|
||||
|
||||
await executor.execute('测试', {});
|
||||
|
||||
const call = vi.mocked(generateText).mock.calls[0][0];
|
||||
expect(call.system).toBe('自定义提示词');
|
||||
});
|
||||
|
||||
it('无自定义提示词使用基础配置', async () => {
|
||||
delete mockAgentInfo.prompt;
|
||||
executor = new AgentExecutor(mockAgentInfo, mockBaseConfig, mockToolRegistry);
|
||||
|
||||
await executor.execute('测试', {});
|
||||
|
||||
const call = vi.mocked(generateText).mock.calls[0][0];
|
||||
expect(call.system).toBe('默认系统提示词');
|
||||
});
|
||||
});
|
||||
|
||||
describe('模型配置', () => {
|
||||
it('使用 Agent 指定的模型', async () => {
|
||||
mockAgentInfo.model = { model: 'claude-3-opus' };
|
||||
executor = new AgentExecutor(mockAgentInfo, mockBaseConfig, mockToolRegistry);
|
||||
|
||||
await executor.execute('测试', {});
|
||||
|
||||
expect(generateText).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('使用 Agent 指定的 maxSteps', async () => {
|
||||
mockAgentInfo.maxSteps = 5;
|
||||
executor = new AgentExecutor(mockAgentInfo, mockBaseConfig, mockToolRegistry);
|
||||
|
||||
await executor.execute('测试', {});
|
||||
|
||||
expect(generateText).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('使用 Agent 指定的 maxTokens', async () => {
|
||||
mockAgentInfo.model = { maxTokens: 8192 };
|
||||
executor = new AgentExecutor(mockAgentInfo, mockBaseConfig, mockToolRegistry);
|
||||
|
||||
await executor.execute('测试', {});
|
||||
|
||||
const call = vi.mocked(generateText).mock.calls[0][0];
|
||||
expect(call.maxOutputTokens).toBe(8192);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,326 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { AgentManager, resetAgentManager, getAgentManager } from '../../../src/agent/manager.js';
|
||||
import type { AgentInfo } from '../../../src/agent/types.js';
|
||||
import type { AgentConfig } from '../../../src/types/index.js';
|
||||
|
||||
// Mock AgentExecutor - 使用延迟执行
|
||||
vi.mock('../../../src/agent/executor.js', () => ({
|
||||
AgentExecutor: vi.fn().mockImplementation(() => ({
|
||||
execute: vi.fn().mockImplementation(async () => {
|
||||
// 延迟 200ms,让测试能在执行完成前检查状态
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
return {
|
||||
success: true,
|
||||
text: '任务完成',
|
||||
steps: 3,
|
||||
sessionId: 'test-session',
|
||||
};
|
||||
}),
|
||||
})),
|
||||
}));
|
||||
|
||||
describe('AgentManager - Agent 管理器', () => {
|
||||
let manager: AgentManager;
|
||||
const mockAgentInfo: AgentInfo = {
|
||||
name: 'test-agent',
|
||||
description: '测试 Agent',
|
||||
mode: 'subagent',
|
||||
prompt: '你是一个测试助手',
|
||||
};
|
||||
|
||||
const mockConfig: AgentConfig = {
|
||||
provider: 'anthropic',
|
||||
apiKey: 'test-key',
|
||||
model: 'claude-3-5-sonnet-20241022',
|
||||
maxTokens: 4096,
|
||||
systemPrompt: '测试系统提示词',
|
||||
};
|
||||
|
||||
const mockContext = {
|
||||
parentSessionId: 'parent-123',
|
||||
workdir: '/test',
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
resetAgentManager();
|
||||
manager = new AgentManager();
|
||||
});
|
||||
|
||||
describe('getAgentManager - 单例获取', () => {
|
||||
it('返回相同的实例', () => {
|
||||
resetAgentManager();
|
||||
const instance1 = getAgentManager();
|
||||
const instance2 = getAgentManager();
|
||||
expect(instance1).toBe(instance2);
|
||||
});
|
||||
|
||||
it('resetAgentManager 重置实例', () => {
|
||||
const instance1 = getAgentManager();
|
||||
resetAgentManager();
|
||||
const instance2 = getAgentManager();
|
||||
expect(instance1).not.toBe(instance2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('runInBackground - 后台运行', () => {
|
||||
it('返回 agent ID', async () => {
|
||||
const agentId = await manager.runInBackground(
|
||||
mockAgentInfo,
|
||||
'测试任务',
|
||||
'执行测试',
|
||||
mockConfig,
|
||||
{} as any,
|
||||
mockContext
|
||||
);
|
||||
|
||||
expect(agentId).toBeDefined();
|
||||
expect(typeof agentId).toBe('string');
|
||||
expect(agentId.length).toBe(8); // 短 ID
|
||||
});
|
||||
|
||||
it('创建初始状态的记录', async () => {
|
||||
const agentId = await manager.runInBackground(
|
||||
mockAgentInfo,
|
||||
'测试任务',
|
||||
'执行测试',
|
||||
mockConfig,
|
||||
{} as any,
|
||||
mockContext
|
||||
);
|
||||
|
||||
const agent = manager.getAgent(agentId);
|
||||
expect(agent).not.toBeNull();
|
||||
expect(agent!.agentName).toBe('test-agent');
|
||||
expect(agent!.description).toBe('测试任务');
|
||||
expect(agent!.prompt).toBe('执行测试');
|
||||
expect(agent!.startedAt).toBeInstanceOf(Date);
|
||||
// 由于是异步执行,初始状态应该是 running
|
||||
// 但由于测试环境的原因,可能已经变成其他状态
|
||||
expect(['running', 'completed', 'failed']).toContain(agent!.status);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getAgent - 获取 Agent', () => {
|
||||
it('返回存在的 Agent', async () => {
|
||||
const agentId = await manager.runInBackground(
|
||||
mockAgentInfo,
|
||||
'测试任务',
|
||||
'执行测试',
|
||||
mockConfig,
|
||||
{} as any,
|
||||
mockContext
|
||||
);
|
||||
|
||||
const agent = manager.getAgent(agentId);
|
||||
expect(agent).not.toBeNull();
|
||||
expect(agent!.id).toBe(agentId);
|
||||
});
|
||||
|
||||
it('返回 null 当 Agent 不存在', () => {
|
||||
const agent = manager.getAgent('non-existent');
|
||||
expect(agent).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getAgentOutput - 获取输出', () => {
|
||||
it('返回 null 当 Agent 不存在', async () => {
|
||||
const result = await manager.getAgentOutput('non-existent', false);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('非阻塞模式立即返回状态', async () => {
|
||||
const agentId = await manager.runInBackground(
|
||||
mockAgentInfo,
|
||||
'测试任务',
|
||||
'执行测试',
|
||||
mockConfig,
|
||||
{} as any,
|
||||
mockContext
|
||||
);
|
||||
|
||||
const result = await manager.getAgentOutput(agentId, false);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.id).toBe(agentId);
|
||||
});
|
||||
|
||||
it('阻塞模式等待完成', async () => {
|
||||
const agentId = await manager.runInBackground(
|
||||
mockAgentInfo,
|
||||
'测试任务',
|
||||
'执行测试',
|
||||
mockConfig,
|
||||
{} as any,
|
||||
mockContext
|
||||
);
|
||||
|
||||
// 使用阻塞模式等待
|
||||
const result = await manager.getAgentOutput(agentId, true, 5);
|
||||
expect(result).not.toBeNull();
|
||||
// 等待后应该是完成状态
|
||||
expect(['completed', 'failed']).toContain(result!.status);
|
||||
});
|
||||
});
|
||||
|
||||
describe('listAgents - 列出所有 Agent', () => {
|
||||
it('返回所有 Agent', async () => {
|
||||
await manager.runInBackground(
|
||||
mockAgentInfo,
|
||||
'任务1',
|
||||
'执行1',
|
||||
mockConfig,
|
||||
{} as any,
|
||||
mockContext
|
||||
);
|
||||
await manager.runInBackground(
|
||||
mockAgentInfo,
|
||||
'任务2',
|
||||
'执行2',
|
||||
mockConfig,
|
||||
{} as any,
|
||||
mockContext
|
||||
);
|
||||
|
||||
const agents = manager.listAgents();
|
||||
expect(agents.length).toBe(2);
|
||||
});
|
||||
|
||||
it('空管理器返回空数组', () => {
|
||||
const agents = manager.listAgents();
|
||||
expect(agents).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('listRunningAgents - 列出运行中的 Agent', () => {
|
||||
it('返回数组', async () => {
|
||||
await manager.runInBackground(
|
||||
mockAgentInfo,
|
||||
'任务1',
|
||||
'执行1',
|
||||
mockConfig,
|
||||
{} as any,
|
||||
mockContext
|
||||
);
|
||||
|
||||
const running = manager.listRunningAgents();
|
||||
expect(Array.isArray(running)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('cleanup - 清理', () => {
|
||||
it('清理已完成的 Agent', async () => {
|
||||
const agentId = await manager.runInBackground(
|
||||
mockAgentInfo,
|
||||
'任务',
|
||||
'执行',
|
||||
mockConfig,
|
||||
{} as any,
|
||||
mockContext
|
||||
);
|
||||
|
||||
// 等待一段时间让 Agent 完成(无论成功或失败)
|
||||
await new Promise((r) => setTimeout(r, 100));
|
||||
|
||||
// 确认 Agent 不再是 running 状态
|
||||
const beforeCleanup = manager.getAgent(agentId);
|
||||
expect(beforeCleanup).not.toBeNull();
|
||||
expect(['completed', 'failed']).toContain(beforeCleanup?.status);
|
||||
|
||||
// 设置 maxAge 为 0,立即清理
|
||||
manager.cleanup(0);
|
||||
|
||||
const agent = manager.getAgent(agentId);
|
||||
expect(agent).toBeNull();
|
||||
});
|
||||
|
||||
it('不清理运行中的 Agent', async () => {
|
||||
const agentId = await manager.runInBackground(
|
||||
mockAgentInfo,
|
||||
'任务',
|
||||
'执行',
|
||||
mockConfig,
|
||||
{} as any,
|
||||
mockContext
|
||||
);
|
||||
|
||||
// 立即清理(不等待)
|
||||
manager.cleanup(0);
|
||||
|
||||
// cleanup 应该不会报错
|
||||
// Agent 可能还在运行(取决于执行速度)
|
||||
// 这里只验证 cleanup 不会崩溃
|
||||
expect(true).toBe(true);
|
||||
});
|
||||
|
||||
it('使用默认 maxAge', async () => {
|
||||
const agentId = await manager.runInBackground(
|
||||
mockAgentInfo,
|
||||
'任务',
|
||||
'执行',
|
||||
mockConfig,
|
||||
{} as any,
|
||||
mockContext
|
||||
);
|
||||
|
||||
// 等待完成
|
||||
await new Promise((r) => setTimeout(r, 300));
|
||||
|
||||
// 使用默认 maxAge(1 小时),不应该清理刚完成的
|
||||
manager.cleanup();
|
||||
|
||||
// Agent 应该还在(因为未超过 1 小时)
|
||||
const agent = manager.getAgent(agentId);
|
||||
expect(agent).not.toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('完成回调', () => {
|
||||
it('多个等待者都收到通知', async () => {
|
||||
const agentId = await manager.runInBackground(
|
||||
mockAgentInfo,
|
||||
'任务',
|
||||
'执行',
|
||||
mockConfig,
|
||||
{} as any,
|
||||
mockContext
|
||||
);
|
||||
|
||||
// 并发等待
|
||||
const [result1, result2] = await Promise.all([
|
||||
manager.getAgentOutput(agentId, true, 5),
|
||||
manager.getAgentOutput(agentId, true, 5),
|
||||
]);
|
||||
|
||||
expect(result1).not.toBeNull();
|
||||
expect(result2).not.toBeNull();
|
||||
expect(result1!.id).toBe(agentId);
|
||||
expect(result2!.id).toBe(agentId);
|
||||
});
|
||||
});
|
||||
|
||||
describe('已完成 Agent 的阻塞查询', () => {
|
||||
it('已完成的 Agent 阻塞查询立即返回', async () => {
|
||||
const agentId = await manager.runInBackground(
|
||||
mockAgentInfo,
|
||||
'任务',
|
||||
'执行',
|
||||
mockConfig,
|
||||
{} as any,
|
||||
mockContext
|
||||
);
|
||||
|
||||
// 等待完成
|
||||
await manager.getAgentOutput(agentId, true, 5);
|
||||
|
||||
// 再次阻塞查询应立即返回
|
||||
const startTime = Date.now();
|
||||
const result = await manager.getAgentOutput(agentId, true, 10);
|
||||
const elapsed = Date.now() - startTime;
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.status).not.toBe('running');
|
||||
// 应该立即返回,不需要等 10 秒
|
||||
expect(elapsed).toBeLessThan(1000);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,234 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import {
|
||||
mergePermissions,
|
||||
matchRule,
|
||||
checkBashPermission,
|
||||
checkFilePathPermission,
|
||||
SYSTEM_DEFAULT_PERMISSION,
|
||||
} from '../../../src/agent/permission-merger.js';
|
||||
import type { AgentPermission, AgentBashPermission } from '../../../src/agent/types.js';
|
||||
|
||||
describe('matchRule - 命令规则匹配', () => {
|
||||
describe('通配符 * 匹配', () => {
|
||||
it('git diff* 匹配 git diff --staged', () => {
|
||||
expect(matchRule('git diff --staged', 'git diff*')).toBe(true);
|
||||
});
|
||||
|
||||
it('git diff* 不匹配 git status', () => {
|
||||
expect(matchRule('git status', 'git diff*')).toBe(false);
|
||||
});
|
||||
|
||||
it('rm -rf* 匹配危险命令', () => {
|
||||
expect(matchRule('rm -rf /', 'rm -rf*')).toBe(true);
|
||||
expect(matchRule('rm -rf /home/user', 'rm -rf*')).toBe(true);
|
||||
});
|
||||
|
||||
it('rm -rf* 不匹配普通 rm', () => {
|
||||
expect(matchRule('rm file.txt', 'rm -rf*')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('精确匹配', () => {
|
||||
it('pwd 精确匹配', () => {
|
||||
expect(matchRule('pwd', 'pwd')).toBe(true);
|
||||
});
|
||||
|
||||
it('pwd 不匹配带参数', () => {
|
||||
expect(matchRule('pwd /home', 'pwd')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('大小写不敏感', () => {
|
||||
it('忽略大小写', () => {
|
||||
expect(matchRule('GIT DIFF', 'git diff*')).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('checkBashPermission - Bash 权限检查', () => {
|
||||
it('禁用时返回 deny', () => {
|
||||
const permission: AgentBashPermission = { enabled: false };
|
||||
expect(checkBashPermission('ls', permission)).toBe('deny');
|
||||
});
|
||||
|
||||
it('匹配 allow 规则', () => {
|
||||
const permission: AgentBashPermission = {
|
||||
enabled: true,
|
||||
rules: [{ pattern: 'ls *', action: 'allow' }],
|
||||
default: 'deny',
|
||||
};
|
||||
expect(checkBashPermission('ls -la', permission)).toBe('allow');
|
||||
});
|
||||
|
||||
it('匹配 deny 规则', () => {
|
||||
const permission: AgentBashPermission = {
|
||||
enabled: true,
|
||||
rules: [{ pattern: 'rm -rf*', action: 'deny' }],
|
||||
default: 'allow',
|
||||
};
|
||||
expect(checkBashPermission('rm -rf /', permission)).toBe('deny');
|
||||
});
|
||||
|
||||
it('无匹配时返回默认值', () => {
|
||||
const permission: AgentBashPermission = {
|
||||
enabled: true,
|
||||
rules: [],
|
||||
default: 'ask',
|
||||
};
|
||||
expect(checkBashPermission('npm install', permission)).toBe('ask');
|
||||
});
|
||||
|
||||
it('规则优先级:先匹配的规则优先', () => {
|
||||
const permission: AgentBashPermission = {
|
||||
enabled: true,
|
||||
rules: [
|
||||
{ pattern: 'git push --force*', action: 'deny' },
|
||||
{ pattern: 'git push*', action: 'ask' },
|
||||
],
|
||||
default: 'allow',
|
||||
};
|
||||
expect(checkBashPermission('git push --force origin', permission)).toBe('deny');
|
||||
expect(checkBashPermission('git push origin', permission)).toBe('ask');
|
||||
});
|
||||
});
|
||||
|
||||
describe('checkFilePathPermission - 文件路径权限检查', () => {
|
||||
it('无敏感路径规则返回 null', () => {
|
||||
expect(checkFilePathPermission('/home/user/file.txt', undefined)).toBeNull();
|
||||
expect(checkFilePathPermission('/home/user/file.txt', [])).toBeNull();
|
||||
});
|
||||
|
||||
it('匹配敏感路径规则', () => {
|
||||
const rules = [
|
||||
{ pattern: '*.env', action: 'deny' as const },
|
||||
{ pattern: '/etc/*', action: 'ask' as const },
|
||||
];
|
||||
expect(checkFilePathPermission('.env', rules)).toBe('deny');
|
||||
expect(checkFilePathPermission('/etc/passwd', rules)).toBe('ask');
|
||||
});
|
||||
|
||||
it('不匹配时返回 null', () => {
|
||||
const rules = [{ pattern: '*.env', action: 'deny' as const }];
|
||||
expect(checkFilePathPermission('config.json', rules)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('mergePermissions - 权限合并', () => {
|
||||
describe('优先级:Agent > Global > System', () => {
|
||||
it('Agent 配置覆盖 Global 和 System', () => {
|
||||
const system: AgentPermission = { file: { read: 'allow', write: 'ask' } };
|
||||
const global: AgentPermission = { file: { write: 'allow' } };
|
||||
const agent: AgentPermission = { file: { write: 'deny' } };
|
||||
|
||||
const merged = mergePermissions(system, global, agent);
|
||||
expect(merged.file?.write).toBe('deny');
|
||||
});
|
||||
|
||||
it('Global 配置覆盖 System', () => {
|
||||
const system: AgentPermission = { file: { write: 'ask' } };
|
||||
const global: AgentPermission = { file: { write: 'allow' } };
|
||||
|
||||
const merged = mergePermissions(system, global, undefined);
|
||||
expect(merged.file?.write).toBe('allow');
|
||||
});
|
||||
|
||||
it('无覆盖时使用 System 默认值', () => {
|
||||
const merged = mergePermissions(SYSTEM_DEFAULT_PERMISSION, undefined, undefined);
|
||||
expect(merged.file?.read).toBe('allow');
|
||||
expect(merged.file?.write).toBe('ask');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Bash 权限合并', () => {
|
||||
it('Agent 禁用 bash 覆盖全局', () => {
|
||||
const system: AgentPermission = { bash: { enabled: true } };
|
||||
const global: AgentPermission = { bash: { enabled: true } };
|
||||
const agent: AgentPermission = { bash: { enabled: false } };
|
||||
|
||||
const merged = mergePermissions(system, global, agent);
|
||||
expect(merged.bash?.enabled).toBe(false);
|
||||
});
|
||||
|
||||
it('Global 禁用 bash 且 Agent 未覆盖', () => {
|
||||
const system: AgentPermission = { bash: { enabled: true } };
|
||||
const global: AgentPermission = { bash: { enabled: false } };
|
||||
|
||||
const merged = mergePermissions(system, global, undefined);
|
||||
expect(merged.bash?.enabled).toBe(false);
|
||||
});
|
||||
|
||||
it('规则按优先级合并:Agent > Global > System', () => {
|
||||
const system: AgentPermission = {
|
||||
bash: { rules: [{ pattern: 'ls *', action: 'allow' }] },
|
||||
};
|
||||
const global: AgentPermission = {
|
||||
bash: { rules: [{ pattern: 'cat *', action: 'allow' }] },
|
||||
};
|
||||
const agent: AgentPermission = {
|
||||
bash: { rules: [{ pattern: 'rm *', action: 'deny' }] },
|
||||
};
|
||||
|
||||
const merged = mergePermissions(system, global, agent);
|
||||
// Agent 规则在前
|
||||
expect(merged.bash?.rules?.[0].pattern).toBe('rm *');
|
||||
expect(merged.bash?.rules?.[1].pattern).toBe('cat *');
|
||||
expect(merged.bash?.rules?.[2].pattern).toBe('ls *');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Git 权限合并', () => {
|
||||
it('合并所有级别的 Git 权限', () => {
|
||||
const system: AgentPermission = { git: { read: 'allow', write: 'ask', dangerous: 'deny' } };
|
||||
const agent: AgentPermission = { git: { write: 'deny' } };
|
||||
|
||||
const merged = mergePermissions(system, undefined, agent);
|
||||
expect(merged.git?.read).toBe('allow'); // 来自 system
|
||||
expect(merged.git?.write).toBe('deny'); // 来自 agent
|
||||
expect(merged.git?.dangerous).toBe('deny'); // 来自 system
|
||||
});
|
||||
});
|
||||
|
||||
describe('Web 权限合并', () => {
|
||||
it('合并 Web 权限', () => {
|
||||
const system: AgentPermission = { web: 'ask' };
|
||||
const agent: AgentPermission = { web: 'deny' };
|
||||
|
||||
const merged = mergePermissions(system, undefined, agent);
|
||||
expect(merged.web).toBe('deny');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('SYSTEM_DEFAULT_PERMISSION - 系统默认权限', () => {
|
||||
it('文件读取默认允许', () => {
|
||||
expect(SYSTEM_DEFAULT_PERMISSION.file?.read).toBe('allow');
|
||||
});
|
||||
|
||||
it('文件写入默认询问', () => {
|
||||
expect(SYSTEM_DEFAULT_PERMISSION.file?.write).toBe('ask');
|
||||
});
|
||||
|
||||
it('Bash 默认启用', () => {
|
||||
expect(SYSTEM_DEFAULT_PERMISSION.bash?.enabled).toBe(true);
|
||||
});
|
||||
|
||||
it('包含安全命令白名单', () => {
|
||||
const rules = SYSTEM_DEFAULT_PERMISSION.bash?.rules ?? [];
|
||||
const lsRule = rules.find((r) => r.pattern === 'ls *');
|
||||
expect(lsRule?.action).toBe('allow');
|
||||
});
|
||||
|
||||
it('包含危险命令黑名单', () => {
|
||||
const rules = SYSTEM_DEFAULT_PERMISSION.bash?.rules ?? [];
|
||||
const rmRule = rules.find((r) => r.pattern === 'rm -rf *');
|
||||
expect(rmRule?.action).toBe('deny');
|
||||
});
|
||||
|
||||
it('Git 读取默认允许', () => {
|
||||
expect(SYSTEM_DEFAULT_PERMISSION.git?.read).toBe('allow');
|
||||
});
|
||||
|
||||
it('Git 危险操作默认拒绝', () => {
|
||||
expect(SYSTEM_DEFAULT_PERMISSION.git?.dangerous).toBe('deny');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,201 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import {
|
||||
presetAgents,
|
||||
getPresetAgentNames,
|
||||
isPresetAgent,
|
||||
generalAgent,
|
||||
exploreAgent,
|
||||
codeReviewerAgent,
|
||||
buildAgent,
|
||||
planAgent,
|
||||
visionAgent,
|
||||
} from '../../../../src/agent/presets/index.js';
|
||||
|
||||
describe('Agent Presets - 预设 Agent', () => {
|
||||
describe('presetAgents 集合', () => {
|
||||
it('包含所有预设 Agent', () => {
|
||||
expect(Object.keys(presetAgents)).toHaveLength(6);
|
||||
});
|
||||
|
||||
it('包含 general Agent', () => {
|
||||
expect(presetAgents['general']).toBeDefined();
|
||||
expect(presetAgents['general']).toBe(generalAgent);
|
||||
});
|
||||
|
||||
it('包含 explore Agent', () => {
|
||||
expect(presetAgents['explore']).toBeDefined();
|
||||
expect(presetAgents['explore']).toBe(exploreAgent);
|
||||
});
|
||||
|
||||
it('包含 code-reviewer Agent', () => {
|
||||
expect(presetAgents['code-reviewer']).toBeDefined();
|
||||
expect(presetAgents['code-reviewer']).toBe(codeReviewerAgent);
|
||||
});
|
||||
|
||||
it('包含 build Agent', () => {
|
||||
expect(presetAgents['build']).toBeDefined();
|
||||
expect(presetAgents['build']).toBe(buildAgent);
|
||||
});
|
||||
|
||||
it('包含 plan Agent', () => {
|
||||
expect(presetAgents['plan']).toBeDefined();
|
||||
expect(presetAgents['plan']).toBe(planAgent);
|
||||
});
|
||||
|
||||
it('包含 vision Agent', () => {
|
||||
expect(presetAgents['vision']).toBeDefined();
|
||||
expect(presetAgents['vision']).toBe(visionAgent);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getPresetAgentNames - 获取预设名称', () => {
|
||||
it('返回所有预设 Agent 名称', () => {
|
||||
const names = getPresetAgentNames();
|
||||
|
||||
expect(names).toContain('general');
|
||||
expect(names).toContain('explore');
|
||||
expect(names).toContain('code-reviewer');
|
||||
expect(names).toContain('build');
|
||||
expect(names).toContain('plan');
|
||||
expect(names).toContain('vision');
|
||||
});
|
||||
|
||||
it('返回数组类型', () => {
|
||||
const names = getPresetAgentNames();
|
||||
expect(Array.isArray(names)).toBe(true);
|
||||
});
|
||||
|
||||
it('返回正确数量', () => {
|
||||
const names = getPresetAgentNames();
|
||||
expect(names).toHaveLength(6);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isPresetAgent - 检查是否为预设', () => {
|
||||
it('识别 general', () => {
|
||||
expect(isPresetAgent('general')).toBe(true);
|
||||
});
|
||||
|
||||
it('识别 explore', () => {
|
||||
expect(isPresetAgent('explore')).toBe(true);
|
||||
});
|
||||
|
||||
it('识别 code-reviewer', () => {
|
||||
expect(isPresetAgent('code-reviewer')).toBe(true);
|
||||
});
|
||||
|
||||
it('识别 build', () => {
|
||||
expect(isPresetAgent('build')).toBe(true);
|
||||
});
|
||||
|
||||
it('识别 plan', () => {
|
||||
expect(isPresetAgent('plan')).toBe(true);
|
||||
});
|
||||
|
||||
it('识别 vision', () => {
|
||||
expect(isPresetAgent('vision')).toBe(true);
|
||||
});
|
||||
|
||||
it('不识别未知 Agent', () => {
|
||||
expect(isPresetAgent('unknown')).toBe(false);
|
||||
});
|
||||
|
||||
it('不识别空字符串', () => {
|
||||
expect(isPresetAgent('')).toBe(false);
|
||||
});
|
||||
|
||||
it('区分大小写', () => {
|
||||
expect(isPresetAgent('General')).toBe(false);
|
||||
expect(isPresetAgent('GENERAL')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('generalAgent - 通用 Agent', () => {
|
||||
it('有正确的描述', () => {
|
||||
expect(generalAgent.description).toContain('通用');
|
||||
});
|
||||
|
||||
it('mode 为 subagent', () => {
|
||||
expect(generalAgent.mode).toBe('subagent');
|
||||
});
|
||||
|
||||
it('禁止嵌套 Task', () => {
|
||||
expect(generalAgent.tools?.noTask).toBe(true);
|
||||
});
|
||||
|
||||
it('有 maxSteps 限制', () => {
|
||||
expect(generalAgent.maxSteps).toBeDefined();
|
||||
expect(generalAgent.maxSteps).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('exploreAgent - 探索 Agent', () => {
|
||||
it('有描述', () => {
|
||||
expect(exploreAgent.description).toBeDefined();
|
||||
});
|
||||
|
||||
it('mode 为 subagent', () => {
|
||||
expect(exploreAgent.mode).toBe('subagent');
|
||||
});
|
||||
});
|
||||
|
||||
describe('codeReviewerAgent - 代码审查 Agent', () => {
|
||||
it('有描述', () => {
|
||||
expect(codeReviewerAgent.description).toBeDefined();
|
||||
});
|
||||
|
||||
it('mode 为 subagent', () => {
|
||||
expect(codeReviewerAgent.mode).toBe('subagent');
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildAgent - 构建 Agent', () => {
|
||||
it('有描述', () => {
|
||||
expect(buildAgent.description).toBeDefined();
|
||||
});
|
||||
|
||||
it('mode 为 primary(主模式)', () => {
|
||||
expect(buildAgent.mode).toBe('primary');
|
||||
});
|
||||
});
|
||||
|
||||
describe('planAgent - 计划 Agent', () => {
|
||||
it('有描述', () => {
|
||||
expect(planAgent.description).toBeDefined();
|
||||
});
|
||||
|
||||
it('mode 为 primary(主模式)', () => {
|
||||
expect(planAgent.mode).toBe('primary');
|
||||
});
|
||||
});
|
||||
|
||||
describe('visionAgent - 视觉 Agent', () => {
|
||||
it('有描述', () => {
|
||||
expect(visionAgent.description).toBeDefined();
|
||||
});
|
||||
|
||||
it('mode 为 subagent', () => {
|
||||
expect(visionAgent.mode).toBe('subagent');
|
||||
});
|
||||
});
|
||||
|
||||
describe('所有预设 Agent 共同特性', () => {
|
||||
it('都有 description', () => {
|
||||
Object.entries(presetAgents).forEach(([name, agent]) => {
|
||||
expect(agent.description, `${name} 缺少 description`).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
it('都有 mode', () => {
|
||||
Object.entries(presetAgents).forEach(([name, agent]) => {
|
||||
expect(agent.mode, `${name} 缺少 mode`).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
it('mode 值是 subagent 或 primary', () => {
|
||||
Object.entries(presetAgents).forEach(([name, agent]) => {
|
||||
expect(['subagent', 'primary'], `${name} mode 无效`).toContain(agent.mode);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,350 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { AgentRegistry } from '../../../src/agent/registry.js';
|
||||
import type { AgentInfo, AgentConfigFile } from '../../../src/agent/types.js';
|
||||
|
||||
// Mock config-loader
|
||||
vi.mock('../../../src/agent/config-loader.js', () => ({
|
||||
loadAgentConfig: vi.fn(),
|
||||
}));
|
||||
|
||||
// Mock presets
|
||||
vi.mock('../../../src/agent/presets/index.js', () => ({
|
||||
presetAgents: {
|
||||
explore: {
|
||||
description: '代码探索 Agent',
|
||||
mode: 'subagent' as const,
|
||||
prompt: '你是代码探索助手',
|
||||
maxSteps: 5,
|
||||
},
|
||||
'code-reviewer': {
|
||||
description: '代码审查 Agent',
|
||||
mode: 'subagent' as const,
|
||||
prompt: '你是代码审查助手',
|
||||
},
|
||||
build: {
|
||||
description: '构建 Agent',
|
||||
mode: 'all' as const,
|
||||
prompt: '你是构建助手',
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
import { loadAgentConfig } from '../../../src/agent/config-loader.js';
|
||||
|
||||
describe('AgentRegistry - Agent 注册表', () => {
|
||||
let registry: AgentRegistry;
|
||||
|
||||
beforeEach(() => {
|
||||
registry = new AgentRegistry();
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(loadAgentConfig).mockResolvedValue(null);
|
||||
});
|
||||
|
||||
describe('init - 初始化', () => {
|
||||
it('初始化后注册预设 Agent', async () => {
|
||||
await registry.init('/test/project');
|
||||
|
||||
expect(registry.has('explore')).toBe(true);
|
||||
expect(registry.has('code-reviewer')).toBe(true);
|
||||
expect(registry.has('build')).toBe(true);
|
||||
});
|
||||
|
||||
it('初始化加载用户配置', async () => {
|
||||
const userConfig: AgentConfigFile = {
|
||||
defaults: {
|
||||
maxSteps: 20,
|
||||
},
|
||||
agents: {
|
||||
'custom-agent': {
|
||||
description: '自定义 Agent',
|
||||
mode: 'subagent',
|
||||
prompt: '你是自定义助手',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
vi.mocked(loadAgentConfig).mockResolvedValue(userConfig);
|
||||
|
||||
await registry.init('/test/project');
|
||||
|
||||
expect(registry.has('custom-agent')).toBe(true);
|
||||
});
|
||||
|
||||
it('重复初始化只执行一次', async () => {
|
||||
await registry.init('/test/project');
|
||||
await registry.init('/test/project');
|
||||
|
||||
expect(loadAgentConfig).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('get - 获取 Agent', () => {
|
||||
beforeEach(async () => {
|
||||
await registry.init('/test/project');
|
||||
});
|
||||
|
||||
it('获取存在的 Agent', () => {
|
||||
const agent = registry.get('explore');
|
||||
|
||||
expect(agent).toBeDefined();
|
||||
expect(agent?.name).toBe('explore');
|
||||
expect(agent?.description).toBe('代码探索 Agent');
|
||||
});
|
||||
|
||||
it('获取不存在的 Agent 返回 undefined', () => {
|
||||
const agent = registry.get('non-existent');
|
||||
|
||||
expect(agent).toBeUndefined();
|
||||
});
|
||||
|
||||
it('获取的 Agent 应用全局配置', async () => {
|
||||
vi.mocked(loadAgentConfig).mockResolvedValue({
|
||||
defaults: {
|
||||
maxSteps: 25,
|
||||
},
|
||||
});
|
||||
|
||||
const newRegistry = new AgentRegistry();
|
||||
await newRegistry.init('/test/project');
|
||||
|
||||
const agent = newRegistry.get('code-reviewer');
|
||||
|
||||
// code-reviewer 没有设置 maxSteps,应该使用全局默认值
|
||||
expect(agent?.maxSteps).toBe(25);
|
||||
});
|
||||
|
||||
it('Agent 自己的配置优先于全局配置', async () => {
|
||||
vi.mocked(loadAgentConfig).mockResolvedValue({
|
||||
defaults: {
|
||||
maxSteps: 25,
|
||||
},
|
||||
});
|
||||
|
||||
const newRegistry = new AgentRegistry();
|
||||
await newRegistry.init('/test/project');
|
||||
|
||||
const agent = newRegistry.get('explore');
|
||||
|
||||
// explore 设置了 maxSteps: 5
|
||||
expect(agent?.maxSteps).toBe(5);
|
||||
});
|
||||
});
|
||||
|
||||
describe('list - 列出 Agent', () => {
|
||||
beforeEach(async () => {
|
||||
await registry.init('/test/project');
|
||||
});
|
||||
|
||||
it('列出所有 Agent', () => {
|
||||
const agents = registry.list();
|
||||
|
||||
expect(agents.length).toBeGreaterThan(0);
|
||||
expect(agents.some(a => a.name === 'explore')).toBe(true);
|
||||
expect(agents.some(a => a.name === 'code-reviewer')).toBe(true);
|
||||
});
|
||||
|
||||
it('按 mode 过滤 Agent', () => {
|
||||
const subagents = registry.list('subagent');
|
||||
|
||||
expect(subagents.every(a => a.mode === 'subagent' || a.mode === 'all')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('listSubagents - 列出子 Agent', () => {
|
||||
beforeEach(async () => {
|
||||
await registry.init('/test/project');
|
||||
});
|
||||
|
||||
it('排除 primary-only 的 Agent', () => {
|
||||
const subagents = registry.listSubagents();
|
||||
|
||||
expect(subagents.every(a => a.mode !== 'primary')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('listPrimaryAgents - 列出主 Agent', () => {
|
||||
beforeEach(async () => {
|
||||
await registry.init('/test/project');
|
||||
});
|
||||
|
||||
it('排除 subagent-only 的 Agent', () => {
|
||||
const primaryAgents = registry.listPrimaryAgents();
|
||||
|
||||
expect(primaryAgents.every(a => a.mode !== 'subagent')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('register - 动态注册', () => {
|
||||
beforeEach(async () => {
|
||||
await registry.init('/test/project');
|
||||
});
|
||||
|
||||
it('注册新 Agent', () => {
|
||||
const newAgent: AgentInfo = {
|
||||
name: 'dynamic-agent',
|
||||
description: '动态注册的 Agent',
|
||||
mode: 'subagent',
|
||||
prompt: '你是动态 Agent',
|
||||
};
|
||||
|
||||
registry.register(newAgent);
|
||||
|
||||
expect(registry.has('dynamic-agent')).toBe(true);
|
||||
expect(registry.get('dynamic-agent')?.description).toBe('动态注册的 Agent');
|
||||
});
|
||||
|
||||
it('覆盖已有 Agent', () => {
|
||||
const updatedAgent: AgentInfo = {
|
||||
name: 'explore',
|
||||
description: '更新后的探索 Agent',
|
||||
mode: 'all',
|
||||
prompt: '更新后的提示',
|
||||
};
|
||||
|
||||
registry.register(updatedAgent);
|
||||
|
||||
const agent = registry.get('explore');
|
||||
expect(agent?.description).toBe('更新后的探索 Agent');
|
||||
});
|
||||
});
|
||||
|
||||
describe('remove - 移除 Agent', () => {
|
||||
beforeEach(async () => {
|
||||
await registry.init('/test/project');
|
||||
});
|
||||
|
||||
it('移除存在的 Agent', () => {
|
||||
const result = registry.remove('explore');
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(registry.has('explore')).toBe(false);
|
||||
});
|
||||
|
||||
it('移除不存在的 Agent 返回 false', () => {
|
||||
const result = registry.remove('non-existent');
|
||||
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('has - 检查 Agent 是否存在', () => {
|
||||
beforeEach(async () => {
|
||||
await registry.init('/test/project');
|
||||
});
|
||||
|
||||
it('存在的 Agent 返回 true', () => {
|
||||
expect(registry.has('explore')).toBe(true);
|
||||
});
|
||||
|
||||
it('不存在的 Agent 返回 false', () => {
|
||||
expect(registry.has('non-existent')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('size - 获取 Agent 数量', () => {
|
||||
beforeEach(async () => {
|
||||
await registry.init('/test/project');
|
||||
});
|
||||
|
||||
it('返回正确的数量', () => {
|
||||
expect(registry.size).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('添加后数量增加', () => {
|
||||
const initialSize = registry.size;
|
||||
|
||||
registry.register({
|
||||
name: 'new-agent',
|
||||
description: 'New',
|
||||
mode: 'subagent',
|
||||
prompt: 'New agent',
|
||||
});
|
||||
|
||||
expect(registry.size).toBe(initialSize + 1);
|
||||
});
|
||||
|
||||
it('移除后数量减少', () => {
|
||||
const initialSize = registry.size;
|
||||
|
||||
registry.remove('explore');
|
||||
|
||||
expect(registry.size).toBe(initialSize - 1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getNames - 获取所有 Agent 名称', () => {
|
||||
beforeEach(async () => {
|
||||
await registry.init('/test/project');
|
||||
});
|
||||
|
||||
it('返回所有 Agent 名称', () => {
|
||||
const names = registry.getNames();
|
||||
|
||||
expect(names).toContain('explore');
|
||||
expect(names).toContain('code-reviewer');
|
||||
expect(names).toContain('build');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getGlobalConfig - 获取全局配置', () => {
|
||||
it('无用户配置时返回 null', async () => {
|
||||
vi.mocked(loadAgentConfig).mockResolvedValue(null);
|
||||
|
||||
await registry.init('/test/project');
|
||||
|
||||
expect(registry.getGlobalConfig()).toBeNull();
|
||||
});
|
||||
|
||||
it('有用户配置时返回 defaults', async () => {
|
||||
vi.mocked(loadAgentConfig).mockResolvedValue({
|
||||
defaults: {
|
||||
maxSteps: 30,
|
||||
model: {
|
||||
temperature: 0.5,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const newRegistry = new AgentRegistry();
|
||||
await newRegistry.init('/test/project');
|
||||
|
||||
const globalConfig = newRegistry.getGlobalConfig();
|
||||
|
||||
expect(globalConfig?.maxSteps).toBe(30);
|
||||
expect(globalConfig?.model?.temperature).toBe(0.5);
|
||||
});
|
||||
});
|
||||
|
||||
describe('generateSubagentDescription - 生成子 Agent 描述', () => {
|
||||
beforeEach(async () => {
|
||||
await registry.init('/test/project');
|
||||
});
|
||||
|
||||
it('生成包含所有子 Agent 的描述', () => {
|
||||
const description = registry.generateSubagentDescription();
|
||||
|
||||
expect(description).toContain('explore');
|
||||
expect(description).toContain('代码探索');
|
||||
});
|
||||
|
||||
it('无子 Agent 时返回提示信息', async () => {
|
||||
// 移除所有 Agent
|
||||
for (const name of registry.getNames()) {
|
||||
registry.remove(name);
|
||||
}
|
||||
|
||||
const description = registry.generateSubagentDescription();
|
||||
|
||||
expect(description).toContain('没有可用');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('agentRegistry 单例', () => {
|
||||
it('导出单例实例', async () => {
|
||||
const { agentRegistry } = await import('../../../src/agent/registry.js');
|
||||
|
||||
expect(agentRegistry).toBeDefined();
|
||||
expect(agentRegistry).toBeInstanceOf(AgentRegistry);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,296 @@
|
||||
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
|
||||
import * as fs from 'fs/promises';
|
||||
import * as childProcess from 'child_process';
|
||||
import {
|
||||
CommandExecutor,
|
||||
createCommandExecutor,
|
||||
getCommandRegistry,
|
||||
resetCommandRegistry,
|
||||
} from '../../../src/commands/index.js';
|
||||
import type { Command } from '../../../src/commands/types.js';
|
||||
|
||||
// Mock fs
|
||||
vi.mock('fs/promises', () => ({
|
||||
readFile: vi.fn(),
|
||||
}));
|
||||
|
||||
// Mock child_process
|
||||
vi.mock('child_process', () => ({
|
||||
exec: vi.fn(),
|
||||
}));
|
||||
|
||||
// Mock util.promisify to return our mocked exec
|
||||
vi.mock('util', () => ({
|
||||
promisify: (fn: unknown) => {
|
||||
if (fn === childProcess.exec) {
|
||||
return vi.fn().mockImplementation((cmd: string, _options: unknown) => {
|
||||
// 默认返回命令输出
|
||||
return Promise.resolve({ stdout: `output of: ${cmd}`, stderr: '' });
|
||||
});
|
||||
}
|
||||
return fn;
|
||||
},
|
||||
}));
|
||||
|
||||
// Mock loader and builtin commands
|
||||
vi.mock('../../../src/commands/loader.js', () => ({
|
||||
commandLoader: {
|
||||
loadFromDirectory: vi.fn().mockResolvedValue([]),
|
||||
getUserCommandsDir: vi.fn().mockReturnValue('/mock/user/commands'),
|
||||
getProjectCommandsDir: vi.fn().mockReturnValue('/mock/project/commands'),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('../../../src/commands/builtin/index.js', () => ({
|
||||
builtinCommands: [],
|
||||
}));
|
||||
|
||||
describe('CommandExecutor - Command 执行器', () => {
|
||||
let executor: CommandExecutor;
|
||||
|
||||
beforeEach(() => {
|
||||
resetCommandRegistry();
|
||||
executor = new CommandExecutor('/test/workdir');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('parseInput - 解析输入', () => {
|
||||
it('解析简单命令', () => {
|
||||
const result = executor.parseInput('/test');
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.command).toBe('test');
|
||||
expect(result?.arguments).toBe('');
|
||||
expect(result?.args).toEqual([]);
|
||||
});
|
||||
|
||||
it('解析带参数的命令', () => {
|
||||
const result = executor.parseInput('/review main..feature');
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.command).toBe('review');
|
||||
expect(result?.arguments).toBe('main..feature');
|
||||
expect(result?.args).toEqual(['main..feature']);
|
||||
});
|
||||
|
||||
it('解析多个参数', () => {
|
||||
const result = executor.parseInput('/deploy staging production');
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.command).toBe('deploy');
|
||||
expect(result?.arguments).toBe('staging production');
|
||||
expect(result?.args).toEqual(['staging', 'production']);
|
||||
});
|
||||
|
||||
it('解析带引号的参数', () => {
|
||||
const result = executor.parseInput('/commit "fix: bug fix"');
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.command).toBe('commit');
|
||||
expect(result?.args).toEqual(['fix: bug fix']);
|
||||
});
|
||||
|
||||
it('解析带单引号的参数', () => {
|
||||
const result = executor.parseInput("/test 'hello world' foo");
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.args).toEqual(['hello world', 'foo']);
|
||||
});
|
||||
|
||||
it('不是命令时返回 null', () => {
|
||||
const result = executor.parseInput('not a command');
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('空输入返回 null', () => {
|
||||
const result = executor.parseInput('');
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('包含工作目录', () => {
|
||||
const result = executor.parseInput('/test');
|
||||
expect(result?.workdir).toBe('/test/workdir');
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行命令', () => {
|
||||
beforeEach(async () => {
|
||||
const registry = getCommandRegistry();
|
||||
await registry.initialize('/test');
|
||||
|
||||
// 手动注册测试命令
|
||||
const testCommand: Command = {
|
||||
name: 'greet',
|
||||
description: '打招呼',
|
||||
template: 'Hello, $ARGUMENTS!',
|
||||
source: 'builtin',
|
||||
};
|
||||
registry.register(testCommand);
|
||||
});
|
||||
|
||||
it('执行存在的命令', async () => {
|
||||
const input = executor.parseInput('/greet World');
|
||||
expect(input).not.toBeNull();
|
||||
|
||||
const result = await executor.execute(input!);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.prompt).toBe('Hello, World!');
|
||||
});
|
||||
|
||||
it('命令不存在时返回错误', async () => {
|
||||
const input = executor.parseInput('/nonexistent');
|
||||
expect(input).not.toBeNull();
|
||||
|
||||
const result = await executor.execute(input!);
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('Command 不存在');
|
||||
});
|
||||
|
||||
it('返回 agent 配置', async () => {
|
||||
const registry = getCommandRegistry();
|
||||
const agentCommand: Command = {
|
||||
name: 'code',
|
||||
description: '编码',
|
||||
template: 'Write code',
|
||||
agent: 'coder',
|
||||
source: 'builtin',
|
||||
};
|
||||
registry.register(agentCommand);
|
||||
|
||||
const input = executor.parseInput('/code');
|
||||
const result = await executor.execute(input!);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.agent).toBe('coder');
|
||||
});
|
||||
|
||||
it('返回 model 配置', async () => {
|
||||
const registry = getCommandRegistry();
|
||||
const modelCommand: Command = {
|
||||
name: 'complex',
|
||||
description: '复杂任务',
|
||||
template: 'Complex task',
|
||||
model: 'opus',
|
||||
source: 'builtin',
|
||||
};
|
||||
registry.register(modelCommand);
|
||||
|
||||
const input = executor.parseInput('/complex');
|
||||
const result = await executor.execute(input!);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.model).toBe('opus');
|
||||
});
|
||||
});
|
||||
|
||||
describe('renderTemplate - 模板渲染', () => {
|
||||
it('替换 $ARGUMENTS', async () => {
|
||||
const input = {
|
||||
command: 'test',
|
||||
arguments: 'hello world',
|
||||
args: ['hello', 'world'],
|
||||
workdir: '/test',
|
||||
};
|
||||
|
||||
const result = await executor.renderTemplate('Say: $ARGUMENTS', input);
|
||||
expect(result).toBe('Say: hello world');
|
||||
});
|
||||
|
||||
it('替换位置参数 $1(单个参数时获取全部)', async () => {
|
||||
const input = {
|
||||
command: 'test',
|
||||
arguments: 'foo bar',
|
||||
args: ['foo', 'bar'],
|
||||
workdir: '/test',
|
||||
};
|
||||
|
||||
// 当 $1 是最大参数索引时,获取所有剩余参数
|
||||
const result = await executor.renderTemplate('First: $1', input);
|
||||
expect(result).toBe('First: foo bar');
|
||||
});
|
||||
|
||||
it('替换多个位置参数', async () => {
|
||||
const input = {
|
||||
command: 'test',
|
||||
arguments: 'a b c',
|
||||
args: ['a', 'b', 'c'],
|
||||
workdir: '/test',
|
||||
};
|
||||
|
||||
const result = await executor.renderTemplate('$1 and $2', input);
|
||||
expect(result).toBe('a and b c');
|
||||
});
|
||||
|
||||
it('最后一个位置参数获取剩余所有', async () => {
|
||||
const input = {
|
||||
command: 'test',
|
||||
arguments: 'a b c d',
|
||||
args: ['a', 'b', 'c', 'd'],
|
||||
workdir: '/test',
|
||||
};
|
||||
|
||||
const result = await executor.renderTemplate('First: $1, Rest: $2', input);
|
||||
expect(result).toBe('First: a, Rest: b c d');
|
||||
});
|
||||
|
||||
it('缺少参数时替换为空字符串', async () => {
|
||||
const input = {
|
||||
command: 'test',
|
||||
arguments: '',
|
||||
args: [],
|
||||
workdir: '/test',
|
||||
};
|
||||
|
||||
const result = await executor.renderTemplate('Value: $1', input);
|
||||
expect(result).toBe('Value: ');
|
||||
});
|
||||
|
||||
it('处理文件引用 @filepath', async () => {
|
||||
const mockContent = 'file content here';
|
||||
vi.mocked(fs.readFile).mockResolvedValue(mockContent);
|
||||
|
||||
const input = {
|
||||
command: 'test',
|
||||
arguments: '',
|
||||
args: [],
|
||||
workdir: '/test',
|
||||
};
|
||||
|
||||
const result = await executor.renderTemplate('Review: @src/file.ts', input);
|
||||
expect(result).toContain('file content here');
|
||||
expect(result).toContain('```ts');
|
||||
});
|
||||
|
||||
it('文件不存在时显示提示', async () => {
|
||||
vi.mocked(fs.readFile).mockRejectedValue(new Error('ENOENT'));
|
||||
|
||||
const input = {
|
||||
command: 'test',
|
||||
arguments: '',
|
||||
args: [],
|
||||
workdir: '/test',
|
||||
};
|
||||
|
||||
const result = await executor.renderTemplate('Review: @nonexistent.ts', input);
|
||||
expect(result).toContain('[文件不存在: nonexistent.ts]');
|
||||
});
|
||||
});
|
||||
|
||||
describe('createCommandExecutor - 工厂函数', () => {
|
||||
it('创建执行器实例', () => {
|
||||
const exec = createCommandExecutor('/custom/path');
|
||||
expect(exec).toBeInstanceOf(CommandExecutor);
|
||||
});
|
||||
|
||||
it('默认使用 process.cwd', () => {
|
||||
const exec = createCommandExecutor();
|
||||
const input = exec.parseInput('/test');
|
||||
expect(input?.workdir).toBe(process.cwd());
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,206 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { CommandLoader } from '../../../src/commands/loader.js';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
access: vi.fn(),
|
||||
readdir: vi.fn(),
|
||||
readFile: vi.fn(),
|
||||
}));
|
||||
|
||||
import * as fs from 'fs/promises';
|
||||
|
||||
describe('CommandLoader - Command 加载器', () => {
|
||||
let loader: CommandLoader;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
loader = new CommandLoader();
|
||||
});
|
||||
|
||||
describe('parseMarkdownCommand - 解析 Markdown', () => {
|
||||
it('解析带 frontmatter 的 Markdown', () => {
|
||||
const content = `---
|
||||
description: 代码审查
|
||||
agent: explore
|
||||
model: sonnet
|
||||
subtask: true
|
||||
---
|
||||
|
||||
You are a code reviewer.
|
||||
|
||||
Input: $ARGUMENTS
|
||||
`;
|
||||
|
||||
const command = loader.parseMarkdownCommand(content, 'review', 'user');
|
||||
|
||||
expect(command).not.toBeNull();
|
||||
expect(command?.name).toBe('review');
|
||||
expect(command?.description).toBe('代码审查');
|
||||
expect(command?.agent).toBe('explore');
|
||||
expect(command?.model).toBe('sonnet');
|
||||
expect(command?.subtask).toBe(true);
|
||||
expect(command?.template).toContain('You are a code reviewer');
|
||||
expect(command?.template).toContain('$ARGUMENTS');
|
||||
expect(command?.source).toBe('user');
|
||||
});
|
||||
|
||||
it('解析不带 frontmatter 的 Markdown', () => {
|
||||
const content = `# Simple Command
|
||||
|
||||
This is a simple prompt template.
|
||||
|
||||
$ARGUMENTS
|
||||
`;
|
||||
|
||||
const command = loader.parseMarkdownCommand(content, 'simple', 'project');
|
||||
|
||||
expect(command).not.toBeNull();
|
||||
expect(command?.name).toBe('simple');
|
||||
expect(command?.description).toBeUndefined();
|
||||
expect(command?.template).toContain('Simple Command');
|
||||
expect(command?.template).toContain('$ARGUMENTS');
|
||||
expect(command?.source).toBe('project');
|
||||
});
|
||||
|
||||
it('空模板返回 null', () => {
|
||||
const content = `---
|
||||
description: Empty
|
||||
---
|
||||
|
||||
`;
|
||||
|
||||
const command = loader.parseMarkdownCommand(content, 'empty', 'user');
|
||||
|
||||
expect(command).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadFromFile - 从文件加载', () => {
|
||||
it('从 Markdown 文件加载 Command', async () => {
|
||||
const mdContent = `---
|
||||
description: Test command
|
||||
---
|
||||
|
||||
Test template $ARGUMENTS
|
||||
`;
|
||||
|
||||
vi.mocked(fs.readFile).mockResolvedValue(mdContent);
|
||||
|
||||
const command = await loader.loadFromFile(
|
||||
'/base/commands/test.md',
|
||||
'/base/commands',
|
||||
'user'
|
||||
);
|
||||
|
||||
expect(command).not.toBeNull();
|
||||
expect(command?.name).toBe('test');
|
||||
expect(command?.description).toBe('Test command');
|
||||
});
|
||||
|
||||
it('处理嵌套目录路径', async () => {
|
||||
const mdContent = `---
|
||||
description: Deploy staging
|
||||
---
|
||||
|
||||
Deploy to staging
|
||||
`;
|
||||
|
||||
vi.mocked(fs.readFile).mockResolvedValue(mdContent);
|
||||
|
||||
const command = await loader.loadFromFile(
|
||||
'/base/commands/deploy/staging.md',
|
||||
'/base/commands',
|
||||
'project'
|
||||
);
|
||||
|
||||
expect(command).not.toBeNull();
|
||||
expect(command?.name).toBe('deploy/staging');
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadFromDirectory - 从目录加载', () => {
|
||||
it('目录不存在时返回空数组', async () => {
|
||||
vi.mocked(fs.access).mockRejectedValue(new Error('Not found'));
|
||||
|
||||
const commands = await loader.loadFromDirectory('/non-existent', 'user');
|
||||
|
||||
expect(commands).toEqual([]);
|
||||
});
|
||||
|
||||
it('加载目录中的所有 Markdown 文件', async () => {
|
||||
vi.mocked(fs.access).mockResolvedValue(undefined);
|
||||
vi.mocked(fs.readdir).mockResolvedValue([
|
||||
{ name: 'cmd1.md', isFile: () => true, isDirectory: () => false },
|
||||
{ name: 'cmd2.md', isFile: () => true, isDirectory: () => false },
|
||||
{ name: 'readme.txt', isFile: () => true, isDirectory: () => false }, // 非 .md
|
||||
] as any);
|
||||
|
||||
vi.mocked(fs.readFile)
|
||||
.mockResolvedValueOnce('---\ndescription: Cmd1\n---\nTemplate 1')
|
||||
.mockResolvedValueOnce('---\ndescription: Cmd2\n---\nTemplate 2');
|
||||
|
||||
const commands = await loader.loadFromDirectory('/test', 'user');
|
||||
|
||||
expect(commands.length).toBe(2);
|
||||
expect(commands.map((c) => c.name)).toContain('cmd1');
|
||||
expect(commands.map((c) => c.name)).toContain('cmd2');
|
||||
});
|
||||
|
||||
it('递归加载子目录', async () => {
|
||||
vi.mocked(fs.access).mockResolvedValue(undefined);
|
||||
vi.mocked(fs.readdir)
|
||||
.mockResolvedValueOnce([
|
||||
{ name: 'root.md', isFile: () => true, isDirectory: () => false },
|
||||
{ name: 'subdir', isFile: () => false, isDirectory: () => true },
|
||||
] as any)
|
||||
.mockResolvedValueOnce([
|
||||
{ name: 'sub.md', isFile: () => true, isDirectory: () => false },
|
||||
] as any);
|
||||
|
||||
vi.mocked(fs.readFile)
|
||||
.mockResolvedValueOnce('Root template')
|
||||
.mockResolvedValueOnce('Sub template');
|
||||
|
||||
const commands = await loader.loadFromDirectory('/test', 'project');
|
||||
|
||||
expect(commands.length).toBe(2);
|
||||
expect(commands.map((c) => c.name)).toContain('root');
|
||||
expect(commands.map((c) => c.name)).toContain('subdir/sub');
|
||||
});
|
||||
|
||||
it('跳过隐藏目录', async () => {
|
||||
vi.mocked(fs.access).mockResolvedValue(undefined);
|
||||
vi.mocked(fs.readdir).mockResolvedValue([
|
||||
{ name: '.hidden', isFile: () => false, isDirectory: () => true },
|
||||
{ name: 'visible.md', isFile: () => true, isDirectory: () => false },
|
||||
] as any);
|
||||
|
||||
vi.mocked(fs.readFile).mockResolvedValue('Template');
|
||||
|
||||
const commands = await loader.loadFromDirectory('/test', 'user');
|
||||
|
||||
expect(commands.length).toBe(1);
|
||||
expect(commands[0].name).toBe('visible');
|
||||
});
|
||||
});
|
||||
|
||||
describe('目录路径', () => {
|
||||
it('getUserCommandsDir 返回用户 Commands 目录', () => {
|
||||
const originalHome = process.env.HOME;
|
||||
process.env.HOME = '/home/testuser';
|
||||
|
||||
const dir = loader.getUserCommandsDir();
|
||||
|
||||
expect(dir).toBe('/home/testuser/.config/ai-terminal/commands');
|
||||
|
||||
process.env.HOME = originalHome;
|
||||
});
|
||||
|
||||
it('getProjectCommandsDir 返回项目 Commands 目录', () => {
|
||||
const dir = loader.getProjectCommandsDir('/workspace');
|
||||
|
||||
expect(dir).toBe('/workspace/.ai-terminal/commands');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,250 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import {
|
||||
CommandRegistry,
|
||||
getCommandRegistry,
|
||||
resetCommandRegistry,
|
||||
} from '../../../src/commands/registry.js';
|
||||
import type { Command } from '../../../src/commands/types.js';
|
||||
|
||||
// Mock loader
|
||||
vi.mock('../../../src/commands/loader.js', () => ({
|
||||
commandLoader: {
|
||||
loadFromDirectory: vi.fn().mockResolvedValue([]),
|
||||
getUserCommandsDir: vi.fn().mockReturnValue('/mock/user/commands'),
|
||||
getProjectCommandsDir: vi.fn().mockReturnValue('/mock/project/commands'),
|
||||
},
|
||||
}));
|
||||
|
||||
// Mock builtin commands
|
||||
vi.mock('../../../src/commands/builtin/index.js', () => ({
|
||||
builtinCommands: [
|
||||
{
|
||||
name: 'test-builtin',
|
||||
description: '内置测试命令',
|
||||
template: '测试模板 $ARGUMENTS',
|
||||
source: 'builtin',
|
||||
},
|
||||
],
|
||||
}));
|
||||
|
||||
describe('CommandRegistry - Command 注册表', () => {
|
||||
let registry: CommandRegistry;
|
||||
|
||||
beforeEach(() => {
|
||||
resetCommandRegistry();
|
||||
registry = new CommandRegistry();
|
||||
});
|
||||
|
||||
describe('register - 注册', () => {
|
||||
it('成功注册 Command', () => {
|
||||
const command: Command = {
|
||||
name: 'test',
|
||||
description: '测试',
|
||||
template: '模板',
|
||||
source: 'user',
|
||||
};
|
||||
|
||||
registry.register(command);
|
||||
expect(registry.has('test')).toBe(true);
|
||||
});
|
||||
|
||||
it('高优先级可以覆盖低优先级', () => {
|
||||
const builtin: Command = {
|
||||
name: 'same',
|
||||
description: '内置版本',
|
||||
template: '内置模板',
|
||||
source: 'builtin',
|
||||
};
|
||||
|
||||
const project: Command = {
|
||||
name: 'same',
|
||||
description: '项目版本',
|
||||
template: '项目模板',
|
||||
source: 'project',
|
||||
};
|
||||
|
||||
registry.register(builtin);
|
||||
registry.register(project);
|
||||
|
||||
const result = registry.get('same');
|
||||
expect(result?.source).toBe('project');
|
||||
expect(result?.description).toBe('项目版本');
|
||||
});
|
||||
|
||||
it('低优先级不能覆盖高优先级', () => {
|
||||
const project: Command = {
|
||||
name: 'same',
|
||||
description: '项目版本',
|
||||
template: '项目模板',
|
||||
source: 'project',
|
||||
};
|
||||
|
||||
const builtin: Command = {
|
||||
name: 'same',
|
||||
description: '内置版本',
|
||||
template: '内置模板',
|
||||
source: 'builtin',
|
||||
};
|
||||
|
||||
registry.register(project);
|
||||
registry.register(builtin);
|
||||
|
||||
const result = registry.get('same');
|
||||
expect(result?.source).toBe('project');
|
||||
});
|
||||
});
|
||||
|
||||
describe('get - 获取', () => {
|
||||
it('获取存在的 Command', () => {
|
||||
const command: Command = {
|
||||
name: 'test',
|
||||
description: '测试',
|
||||
template: '模板',
|
||||
source: 'user',
|
||||
};
|
||||
|
||||
registry.register(command);
|
||||
const result = registry.get('test');
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.name).toBe('test');
|
||||
});
|
||||
|
||||
it('获取不存在的 Command 返回 undefined', () => {
|
||||
const result = registry.get('non-existent');
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('search - 搜索', () => {
|
||||
beforeEach(() => {
|
||||
const commands: Command[] = [
|
||||
{
|
||||
name: 'review',
|
||||
description: '代码审查',
|
||||
template: '审查模板',
|
||||
source: 'builtin',
|
||||
},
|
||||
{
|
||||
name: 'test',
|
||||
description: '运行测试',
|
||||
template: '测试模板',
|
||||
source: 'builtin',
|
||||
},
|
||||
{
|
||||
name: 'deploy/staging',
|
||||
description: '部署到 staging',
|
||||
template: '部署模板',
|
||||
source: 'project',
|
||||
},
|
||||
];
|
||||
|
||||
for (const cmd of commands) {
|
||||
registry.register(cmd);
|
||||
}
|
||||
});
|
||||
|
||||
it('按名称精确匹配', () => {
|
||||
const results = registry.search('review');
|
||||
expect(results.length).toBeGreaterThan(0);
|
||||
expect(results[0].command.name).toBe('review');
|
||||
expect(results[0].score).toBe(100);
|
||||
});
|
||||
|
||||
it('按名称前缀匹配', () => {
|
||||
const results = registry.search('deploy');
|
||||
expect(results.length).toBeGreaterThan(0);
|
||||
expect(results[0].command.name).toBe('deploy/staging');
|
||||
});
|
||||
|
||||
it('按描述匹配', () => {
|
||||
const results = registry.search('代码');
|
||||
expect(results.length).toBeGreaterThan(0);
|
||||
expect(results[0].command.name).toBe('review');
|
||||
});
|
||||
|
||||
it('限制结果数量', () => {
|
||||
const results = registry.search('', 1);
|
||||
expect(results.length).toBeLessThanOrEqual(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('list - 列表', () => {
|
||||
it('返回所有 Commands 的摘要', () => {
|
||||
const command: Command = {
|
||||
name: 'test',
|
||||
description: '测试',
|
||||
template: '模板',
|
||||
source: 'user',
|
||||
};
|
||||
|
||||
registry.register(command);
|
||||
const list = registry.list();
|
||||
|
||||
expect(list.length).toBe(1);
|
||||
expect(list[0].name).toBe('test');
|
||||
expect(list[0].description).toBe('测试');
|
||||
expect(list[0].source).toBe('user');
|
||||
});
|
||||
|
||||
it('按名称排序', () => {
|
||||
const commands: Command[] = [
|
||||
{ name: 'zebra', description: '', template: '', source: 'user' },
|
||||
{ name: 'alpha', description: '', template: '', source: 'user' },
|
||||
{ name: 'beta', description: '', template: '', source: 'user' },
|
||||
];
|
||||
|
||||
for (const cmd of commands) {
|
||||
registry.register(cmd);
|
||||
}
|
||||
|
||||
const list = registry.list();
|
||||
expect(list[0].name).toBe('alpha');
|
||||
expect(list[1].name).toBe('beta');
|
||||
expect(list[2].name).toBe('zebra');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getStats - 统计', () => {
|
||||
it('返回正确的统计信息', () => {
|
||||
const commands: Command[] = [
|
||||
{ name: 'b1', description: '', template: '', source: 'builtin' },
|
||||
{ name: 'b2', description: '', template: '', source: 'builtin' },
|
||||
{ name: 'u1', description: '', template: '', source: 'user' },
|
||||
{ name: 'p1', description: '', template: '', source: 'project' },
|
||||
];
|
||||
|
||||
for (const cmd of commands) {
|
||||
registry.register(cmd);
|
||||
}
|
||||
|
||||
const stats = registry.getStats();
|
||||
|
||||
expect(stats.total).toBe(4);
|
||||
expect(stats.bySource.builtin).toBe(2);
|
||||
expect(stats.bySource.user).toBe(1);
|
||||
expect(stats.bySource.project).toBe(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getCommandRegistry - 全局注册表', () => {
|
||||
beforeEach(() => {
|
||||
resetCommandRegistry();
|
||||
});
|
||||
|
||||
it('返回单例实例', () => {
|
||||
const registry1 = getCommandRegistry();
|
||||
const registry2 = getCommandRegistry();
|
||||
|
||||
expect(registry1).toBe(registry2);
|
||||
});
|
||||
|
||||
it('resetCommandRegistry 重置实例', () => {
|
||||
const registry1 = getCommandRegistry();
|
||||
resetCommandRegistry();
|
||||
const registry2 = getCommandRegistry();
|
||||
|
||||
expect(registry1).not.toBe(registry2);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,279 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { isSummaryMessage, simpleCompact } from '../../../src/context/compaction.js';
|
||||
import {
|
||||
SUMMARY_MARKER,
|
||||
type CompressionConfig,
|
||||
} from '../../../src/context/types.js';
|
||||
import type { ModelMessage } from 'ai';
|
||||
|
||||
// 创建用户消息
|
||||
function createUserMessage(content: string): ModelMessage {
|
||||
return { role: 'user', content };
|
||||
}
|
||||
|
||||
// 创建助手消息
|
||||
function createAssistantMessage(content: string): ModelMessage {
|
||||
return { role: 'assistant', content };
|
||||
}
|
||||
|
||||
// 创建摘要消息
|
||||
function createSummaryMessage(summary: string): ModelMessage {
|
||||
return {
|
||||
role: 'assistant',
|
||||
content: `${SUMMARY_MARKER}\n## 对话摘要\n\n${summary}\n${SUMMARY_MARKER}`,
|
||||
};
|
||||
}
|
||||
|
||||
// 创建带文本部分的消息
|
||||
function createMessageWithTextParts(texts: string[]): ModelMessage {
|
||||
return {
|
||||
role: 'assistant',
|
||||
content: texts.map((text) => ({ type: 'text', text })),
|
||||
} as ModelMessage;
|
||||
}
|
||||
|
||||
describe('isSummaryMessage - 检测摘要消息', () => {
|
||||
it('字符串内容包含摘要标记返回 true', () => {
|
||||
const message = createSummaryMessage('这是摘要内容');
|
||||
|
||||
expect(isSummaryMessage(message)).toBe(true);
|
||||
});
|
||||
|
||||
it('字符串内容不包含摘要标记返回 false', () => {
|
||||
const message = createAssistantMessage('普通助手消息');
|
||||
|
||||
expect(isSummaryMessage(message)).toBe(false);
|
||||
});
|
||||
|
||||
it('数组内容包含摘要标记返回 true', () => {
|
||||
const message = createMessageWithTextParts([
|
||||
'一些文本',
|
||||
`${SUMMARY_MARKER}\n摘要内容\n${SUMMARY_MARKER}`,
|
||||
]);
|
||||
|
||||
expect(isSummaryMessage(message)).toBe(true);
|
||||
});
|
||||
|
||||
it('数组内容不包含摘要标记返回 false', () => {
|
||||
const message = createMessageWithTextParts(['文本1', '文本2']);
|
||||
|
||||
expect(isSummaryMessage(message)).toBe(false);
|
||||
});
|
||||
|
||||
it('用户消息不是摘要消息', () => {
|
||||
const message = createUserMessage(SUMMARY_MARKER);
|
||||
|
||||
// 虽然包含标记,但这种情况下 isSummaryMessage 还是会返回 true
|
||||
// 因为它只检查内容是否包含标记
|
||||
expect(isSummaryMessage(message)).toBe(true);
|
||||
});
|
||||
|
||||
it('空数组内容返回 false', () => {
|
||||
const message: ModelMessage = {
|
||||
role: 'assistant',
|
||||
content: [],
|
||||
};
|
||||
|
||||
expect(isSummaryMessage(message)).toBe(false);
|
||||
});
|
||||
|
||||
it('非文本部分不匹配', () => {
|
||||
const message: ModelMessage = {
|
||||
role: 'assistant',
|
||||
content: [
|
||||
{
|
||||
type: 'tool-call',
|
||||
toolCallId: 'call_1',
|
||||
toolName: 'test',
|
||||
args: { key: SUMMARY_MARKER },
|
||||
},
|
||||
],
|
||||
} as ModelMessage;
|
||||
|
||||
expect(isSummaryMessage(message)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('simpleCompact - 简单压缩', () => {
|
||||
const testConfig: CompressionConfig = {
|
||||
contextLimit: 1000,
|
||||
outputReserve: 100,
|
||||
pruneProtect: 200, // 保护最近 200 tokens
|
||||
pruneMinimum: 50,
|
||||
overflowThreshold: 0.85,
|
||||
};
|
||||
|
||||
describe('基本压缩行为', () => {
|
||||
it('空消息数组不压缩', () => {
|
||||
const result = simpleCompact([], testConfig);
|
||||
|
||||
expect(result.messages).toHaveLength(0);
|
||||
expect(result.freedTokens).toBe(0);
|
||||
});
|
||||
|
||||
it('消息在保护范围内不压缩', () => {
|
||||
const messages: ModelMessage[] = [
|
||||
createUserMessage('Hello'),
|
||||
createAssistantMessage('Hi'),
|
||||
];
|
||||
|
||||
const result = simpleCompact(messages, testConfig);
|
||||
|
||||
// 消息很短,应该在保护范围内
|
||||
expect(result.freedTokens).toBe(0);
|
||||
expect(result.messages).toEqual(messages);
|
||||
});
|
||||
|
||||
it('压缩超出保护范围的消息', () => {
|
||||
// 创建大量消息超出保护范围
|
||||
const messages: ModelMessage[] = [];
|
||||
for (let i = 0; i < 50; i++) {
|
||||
messages.push(createUserMessage(`Message ${i}: ${'a'.repeat(100)}`));
|
||||
messages.push(createAssistantMessage(`Response ${i}: ${'b'.repeat(100)}`));
|
||||
}
|
||||
|
||||
const result = simpleCompact(messages, testConfig);
|
||||
|
||||
// 应该压缩一些旧消息
|
||||
expect(result.freedTokens).toBeGreaterThan(0);
|
||||
expect(result.messages.length).toBeLessThan(messages.length);
|
||||
});
|
||||
});
|
||||
|
||||
describe('保留消息数量', () => {
|
||||
it('至少保留 2 条消息(正常模式)', () => {
|
||||
const messages: ModelMessage[] = [];
|
||||
for (let i = 0; i < 10; i++) {
|
||||
messages.push(createUserMessage(`Long message ${'a'.repeat(500)}`));
|
||||
}
|
||||
|
||||
const result = simpleCompact(messages, testConfig);
|
||||
|
||||
// 即使压缩,也至少保留 2 条
|
||||
expect(result.messages.length).toBeGreaterThanOrEqual(2 + 1); // 2 保留 + 1 摘要
|
||||
});
|
||||
|
||||
it('强制模式下至少保留 1 条消息', () => {
|
||||
const forceConfig: CompressionConfig = {
|
||||
...testConfig,
|
||||
pruneProtect: 0, // 强制模式
|
||||
};
|
||||
|
||||
const messages: ModelMessage[] = [];
|
||||
for (let i = 0; i < 10; i++) {
|
||||
messages.push(createUserMessage(`Message ${i}`));
|
||||
}
|
||||
|
||||
const result = simpleCompact(messages, forceConfig);
|
||||
|
||||
// 强制模式下至少保留 1 条消息
|
||||
expect(result.messages.length).toBeGreaterThanOrEqual(2); // 1 保留 + 1 摘要
|
||||
});
|
||||
});
|
||||
|
||||
describe('摘要消息生成', () => {
|
||||
it('压缩后生成摘要消息', () => {
|
||||
const messages: ModelMessage[] = [];
|
||||
for (let i = 0; i < 50; i++) {
|
||||
messages.push(createUserMessage(`Message ${i}: ${'a'.repeat(100)}`));
|
||||
}
|
||||
|
||||
const result = simpleCompact(messages, testConfig);
|
||||
|
||||
if (result.freedTokens > 0) {
|
||||
// 第一条消息应该是摘要
|
||||
expect(isSummaryMessage(result.messages[0])).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it('摘要消息包含移除数量信息', () => {
|
||||
const messages: ModelMessage[] = [];
|
||||
for (let i = 0; i < 50; i++) {
|
||||
messages.push(createUserMessage(`Message ${i}: ${'a'.repeat(100)}`));
|
||||
}
|
||||
|
||||
const result = simpleCompact(messages, testConfig);
|
||||
|
||||
if (result.freedTokens > 0) {
|
||||
const summaryContent = result.messages[0].content as string;
|
||||
expect(summaryContent).toContain('对话历史已压缩');
|
||||
expect(summaryContent).toContain('条消息');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('保护范围计算', () => {
|
||||
it('短消息全部在保护范围内', () => {
|
||||
const messages: ModelMessage[] = [
|
||||
createUserMessage('Hi'),
|
||||
createAssistantMessage('Hello'),
|
||||
createUserMessage('How?'),
|
||||
createAssistantMessage('Good!'),
|
||||
];
|
||||
|
||||
const result = simpleCompact(messages, testConfig);
|
||||
|
||||
// 短消息应该全部保留
|
||||
expect(result.messages).toEqual(messages);
|
||||
});
|
||||
});
|
||||
|
||||
describe('不修改原数组', () => {
|
||||
it('原消息数组不被修改', () => {
|
||||
const messages: ModelMessage[] = [];
|
||||
for (let i = 0; i < 20; i++) {
|
||||
messages.push(createUserMessage(`Message ${i}: ${'a'.repeat(100)}`));
|
||||
}
|
||||
|
||||
const originalLength = messages.length;
|
||||
simpleCompact(messages, testConfig);
|
||||
|
||||
expect(messages.length).toBe(originalLength);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// 注意: compact 函数需要真实的 LanguageModel,
|
||||
// 这里只测试 simpleCompact 和辅助函数
|
||||
// compact 的测试应该在集成测试中进行
|
||||
|
||||
describe('Compaction 边界情况', () => {
|
||||
it('单条消息不压缩', () => {
|
||||
const messages: ModelMessage[] = [
|
||||
createUserMessage('Single message'),
|
||||
];
|
||||
|
||||
const testConfig: CompressionConfig = {
|
||||
contextLimit: 100,
|
||||
outputReserve: 10,
|
||||
pruneProtect: 50,
|
||||
pruneMinimum: 10,
|
||||
overflowThreshold: 0.85,
|
||||
};
|
||||
|
||||
const result = simpleCompact(messages, testConfig);
|
||||
|
||||
expect(result.messages).toEqual(messages);
|
||||
expect(result.freedTokens).toBe(0);
|
||||
});
|
||||
|
||||
it('两条消息不压缩(最小保留)', () => {
|
||||
const messages: ModelMessage[] = [
|
||||
createUserMessage('First'),
|
||||
createAssistantMessage('Second'),
|
||||
];
|
||||
|
||||
const testConfig: CompressionConfig = {
|
||||
contextLimit: 100,
|
||||
outputReserve: 10,
|
||||
pruneProtect: 10, // 很小的保护范围
|
||||
pruneMinimum: 1,
|
||||
overflowThreshold: 0.85,
|
||||
};
|
||||
|
||||
const result = simpleCompact(messages, testConfig);
|
||||
|
||||
// 即使配置很激进,也至少保留 2 条
|
||||
expect(result.messages.length).toBeGreaterThanOrEqual(2);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,317 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import type { ModelMessage, LanguageModel } from 'ai';
|
||||
|
||||
// Mock prune module
|
||||
const mockPrune = vi.fn();
|
||||
const mockFilterCompacted = vi.fn();
|
||||
vi.mock('../../../src/context/prune.js', () => ({
|
||||
prune: (...args: unknown[]) => mockPrune(...args),
|
||||
filterCompacted: (...args: unknown[]) => mockFilterCompacted(...args),
|
||||
}));
|
||||
|
||||
// Mock compaction module
|
||||
const mockCompact = vi.fn();
|
||||
const mockSimpleCompact = vi.fn();
|
||||
const mockIsSummaryMessage = vi.fn();
|
||||
vi.mock('../../../src/context/compaction.js', () => ({
|
||||
compact: (...args: unknown[]) => mockCompact(...args),
|
||||
simpleCompact: (...args: unknown[]) => mockSimpleCompact(...args),
|
||||
isSummaryMessage: (...args: unknown[]) => mockIsSummaryMessage(...args),
|
||||
}));
|
||||
|
||||
// Mock token counter
|
||||
const mockEstimateMessages = vi.fn();
|
||||
const mockFormat = vi.fn();
|
||||
vi.mock('../../../src/context/token-counter.js', () => ({
|
||||
TokenCounter: {
|
||||
estimateMessages: (...args: unknown[]) => mockEstimateMessages(...args),
|
||||
format: (...args: unknown[]) => mockFormat(...args),
|
||||
},
|
||||
}));
|
||||
|
||||
import { CompressionManager } from '../../../src/context/manager.js';
|
||||
|
||||
describe('CompressionManager - 压缩管理器扩展测试', () => {
|
||||
let manager: CompressionManager;
|
||||
|
||||
const createMessages = (count: number): ModelMessage[] => {
|
||||
return Array.from({ length: count }, (_, i) => ({
|
||||
role: 'user' as const,
|
||||
content: `Message ${i}`,
|
||||
}));
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
manager = new CompressionManager();
|
||||
|
||||
// 默认 mock 返回值
|
||||
mockEstimateMessages.mockReturnValue(1000);
|
||||
mockFormat.mockReturnValue('1K');
|
||||
mockPrune.mockReturnValue({ messages: [], freedTokens: 0 });
|
||||
mockFilterCompacted.mockImplementation((msgs) => msgs);
|
||||
mockCompact.mockResolvedValue({ messages: [], freedTokens: 0 });
|
||||
mockSimpleCompact.mockReturnValue({ messages: [], freedTokens: 0 });
|
||||
mockIsSummaryMessage.mockReturnValue(false);
|
||||
});
|
||||
|
||||
describe('constructor - 构造函数', () => {
|
||||
it('使用默认配置', () => {
|
||||
const config = manager.getConfig();
|
||||
|
||||
expect(config.contextLimit).toBeDefined();
|
||||
expect(config.outputReserve).toBeDefined();
|
||||
expect(config.overflowThreshold).toBeDefined();
|
||||
});
|
||||
|
||||
it('合并自定义配置', () => {
|
||||
const customManager = new CompressionManager({
|
||||
contextLimit: 100000,
|
||||
outputReserve: 5000,
|
||||
});
|
||||
|
||||
const config = customManager.getConfig();
|
||||
expect(config.contextLimit).toBe(100000);
|
||||
expect(config.outputReserve).toBe(5000);
|
||||
});
|
||||
});
|
||||
|
||||
describe('setModel - 设置模型', () => {
|
||||
it('设置模型用于摘要生成', () => {
|
||||
const mockModel = {} as LanguageModel;
|
||||
manager.setModel(mockModel);
|
||||
|
||||
// 模型被设置后 compact 应该使用它
|
||||
expect(manager['model']).toBe(mockModel);
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateConfig - 更新配置', () => {
|
||||
it('更新部分配置', () => {
|
||||
const originalConfig = manager.getConfig();
|
||||
|
||||
manager.updateConfig({ contextLimit: 200000 });
|
||||
|
||||
const updatedConfig = manager.getConfig();
|
||||
expect(updatedConfig.contextLimit).toBe(200000);
|
||||
expect(updatedConfig.outputReserve).toBe(originalConfig.outputReserve);
|
||||
});
|
||||
});
|
||||
|
||||
describe('calculateUsage - 计算使用量', () => {
|
||||
it('计算 token 使用情况', () => {
|
||||
mockEstimateMessages.mockReturnValue(50000);
|
||||
|
||||
const messages = createMessages(10);
|
||||
const usage = manager.calculateUsage(messages);
|
||||
|
||||
expect(usage.input).toBe(50000);
|
||||
expect(usage.contextLimit).toBeDefined();
|
||||
expect(usage.available).toBeDefined();
|
||||
expect(usage.usagePercent).toBeGreaterThanOrEqual(0);
|
||||
});
|
||||
|
||||
it('使用百分比不超过 100', () => {
|
||||
// 模拟超出限制
|
||||
mockEstimateMessages.mockReturnValue(300000);
|
||||
|
||||
const usage = manager.calculateUsage([]);
|
||||
|
||||
expect(usage.usagePercent).toBeLessThanOrEqual(100);
|
||||
});
|
||||
});
|
||||
|
||||
describe('shouldCompress - 是否需要压缩', () => {
|
||||
it('未超过阈值返回 false', () => {
|
||||
mockEstimateMessages.mockReturnValue(10000);
|
||||
|
||||
const result = manager.shouldCompress([]);
|
||||
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it('超过阈值返回 true', () => {
|
||||
// 设置接近阈值的使用量
|
||||
mockEstimateMessages.mockReturnValue(150000);
|
||||
|
||||
const result = manager.shouldCompress([]);
|
||||
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isOverflow - 是否溢出', () => {
|
||||
it('未溢出返回 false', () => {
|
||||
mockEstimateMessages.mockReturnValue(10000);
|
||||
|
||||
const result = manager.isOverflow([]);
|
||||
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it('溢出返回 true', () => {
|
||||
mockEstimateMessages.mockReturnValue(500000);
|
||||
|
||||
const result = manager.isOverflow([]);
|
||||
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('prune - 执行裁剪', () => {
|
||||
it('调用 prune 函数', () => {
|
||||
const messages = createMessages(5);
|
||||
mockPrune.mockReturnValue({ messages: messages.slice(2), freedTokens: 1000 });
|
||||
|
||||
const result = manager.prune(messages);
|
||||
|
||||
expect(mockPrune).toHaveBeenCalledWith(messages, expect.any(Object));
|
||||
expect(result.freedTokens).toBe(1000);
|
||||
});
|
||||
});
|
||||
|
||||
describe('compact - 执行压缩', () => {
|
||||
it('有模型时使用 AI 压缩', async () => {
|
||||
const mockModel = {} as LanguageModel;
|
||||
manager.setModel(mockModel);
|
||||
mockCompact.mockResolvedValue({ messages: [], freedTokens: 2000 });
|
||||
|
||||
const messages = createMessages(5);
|
||||
const result = await manager.compact(messages);
|
||||
|
||||
expect(mockCompact).toHaveBeenCalled();
|
||||
expect(result.freedTokens).toBe(2000);
|
||||
});
|
||||
|
||||
it('无模型时使用简单压缩', async () => {
|
||||
mockSimpleCompact.mockReturnValue({ messages: [], freedTokens: 500 });
|
||||
|
||||
const messages = createMessages(5);
|
||||
const result = await manager.compact(messages);
|
||||
|
||||
expect(mockSimpleCompact).toHaveBeenCalled();
|
||||
expect(result.freedTokens).toBe(500);
|
||||
});
|
||||
});
|
||||
|
||||
describe('compress - 自动压缩', () => {
|
||||
it('先 prune 后不需要 compact', async () => {
|
||||
mockEstimateMessages.mockReturnValue(10000); // 低于阈值
|
||||
mockPrune.mockReturnValue({ messages: [], freedTokens: 500 });
|
||||
|
||||
const messages = createMessages(5);
|
||||
const result = await manager.compress(messages);
|
||||
|
||||
expect(result.type).toBe('prune');
|
||||
expect(mockPrune).toHaveBeenCalled();
|
||||
expect(mockCompact).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('prune 后仍需 compact', async () => {
|
||||
mockEstimateMessages.mockReturnValue(150000); // 高于阈值
|
||||
mockPrune.mockReturnValue({ messages: createMessages(3), freedTokens: 500 });
|
||||
mockSimpleCompact.mockReturnValue({ messages: [], freedTokens: 1000 });
|
||||
|
||||
const messages = createMessages(5);
|
||||
const result = await manager.compress(messages);
|
||||
|
||||
expect(result.type).toBe('both');
|
||||
expect(mockPrune).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('只 compact 时类型为 compaction', async () => {
|
||||
mockEstimateMessages.mockReturnValue(150000);
|
||||
mockPrune.mockReturnValue({ messages: createMessages(5), freedTokens: 0 });
|
||||
mockSimpleCompact.mockReturnValue({ messages: [], freedTokens: 1000 });
|
||||
|
||||
const messages = createMessages(5);
|
||||
const result = await manager.compress(messages);
|
||||
|
||||
expect(result.type).toBe('compaction');
|
||||
});
|
||||
});
|
||||
|
||||
describe('forceCompress - 强制压缩', () => {
|
||||
it('消息太少不压缩', async () => {
|
||||
const messages = createMessages(3);
|
||||
const result = await manager.forceCompress(messages);
|
||||
|
||||
expect(result.messages).toEqual(messages);
|
||||
expect(result.freedTokens).toBe(0);
|
||||
});
|
||||
|
||||
it('足够消息时执行压缩', async () => {
|
||||
mockEstimateMessages.mockReturnValue(10000);
|
||||
mockPrune.mockReturnValue({ messages: createMessages(3), freedTokens: 500 });
|
||||
mockSimpleCompact.mockReturnValue({ messages: createMessages(2), freedTokens: 300 });
|
||||
|
||||
const messages = createMessages(10);
|
||||
const result = await manager.forceCompress(messages);
|
||||
|
||||
expect(mockPrune).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('有模型时尝试 AI 压缩', async () => {
|
||||
const mockModel = {} as LanguageModel;
|
||||
manager.setModel(mockModel);
|
||||
mockEstimateMessages.mockReturnValue(10000);
|
||||
mockPrune.mockReturnValue({ messages: createMessages(5), freedTokens: 500 });
|
||||
mockCompact.mockResolvedValue({ messages: createMessages(2), freedTokens: 1000 });
|
||||
|
||||
const messages = createMessages(10);
|
||||
await manager.forceCompress(messages);
|
||||
|
||||
expect(mockCompact).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('AI 压缩失败时回退到简单压缩', async () => {
|
||||
const mockModel = {} as LanguageModel;
|
||||
manager.setModel(mockModel);
|
||||
mockEstimateMessages.mockReturnValue(10000);
|
||||
mockPrune.mockReturnValue({ messages: createMessages(5), freedTokens: 500 });
|
||||
mockCompact.mockRejectedValue(new Error('AI error'));
|
||||
mockSimpleCompact.mockReturnValue({ messages: createMessages(2), freedTokens: 800 });
|
||||
|
||||
const messages = createMessages(10);
|
||||
const result = await manager.forceCompress(messages);
|
||||
|
||||
expect(mockSimpleCompact).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('filterCompacted - 过滤压缩内容', () => {
|
||||
it('调用 filterCompacted 函数', () => {
|
||||
const messages = createMessages(5);
|
||||
mockFilterCompacted.mockReturnValue(messages.slice(1));
|
||||
|
||||
const result = manager.filterCompacted(messages);
|
||||
|
||||
expect(mockFilterCompacted).toHaveBeenCalledWith(messages);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isSummaryMessage - 检查摘要消息', () => {
|
||||
it('调用 isSummaryMessage 函数', () => {
|
||||
mockIsSummaryMessage.mockReturnValue(true);
|
||||
|
||||
const message: ModelMessage = { role: 'assistant', content: 'summary' };
|
||||
const result = manager.isSummaryMessage(message);
|
||||
|
||||
expect(mockIsSummaryMessage).toHaveBeenCalledWith(message);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatUsage - 格式化使用情况', () => {
|
||||
it('返回格式化字符串', () => {
|
||||
mockEstimateMessages.mockReturnValue(50000);
|
||||
mockFormat.mockImplementation((n) => `${Math.round(n / 1000)}K`);
|
||||
|
||||
const messages = createMessages(5);
|
||||
const result = manager.formatUsage(messages);
|
||||
|
||||
expect(result).toMatch(/\d+K\/\d+K/);
|
||||
expect(result).toContain('%');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,300 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { CompressionManager } from '../../../src/context/manager.js';
|
||||
import { DEFAULT_COMPRESSION_CONFIG, SUMMARY_MARKER } from '../../../src/context/types.js';
|
||||
import type { ModelMessage, LanguageModel } from 'ai';
|
||||
|
||||
// 创建测试用消息
|
||||
function createUserMessage(content: string): ModelMessage {
|
||||
return { role: 'user', content };
|
||||
}
|
||||
|
||||
function createAssistantMessage(content: string): ModelMessage {
|
||||
return { role: 'assistant', content };
|
||||
}
|
||||
|
||||
function createSummaryMessage(summary: string): ModelMessage {
|
||||
return {
|
||||
role: 'assistant',
|
||||
content: `${SUMMARY_MARKER}\n## 对话摘要\n\n${summary}\n${SUMMARY_MARKER}`,
|
||||
};
|
||||
}
|
||||
|
||||
// 创建大量消息以超过阈值
|
||||
function createLargeConversation(count: number): ModelMessage[] {
|
||||
const messages: ModelMessage[] = [];
|
||||
for (let i = 0; i < count; i++) {
|
||||
messages.push(createUserMessage(`Message ${i}: ${'a'.repeat(100)}`));
|
||||
messages.push(createAssistantMessage(`Response ${i}: ${'b'.repeat(100)}`));
|
||||
}
|
||||
return messages;
|
||||
}
|
||||
|
||||
describe('CompressionManager - 压缩管理器', () => {
|
||||
let manager: CompressionManager;
|
||||
|
||||
beforeEach(() => {
|
||||
manager = new CompressionManager();
|
||||
});
|
||||
|
||||
describe('配置管理', () => {
|
||||
it('使用默认配置', () => {
|
||||
const config = manager.getConfig();
|
||||
|
||||
expect(config.contextLimit).toBe(DEFAULT_COMPRESSION_CONFIG.contextLimit);
|
||||
expect(config.outputReserve).toBe(DEFAULT_COMPRESSION_CONFIG.outputReserve);
|
||||
expect(config.overflowThreshold).toBe(DEFAULT_COMPRESSION_CONFIG.overflowThreshold);
|
||||
});
|
||||
|
||||
it('自定义配置覆盖默认值', () => {
|
||||
const customManager = new CompressionManager({
|
||||
contextLimit: 50000,
|
||||
outputReserve: 5000,
|
||||
});
|
||||
|
||||
const config = customManager.getConfig();
|
||||
|
||||
expect(config.contextLimit).toBe(50000);
|
||||
expect(config.outputReserve).toBe(5000);
|
||||
// 未指定的使用默认值
|
||||
expect(config.overflowThreshold).toBe(DEFAULT_COMPRESSION_CONFIG.overflowThreshold);
|
||||
});
|
||||
|
||||
it('updateConfig 更新配置', () => {
|
||||
manager.updateConfig({ pruneProtect: 10000 });
|
||||
|
||||
const config = manager.getConfig();
|
||||
expect(config.pruneProtect).toBe(10000);
|
||||
});
|
||||
});
|
||||
|
||||
describe('calculateUsage - 计算 token 使用情况', () => {
|
||||
it('空消息返回零使用', () => {
|
||||
const usage = manager.calculateUsage([]);
|
||||
|
||||
expect(usage.input).toBe(0);
|
||||
expect(usage.usagePercent).toBe(0);
|
||||
});
|
||||
|
||||
it('计算简单消息的使用量', () => {
|
||||
const messages: ModelMessage[] = [
|
||||
createUserMessage('Hello'),
|
||||
createAssistantMessage('Hi there'),
|
||||
];
|
||||
|
||||
const usage = manager.calculateUsage(messages);
|
||||
|
||||
expect(usage.input).toBeGreaterThan(0);
|
||||
expect(usage.usagePercent).toBeGreaterThan(0);
|
||||
expect(usage.usagePercent).toBeLessThan(100);
|
||||
});
|
||||
|
||||
it('大量消息使用量更高', () => {
|
||||
const smallMessages: ModelMessage[] = [
|
||||
createUserMessage('Hello'),
|
||||
];
|
||||
const largeMessages = createLargeConversation(50);
|
||||
|
||||
const smallUsage = manager.calculateUsage(smallMessages);
|
||||
const largeUsage = manager.calculateUsage(largeMessages);
|
||||
|
||||
expect(largeUsage.input).toBeGreaterThan(smallUsage.input);
|
||||
});
|
||||
|
||||
it('使用量百分比不超过 100', () => {
|
||||
// 创建超大对话
|
||||
const messages = createLargeConversation(500);
|
||||
const usage = manager.calculateUsage(messages);
|
||||
|
||||
expect(usage.usagePercent).toBeLessThanOrEqual(100);
|
||||
});
|
||||
});
|
||||
|
||||
describe('shouldCompress - 判断是否需要压缩', () => {
|
||||
it('小对话不需要压缩', () => {
|
||||
const messages: ModelMessage[] = [
|
||||
createUserMessage('Hello'),
|
||||
createAssistantMessage('Hi'),
|
||||
];
|
||||
|
||||
expect(manager.shouldCompress(messages)).toBe(false);
|
||||
});
|
||||
|
||||
it('大对话可能需要压缩', () => {
|
||||
// 创建足够大的对话以超过阈值
|
||||
const messages = createLargeConversation(200);
|
||||
|
||||
// 取决于配置的阈值
|
||||
const usage = manager.calculateUsage(messages);
|
||||
const threshold = manager.getConfig().overflowThreshold * 100;
|
||||
const shouldCompress = usage.usagePercent >= threshold;
|
||||
|
||||
expect(manager.shouldCompress(messages)).toBe(shouldCompress);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isOverflow - 判断是否溢出', () => {
|
||||
it('小对话不溢出', () => {
|
||||
const messages: ModelMessage[] = [
|
||||
createUserMessage('Hello'),
|
||||
createAssistantMessage('Hi'),
|
||||
];
|
||||
|
||||
expect(manager.isOverflow(messages)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('prune - 执行裁剪', () => {
|
||||
it('空消息不裁剪', () => {
|
||||
const result = manager.prune([]);
|
||||
|
||||
expect(result.messages).toHaveLength(0);
|
||||
expect(result.freedTokens).toBe(0);
|
||||
});
|
||||
|
||||
it('小对话不裁剪', () => {
|
||||
const messages: ModelMessage[] = [
|
||||
createUserMessage('Hello'),
|
||||
createAssistantMessage('Hi'),
|
||||
];
|
||||
|
||||
const result = manager.prune(messages);
|
||||
|
||||
expect(result.messages).toEqual(messages);
|
||||
expect(result.freedTokens).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('compact - 执行压缩', () => {
|
||||
it('无模型时使用简单压缩', async () => {
|
||||
const messages = createLargeConversation(50);
|
||||
|
||||
const result = await manager.compact(messages);
|
||||
|
||||
// 简单压缩会根据配置决定是否压缩
|
||||
expect(result.messages).toBeDefined();
|
||||
expect(result.freedTokens).toBeGreaterThanOrEqual(0);
|
||||
});
|
||||
|
||||
it('设置模型后使用 AI 压缩', async () => {
|
||||
// 创建 mock 模型
|
||||
const mockModel = {
|
||||
doGenerate: vi.fn().mockResolvedValue({
|
||||
text: '这是一个摘要',
|
||||
}),
|
||||
} as unknown as LanguageModel;
|
||||
|
||||
manager.setModel(mockModel);
|
||||
|
||||
const messages = createLargeConversation(50);
|
||||
const result = await manager.compact(messages);
|
||||
|
||||
expect(result.messages).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('compress - 自动压缩', () => {
|
||||
it('小对话不压缩', async () => {
|
||||
const messages: ModelMessage[] = [
|
||||
createUserMessage('Hello'),
|
||||
createAssistantMessage('Hi'),
|
||||
];
|
||||
|
||||
const result = await manager.compress(messages);
|
||||
|
||||
expect(result.messages).toEqual(messages);
|
||||
expect(result.freedTokens).toBe(0);
|
||||
});
|
||||
|
||||
it('返回正确的压缩类型', async () => {
|
||||
const messages: ModelMessage[] = [
|
||||
createUserMessage('Hello'),
|
||||
createAssistantMessage('Hi'),
|
||||
];
|
||||
|
||||
const result = await manager.compress(messages);
|
||||
|
||||
expect(['prune', 'compaction', 'both']).toContain(result.type);
|
||||
});
|
||||
});
|
||||
|
||||
describe('forceCompress - 强制压缩', () => {
|
||||
it('消息数量少于 4 条不压缩', async () => {
|
||||
const messages: ModelMessage[] = [
|
||||
createUserMessage('Hello'),
|
||||
createAssistantMessage('Hi'),
|
||||
];
|
||||
|
||||
const result = await manager.forceCompress(messages);
|
||||
|
||||
expect(result.messages).toEqual(messages);
|
||||
expect(result.freedTokens).toBe(0);
|
||||
});
|
||||
|
||||
it('强制压缩大对话', async () => {
|
||||
const messages = createLargeConversation(20);
|
||||
|
||||
const result = await manager.forceCompress(messages);
|
||||
|
||||
// 强制压缩应该减少消息数量
|
||||
expect(result.messages.length).toBeLessThanOrEqual(messages.length);
|
||||
});
|
||||
});
|
||||
|
||||
describe('filterCompacted - 过滤已压缩内容', () => {
|
||||
it('不修改普通消息', () => {
|
||||
const messages: ModelMessage[] = [
|
||||
createUserMessage('Hello'),
|
||||
createAssistantMessage('Hi'),
|
||||
];
|
||||
|
||||
const result = manager.filterCompacted(messages);
|
||||
|
||||
expect(result).toEqual(messages);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isSummaryMessage - 检测摘要消息', () => {
|
||||
it('检测摘要消息', () => {
|
||||
const summary = createSummaryMessage('这是摘要');
|
||||
|
||||
expect(manager.isSummaryMessage(summary)).toBe(true);
|
||||
});
|
||||
|
||||
it('普通消息不是摘要', () => {
|
||||
const normal = createAssistantMessage('普通回复');
|
||||
|
||||
expect(manager.isSummaryMessage(normal)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatUsage - 格式化使用情况', () => {
|
||||
it('格式化空消息', () => {
|
||||
const formatted = manager.formatUsage([]);
|
||||
|
||||
expect(formatted).toContain('/');
|
||||
expect(formatted).toContain('(');
|
||||
expect(formatted).toContain('%)');
|
||||
});
|
||||
|
||||
it('格式化包含消息的使用情况', () => {
|
||||
const messages: ModelMessage[] = [
|
||||
createUserMessage('Hello'),
|
||||
createAssistantMessage('Hi there'),
|
||||
];
|
||||
|
||||
const formatted = manager.formatUsage(messages);
|
||||
|
||||
expect(typeof formatted).toBe('string');
|
||||
expect(formatted).toContain('/');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('compressionManager 单例', () => {
|
||||
it('导出单例实例', async () => {
|
||||
const { compressionManager } = await import('../../../src/context/manager.js');
|
||||
|
||||
expect(compressionManager).toBeDefined();
|
||||
expect(compressionManager).toBeInstanceOf(CompressionManager);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,307 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { prune, filterCompacted } from '../../../src/context/prune.js';
|
||||
import {
|
||||
COMPACTED_PLACEHOLDER,
|
||||
SUMMARY_MARKER,
|
||||
COMPACTED_MARKER,
|
||||
type CompressionConfig,
|
||||
} from '../../../src/context/types.js';
|
||||
import type { ModelMessage } from 'ai';
|
||||
|
||||
// 创建测试用的工具结果消息
|
||||
function createToolResultMessage(
|
||||
toolCallId: string,
|
||||
result: unknown,
|
||||
compacted = false
|
||||
): ModelMessage {
|
||||
const content: Record<string, unknown> = {
|
||||
type: 'tool-result',
|
||||
toolCallId,
|
||||
toolName: 'test_tool',
|
||||
result,
|
||||
};
|
||||
|
||||
if (compacted) {
|
||||
content[COMPACTED_MARKER] = {
|
||||
compactedAt: Date.now(),
|
||||
originalSize: 100,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
role: 'tool',
|
||||
content: [content],
|
||||
} as ModelMessage;
|
||||
}
|
||||
|
||||
// 创建测试用的工具调用消息
|
||||
function createToolCallMessage(toolCallId: string): ModelMessage {
|
||||
return {
|
||||
role: 'assistant',
|
||||
content: [
|
||||
{
|
||||
type: 'tool-call',
|
||||
toolCallId,
|
||||
toolName: 'test_tool',
|
||||
args: { param: 'value' },
|
||||
},
|
||||
],
|
||||
} as ModelMessage;
|
||||
}
|
||||
|
||||
// 创建用户消息
|
||||
function createUserMessage(content: string): ModelMessage {
|
||||
return { role: 'user', content };
|
||||
}
|
||||
|
||||
// 创建助手消息
|
||||
function createAssistantMessage(content: string): ModelMessage {
|
||||
return { role: 'assistant', content };
|
||||
}
|
||||
|
||||
// 创建摘要消息
|
||||
function createSummaryMessage(): ModelMessage {
|
||||
return {
|
||||
role: 'assistant',
|
||||
content: `${SUMMARY_MARKER}\n## 对话摘要\n\n这是一个摘要\n${SUMMARY_MARKER}`,
|
||||
};
|
||||
}
|
||||
|
||||
// 创建大工具结果(指定 token 数)
|
||||
function createLargeToolResult(toolCallId: string, sizeInChars: number): ModelMessage {
|
||||
// 约 4 字符/token(英文)
|
||||
const content = 'a'.repeat(sizeInChars);
|
||||
return createToolResultMessage(toolCallId, { output: content });
|
||||
}
|
||||
|
||||
describe('prune - 消息裁剪策略', () => {
|
||||
// 测试配置:小值便于测试
|
||||
const testConfig: CompressionConfig = {
|
||||
contextLimit: 1000,
|
||||
outputReserve: 100,
|
||||
pruneProtect: 200, // 保护最近 200 tokens
|
||||
pruneMinimum: 50, // 至少释放 50 tokens 才执行
|
||||
overflowThreshold: 0.85,
|
||||
};
|
||||
|
||||
describe('基本裁剪行为', () => {
|
||||
it('空消息数组不裁剪', () => {
|
||||
const result = prune([], testConfig);
|
||||
|
||||
expect(result.messages).toHaveLength(0);
|
||||
expect(result.freedTokens).toBe(0);
|
||||
});
|
||||
|
||||
it('只有用户消息不裁剪', () => {
|
||||
const messages: ModelMessage[] = [
|
||||
createUserMessage('Hello'),
|
||||
createUserMessage('How are you?'),
|
||||
];
|
||||
|
||||
const result = prune(messages, testConfig);
|
||||
|
||||
expect(result.messages).toEqual(messages);
|
||||
expect(result.freedTokens).toBe(0);
|
||||
});
|
||||
|
||||
it('保护范围内的工具结果不裁剪', () => {
|
||||
// 创建小的工具结果(在保护范围内)
|
||||
const messages: ModelMessage[] = [
|
||||
createUserMessage('Use tool'),
|
||||
createToolCallMessage('call_1'),
|
||||
createToolResultMessage('call_1', { output: 'small result' }),
|
||||
];
|
||||
|
||||
const result = prune(messages, testConfig);
|
||||
|
||||
// 不应该裁剪
|
||||
expect(result.freedTokens).toBe(0);
|
||||
});
|
||||
|
||||
it('裁剪超出保护范围的工具结果', () => {
|
||||
// 创建多个工具结果,超出保护范围
|
||||
const messages: ModelMessage[] = [
|
||||
createUserMessage('Task 1'),
|
||||
createToolCallMessage('call_1'),
|
||||
createLargeToolResult('call_1', 1000), // ~250 tokens, 超出 pruneProtect
|
||||
createUserMessage('Task 2'),
|
||||
createToolCallMessage('call_2'),
|
||||
createLargeToolResult('call_2', 400), // ~100 tokens
|
||||
createUserMessage('Task 3'),
|
||||
createToolCallMessage('call_3'),
|
||||
createToolResultMessage('call_3', { output: 'recent' }), // 最近的,在保护范围内
|
||||
];
|
||||
|
||||
const result = prune(messages, testConfig);
|
||||
|
||||
// 应该裁剪旧的大工具结果
|
||||
expect(result.freedTokens).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('摘要消息边界', () => {
|
||||
it('遇到摘要消息停止裁剪', () => {
|
||||
const messages: ModelMessage[] = [
|
||||
createSummaryMessage(), // 摘要消息
|
||||
createUserMessage('After summary'),
|
||||
createToolCallMessage('call_1'),
|
||||
createLargeToolResult('call_1', 2000), // 大结果
|
||||
];
|
||||
|
||||
const result = prune(messages, testConfig);
|
||||
|
||||
// 因为遇到摘要消息,不会继续向前裁剪
|
||||
// 但是摘要后面的大工具结果如果超出保护范围仍会被裁剪
|
||||
expect(result.messages[0]).toEqual(messages[0]); // 摘要保留
|
||||
});
|
||||
|
||||
it('摘要消息前的内容不裁剪', () => {
|
||||
const messages: ModelMessage[] = [
|
||||
createUserMessage('Before summary'),
|
||||
createToolCallMessage('call_old'),
|
||||
createLargeToolResult('call_old', 2000), // 摘要前的大结果
|
||||
createSummaryMessage(),
|
||||
createUserMessage('After summary'),
|
||||
];
|
||||
|
||||
const result = prune(messages, testConfig);
|
||||
|
||||
// 摘要前的内容因为遇到摘要边界停止
|
||||
expect(result.messages.length).toBe(messages.length);
|
||||
});
|
||||
});
|
||||
|
||||
describe('已压缩内容处理', () => {
|
||||
it('遇到已压缩的工具结果停止', () => {
|
||||
const messages: ModelMessage[] = [
|
||||
createUserMessage('Task 1'),
|
||||
createToolCallMessage('call_1'),
|
||||
createToolResultMessage('call_1', COMPACTED_PLACEHOLDER, true), // 已压缩
|
||||
createUserMessage('Task 2'),
|
||||
createToolCallMessage('call_2'),
|
||||
createLargeToolResult('call_2', 1000), // 新的大结果
|
||||
];
|
||||
|
||||
const result = prune(messages, testConfig);
|
||||
|
||||
// 遇到已压缩的结果应该停止继续向前
|
||||
expect(result.messages).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('最小裁剪量检查', () => {
|
||||
it('释放量不足最小值时不执行裁剪', () => {
|
||||
// 使用较大的 pruneMinimum
|
||||
const strictConfig: CompressionConfig = {
|
||||
...testConfig,
|
||||
pruneMinimum: 10000, // 要求至少释放 10000 tokens
|
||||
};
|
||||
|
||||
const messages: ModelMessage[] = [
|
||||
createUserMessage('Task'),
|
||||
createToolCallMessage('call_1'),
|
||||
createLargeToolResult('call_1', 400), // 只有 ~100 tokens
|
||||
];
|
||||
|
||||
const result = prune(messages, strictConfig);
|
||||
|
||||
expect(result.freedTokens).toBe(0);
|
||||
expect(result.messages).toEqual(messages);
|
||||
});
|
||||
});
|
||||
|
||||
describe('深拷贝验证', () => {
|
||||
it('不修改原消息数组', () => {
|
||||
const messages: ModelMessage[] = [
|
||||
createUserMessage('Task'),
|
||||
createToolCallMessage('call_1'),
|
||||
createLargeToolResult('call_1', 2000),
|
||||
createUserMessage('Recent'),
|
||||
];
|
||||
|
||||
const originalLength = messages.length;
|
||||
const originalFirstMessage = { ...messages[0] };
|
||||
|
||||
prune(messages, testConfig);
|
||||
|
||||
expect(messages.length).toBe(originalLength);
|
||||
expect(messages[0]).toEqual(originalFirstMessage);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('filterCompacted - 过滤已压缩内容', () => {
|
||||
it('不修改普通消息', () => {
|
||||
const messages: ModelMessage[] = [
|
||||
createUserMessage('Hello'),
|
||||
createAssistantMessage('Hi there'),
|
||||
];
|
||||
|
||||
const result = filterCompacted(messages);
|
||||
|
||||
expect(result).toEqual(messages);
|
||||
});
|
||||
|
||||
it('不修改未压缩的工具结果', () => {
|
||||
const messages: ModelMessage[] = [
|
||||
createToolResultMessage('call_1', { output: 'normal result' }),
|
||||
];
|
||||
|
||||
const result = filterCompacted(messages);
|
||||
|
||||
expect(result).toEqual(messages);
|
||||
});
|
||||
|
||||
it('将已压缩工具结果替换为占位符', () => {
|
||||
const messages: ModelMessage[] = [
|
||||
createToolResultMessage('call_1', 'original', true), // 已压缩
|
||||
];
|
||||
|
||||
const result = filterCompacted(messages);
|
||||
|
||||
const content = result[0].content as { result: unknown }[];
|
||||
expect(content[0].result).toBe(COMPACTED_PLACEHOLDER);
|
||||
});
|
||||
|
||||
it('混合内容正确处理', () => {
|
||||
const messages: ModelMessage[] = [
|
||||
createUserMessage('Task'),
|
||||
createToolResultMessage('call_1', 'original', true), // 已压缩
|
||||
createToolResultMessage('call_2', { output: 'normal' }), // 未压缩
|
||||
createAssistantMessage('Done'),
|
||||
];
|
||||
|
||||
const result = filterCompacted(messages);
|
||||
|
||||
expect(result).toHaveLength(4);
|
||||
// 第一个工具结果应该被替换
|
||||
const compactedContent = result[1].content as { result: unknown }[];
|
||||
expect(compactedContent[0].result).toBe(COMPACTED_PLACEHOLDER);
|
||||
// 第二个工具结果应该保持不变
|
||||
const normalContent = result[2].content as { result: unknown }[];
|
||||
expect(normalContent[0].result).toEqual({ output: 'normal' });
|
||||
});
|
||||
|
||||
it('保留消息的其他属性', () => {
|
||||
const messages: ModelMessage[] = [
|
||||
{
|
||||
role: 'tool',
|
||||
content: [
|
||||
{
|
||||
type: 'tool-result',
|
||||
toolCallId: 'call_1',
|
||||
toolName: 'my_tool',
|
||||
result: 'data',
|
||||
[COMPACTED_MARKER]: { compactedAt: 123, originalSize: 100 },
|
||||
},
|
||||
],
|
||||
} as ModelMessage,
|
||||
];
|
||||
|
||||
const result = filterCompacted(messages);
|
||||
|
||||
const content = result[0].content as { toolCallId: string; toolName: string }[];
|
||||
expect(content[0].toolCallId).toBe('call_1');
|
||||
expect(content[0].toolName).toBe('my_tool');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,349 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { TokenCounter } from '../../../src/context/token-counter.js';
|
||||
import type { ModelMessage } from 'ai';
|
||||
|
||||
describe('TokenCounter - Token 计数器', () => {
|
||||
describe('estimateText - 文本估算', () => {
|
||||
it('空文本返回 0', () => {
|
||||
expect(TokenCounter.estimateText('')).toBe(0);
|
||||
expect(TokenCounter.estimateText(null as unknown as string)).toBe(0);
|
||||
expect(TokenCounter.estimateText(undefined as unknown as string)).toBe(0);
|
||||
});
|
||||
|
||||
it('纯英文估算(约 4 字符/token)', () => {
|
||||
// 40 个字符 / 4 = 10 tokens
|
||||
const text = 'This is a test message with some words.';
|
||||
const tokens = TokenCounter.estimateText(text);
|
||||
expect(tokens).toBe(Math.ceil(text.length / 4));
|
||||
});
|
||||
|
||||
it('纯中文估算(约 1.5 字符/token)', () => {
|
||||
// 6 个中文字符 / 1.5 = 4 tokens
|
||||
const text = '这是测试文本';
|
||||
const tokens = TokenCounter.estimateText(text);
|
||||
expect(tokens).toBe(Math.ceil(6 / 1.5));
|
||||
});
|
||||
|
||||
it('中英混合估算', () => {
|
||||
// 中文 4 个 + 英文 10 个
|
||||
// 4/1.5 + 10/4 = 2.67 + 2.5 = 5.17 -> 6
|
||||
const text = '测试test文本text';
|
||||
const tokens = TokenCounter.estimateText(text);
|
||||
|
||||
// 4 个中文: 4/1.5 = 2.67
|
||||
// 8 个其他: 8/4 = 2
|
||||
// 总计: ceil(4.67) = 5
|
||||
expect(tokens).toBeGreaterThan(0);
|
||||
expect(tokens).toBeLessThan(text.length); // 应该小于字符数
|
||||
});
|
||||
|
||||
it('代码片段估算', () => {
|
||||
const code = `function hello() {
|
||||
console.log("Hello World");
|
||||
return true;
|
||||
}`;
|
||||
const tokens = TokenCounter.estimateText(code);
|
||||
expect(tokens).toBeGreaterThan(0);
|
||||
// 代码主要是英文,约 4 字符/token
|
||||
expect(tokens).toBeLessThan(code.length);
|
||||
});
|
||||
|
||||
it('长文本估算', () => {
|
||||
const longText = 'a'.repeat(10000);
|
||||
const tokens = TokenCounter.estimateText(longText);
|
||||
// 10000 / 4 = 2500
|
||||
expect(tokens).toBe(2500);
|
||||
});
|
||||
});
|
||||
|
||||
describe('estimateContent - 内容估算', () => {
|
||||
it('字符串内容', () => {
|
||||
const content = 'Hello World';
|
||||
const tokens = TokenCounter.estimateContent(content);
|
||||
expect(tokens).toBe(TokenCounter.estimateText(content));
|
||||
});
|
||||
|
||||
it('数组内容 - 纯文本部分', () => {
|
||||
const content = ['Hello', 'World'];
|
||||
const tokens = TokenCounter.estimateContent(content);
|
||||
const expected = TokenCounter.estimateText('Hello') + TokenCounter.estimateText('World');
|
||||
expect(tokens).toBe(expected);
|
||||
});
|
||||
|
||||
it('数组内容 - text 对象', () => {
|
||||
const content = [{ type: 'text', text: 'Hello World' }];
|
||||
const tokens = TokenCounter.estimateContent(content);
|
||||
expect(tokens).toBe(TokenCounter.estimateText('Hello World'));
|
||||
});
|
||||
|
||||
it('数组内容 - tool-result', () => {
|
||||
const content = [
|
||||
{
|
||||
type: 'tool-result',
|
||||
toolCallId: 'call_123',
|
||||
toolName: 'read_file',
|
||||
result: { success: true, output: 'file content' },
|
||||
},
|
||||
];
|
||||
const tokens = TokenCounter.estimateContent(content);
|
||||
const expectedText = JSON.stringify({ success: true, output: 'file content' });
|
||||
expect(tokens).toBe(TokenCounter.estimateText(expectedText));
|
||||
});
|
||||
|
||||
it('数组内容 - tool-call', () => {
|
||||
const content = [
|
||||
{
|
||||
type: 'tool-call',
|
||||
toolCallId: 'call_123',
|
||||
toolName: 'read_file',
|
||||
args: { path: '/test.txt' },
|
||||
},
|
||||
];
|
||||
const tokens = TokenCounter.estimateContent(content);
|
||||
const argsText = JSON.stringify({ path: '/test.txt' });
|
||||
// 工具调用增加 20 token 开销
|
||||
expect(tokens).toBe(TokenCounter.estimateText(argsText) + 20);
|
||||
});
|
||||
|
||||
it('混合内容', () => {
|
||||
const content = [
|
||||
{ type: 'text', text: 'Processing file' },
|
||||
{
|
||||
type: 'tool-call',
|
||||
toolCallId: 'call_1',
|
||||
toolName: 'read_file',
|
||||
args: { path: '/a.txt' },
|
||||
},
|
||||
];
|
||||
const tokens = TokenCounter.estimateContent(content);
|
||||
expect(tokens).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('空数组返回 0', () => {
|
||||
expect(TokenCounter.estimateContent([])).toBe(0);
|
||||
});
|
||||
|
||||
it('非字符串非数组返回 0', () => {
|
||||
expect(TokenCounter.estimateContent(null as unknown as string)).toBe(0);
|
||||
expect(TokenCounter.estimateContent(123 as unknown as string)).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('estimateMessage - 单条消息估算', () => {
|
||||
it('用户消息', () => {
|
||||
const message: ModelMessage = {
|
||||
role: 'user',
|
||||
content: 'Hello',
|
||||
};
|
||||
const tokens = TokenCounter.estimateMessage(message);
|
||||
// 4 (角色开销) + 内容 tokens
|
||||
expect(tokens).toBe(4 + TokenCounter.estimateText('Hello'));
|
||||
});
|
||||
|
||||
it('助手消息', () => {
|
||||
const message: ModelMessage = {
|
||||
role: 'assistant',
|
||||
content: 'I can help you with that.',
|
||||
};
|
||||
const tokens = TokenCounter.estimateMessage(message);
|
||||
expect(tokens).toBe(4 + TokenCounter.estimateText('I can help you with that.'));
|
||||
});
|
||||
|
||||
it('系统消息', () => {
|
||||
const message: ModelMessage = {
|
||||
role: 'system',
|
||||
content: 'You are a helpful assistant.',
|
||||
};
|
||||
const tokens = TokenCounter.estimateMessage(message);
|
||||
expect(tokens).toBe(4 + TokenCounter.estimateText('You are a helpful assistant.'));
|
||||
});
|
||||
|
||||
it('工具消息', () => {
|
||||
const message: ModelMessage = {
|
||||
role: 'tool',
|
||||
content: [
|
||||
{
|
||||
type: 'tool-result',
|
||||
toolCallId: 'call_123',
|
||||
toolName: 'bash',
|
||||
result: { success: true, output: 'done' },
|
||||
},
|
||||
],
|
||||
};
|
||||
const tokens = TokenCounter.estimateMessage(message);
|
||||
expect(tokens).toBeGreaterThan(4); // 至少有角色开销
|
||||
});
|
||||
});
|
||||
|
||||
describe('estimateMessages - 消息数组估算', () => {
|
||||
it('空数组返回 0', () => {
|
||||
expect(TokenCounter.estimateMessages([])).toBe(0);
|
||||
});
|
||||
|
||||
it('单条消息', () => {
|
||||
const messages: ModelMessage[] = [{ role: 'user', content: 'Hello' }];
|
||||
const tokens = TokenCounter.estimateMessages(messages);
|
||||
// 消息 tokens + 3 (分隔开销)
|
||||
expect(tokens).toBe(TokenCounter.estimateMessage(messages[0]) + 3);
|
||||
});
|
||||
|
||||
it('多条消息', () => {
|
||||
const messages: ModelMessage[] = [
|
||||
{ role: 'user', content: 'Hello' },
|
||||
{ role: 'assistant', content: 'Hi there!' },
|
||||
{ role: 'user', content: 'How are you?' },
|
||||
];
|
||||
const tokens = TokenCounter.estimateMessages(messages);
|
||||
|
||||
const msgTokens = messages.reduce((sum, m) => sum + TokenCounter.estimateMessage(m), 0);
|
||||
const separatorTokens = messages.length * 3;
|
||||
|
||||
expect(tokens).toBe(msgTokens + separatorTokens);
|
||||
});
|
||||
|
||||
it('包含工具调用的对话', () => {
|
||||
const messages: ModelMessage[] = [
|
||||
{ role: 'user', content: '读取 /tmp/test.txt' },
|
||||
{
|
||||
role: 'assistant',
|
||||
content: [
|
||||
{
|
||||
type: 'tool-call',
|
||||
toolCallId: 'call_1',
|
||||
toolName: 'read_file',
|
||||
args: { path: '/tmp/test.txt' },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
role: 'tool',
|
||||
content: [
|
||||
{
|
||||
type: 'tool-result',
|
||||
toolCallId: 'call_1',
|
||||
toolName: 'read_file',
|
||||
result: { success: true, output: 'file content here' },
|
||||
},
|
||||
],
|
||||
},
|
||||
{ role: 'assistant', content: '文件内容是: file content here' },
|
||||
];
|
||||
|
||||
const tokens = TokenCounter.estimateMessages(messages);
|
||||
expect(tokens).toBeGreaterThan(0);
|
||||
// 应该能处理复杂的消息结构
|
||||
});
|
||||
});
|
||||
|
||||
describe('format - 格式化显示', () => {
|
||||
it('小于 1000 显示原数', () => {
|
||||
expect(TokenCounter.format(0)).toBe('0');
|
||||
expect(TokenCounter.format(100)).toBe('100');
|
||||
expect(TokenCounter.format(999)).toBe('999');
|
||||
});
|
||||
|
||||
it('大于等于 1000 显示 k 单位', () => {
|
||||
expect(TokenCounter.format(1000)).toBe('1.0k');
|
||||
expect(TokenCounter.format(1500)).toBe('1.5k');
|
||||
expect(TokenCounter.format(10000)).toBe('10.0k');
|
||||
expect(TokenCounter.format(100000)).toBe('100.0k');
|
||||
});
|
||||
|
||||
it('小数精度', () => {
|
||||
expect(TokenCounter.format(1234)).toBe('1.2k');
|
||||
expect(TokenCounter.format(1250)).toBe('1.3k'); // 四舍五入
|
||||
expect(TokenCounter.format(12345)).toBe('12.3k');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('TokenCounter 实际场景测试', () => {
|
||||
it('典型对话 token 估算', () => {
|
||||
const messages: ModelMessage[] = [
|
||||
{
|
||||
role: 'system',
|
||||
content:
|
||||
'You are a helpful coding assistant. Help users with programming tasks.',
|
||||
},
|
||||
{
|
||||
role: 'user',
|
||||
content: '请帮我写一个 Python 函数来计算斐波那契数列',
|
||||
},
|
||||
{
|
||||
role: 'assistant',
|
||||
content: `好的,这是一个计算斐波那契数列的 Python 函数:
|
||||
|
||||
\`\`\`python
|
||||
def fibonacci(n):
|
||||
if n <= 0:
|
||||
return []
|
||||
elif n == 1:
|
||||
return [0]
|
||||
elif n == 2:
|
||||
return [0, 1]
|
||||
|
||||
fib = [0, 1]
|
||||
for i in range(2, n):
|
||||
fib.append(fib[i-1] + fib[i-2])
|
||||
return fib
|
||||
\`\`\``,
|
||||
},
|
||||
];
|
||||
|
||||
const tokens = TokenCounter.estimateMessages(messages);
|
||||
expect(tokens).toBeGreaterThan(100); // 应该有一定数量的 tokens
|
||||
expect(tokens).toBeLessThan(1000); // 但不会太多
|
||||
});
|
||||
|
||||
it('大量工具调用的 token 估算', () => {
|
||||
const messages: ModelMessage[] = [];
|
||||
|
||||
// 模拟 10 轮工具调用
|
||||
for (let i = 0; i < 10; i++) {
|
||||
messages.push({
|
||||
role: 'assistant',
|
||||
content: [
|
||||
{
|
||||
type: 'tool-call',
|
||||
toolCallId: `call_${i}`,
|
||||
toolName: 'bash',
|
||||
args: { command: `echo "iteration ${i}"` },
|
||||
},
|
||||
],
|
||||
});
|
||||
messages.push({
|
||||
role: 'tool',
|
||||
content: [
|
||||
{
|
||||
type: 'tool-result',
|
||||
toolCallId: `call_${i}`,
|
||||
toolName: 'bash',
|
||||
result: { success: true, output: `iteration ${i}` },
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
const tokens = TokenCounter.estimateMessages(messages);
|
||||
expect(tokens).toBeGreaterThan(0);
|
||||
// 20 条消息应该有合理的 token 数
|
||||
expect(TokenCounter.format(tokens)).toBeDefined();
|
||||
});
|
||||
|
||||
it('上下文窗口占用估算', () => {
|
||||
// 模拟 200k token 上下文窗口
|
||||
const maxContextTokens = 200000;
|
||||
|
||||
// 创建一个大消息
|
||||
const largeContent = 'a'.repeat(40000); // 约 10k tokens
|
||||
const messages: ModelMessage[] = [
|
||||
{ role: 'user', content: largeContent },
|
||||
];
|
||||
|
||||
const tokens = TokenCounter.estimateMessages(messages);
|
||||
const usagePercent = (tokens / maxContextTokens) * 100;
|
||||
|
||||
expect(usagePercent).toBeLessThan(10); // 应该占用不到 10%
|
||||
expect(usagePercent).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,298 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import type { Tool } from '../../../src/types/index.js';
|
||||
import type { AgentInfo, AgentToolConfig } from '../../../src/agent/types.js';
|
||||
|
||||
/**
|
||||
* 模拟 Agent 类中的 filterToolsByAgentConfig 逻辑
|
||||
* 由于 Agent 类有复杂的依赖,我们提取核心过滤逻辑进行测试
|
||||
*/
|
||||
function filterToolsByAgentConfig(
|
||||
tools: Tool[],
|
||||
toolConfig: AgentToolConfig | undefined
|
||||
): Tool[] {
|
||||
if (!toolConfig) return tools;
|
||||
|
||||
let filteredTools = tools;
|
||||
|
||||
// 如果设置了 enabled 列表,只保留这些工具
|
||||
if (toolConfig.enabled && toolConfig.enabled.length > 0) {
|
||||
const enabledSet = new Set(toolConfig.enabled);
|
||||
filteredTools = filteredTools.filter((t) => enabledSet.has(t.name));
|
||||
}
|
||||
|
||||
// 如果设置了 disabled 列表,排除这些工具
|
||||
if (toolConfig.disabled && toolConfig.disabled.length > 0) {
|
||||
const disabledSet = new Set(toolConfig.disabled);
|
||||
filteredTools = filteredTools.filter((t) => !disabledSet.has(t.name));
|
||||
}
|
||||
|
||||
// 如果禁止嵌套 Task,移除 task 工具
|
||||
if (toolConfig.noTask) {
|
||||
filteredTools = filteredTools.filter((t) => t.name !== 'task');
|
||||
}
|
||||
|
||||
return filteredTools;
|
||||
}
|
||||
|
||||
// 创建测试用的 mock 工具
|
||||
function createMockTool(name: string): Tool {
|
||||
return {
|
||||
name,
|
||||
description: `Mock tool: ${name}`,
|
||||
parameters: { type: 'object', properties: {}, required: [] },
|
||||
execute: async () => ({ success: true, output: 'mock' }),
|
||||
};
|
||||
}
|
||||
|
||||
describe('Agent 工具过滤 - filterToolsByAgentConfig', () => {
|
||||
let allTools: Tool[];
|
||||
|
||||
beforeEach(() => {
|
||||
// 创建一组测试工具
|
||||
allTools = [
|
||||
createMockTool('read_file'),
|
||||
createMockTool('write_file'),
|
||||
createMockTool('bash'),
|
||||
createMockTool('task'),
|
||||
createMockTool('tool_search'),
|
||||
createMockTool('glob'),
|
||||
createMockTool('grep'),
|
||||
];
|
||||
});
|
||||
|
||||
describe('无过滤配置', () => {
|
||||
it('toolConfig 为 undefined 时返回所有工具', () => {
|
||||
const result = filterToolsByAgentConfig(allTools, undefined);
|
||||
expect(result).toHaveLength(allTools.length);
|
||||
expect(result).toEqual(allTools);
|
||||
});
|
||||
|
||||
it('toolConfig 为空对象时返回所有工具', () => {
|
||||
const result = filterToolsByAgentConfig(allTools, {});
|
||||
expect(result).toHaveLength(allTools.length);
|
||||
});
|
||||
});
|
||||
|
||||
describe('enabled 白名单过滤', () => {
|
||||
it('只保留 enabled 列表中的工具', () => {
|
||||
const config: AgentToolConfig = {
|
||||
enabled: ['read_file', 'glob', 'grep'],
|
||||
};
|
||||
const result = filterToolsByAgentConfig(allTools, config);
|
||||
|
||||
expect(result).toHaveLength(3);
|
||||
expect(result.map((t) => t.name)).toEqual(['read_file', 'glob', 'grep']);
|
||||
});
|
||||
|
||||
it('enabled 为空数组时返回空列表', () => {
|
||||
const config: AgentToolConfig = {
|
||||
enabled: [],
|
||||
};
|
||||
const result = filterToolsByAgentConfig(allTools, config);
|
||||
expect(result).toHaveLength(allTools.length); // 空数组不触发过滤
|
||||
});
|
||||
|
||||
it('enabled 中不存在的工具被忽略', () => {
|
||||
const config: AgentToolConfig = {
|
||||
enabled: ['read_file', 'non_existent_tool'],
|
||||
};
|
||||
const result = filterToolsByAgentConfig(allTools, config);
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0].name).toBe('read_file');
|
||||
});
|
||||
});
|
||||
|
||||
describe('disabled 黑名单过滤', () => {
|
||||
it('排除 disabled 列表中的工具', () => {
|
||||
const config: AgentToolConfig = {
|
||||
disabled: ['bash', 'write_file'],
|
||||
};
|
||||
const result = filterToolsByAgentConfig(allTools, config);
|
||||
|
||||
expect(result).toHaveLength(5);
|
||||
expect(result.map((t) => t.name)).not.toContain('bash');
|
||||
expect(result.map((t) => t.name)).not.toContain('write_file');
|
||||
});
|
||||
|
||||
it('disabled 为空数组时返回所有工具', () => {
|
||||
const config: AgentToolConfig = {
|
||||
disabled: [],
|
||||
};
|
||||
const result = filterToolsByAgentConfig(allTools, config);
|
||||
expect(result).toHaveLength(allTools.length);
|
||||
});
|
||||
|
||||
it('disabled 中不存在的工具被忽略', () => {
|
||||
const config: AgentToolConfig = {
|
||||
disabled: ['non_existent_tool'],
|
||||
};
|
||||
const result = filterToolsByAgentConfig(allTools, config);
|
||||
expect(result).toHaveLength(allTools.length);
|
||||
});
|
||||
});
|
||||
|
||||
describe('noTask 过滤', () => {
|
||||
it('noTask=true 时移除 task 工具', () => {
|
||||
const config: AgentToolConfig = {
|
||||
noTask: true,
|
||||
};
|
||||
const result = filterToolsByAgentConfig(allTools, config);
|
||||
|
||||
expect(result).toHaveLength(6);
|
||||
expect(result.map((t) => t.name)).not.toContain('task');
|
||||
});
|
||||
|
||||
it('noTask=false 时保留 task 工具', () => {
|
||||
const config: AgentToolConfig = {
|
||||
noTask: false,
|
||||
};
|
||||
const result = filterToolsByAgentConfig(allTools, config);
|
||||
|
||||
expect(result).toHaveLength(allTools.length);
|
||||
expect(result.map((t) => t.name)).toContain('task');
|
||||
});
|
||||
|
||||
it('noTask 未设置时保留 task 工具', () => {
|
||||
const config: AgentToolConfig = {};
|
||||
const result = filterToolsByAgentConfig(allTools, config);
|
||||
|
||||
expect(result.map((t) => t.name)).toContain('task');
|
||||
});
|
||||
});
|
||||
|
||||
describe('组合过滤', () => {
|
||||
it('enabled + noTask 组合', () => {
|
||||
const config: AgentToolConfig = {
|
||||
enabled: ['read_file', 'task', 'glob'],
|
||||
noTask: true,
|
||||
};
|
||||
const result = filterToolsByAgentConfig(allTools, config);
|
||||
|
||||
// enabled 先过滤为 [read_file, task, glob]
|
||||
// noTask 再移除 task
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result.map((t) => t.name)).toEqual(['read_file', 'glob']);
|
||||
});
|
||||
|
||||
it('disabled + noTask 组合', () => {
|
||||
const config: AgentToolConfig = {
|
||||
disabled: ['bash'],
|
||||
noTask: true,
|
||||
};
|
||||
const result = filterToolsByAgentConfig(allTools, config);
|
||||
|
||||
// 原始 7 个工具,移除 bash 和 task
|
||||
expect(result).toHaveLength(5);
|
||||
expect(result.map((t) => t.name)).not.toContain('bash');
|
||||
expect(result.map((t) => t.name)).not.toContain('task');
|
||||
});
|
||||
|
||||
it('enabled + disabled 组合(enabled 优先)', () => {
|
||||
const config: AgentToolConfig = {
|
||||
enabled: ['read_file', 'bash', 'glob'],
|
||||
disabled: ['bash'], // bash 在 enabled 中,也在 disabled 中
|
||||
};
|
||||
const result = filterToolsByAgentConfig(allTools, config);
|
||||
|
||||
// enabled 先过滤为 [read_file, bash, glob]
|
||||
// disabled 再移除 bash
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result.map((t) => t.name)).toEqual(['read_file', 'glob']);
|
||||
});
|
||||
|
||||
it('enabled + disabled + noTask 全部组合', () => {
|
||||
const config: AgentToolConfig = {
|
||||
enabled: ['read_file', 'write_file', 'task', 'glob'],
|
||||
disabled: ['write_file'],
|
||||
noTask: true,
|
||||
};
|
||||
const result = filterToolsByAgentConfig(allTools, config);
|
||||
|
||||
// enabled: [read_file, write_file, task, glob]
|
||||
// disabled: 移除 write_file -> [read_file, task, glob]
|
||||
// noTask: 移除 task -> [read_file, glob]
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result.map((t) => t.name)).toEqual(['read_file', 'glob']);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('AgentInfo 工具配置集成', () => {
|
||||
it('explore agent 典型配置:只读工具', () => {
|
||||
const exploreAgent: AgentInfo = {
|
||||
name: 'explore',
|
||||
description: '代码探索 Agent',
|
||||
mode: 'subagent',
|
||||
tools: {
|
||||
enabled: ['read_file', 'glob', 'grep', 'tool_search'],
|
||||
noTask: true,
|
||||
},
|
||||
};
|
||||
|
||||
const allTools = [
|
||||
createMockTool('read_file'),
|
||||
createMockTool('write_file'),
|
||||
createMockTool('bash'),
|
||||
createMockTool('task'),
|
||||
createMockTool('glob'),
|
||||
createMockTool('grep'),
|
||||
createMockTool('tool_search'),
|
||||
];
|
||||
|
||||
const result = filterToolsByAgentConfig(allTools, exploreAgent.tools);
|
||||
|
||||
expect(result).toHaveLength(4);
|
||||
expect(result.map((t) => t.name).sort()).toEqual(['glob', 'grep', 'read_file', 'tool_search']);
|
||||
});
|
||||
|
||||
it('code-reviewer agent 典型配置:禁用写操作', () => {
|
||||
const reviewerAgent: AgentInfo = {
|
||||
name: 'code-reviewer',
|
||||
description: '代码审查 Agent',
|
||||
mode: 'subagent',
|
||||
tools: {
|
||||
disabled: ['write_file', 'bash'],
|
||||
noTask: true,
|
||||
},
|
||||
};
|
||||
|
||||
const allTools = [
|
||||
createMockTool('read_file'),
|
||||
createMockTool('write_file'),
|
||||
createMockTool('bash'),
|
||||
createMockTool('task'),
|
||||
createMockTool('glob'),
|
||||
createMockTool('grep'),
|
||||
];
|
||||
|
||||
const result = filterToolsByAgentConfig(allTools, reviewerAgent.tools);
|
||||
|
||||
// 移除 write_file, bash, task
|
||||
expect(result).toHaveLength(3);
|
||||
expect(result.map((t) => t.name).sort()).toEqual(['glob', 'grep', 'read_file']);
|
||||
});
|
||||
|
||||
it('build agent 典型配置:允许嵌套 Task', () => {
|
||||
const buildAgent: AgentInfo = {
|
||||
name: 'build',
|
||||
description: '构建 Agent',
|
||||
mode: 'primary',
|
||||
tools: {
|
||||
noTask: false, // 明确允许 task
|
||||
},
|
||||
};
|
||||
|
||||
const allTools = [
|
||||
createMockTool('read_file'),
|
||||
createMockTool('write_file'),
|
||||
createMockTool('bash'),
|
||||
createMockTool('task'),
|
||||
];
|
||||
|
||||
const result = filterToolsByAgentConfig(allTools, buildAgent.tools);
|
||||
|
||||
expect(result).toHaveLength(4);
|
||||
expect(result.map((t) => t.name)).toContain('task');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,411 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { Agent } from '../../../src/core/agent.js';
|
||||
import { ToolRegistry } from '../../../src/tools/registry.js';
|
||||
import { SessionManager } from '../../../src/session/index.js';
|
||||
import type { AgentConfig, Tool } from '../../../src/types/index.js';
|
||||
import type { AgentInfo } from '../../../src/agent/types.js';
|
||||
|
||||
// Mock providers
|
||||
vi.mock('../../../src/core/providers.js', () => ({
|
||||
getModelFactory: vi.fn(() => {
|
||||
return (model: string) => ({
|
||||
modelId: `mock:${model}`,
|
||||
doGenerate: vi.fn(),
|
||||
doStream: vi.fn(),
|
||||
});
|
||||
}),
|
||||
}));
|
||||
|
||||
// Mock AI SDK
|
||||
vi.mock('ai', () => ({
|
||||
generateText: vi.fn().mockResolvedValue({
|
||||
text: 'Mock response',
|
||||
response: { messages: [] },
|
||||
steps: [],
|
||||
}),
|
||||
streamText: vi.fn(() => ({
|
||||
textStream: (async function* () {
|
||||
yield 'Mock ';
|
||||
yield 'streamed ';
|
||||
yield 'response';
|
||||
})(),
|
||||
response: Promise.resolve({ messages: [] }),
|
||||
})),
|
||||
stepCountIs: vi.fn(() => () => false),
|
||||
}));
|
||||
|
||||
// Mock agent registry and executor
|
||||
vi.mock('../../../src/agent/index.js', () => ({
|
||||
agentRegistry: {
|
||||
get: vi.fn(),
|
||||
},
|
||||
AgentExecutor: vi.fn().mockImplementation(() => ({
|
||||
execute: vi.fn().mockResolvedValue({
|
||||
success: true,
|
||||
text: 'Vision analysis result',
|
||||
steps: 1,
|
||||
sessionId: 'test',
|
||||
}),
|
||||
})),
|
||||
}));
|
||||
|
||||
// Mock vision config
|
||||
vi.mock('../../../src/utils/config.js', () => ({
|
||||
loadVisionConfig: vi.fn(() => null),
|
||||
}));
|
||||
|
||||
// Create mock tool
|
||||
function createMockTool(name: string, category = 'core'): Tool & { metadata?: { deferLoading?: boolean } } {
|
||||
return {
|
||||
name,
|
||||
description: `Mock ${name} tool`,
|
||||
parameters: { type: 'object', properties: {}, required: [] },
|
||||
execute: vi.fn().mockResolvedValue({ success: true, output: `${name} result` }),
|
||||
metadata: { deferLoading: category !== 'core' },
|
||||
};
|
||||
}
|
||||
|
||||
// Default test config
|
||||
const testConfig: AgentConfig = {
|
||||
provider: 'anthropic',
|
||||
apiKey: 'test-api-key',
|
||||
model: 'claude-3-sonnet',
|
||||
systemPrompt: 'You are a helpful assistant.',
|
||||
maxTokens: 4096,
|
||||
};
|
||||
|
||||
describe('Agent', () => {
|
||||
let agent: Agent;
|
||||
let registry: ToolRegistry;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
registry = new ToolRegistry();
|
||||
|
||||
// Register some tools
|
||||
registry.register(createMockTool('tool_search', 'core') as any);
|
||||
registry.register(createMockTool('bash', 'core') as any);
|
||||
registry.register(createMockTool('read_file', 'filesystem') as any);
|
||||
registry.register(createMockTool('write_file', 'filesystem') as any);
|
||||
|
||||
agent = new Agent(testConfig);
|
||||
agent.setRegistry(registry);
|
||||
});
|
||||
|
||||
describe('constructor', () => {
|
||||
it('初始化 Agent 配置', () => {
|
||||
const config = agent.getConfig();
|
||||
expect(config.provider).toBe('anthropic');
|
||||
expect(config.model).toBe('claude-3-sonnet');
|
||||
expect(config.systemPrompt).toBe('You are a helpful assistant.');
|
||||
});
|
||||
|
||||
it('支持自定义压缩配置', () => {
|
||||
const agentWithCompression = new Agent(testConfig, {
|
||||
maxContextTokens: 50000,
|
||||
compressionThreshold: 0.7,
|
||||
});
|
||||
|
||||
expect(agentWithCompression.getCompressionManager()).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('setRegistry', () => {
|
||||
it('设置工具注册表', () => {
|
||||
const newAgent = new Agent(testConfig);
|
||||
expect(newAgent.getToolCount()).toEqual({ core: 0, discovered: 0, total: 0 });
|
||||
|
||||
newAgent.setRegistry(registry);
|
||||
const counts = newAgent.getToolCount();
|
||||
expect(counts.core).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('setSessionManager', () => {
|
||||
it('设置会话管理器', async () => {
|
||||
// Mock SessionManager
|
||||
const mockSessionManager = {
|
||||
getSession: vi.fn().mockReturnValue(null),
|
||||
setMessages: vi.fn(),
|
||||
setDiscoveredTools: vi.fn(),
|
||||
newSession: vi.fn(),
|
||||
} as unknown as SessionManager;
|
||||
|
||||
agent.setSessionManager(mockSessionManager);
|
||||
expect(agent.getSessionManager()).toBe(mockSessionManager);
|
||||
});
|
||||
|
||||
it('从会话恢复状态', async () => {
|
||||
// Mock SessionManager with existing session
|
||||
const mockSessionManager = {
|
||||
getSession: vi.fn().mockReturnValue({
|
||||
id: 'test-session',
|
||||
messages: [{ role: 'user', content: 'Hello' }],
|
||||
discoveredTools: ['read_file', 'write_file'],
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
}),
|
||||
setMessages: vi.fn(),
|
||||
setDiscoveredTools: vi.fn(),
|
||||
newSession: vi.fn(),
|
||||
} as unknown as SessionManager;
|
||||
|
||||
agent.setSessionManager(mockSessionManager);
|
||||
|
||||
const counts = agent.getToolCount();
|
||||
expect(counts.discovered).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getToolCount', () => {
|
||||
it('返回工具数量统计', () => {
|
||||
const counts = agent.getToolCount();
|
||||
expect(counts).toHaveProperty('core');
|
||||
expect(counts).toHaveProperty('discovered');
|
||||
expect(counts).toHaveProperty('total');
|
||||
expect(counts.total).toBe(counts.core + counts.discovered);
|
||||
});
|
||||
|
||||
it('registry 未设置时返回 0', () => {
|
||||
const newAgent = new Agent(testConfig);
|
||||
const counts = newAgent.getToolCount();
|
||||
expect(counts).toEqual({ core: 0, discovered: 0, total: 0 });
|
||||
});
|
||||
});
|
||||
|
||||
describe('getHistory', () => {
|
||||
it('初始历史为空', () => {
|
||||
expect(agent.getHistory()).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('clearHistory', () => {
|
||||
it('清空对话历史和发现的工具', async () => {
|
||||
// Add some history by calling chat (mocked)
|
||||
await agent.chat('Hello');
|
||||
|
||||
await agent.clearHistory();
|
||||
expect(agent.getHistory()).toEqual([]);
|
||||
expect(agent.getToolCount().discovered).toBe(0);
|
||||
});
|
||||
|
||||
it('有会话管理器时创建新会话', async () => {
|
||||
const mockSessionManager = {
|
||||
getSession: vi.fn().mockReturnValue(null),
|
||||
setMessages: vi.fn(),
|
||||
setDiscoveredTools: vi.fn(),
|
||||
newSession: vi.fn().mockResolvedValue(undefined),
|
||||
} as unknown as SessionManager;
|
||||
|
||||
agent.setSessionManager(mockSessionManager);
|
||||
await agent.clearHistory();
|
||||
|
||||
expect(mockSessionManager.newSession).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Agent Mode', () => {
|
||||
const exploreAgent: AgentInfo = {
|
||||
name: 'explore',
|
||||
description: '代码探索 Agent',
|
||||
mode: 'subagent',
|
||||
prompt: 'You are an explore agent.',
|
||||
tools: {
|
||||
enabled: ['read_file', 'tool_search'],
|
||||
noTask: true,
|
||||
},
|
||||
};
|
||||
|
||||
it('设置 Agent 模式', () => {
|
||||
agent.setAgentMode(exploreAgent);
|
||||
expect(agent.getAgentMode()).toBe(exploreAgent);
|
||||
expect(agent.getAgentModeName()).toBe('explore');
|
||||
});
|
||||
|
||||
it('切换 Agent 时更新 system prompt', () => {
|
||||
agent.setAgentMode(exploreAgent);
|
||||
expect(agent.getConfig().systemPrompt).toBe('You are an explore agent.');
|
||||
});
|
||||
|
||||
it('切换回 default 时恢复原始 prompt', () => {
|
||||
agent.setAgentMode(exploreAgent);
|
||||
agent.setAgentMode(null);
|
||||
|
||||
expect(agent.getAgentModeName()).toBe('default');
|
||||
expect(agent.getConfig().systemPrompt).toBe('You are a helpful assistant.');
|
||||
});
|
||||
|
||||
it('Agent 没有自定义 prompt 时保持原始 prompt', () => {
|
||||
const agentWithoutPrompt: AgentInfo = {
|
||||
name: 'simple',
|
||||
description: 'Simple agent',
|
||||
mode: 'subagent',
|
||||
};
|
||||
|
||||
agent.setAgentMode(agentWithoutPrompt);
|
||||
expect(agent.getConfig().systemPrompt).toBe('You are a helpful assistant.');
|
||||
});
|
||||
});
|
||||
|
||||
describe('supportsVision', () => {
|
||||
it('Anthropic Claude-3 支持 vision', () => {
|
||||
const claudeAgent = new Agent({
|
||||
...testConfig,
|
||||
provider: 'anthropic',
|
||||
model: 'claude-3-opus',
|
||||
});
|
||||
expect(claudeAgent.supportsVision()).toBe(true);
|
||||
});
|
||||
|
||||
it('Anthropic Claude-4 支持 vision', () => {
|
||||
const claudeAgent = new Agent({
|
||||
...testConfig,
|
||||
provider: 'anthropic',
|
||||
model: 'claude-4-sonnet',
|
||||
});
|
||||
expect(claudeAgent.supportsVision()).toBe(true);
|
||||
});
|
||||
|
||||
it('OpenAI GPT-4 支持 vision', () => {
|
||||
const gptAgent = new Agent({
|
||||
...testConfig,
|
||||
provider: 'openai',
|
||||
model: 'gpt-4-turbo',
|
||||
});
|
||||
expect(gptAgent.supportsVision()).toBe(true);
|
||||
});
|
||||
|
||||
it('DeepSeek 不支持 vision', () => {
|
||||
const deepseekAgent = new Agent({
|
||||
...testConfig,
|
||||
provider: 'deepseek',
|
||||
model: 'deepseek-chat',
|
||||
});
|
||||
expect(deepseekAgent.supportsVision()).toBe(false);
|
||||
});
|
||||
|
||||
it('旧版 Claude 不支持 vision', () => {
|
||||
const oldClaudeAgent = new Agent({
|
||||
...testConfig,
|
||||
provider: 'anthropic',
|
||||
model: 'claude-2',
|
||||
});
|
||||
expect(oldClaudeAgent.supportsVision()).toBe(false);
|
||||
});
|
||||
|
||||
it('GPT-3.5 不支持 vision', () => {
|
||||
const gpt35Agent = new Agent({
|
||||
...testConfig,
|
||||
provider: 'openai',
|
||||
model: 'gpt-3.5-turbo',
|
||||
});
|
||||
expect(gpt35Agent.supportsVision()).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getContextUsage', () => {
|
||||
it('返回 token 使用情况', () => {
|
||||
const usage = agent.getContextUsage();
|
||||
expect(usage).toHaveProperty('input');
|
||||
expect(usage).toHaveProperty('contextLimit');
|
||||
expect(usage).toHaveProperty('available');
|
||||
expect(usage).toHaveProperty('usagePercent');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getContextUsageFormatted', () => {
|
||||
it('返回格式化的使用情况字符串', () => {
|
||||
const formatted = agent.getContextUsageFormatted();
|
||||
expect(typeof formatted).toBe('string');
|
||||
expect(formatted).toContain('k');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getCompressionManager', () => {
|
||||
it('返回压缩管理器实例', () => {
|
||||
const manager = agent.getCompressionManager();
|
||||
expect(manager).toBeDefined();
|
||||
expect(manager).toHaveProperty('compress');
|
||||
expect(manager).toHaveProperty('forceCompress');
|
||||
});
|
||||
});
|
||||
|
||||
describe('compactHistory', () => {
|
||||
it('手动压缩对话历史', async () => {
|
||||
const result = await agent.compactHistory();
|
||||
expect(result).toHaveProperty('freedTokens');
|
||||
expect(result).toHaveProperty('type');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getConfig', () => {
|
||||
it('返回配置副本', () => {
|
||||
const config = agent.getConfig();
|
||||
expect(config).toEqual(testConfig);
|
||||
|
||||
// 修改返回值不影响原始配置
|
||||
config.model = 'modified';
|
||||
expect(agent.getConfig().model).toBe('claude-3-sonnet');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Agent - getAvailableTools error handling', () => {
|
||||
it('registry 未设置时抛出错误', () => {
|
||||
const agent = new Agent(testConfig);
|
||||
|
||||
// 使用 getToolCount 间接测试(因为 getAvailableTools 是 private)
|
||||
// 当 registry 为 null 时,getToolCount 返回 0
|
||||
const counts = agent.getToolCount();
|
||||
expect(counts.total).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Agent - chat with images', () => {
|
||||
let agent: Agent;
|
||||
let registry: ToolRegistry;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
registry = new ToolRegistry();
|
||||
registry.register(createMockTool('tool_search', 'core') as any);
|
||||
|
||||
agent = new Agent(testConfig);
|
||||
agent.setRegistry(registry);
|
||||
});
|
||||
|
||||
it('支持 vision 的模型直接处理图片', async () => {
|
||||
const visionAgent = new Agent({
|
||||
...testConfig,
|
||||
model: 'claude-3-opus',
|
||||
});
|
||||
visionAgent.setRegistry(registry);
|
||||
|
||||
const result = await visionAgent.chat({
|
||||
text: 'What is in this image?',
|
||||
images: [
|
||||
{ data: 'base64data', mimeType: 'image/png' },
|
||||
],
|
||||
});
|
||||
|
||||
expect(result).toBeDefined();
|
||||
});
|
||||
|
||||
it('不支持 vision 时返回错误消息(Vision 未配置)', async () => {
|
||||
const deepseekAgent = new Agent({
|
||||
...testConfig,
|
||||
provider: 'deepseek',
|
||||
model: 'deepseek-chat',
|
||||
});
|
||||
deepseekAgent.setRegistry(registry);
|
||||
|
||||
const result = await deepseekAgent.chat({
|
||||
text: 'What is in this image?',
|
||||
images: [
|
||||
{ data: 'base64data', mimeType: 'image/png' },
|
||||
],
|
||||
});
|
||||
|
||||
expect(result).toContain('无法处理图片');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,264 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { getModelFactory, providers } from '../../../src/core/providers.js';
|
||||
|
||||
// Mock AI SDK providers
|
||||
vi.mock('@ai-sdk/anthropic', () => ({
|
||||
createAnthropic: vi.fn(() => {
|
||||
const modelFn = (model: string) => ({ modelId: `anthropic:${model}` });
|
||||
return modelFn;
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock('@ai-sdk/deepseek', () => ({
|
||||
createDeepSeek: vi.fn(() => {
|
||||
const modelFn = (model: string) => ({ modelId: `deepseek:${model}` });
|
||||
return modelFn;
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock('@ai-sdk/openai', () => ({
|
||||
createOpenAI: vi.fn(() => {
|
||||
const modelFn = (model: string) => ({ modelId: `openai:${model}` });
|
||||
return modelFn;
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock('qwen-ai-provider-v5', () => ({
|
||||
createQwen: vi.fn(() => {
|
||||
const modelFn = (model: string) => ({ modelId: `qwen:${model}` });
|
||||
return modelFn;
|
||||
}),
|
||||
}));
|
||||
|
||||
import { createAnthropic } from '@ai-sdk/anthropic';
|
||||
import { createDeepSeek } from '@ai-sdk/deepseek';
|
||||
import { createOpenAI } from '@ai-sdk/openai';
|
||||
import { createQwen } from 'qwen-ai-provider-v5';
|
||||
|
||||
describe('providers', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('providers 注册表', () => {
|
||||
it('包含所有支持的 provider 类型', () => {
|
||||
expect(providers).toHaveProperty('anthropic');
|
||||
expect(providers).toHaveProperty('deepseek');
|
||||
expect(providers).toHaveProperty('openai');
|
||||
});
|
||||
|
||||
it('每个 provider 是一个工厂函数', () => {
|
||||
expect(typeof providers.anthropic).toBe('function');
|
||||
expect(typeof providers.deepseek).toBe('function');
|
||||
expect(typeof providers.openai).toBe('function');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Anthropic provider', () => {
|
||||
it('创建 Anthropic 客户端', () => {
|
||||
const factory = providers.anthropic({
|
||||
apiKey: 'test-api-key',
|
||||
});
|
||||
|
||||
expect(createAnthropic).toHaveBeenCalledWith({
|
||||
apiKey: 'test-api-key',
|
||||
baseURL: undefined,
|
||||
});
|
||||
expect(typeof factory).toBe('function');
|
||||
});
|
||||
|
||||
it('支持自定义 baseUrl', () => {
|
||||
providers.anthropic({
|
||||
apiKey: 'test-api-key',
|
||||
baseUrl: 'https://custom.anthropic.com',
|
||||
});
|
||||
|
||||
expect(createAnthropic).toHaveBeenCalledWith({
|
||||
apiKey: 'test-api-key',
|
||||
baseURL: 'https://custom.anthropic.com',
|
||||
});
|
||||
});
|
||||
|
||||
it('返回的工厂函数可以创建模型', () => {
|
||||
const factory = providers.anthropic({
|
||||
apiKey: 'test-api-key',
|
||||
});
|
||||
|
||||
const model = factory('claude-3-opus');
|
||||
expect(model).toEqual({ modelId: 'anthropic:claude-3-opus' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('DeepSeek provider', () => {
|
||||
it('创建 DeepSeek 客户端', () => {
|
||||
const factory = providers.deepseek({
|
||||
apiKey: 'test-deepseek-key',
|
||||
});
|
||||
|
||||
expect(createDeepSeek).toHaveBeenCalledWith({
|
||||
apiKey: 'test-deepseek-key',
|
||||
baseURL: undefined,
|
||||
});
|
||||
expect(typeof factory).toBe('function');
|
||||
});
|
||||
|
||||
it('支持自定义 baseUrl', () => {
|
||||
providers.deepseek({
|
||||
apiKey: 'test-deepseek-key',
|
||||
baseUrl: 'https://custom.deepseek.com',
|
||||
});
|
||||
|
||||
expect(createDeepSeek).toHaveBeenCalledWith({
|
||||
apiKey: 'test-deepseek-key',
|
||||
baseURL: 'https://custom.deepseek.com',
|
||||
});
|
||||
});
|
||||
|
||||
it('返回的工厂函数可以创建模型', () => {
|
||||
const factory = providers.deepseek({
|
||||
apiKey: 'test-deepseek-key',
|
||||
});
|
||||
|
||||
const model = factory('deepseek-chat');
|
||||
expect(model).toEqual({ modelId: 'deepseek:deepseek-chat' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('OpenAI provider', () => {
|
||||
it('创建 OpenAI 客户端(标准 URL)', () => {
|
||||
const factory = providers.openai({
|
||||
apiKey: 'test-openai-key',
|
||||
});
|
||||
|
||||
expect(createOpenAI).toHaveBeenCalledWith({
|
||||
apiKey: 'test-openai-key',
|
||||
baseURL: undefined,
|
||||
});
|
||||
expect(createQwen).not.toHaveBeenCalled();
|
||||
expect(typeof factory).toBe('function');
|
||||
});
|
||||
|
||||
it('支持自定义 baseUrl(非 DashScope)', () => {
|
||||
providers.openai({
|
||||
apiKey: 'test-openai-key',
|
||||
baseUrl: 'https://custom.openai.com/v1',
|
||||
});
|
||||
|
||||
expect(createOpenAI).toHaveBeenCalledWith({
|
||||
apiKey: 'test-openai-key',
|
||||
baseURL: 'https://custom.openai.com/v1',
|
||||
});
|
||||
expect(createQwen).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('返回的工厂函数可以创建模型', () => {
|
||||
const factory = providers.openai({
|
||||
apiKey: 'test-openai-key',
|
||||
});
|
||||
|
||||
const model = factory('gpt-4');
|
||||
expect(model).toEqual({ modelId: 'openai:gpt-4' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('DashScope (Qwen) 检测', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('检测 dashscope URL 并使用 Qwen provider', () => {
|
||||
providers.openai({
|
||||
apiKey: 'test-qwen-key',
|
||||
baseUrl: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
|
||||
});
|
||||
|
||||
expect(createQwen).toHaveBeenCalledWith({
|
||||
apiKey: 'test-qwen-key',
|
||||
baseURL: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
|
||||
});
|
||||
expect(createOpenAI).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('检测包含 dashscope 的任意 URL', () => {
|
||||
providers.openai({
|
||||
apiKey: 'test-qwen-key',
|
||||
baseUrl: 'https://api.dashscope.example.com/v1',
|
||||
});
|
||||
|
||||
expect(createQwen).toHaveBeenCalled();
|
||||
expect(createOpenAI).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('Qwen 工厂函数可以创建模型', () => {
|
||||
const factory = providers.openai({
|
||||
apiKey: 'test-qwen-key',
|
||||
baseUrl: 'https://dashscope.aliyuncs.com/v1',
|
||||
});
|
||||
|
||||
const model = factory('qwen-turbo');
|
||||
expect(model).toEqual({ modelId: 'qwen:qwen-turbo' });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getModelFactory', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('获取 Anthropic 模型工厂', () => {
|
||||
const factory = getModelFactory('anthropic', {
|
||||
apiKey: 'test-key',
|
||||
});
|
||||
|
||||
expect(typeof factory).toBe('function');
|
||||
expect(createAnthropic).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('获取 DeepSeek 模型工厂', () => {
|
||||
const factory = getModelFactory('deepseek', {
|
||||
apiKey: 'test-key',
|
||||
});
|
||||
|
||||
expect(typeof factory).toBe('function');
|
||||
expect(createDeepSeek).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('获取 OpenAI 模型工厂', () => {
|
||||
const factory = getModelFactory('openai', {
|
||||
apiKey: 'test-key',
|
||||
});
|
||||
|
||||
expect(typeof factory).toBe('function');
|
||||
expect(createOpenAI).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('传递正确的选项给 provider', () => {
|
||||
getModelFactory('anthropic', {
|
||||
apiKey: 'my-api-key',
|
||||
baseUrl: 'https://my-proxy.com',
|
||||
});
|
||||
|
||||
expect(createAnthropic).toHaveBeenCalledWith({
|
||||
apiKey: 'my-api-key',
|
||||
baseURL: 'https://my-proxy.com',
|
||||
});
|
||||
});
|
||||
|
||||
it('不支持的 provider 抛出错误', () => {
|
||||
expect(() => {
|
||||
getModelFactory('unsupported' as any, {
|
||||
apiKey: 'test-key',
|
||||
});
|
||||
}).toThrow('不支持的 provider: unsupported');
|
||||
});
|
||||
|
||||
it('返回的工厂函数可以创建模型实例', () => {
|
||||
const factory = getModelFactory('anthropic', {
|
||||
apiKey: 'test-key',
|
||||
});
|
||||
|
||||
const model = factory('claude-3-sonnet');
|
||||
expect(model).toEqual({ modelId: 'anthropic:claude-3-sonnet' });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,356 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import {
|
||||
listServers,
|
||||
printServerList,
|
||||
installServer,
|
||||
installAllServers,
|
||||
showServerInfo,
|
||||
type ServerStatus,
|
||||
} from '../../../src/lsp/cli.js';
|
||||
|
||||
// Mock child_process
|
||||
vi.mock('child_process', () => ({
|
||||
execSync: vi.fn(),
|
||||
spawnSync: vi.fn(),
|
||||
}));
|
||||
|
||||
// Mock server module
|
||||
vi.mock('../../../src/lsp/server.js', () => ({
|
||||
getUniqueServers: vi.fn(() => [
|
||||
{
|
||||
id: 'typescript-language-server',
|
||||
languages: ['typescript', 'javascript'],
|
||||
config: {
|
||||
displayName: 'TypeScript Language Server',
|
||||
description: 'TypeScript/JavaScript 语言服务器',
|
||||
command: 'typescript-language-server',
|
||||
install: {
|
||||
npm: 'typescript-language-server typescript',
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'pylsp',
|
||||
languages: ['python'],
|
||||
config: {
|
||||
displayName: 'Python LSP Server',
|
||||
description: 'Python 语言服务器',
|
||||
command: 'pylsp',
|
||||
install: {
|
||||
pip: 'python-lsp-server',
|
||||
manual: '也可以使用: pip3 install python-lsp-server',
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'gopls',
|
||||
languages: ['go'],
|
||||
config: {
|
||||
displayName: 'Go Language Server',
|
||||
description: 'Go 语言服务器',
|
||||
command: 'gopls',
|
||||
install: {
|
||||
go: 'golang.org/x/tools/gopls@latest',
|
||||
},
|
||||
},
|
||||
},
|
||||
]),
|
||||
}));
|
||||
|
||||
import { execSync, spawnSync } from 'child_process';
|
||||
|
||||
describe('LSP CLI - LSP 命令行工具', () => {
|
||||
let consoleLogSpy: ReturnType<typeof vi.spyOn>;
|
||||
let consoleErrorSpy: ReturnType<typeof vi.spyOn>;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
||||
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||
|
||||
// 默认命令存在
|
||||
vi.mocked(execSync).mockImplementation((cmd: string) => {
|
||||
if (cmd.includes('which npm')) return Buffer.from('/usr/bin/npm');
|
||||
if (cmd.includes('which pip3')) return Buffer.from('/usr/bin/pip3');
|
||||
if (cmd.includes('which go')) return Buffer.from('/usr/bin/go');
|
||||
throw new Error('Command not found');
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
consoleLogSpy.mockRestore();
|
||||
consoleErrorSpy.mockRestore();
|
||||
});
|
||||
|
||||
describe('listServers - 列出服务器', () => {
|
||||
it('返回所有服务器状态', () => {
|
||||
const servers = listServers();
|
||||
|
||||
expect(servers).toHaveLength(3);
|
||||
expect(servers[0].id).toBe('typescript-language-server');
|
||||
expect(servers[1].id).toBe('pylsp');
|
||||
expect(servers[2].id).toBe('gopls');
|
||||
});
|
||||
|
||||
it('检测已安装的服务器', () => {
|
||||
// 只有 typescript-language-server 安装了
|
||||
vi.mocked(execSync).mockImplementation((cmd: string) => {
|
||||
if (cmd === 'which typescript-language-server') return Buffer.from('/usr/bin/tsc');
|
||||
throw new Error('not found');
|
||||
});
|
||||
|
||||
const servers = listServers();
|
||||
|
||||
expect(servers[0].installed).toBe(true);
|
||||
expect(servers[1].installed).toBe(false);
|
||||
expect(servers[2].installed).toBe(false);
|
||||
});
|
||||
|
||||
it('包含服务器详细信息', () => {
|
||||
const servers = listServers();
|
||||
const tsServer = servers[0];
|
||||
|
||||
expect(tsServer.displayName).toBe('TypeScript Language Server');
|
||||
expect(tsServer.description).toContain('TypeScript');
|
||||
expect(tsServer.command).toBe('typescript-language-server');
|
||||
expect(tsServer.languages).toContain('typescript');
|
||||
expect(tsServer.install).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('printServerList - 打印服务器列表', () => {
|
||||
it('输出格式化的服务器列表', () => {
|
||||
printServerList();
|
||||
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('语言服务器状态'));
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('状态'));
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('服务器'));
|
||||
});
|
||||
|
||||
it('显示已安装数量统计', () => {
|
||||
printServerList();
|
||||
|
||||
const calls = consoleLogSpy.mock.calls.flat().join('\n');
|
||||
expect(calls).toContain('已安装');
|
||||
});
|
||||
});
|
||||
|
||||
describe('installServer - 安装服务器', () => {
|
||||
it('服务器不存在时返回 false', async () => {
|
||||
const result = await installServer('non-existent');
|
||||
|
||||
expect(result).toBe(false);
|
||||
expect(consoleErrorSpy).toHaveBeenCalledWith(expect.stringContaining('未找到服务器'));
|
||||
});
|
||||
|
||||
it('服务器已安装时返回 true', async () => {
|
||||
vi.mocked(execSync).mockImplementation((cmd: string) => {
|
||||
if (cmd === 'which typescript-language-server') return Buffer.from('/usr/bin/tsc');
|
||||
if (cmd.includes('which npm')) return Buffer.from('/usr/bin/npm');
|
||||
throw new Error('not found');
|
||||
});
|
||||
|
||||
const result = await installServer('typescript-language-server');
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('已安装'));
|
||||
});
|
||||
|
||||
it('使用 npm 安装 TypeScript 服务器', async () => {
|
||||
vi.mocked(execSync).mockImplementation((cmd: string) => {
|
||||
if (cmd === 'which typescript-language-server') throw new Error('not found');
|
||||
if (cmd.includes('which npm')) return Buffer.from('/usr/bin/npm');
|
||||
throw new Error('not found');
|
||||
});
|
||||
vi.mocked(spawnSync).mockReturnValue({ status: 0 } as any);
|
||||
|
||||
const result = await installServer('typescript-language-server');
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(spawnSync).toHaveBeenCalledWith(
|
||||
expect.stringContaining('npm install -g'),
|
||||
expect.any(Object)
|
||||
);
|
||||
});
|
||||
|
||||
it('安装失败时返回 false', async () => {
|
||||
vi.mocked(execSync).mockImplementation((cmd: string) => {
|
||||
if (cmd === 'which typescript-language-server') throw new Error('not found');
|
||||
if (cmd.includes('which npm')) return Buffer.from('/usr/bin/npm');
|
||||
throw new Error('not found');
|
||||
});
|
||||
vi.mocked(spawnSync).mockReturnValue({ status: 1 } as any);
|
||||
|
||||
const result = await installServer('typescript-language-server');
|
||||
|
||||
expect(result).toBe(false);
|
||||
expect(consoleErrorSpy).toHaveBeenCalledWith(expect.stringContaining('安装失败'));
|
||||
});
|
||||
|
||||
it('按显示名称查找服务器', async () => {
|
||||
vi.mocked(execSync).mockImplementation((cmd: string) => {
|
||||
if (cmd === 'which typescript-language-server') return Buffer.from('/usr/bin/tsc');
|
||||
if (cmd.includes('which npm')) return Buffer.from('/usr/bin/npm');
|
||||
throw new Error('not found');
|
||||
});
|
||||
|
||||
const result = await installServer('TypeScript Language Server');
|
||||
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it('spawnSync 抛出异常时返回 false', async () => {
|
||||
vi.mocked(execSync).mockImplementation((cmd: string) => {
|
||||
if (cmd === 'which typescript-language-server') throw new Error('not found');
|
||||
if (cmd.includes('which npm')) return Buffer.from('/usr/bin/npm');
|
||||
throw new Error('not found');
|
||||
});
|
||||
vi.mocked(spawnSync).mockImplementation(() => {
|
||||
throw new Error('spawn error');
|
||||
});
|
||||
|
||||
const result = await installServer('typescript-language-server');
|
||||
|
||||
expect(result).toBe(false);
|
||||
expect(consoleErrorSpy).toHaveBeenCalledWith(expect.stringContaining('安装出错'));
|
||||
});
|
||||
|
||||
it('无法自动安装时显示手动说明', async () => {
|
||||
vi.mocked(execSync).mockImplementation((cmd: string) => {
|
||||
// pylsp 未安装,且没有可用的 pip
|
||||
if (cmd === 'which pylsp') throw new Error('not found');
|
||||
throw new Error('not found');
|
||||
});
|
||||
|
||||
const result = await installServer('pylsp');
|
||||
|
||||
expect(result).toBe(false);
|
||||
expect(consoleErrorSpy).toHaveBeenCalledWith(expect.stringContaining('无法自动安装'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('installAllServers - 安装所有服务器', () => {
|
||||
it('所有服务器都已安装时显示完成', async () => {
|
||||
vi.mocked(execSync).mockReturnValue(Buffer.from('/usr/bin/cmd'));
|
||||
|
||||
await installAllServers();
|
||||
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('都已安装'));
|
||||
});
|
||||
|
||||
it('安装未安装的服务器', async () => {
|
||||
vi.mocked(execSync).mockImplementation((cmd: string) => {
|
||||
// 所有服务器都未安装,但有 npm
|
||||
if (cmd.includes('which npm')) return Buffer.from('/usr/bin/npm');
|
||||
throw new Error('not found');
|
||||
});
|
||||
vi.mocked(spawnSync).mockReturnValue({ status: 0 } as any);
|
||||
|
||||
await installAllServers();
|
||||
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('将安装'));
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('安装完成'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('showServerInfo - 显示服务器信息', () => {
|
||||
it('显示已安装服务器的信息', () => {
|
||||
vi.mocked(execSync).mockImplementation((cmd: string) => {
|
||||
if (cmd === 'which typescript-language-server') return Buffer.from('/usr/bin/tsc');
|
||||
if (cmd.includes('which npm')) return Buffer.from('/usr/bin/npm');
|
||||
throw new Error('not found');
|
||||
});
|
||||
|
||||
showServerInfo('typescript-language-server');
|
||||
|
||||
const calls = consoleLogSpy.mock.calls.flat().join('\n');
|
||||
expect(calls).toContain('TypeScript Language Server');
|
||||
expect(calls).toContain('已安装');
|
||||
});
|
||||
|
||||
it('显示未安装服务器的安装命令', () => {
|
||||
vi.mocked(execSync).mockImplementation((cmd: string) => {
|
||||
if (cmd === 'which typescript-language-server') throw new Error('not found');
|
||||
if (cmd.includes('which npm')) return Buffer.from('/usr/bin/npm');
|
||||
throw new Error('not found');
|
||||
});
|
||||
|
||||
showServerInfo('typescript-language-server');
|
||||
|
||||
const calls = consoleLogSpy.mock.calls.flat().join('\n');
|
||||
expect(calls).toContain('未安装');
|
||||
expect(calls).toContain('安装命令');
|
||||
expect(calls).toContain('npm install');
|
||||
});
|
||||
|
||||
it('服务器不存在时显示错误', () => {
|
||||
showServerInfo('non-existent');
|
||||
|
||||
expect(consoleErrorSpy).toHaveBeenCalledWith(expect.stringContaining('未找到服务器'));
|
||||
});
|
||||
|
||||
it('按显示名称查找服务器', () => {
|
||||
vi.mocked(execSync).mockReturnValue(Buffer.from('/usr/bin/cmd'));
|
||||
|
||||
showServerInfo('Python LSP Server');
|
||||
|
||||
const calls = consoleLogSpy.mock.calls.flat().join('\n');
|
||||
expect(calls).toContain('Python LSP Server');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getInstallCommand - 安装命令选择', () => {
|
||||
it('pip 作为 Python 服务器的安装方式', async () => {
|
||||
vi.mocked(execSync).mockImplementation((cmd: string) => {
|
||||
if (cmd === 'which pylsp') throw new Error('not found');
|
||||
if (cmd.includes('which pip3')) return Buffer.from('/usr/bin/pip3');
|
||||
throw new Error('not found');
|
||||
});
|
||||
vi.mocked(spawnSync).mockReturnValue({ status: 0 } as any);
|
||||
|
||||
await installServer('pylsp');
|
||||
|
||||
expect(spawnSync).toHaveBeenCalledWith(
|
||||
expect.stringContaining('pip3 install'),
|
||||
expect.any(Object)
|
||||
);
|
||||
});
|
||||
|
||||
it('go install 作为 Go 服务器的安装方式', async () => {
|
||||
vi.mocked(execSync).mockImplementation((cmd: string) => {
|
||||
if (cmd === 'which gopls') throw new Error('not found');
|
||||
if (cmd.includes('which go')) return Buffer.from('/usr/bin/go');
|
||||
throw new Error('not found');
|
||||
});
|
||||
vi.mocked(spawnSync).mockReturnValue({ status: 0 } as any);
|
||||
|
||||
await installServer('gopls');
|
||||
|
||||
expect(spawnSync).toHaveBeenCalledWith(
|
||||
expect.stringContaining('go install'),
|
||||
expect.any(Object)
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('ServerStatus 类型', () => {
|
||||
it('包含所有必要字段', () => {
|
||||
const status: ServerStatus = {
|
||||
id: 'test-server',
|
||||
displayName: 'Test Server',
|
||||
description: 'A test server',
|
||||
command: 'test-cmd',
|
||||
installed: true,
|
||||
languages: ['typescript'],
|
||||
install: { npm: 'test-package' },
|
||||
};
|
||||
|
||||
expect(status.id).toBeDefined();
|
||||
expect(status.displayName).toBeDefined();
|
||||
expect(status.command).toBeDefined();
|
||||
expect(status.installed).toBeDefined();
|
||||
expect(status.languages).toBeDefined();
|
||||
expect(status.install).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,357 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { LSPClientManager } from '../../../src/lsp/client.js';
|
||||
import { DiagnosticSeverity } from 'vscode-languageserver-protocol';
|
||||
|
||||
// Mock child_process
|
||||
const mockSpawn = vi.fn();
|
||||
const mockExecSync = vi.fn();
|
||||
|
||||
vi.mock('child_process', () => ({
|
||||
spawn: (...args: unknown[]) => mockSpawn(...args),
|
||||
execSync: (...args: unknown[]) => mockExecSync(...args),
|
||||
}));
|
||||
|
||||
// Mock vscode-jsonrpc
|
||||
const mockConnection = {
|
||||
listen: vi.fn(),
|
||||
sendRequest: vi.fn().mockResolvedValue({}),
|
||||
sendNotification: vi.fn(),
|
||||
onNotification: vi.fn(),
|
||||
dispose: vi.fn(),
|
||||
};
|
||||
|
||||
vi.mock('vscode-jsonrpc/node.js', () => ({
|
||||
createMessageConnection: vi.fn(() => mockConnection),
|
||||
StreamMessageReader: vi.fn(),
|
||||
StreamMessageWriter: vi.fn(),
|
||||
}));
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
readFile: vi.fn().mockResolvedValue('file content'),
|
||||
}));
|
||||
|
||||
// Mock language module
|
||||
vi.mock('../../../src/lsp/language.js', () => ({
|
||||
getLanguageId: vi.fn((path: string) => {
|
||||
if (path.endsWith('.ts')) return 'typescript';
|
||||
if (path.endsWith('.py')) return 'python';
|
||||
return undefined;
|
||||
}),
|
||||
}));
|
||||
|
||||
// Mock server module
|
||||
vi.mock('../../../src/lsp/server.js', () => ({
|
||||
getServerConfig: vi.fn((languageId: string) => {
|
||||
if (languageId === 'typescript') {
|
||||
return {
|
||||
command: 'typescript-language-server',
|
||||
args: ['--stdio'],
|
||||
env: {},
|
||||
initializationOptions: {},
|
||||
};
|
||||
}
|
||||
if (languageId === 'python') {
|
||||
return {
|
||||
command: 'pylsp',
|
||||
args: [],
|
||||
env: {},
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}),
|
||||
}));
|
||||
|
||||
import { getLanguageId } from '../../../src/lsp/language.js';
|
||||
|
||||
describe('LSPClientManager - 扩展测试', () => {
|
||||
let manager: LSPClientManager;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
manager = new LSPClientManager('/test/project');
|
||||
|
||||
// 默认命令存在
|
||||
mockExecSync.mockReturnValue(Buffer.from(''));
|
||||
|
||||
// 模拟 spawn 返回的进程
|
||||
mockSpawn.mockReturnValue({
|
||||
stdin: { on: vi.fn(), write: vi.fn() },
|
||||
stdout: { on: vi.fn(), pipe: vi.fn() },
|
||||
stderr: { on: vi.fn() },
|
||||
on: vi.fn(),
|
||||
kill: vi.fn(),
|
||||
pid: 12345,
|
||||
});
|
||||
});
|
||||
|
||||
describe('getClient - 启动客户端', () => {
|
||||
it('重复获取同一语言返回相同客户端', async () => {
|
||||
const client1 = await manager.getClient('typescript');
|
||||
const client2 = await manager.getClient('typescript');
|
||||
|
||||
expect(client1).toBe(client2);
|
||||
});
|
||||
|
||||
it('成功启动时设置初始化状态', async () => {
|
||||
const client = await manager.getClient('typescript');
|
||||
|
||||
if (client) {
|
||||
expect(client.initialized).toBe(true);
|
||||
expect(client.languageId).toBe('typescript');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('touchFile - 文件变更通知', () => {
|
||||
it('相对路径转换为绝对路径', async () => {
|
||||
vi.mocked(getLanguageId).mockReturnValue('typescript');
|
||||
|
||||
// 先获取客户端
|
||||
await manager.getClient('typescript');
|
||||
|
||||
// touchFile 应该能处理相对路径
|
||||
const result = await manager.touchFile('relative/file.ts');
|
||||
|
||||
// 由于客户端已初始化,应该调用通知
|
||||
expect(typeof result).toBe('boolean');
|
||||
});
|
||||
|
||||
it('首次启动返回 true', async () => {
|
||||
vi.mocked(getLanguageId).mockReturnValue('typescript');
|
||||
|
||||
const result = await manager.touchFile('/test/file.ts');
|
||||
|
||||
// 首次启动服务器应返回 true
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it('后续调用返回 false', async () => {
|
||||
vi.mocked(getLanguageId).mockReturnValue('typescript');
|
||||
|
||||
// 首次调用
|
||||
await manager.touchFile('/test/file.ts');
|
||||
|
||||
// 第二次调用
|
||||
const result = await manager.touchFile('/test/file.ts');
|
||||
|
||||
// 已经在运行,返回 false
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('closeFile - 关闭文件', () => {
|
||||
it('正常关闭打开的文件', async () => {
|
||||
vi.mocked(getLanguageId).mockReturnValue('typescript');
|
||||
|
||||
// 先打开文件
|
||||
await manager.touchFile('/test/file.ts');
|
||||
|
||||
// 关闭文件
|
||||
await manager.closeFile('/test/file.ts');
|
||||
|
||||
expect(mockConnection.sendNotification).toHaveBeenCalledWith(
|
||||
'textDocument/didClose',
|
||||
expect.any(Object)
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('诊断信息转换', () => {
|
||||
it('正确转换诊断严重性', async () => {
|
||||
// 通过 onNotification 回调模拟诊断
|
||||
let diagnosticsCallback: ((params: unknown) => void) | null = null;
|
||||
mockConnection.onNotification.mockImplementation((method: string, callback: (params: unknown) => void) => {
|
||||
if (method === 'textDocument/publishDiagnostics') {
|
||||
diagnosticsCallback = callback;
|
||||
}
|
||||
});
|
||||
|
||||
vi.mocked(getLanguageId).mockReturnValue('typescript');
|
||||
await manager.getClient('typescript');
|
||||
|
||||
// 模拟收到诊断
|
||||
if (diagnosticsCallback) {
|
||||
diagnosticsCallback({
|
||||
uri: 'file:///test/file.ts',
|
||||
diagnostics: [
|
||||
{
|
||||
range: { start: { line: 0, character: 0 }, end: { line: 0, character: 10 } },
|
||||
severity: DiagnosticSeverity.Error,
|
||||
message: 'Test error',
|
||||
source: 'typescript',
|
||||
code: 'TS2345',
|
||||
},
|
||||
{
|
||||
range: { start: { line: 1, character: 0 }, end: { line: 1, character: 5 } },
|
||||
severity: DiagnosticSeverity.Warning,
|
||||
message: 'Test warning',
|
||||
},
|
||||
{
|
||||
range: { start: { line: 2, character: 0 }, end: { line: 2, character: 5 } },
|
||||
severity: DiagnosticSeverity.Information,
|
||||
message: 'Test info',
|
||||
},
|
||||
{
|
||||
range: { start: { line: 3, character: 0 }, end: { line: 3, character: 5 } },
|
||||
severity: DiagnosticSeverity.Hint,
|
||||
message: 'Test hint',
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
// 获取诊断
|
||||
const diagnostics = manager.getFileDiagnostics('/test/file.ts');
|
||||
|
||||
expect(diagnostics.length).toBe(4);
|
||||
expect(diagnostics[0].severity).toBe('error');
|
||||
expect(diagnostics[1].severity).toBe('warning');
|
||||
expect(diagnostics[2].severity).toBe('info');
|
||||
expect(diagnostics[3].severity).toBe('hint');
|
||||
});
|
||||
|
||||
it('转换行列号(从 0 开始转为 1 开始)', async () => {
|
||||
let diagnosticsCallback: ((params: unknown) => void) | null = null;
|
||||
mockConnection.onNotification.mockImplementation((method: string, callback: (params: unknown) => void) => {
|
||||
if (method === 'textDocument/publishDiagnostics') {
|
||||
diagnosticsCallback = callback;
|
||||
}
|
||||
});
|
||||
|
||||
vi.mocked(getLanguageId).mockReturnValue('typescript');
|
||||
await manager.getClient('typescript');
|
||||
|
||||
if (diagnosticsCallback) {
|
||||
diagnosticsCallback({
|
||||
uri: 'file:///test/file.ts',
|
||||
diagnostics: [
|
||||
{
|
||||
range: { start: { line: 9, character: 4 }, end: { line: 9, character: 20 } },
|
||||
severity: DiagnosticSeverity.Error,
|
||||
message: 'Error',
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
const diagnostics = manager.getFileDiagnostics('/test/file.ts');
|
||||
|
||||
expect(diagnostics[0].line).toBe(10); // 9 + 1
|
||||
expect(diagnostics[0].column).toBe(5); // 4 + 1
|
||||
expect(diagnostics[0].endLine).toBe(10);
|
||||
expect(diagnostics[0].endColumn).toBe(21);
|
||||
});
|
||||
});
|
||||
|
||||
describe('shutdown - 关闭所有客户端', () => {
|
||||
it('关闭所有运行中的客户端', async () => {
|
||||
vi.mocked(getLanguageId).mockImplementation((path: string) => {
|
||||
if (path.endsWith('.ts')) return 'typescript';
|
||||
if (path.endsWith('.py')) return 'python';
|
||||
return undefined;
|
||||
});
|
||||
|
||||
// 启动多个客户端
|
||||
await manager.getClient('typescript');
|
||||
await manager.getClient('python');
|
||||
|
||||
expect(manager.getRunningServers().length).toBe(2);
|
||||
|
||||
// 关闭
|
||||
await manager.shutdown();
|
||||
|
||||
expect(manager.getRunningServers().length).toBe(0);
|
||||
});
|
||||
|
||||
it('处理关闭时的错误', async () => {
|
||||
vi.mocked(getLanguageId).mockReturnValue('typescript');
|
||||
await manager.getClient('typescript');
|
||||
|
||||
// 模拟 dispose 抛出错误
|
||||
mockConnection.dispose.mockImplementation(() => {
|
||||
throw new Error('Dispose error');
|
||||
});
|
||||
|
||||
// 应该不抛出错误
|
||||
await expect(manager.shutdown()).resolves.not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('进程事件处理', () => {
|
||||
it('进程退出时清理客户端', async () => {
|
||||
let exitCallback: (() => void) | null = null;
|
||||
mockSpawn.mockReturnValue({
|
||||
stdin: { on: vi.fn(), write: vi.fn() },
|
||||
stdout: { on: vi.fn(), pipe: vi.fn() },
|
||||
stderr: { on: vi.fn() },
|
||||
on: vi.fn((event: string, callback: () => void) => {
|
||||
if (event === 'exit') {
|
||||
exitCallback = callback;
|
||||
}
|
||||
}),
|
||||
kill: vi.fn(),
|
||||
pid: 12345,
|
||||
});
|
||||
|
||||
vi.mocked(getLanguageId).mockReturnValue('typescript');
|
||||
await manager.getClient('typescript');
|
||||
|
||||
expect(manager.isServerRunning('typescript')).toBe(true);
|
||||
|
||||
// 模拟进程退出
|
||||
if (exitCallback) {
|
||||
exitCallback();
|
||||
}
|
||||
|
||||
expect(manager.isServerRunning('typescript')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getDiagnostics - 获取所有诊断', () => {
|
||||
it('可以过滤指定文件的诊断', async () => {
|
||||
let diagnosticsCallback: ((params: unknown) => void) | null = null;
|
||||
mockConnection.onNotification.mockImplementation((method: string, callback: (params: unknown) => void) => {
|
||||
if (method === 'textDocument/publishDiagnostics') {
|
||||
diagnosticsCallback = callback;
|
||||
}
|
||||
});
|
||||
|
||||
vi.mocked(getLanguageId).mockReturnValue('typescript');
|
||||
await manager.getClient('typescript');
|
||||
|
||||
// 为两个文件添加诊断
|
||||
if (diagnosticsCallback) {
|
||||
diagnosticsCallback({
|
||||
uri: 'file:///test/file1.ts',
|
||||
diagnostics: [
|
||||
{
|
||||
range: { start: { line: 0, character: 0 }, end: { line: 0, character: 10 } },
|
||||
severity: DiagnosticSeverity.Error,
|
||||
message: 'Error in file1',
|
||||
},
|
||||
],
|
||||
});
|
||||
diagnosticsCallback({
|
||||
uri: 'file:///test/file2.ts',
|
||||
diagnostics: [
|
||||
{
|
||||
range: { start: { line: 0, character: 0 }, end: { line: 0, character: 10 } },
|
||||
severity: DiagnosticSeverity.Warning,
|
||||
message: 'Warning in file2',
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
// 获取所有诊断
|
||||
const allDiagnostics = manager.getDiagnostics();
|
||||
expect(allDiagnostics.size).toBe(2);
|
||||
|
||||
// 只获取 file1 的诊断
|
||||
const file1Diagnostics = manager.getDiagnostics('/test/file1.ts');
|
||||
expect(file1Diagnostics.size).toBe(1);
|
||||
expect(file1Diagnostics.has('/test/file1.ts')).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,218 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { LSPClientManager } from '../../../src/lsp/client.js';
|
||||
|
||||
// Mock child_process
|
||||
vi.mock('child_process', () => ({
|
||||
spawn: vi.fn(() => ({
|
||||
stdin: { on: vi.fn() },
|
||||
stdout: { on: vi.fn() },
|
||||
stderr: { on: vi.fn() },
|
||||
on: vi.fn(),
|
||||
kill: vi.fn(),
|
||||
})),
|
||||
execSync: vi.fn(),
|
||||
}));
|
||||
|
||||
// Mock vscode-jsonrpc
|
||||
vi.mock('vscode-jsonrpc/node.js', () => ({
|
||||
createMessageConnection: vi.fn(() => ({
|
||||
listen: vi.fn(),
|
||||
sendRequest: vi.fn().mockResolvedValue({}),
|
||||
sendNotification: vi.fn(),
|
||||
onNotification: vi.fn(),
|
||||
dispose: vi.fn(),
|
||||
})),
|
||||
StreamMessageReader: vi.fn(),
|
||||
StreamMessageWriter: vi.fn(),
|
||||
}));
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
readFile: vi.fn().mockResolvedValue('file content'),
|
||||
}));
|
||||
|
||||
// Mock language module
|
||||
vi.mock('../../../src/lsp/language.js', () => ({
|
||||
getLanguageId: vi.fn((path: string) => {
|
||||
if (path.endsWith('.ts')) return 'typescript';
|
||||
if (path.endsWith('.py')) return 'python';
|
||||
return undefined;
|
||||
}),
|
||||
}));
|
||||
|
||||
// Mock server module
|
||||
vi.mock('../../../src/lsp/server.js', () => ({
|
||||
getServerConfig: vi.fn((languageId: string) => {
|
||||
if (languageId === 'typescript') {
|
||||
return {
|
||||
command: 'typescript-language-server',
|
||||
args: ['--stdio'],
|
||||
env: {},
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}),
|
||||
}));
|
||||
|
||||
import { spawn, execSync } from 'child_process';
|
||||
import { getLanguageId } from '../../../src/lsp/language.js';
|
||||
import { getServerConfig } from '../../../src/lsp/server.js';
|
||||
|
||||
describe('LSPClientManager - LSP 客户端管理器', () => {
|
||||
let manager: LSPClientManager;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
manager = new LSPClientManager('/test/project');
|
||||
|
||||
// 默认命令存在
|
||||
vi.mocked(execSync).mockReturnValue(Buffer.from(''));
|
||||
});
|
||||
|
||||
describe('构造函数', () => {
|
||||
it('使用提供的根路径', () => {
|
||||
const m = new LSPClientManager('/custom/path');
|
||||
expect(m).toBeDefined();
|
||||
});
|
||||
|
||||
it('默认使用 process.cwd()', () => {
|
||||
const m = new LSPClientManager();
|
||||
expect(m).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('setRootPath - 设置根路径', () => {
|
||||
it('更新根路径', () => {
|
||||
manager.setRootPath('/new/path');
|
||||
// 无直接验证方式,但不应报错
|
||||
expect(true).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getClient - 获取客户端', () => {
|
||||
it('无服务器配置返回 undefined', async () => {
|
||||
vi.mocked(getServerConfig).mockReturnValue(null);
|
||||
|
||||
const client = await manager.getClient('unknown' as any);
|
||||
|
||||
expect(client).toBeUndefined();
|
||||
});
|
||||
|
||||
it('命令不存在时返回 undefined', async () => {
|
||||
vi.mocked(execSync).mockImplementation(() => {
|
||||
throw new Error('command not found');
|
||||
});
|
||||
|
||||
const client = await manager.getClient('typescript');
|
||||
|
||||
expect(client).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('touchFile - 通知文件变更', () => {
|
||||
it('不支持的语言返回 false', async () => {
|
||||
vi.mocked(getLanguageId).mockReturnValue(undefined);
|
||||
|
||||
const result = await manager.touchFile('/test/file.xyz');
|
||||
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getDiagnostics - 获取诊断', () => {
|
||||
it('无客户端时返回空 Map', () => {
|
||||
const diagnostics = manager.getDiagnostics();
|
||||
|
||||
expect(diagnostics).toBeInstanceOf(Map);
|
||||
expect(diagnostics.size).toBe(0);
|
||||
});
|
||||
|
||||
it('可以按文件过滤', () => {
|
||||
const diagnostics = manager.getDiagnostics('/test/file.ts');
|
||||
|
||||
expect(diagnostics).toBeInstanceOf(Map);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getFileDiagnostics - 获取单文件诊断', () => {
|
||||
it('无诊断时返回空数组', () => {
|
||||
const diagnostics = manager.getFileDiagnostics('/test/file.ts');
|
||||
|
||||
expect(Array.isArray(diagnostics)).toBe(true);
|
||||
expect(diagnostics.length).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isServerRunning - 检查服务器状态', () => {
|
||||
it('未启动的服务器返回 false', () => {
|
||||
expect(manager.isServerRunning('typescript')).toBe(false);
|
||||
expect(manager.isServerRunning('python')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getRunningServers - 获取运行中的服务器', () => {
|
||||
it('无服务器时返回空数组', () => {
|
||||
const servers = manager.getRunningServers();
|
||||
|
||||
expect(Array.isArray(servers)).toBe(true);
|
||||
expect(servers.length).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('shutdown - 关闭', () => {
|
||||
it('无客户端时正常关闭', async () => {
|
||||
await expect(manager.shutdown()).resolves.not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('closeFile - 关闭文件', () => {
|
||||
it('不支持的语言静默返回', async () => {
|
||||
vi.mocked(getLanguageId).mockReturnValue(undefined);
|
||||
|
||||
await expect(manager.closeFile('/test/file.xyz')).resolves.not.toThrow();
|
||||
});
|
||||
|
||||
it('未打开的文件静默返回', async () => {
|
||||
vi.mocked(getLanguageId).mockReturnValue('typescript');
|
||||
|
||||
await expect(manager.closeFile('/test/file.ts')).resolves.not.toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('FileDiagnostic 类型', () => {
|
||||
it('包含必要字段', () => {
|
||||
const diagnostic = {
|
||||
file: '/test/file.ts',
|
||||
line: 1,
|
||||
column: 1,
|
||||
severity: 'error' as const,
|
||||
message: 'Test error',
|
||||
};
|
||||
|
||||
expect(diagnostic.file).toBeDefined();
|
||||
expect(diagnostic.line).toBeDefined();
|
||||
expect(diagnostic.column).toBeDefined();
|
||||
expect(diagnostic.severity).toBeDefined();
|
||||
expect(diagnostic.message).toBeDefined();
|
||||
});
|
||||
|
||||
it('支持可选字段', () => {
|
||||
const diagnostic = {
|
||||
file: '/test/file.ts',
|
||||
line: 1,
|
||||
column: 1,
|
||||
endLine: 2,
|
||||
endColumn: 5,
|
||||
severity: 'warning' as const,
|
||||
message: 'Test warning',
|
||||
source: 'typescript',
|
||||
code: 'TS2345',
|
||||
};
|
||||
|
||||
expect(diagnostic.endLine).toBe(2);
|
||||
expect(diagnostic.endColumn).toBe(5);
|
||||
expect(diagnostic.source).toBe('typescript');
|
||||
expect(diagnostic.code).toBe('TS2345');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,346 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
|
||||
// Mock functions
|
||||
const mockTouchFile = vi.fn();
|
||||
const mockGetDiagnostics = vi.fn();
|
||||
const mockGetFileDiagnostics = vi.fn();
|
||||
const mockShutdown = vi.fn();
|
||||
|
||||
// 创建 mock class
|
||||
class MockLSPClientManager {
|
||||
touchFile = mockTouchFile;
|
||||
getDiagnostics = mockGetDiagnostics;
|
||||
getFileDiagnostics = mockGetFileDiagnostics;
|
||||
shutdown = mockShutdown;
|
||||
|
||||
constructor(_rootPath?: string) {}
|
||||
}
|
||||
|
||||
// Mock client module
|
||||
vi.mock('../../../src/lsp/client.js', () => ({
|
||||
LSPClientManager: MockLSPClientManager,
|
||||
}));
|
||||
|
||||
// Mock language module
|
||||
vi.mock('../../../src/lsp/language.js', () => ({
|
||||
getLanguageId: vi.fn((path: string) => {
|
||||
if (path.endsWith('.ts')) return 'typescript';
|
||||
if (path.endsWith('.py')) return 'python';
|
||||
return undefined;
|
||||
}),
|
||||
isLanguageSupported: vi.fn((path: string) => {
|
||||
return path.endsWith('.ts') || path.endsWith('.py');
|
||||
}),
|
||||
getSupportedExtensions: vi.fn(() => ['.ts', '.js', '.py']),
|
||||
}));
|
||||
|
||||
// Mock server module
|
||||
vi.mock('../../../src/lsp/server.js', () => ({
|
||||
getServerConfig: vi.fn(),
|
||||
hasServerConfig: vi.fn(),
|
||||
getSupportedLanguages: vi.fn(() => []),
|
||||
}));
|
||||
|
||||
describe('LSP Index - LSP 模块入口', () => {
|
||||
let lspModule: typeof import('../../../src/lsp/index.js');
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.clearAllMocks();
|
||||
// 重置模块缓存以获取干净的状态
|
||||
vi.resetModules();
|
||||
// 重新导入模块
|
||||
lspModule = await import('../../../src/lsp/index.js');
|
||||
});
|
||||
|
||||
describe('initLSP - 初始化 LSP', () => {
|
||||
it('初始化 LSP 管理器', () => {
|
||||
lspModule.initLSP('/test/project');
|
||||
|
||||
expect(lspModule.getLSPManager()).not.toBeNull();
|
||||
});
|
||||
|
||||
it('重复初始化不会创建新实例', () => {
|
||||
lspModule.initLSP('/test/project');
|
||||
const manager1 = lspModule.getLSPManager();
|
||||
|
||||
lspModule.initLSP('/another/project');
|
||||
const manager2 = lspModule.getLSPManager();
|
||||
|
||||
expect(manager1).toBe(manager2);
|
||||
});
|
||||
|
||||
it('默认使用 process.cwd()', () => {
|
||||
lspModule.initLSP();
|
||||
|
||||
expect(lspModule.getLSPManager()).not.toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getLSPManager - 获取管理器', () => {
|
||||
it('未初始化时返回 null', async () => {
|
||||
// 重置模块获取干净状态
|
||||
vi.resetModules();
|
||||
const freshModule = await import('../../../src/lsp/index.js');
|
||||
|
||||
expect(freshModule.getLSPManager()).toBeNull();
|
||||
});
|
||||
|
||||
it('初始化后返回管理器实例', () => {
|
||||
lspModule.initLSP();
|
||||
|
||||
expect(lspModule.getLSPManager()).not.toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('touchFile - 通知文件变更', () => {
|
||||
it('未初始化时返回 false', async () => {
|
||||
vi.resetModules();
|
||||
const freshModule = await import('../../../src/lsp/index.js');
|
||||
|
||||
const result = await freshModule.touchFile('/test/file.ts');
|
||||
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it('不支持的语言返回 false', async () => {
|
||||
lspModule.initLSP();
|
||||
|
||||
const result = await lspModule.touchFile('/test/file.txt');
|
||||
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it('支持的语言调用管理器', async () => {
|
||||
mockTouchFile.mockResolvedValue(true);
|
||||
lspModule.initLSP();
|
||||
|
||||
const result = await lspModule.touchFile('/test/file.ts');
|
||||
|
||||
expect(mockTouchFile).toHaveBeenCalledWith('/test/file.ts', false);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it('新文件传递 isNew 参数', async () => {
|
||||
mockTouchFile.mockResolvedValue(true);
|
||||
lspModule.initLSP();
|
||||
|
||||
await lspModule.touchFile('/test/file.ts', true);
|
||||
|
||||
expect(mockTouchFile).toHaveBeenCalledWith('/test/file.ts', true);
|
||||
});
|
||||
|
||||
it('touchFile 出错时静默返回 false', async () => {
|
||||
mockTouchFile.mockRejectedValue(new Error('LSP error'));
|
||||
lspModule.initLSP();
|
||||
|
||||
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||
const result = await lspModule.touchFile('/test/file.ts');
|
||||
|
||||
expect(result).toBe(false);
|
||||
consoleErrorSpy.mockRestore();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getDiagnostics - 获取所有诊断', () => {
|
||||
it('未初始化时返回空 Map', async () => {
|
||||
vi.resetModules();
|
||||
const freshModule = await import('../../../src/lsp/index.js');
|
||||
|
||||
const result = freshModule.getDiagnostics();
|
||||
|
||||
expect(result).toBeInstanceOf(Map);
|
||||
expect(result.size).toBe(0);
|
||||
});
|
||||
|
||||
it('返回管理器的诊断信息', () => {
|
||||
const mockDiagnostics = new Map([
|
||||
['/test/file.ts', [{ file: '/test/file.ts', line: 1, column: 1, severity: 'error', message: 'Test error' }]],
|
||||
]);
|
||||
mockGetDiagnostics.mockReturnValue(mockDiagnostics);
|
||||
lspModule.initLSP();
|
||||
|
||||
const result = lspModule.getDiagnostics();
|
||||
|
||||
expect(result).toBe(mockDiagnostics);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getFileDiagnostics - 获取单文件诊断', () => {
|
||||
it('未初始化时返回空数组', async () => {
|
||||
vi.resetModules();
|
||||
const freshModule = await import('../../../src/lsp/index.js');
|
||||
|
||||
const result = freshModule.getFileDiagnostics('/test/file.ts');
|
||||
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('返回指定文件的诊断', () => {
|
||||
const mockDiagnostics = [
|
||||
{ file: '/test/file.ts', line: 1, column: 1, severity: 'error' as const, message: 'Error' },
|
||||
];
|
||||
mockGetFileDiagnostics.mockReturnValue(mockDiagnostics);
|
||||
lspModule.initLSP();
|
||||
|
||||
const result = lspModule.getFileDiagnostics('/test/file.ts');
|
||||
|
||||
expect(result).toBe(mockDiagnostics);
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatDiagnostics - 格式化诊断信息', () => {
|
||||
it('空诊断返回空字符串', () => {
|
||||
const result = lspModule.formatDiagnostics([]);
|
||||
|
||||
expect(result).toBe('');
|
||||
});
|
||||
|
||||
it('格式化单个诊断', () => {
|
||||
const diagnostics = [
|
||||
{
|
||||
file: '/test/file.ts',
|
||||
line: 10,
|
||||
column: 5,
|
||||
severity: 'error' as const,
|
||||
message: 'Type error',
|
||||
code: 'TS2345',
|
||||
source: 'typescript',
|
||||
},
|
||||
];
|
||||
|
||||
const result = lspModule.formatDiagnostics(diagnostics);
|
||||
|
||||
expect(result).toContain('10:5');
|
||||
expect(result).toContain('ERROR');
|
||||
expect(result).toContain('TS2345');
|
||||
expect(result).toContain('Type error');
|
||||
expect(result).toContain('typescript');
|
||||
});
|
||||
|
||||
it('格式化多个诊断', () => {
|
||||
const diagnostics = [
|
||||
{ file: '/test/file.ts', line: 1, column: 1, severity: 'error' as const, message: 'Error 1' },
|
||||
{ file: '/test/file.ts', line: 2, column: 2, severity: 'warning' as const, message: 'Warning 1' },
|
||||
];
|
||||
|
||||
const result = lspModule.formatDiagnostics(diagnostics);
|
||||
|
||||
expect(result).toContain('Error 1');
|
||||
expect(result).toContain('Warning 1');
|
||||
expect(result).toContain('ERROR');
|
||||
expect(result).toContain('WARNING');
|
||||
});
|
||||
|
||||
it('无 code 时不显示 code', () => {
|
||||
const diagnostics = [
|
||||
{ file: '/test/file.ts', line: 1, column: 1, severity: 'error' as const, message: 'Error' },
|
||||
];
|
||||
|
||||
const result = lspModule.formatDiagnostics(diagnostics);
|
||||
|
||||
expect(result).not.toContain('[');
|
||||
});
|
||||
|
||||
it('无 source 时不显示 source', () => {
|
||||
const diagnostics = [
|
||||
{ file: '/test/file.ts', line: 1, column: 1, severity: 'error' as const, message: 'Error' },
|
||||
];
|
||||
|
||||
const result = lspModule.formatDiagnostics(diagnostics);
|
||||
|
||||
expect(result).not.toContain('(');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getFormattedFileDiagnostics - 获取格式化的文件诊断', () => {
|
||||
it('无错误和警告时返回空字符串', async () => {
|
||||
mockGetFileDiagnostics.mockReturnValue([
|
||||
{ file: '/test/file.ts', line: 1, column: 1, severity: 'info', message: 'Info' },
|
||||
{ file: '/test/file.ts', line: 2, column: 2, severity: 'hint', message: 'Hint' },
|
||||
]);
|
||||
lspModule.initLSP();
|
||||
|
||||
const result = await lspModule.getFormattedFileDiagnostics('/test/file.ts');
|
||||
|
||||
expect(result).toBe('');
|
||||
});
|
||||
|
||||
it('只显示错误和警告', async () => {
|
||||
mockGetFileDiagnostics.mockReturnValue([
|
||||
{ file: '/test/file.ts', line: 1, column: 1, severity: 'error', message: 'Error' },
|
||||
{ file: '/test/file.ts', line: 2, column: 2, severity: 'warning', message: 'Warning' },
|
||||
{ file: '/test/file.ts', line: 3, column: 3, severity: 'info', message: 'Info' },
|
||||
]);
|
||||
lspModule.initLSP();
|
||||
|
||||
const result = await lspModule.getFormattedFileDiagnostics('/test/file.ts');
|
||||
|
||||
expect(result).toContain('Error');
|
||||
expect(result).toContain('Warning');
|
||||
expect(result).not.toContain('Info');
|
||||
});
|
||||
|
||||
it('包含 file_diagnostics 标签', async () => {
|
||||
mockGetFileDiagnostics.mockReturnValue([
|
||||
{ file: '/test/file.ts', line: 1, column: 1, severity: 'error', message: 'Error' },
|
||||
]);
|
||||
lspModule.initLSP();
|
||||
|
||||
const result = await lspModule.getFormattedFileDiagnostics('/test/file.ts');
|
||||
|
||||
expect(result).toContain('<file_diagnostics');
|
||||
expect(result).toContain('/test/file.ts');
|
||||
expect(result).toContain('</file_diagnostics>');
|
||||
});
|
||||
|
||||
it('显示错误和警告数量', async () => {
|
||||
mockGetFileDiagnostics.mockReturnValue([
|
||||
{ file: '/test/file.ts', line: 1, column: 1, severity: 'error', message: 'Error 1' },
|
||||
{ file: '/test/file.ts', line: 2, column: 2, severity: 'error', message: 'Error 2' },
|
||||
{ file: '/test/file.ts', line: 3, column: 3, severity: 'warning', message: 'Warning' },
|
||||
]);
|
||||
lspModule.initLSP();
|
||||
|
||||
const result = await lspModule.getFormattedFileDiagnostics('/test/file.ts');
|
||||
|
||||
expect(result).toContain('2 个错误');
|
||||
expect(result).toContain('1 个警告');
|
||||
});
|
||||
});
|
||||
|
||||
describe('shutdownLSP - 关闭 LSP', () => {
|
||||
it('关闭管理器', async () => {
|
||||
lspModule.initLSP();
|
||||
|
||||
await lspModule.shutdownLSP();
|
||||
|
||||
expect(mockShutdown).toHaveBeenCalled();
|
||||
expect(lspModule.getLSPManager()).toBeNull();
|
||||
});
|
||||
|
||||
it('未初始化时安全调用', async () => {
|
||||
vi.resetModules();
|
||||
const freshModule = await import('../../../src/lsp/index.js');
|
||||
|
||||
await expect(freshModule.shutdownLSP()).resolves.not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('导出', () => {
|
||||
it('导出 getLanguageId', () => {
|
||||
expect(lspModule.getLanguageId).toBeDefined();
|
||||
});
|
||||
|
||||
it('导出 isLanguageSupported', () => {
|
||||
expect(lspModule.isLanguageSupported).toBeDefined();
|
||||
});
|
||||
|
||||
it('导出 getSupportedExtensions', () => {
|
||||
expect(lspModule.getSupportedExtensions).toBeDefined();
|
||||
});
|
||||
|
||||
it('导出 LSPClientManager', () => {
|
||||
expect(lspModule.LSPClientManager).toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,201 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import {
|
||||
getLanguageId,
|
||||
isLanguageSupported,
|
||||
getSupportedExtensions,
|
||||
} from '../../../src/lsp/language.js';
|
||||
|
||||
describe('LSP Language - 语言识别', () => {
|
||||
describe('getLanguageId - 获取语言 ID', () => {
|
||||
describe('TypeScript/JavaScript', () => {
|
||||
it('识别 TypeScript 文件', () => {
|
||||
expect(getLanguageId('file.ts')).toBe('typescript');
|
||||
expect(getLanguageId('file.mts')).toBe('typescript');
|
||||
expect(getLanguageId('file.cts')).toBe('typescript');
|
||||
});
|
||||
|
||||
it('识别 TSX 文件', () => {
|
||||
expect(getLanguageId('file.tsx')).toBe('typescriptreact');
|
||||
});
|
||||
|
||||
it('识别 JavaScript 文件', () => {
|
||||
expect(getLanguageId('file.js')).toBe('javascript');
|
||||
expect(getLanguageId('file.mjs')).toBe('javascript');
|
||||
expect(getLanguageId('file.cjs')).toBe('javascript');
|
||||
});
|
||||
|
||||
it('识别 JSX 文件', () => {
|
||||
expect(getLanguageId('file.jsx')).toBe('javascriptreact');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Python', () => {
|
||||
it('识别 Python 文件', () => {
|
||||
expect(getLanguageId('script.py')).toBe('python');
|
||||
expect(getLanguageId('stub.pyi')).toBe('python');
|
||||
expect(getLanguageId('script.pyw')).toBe('python');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Go', () => {
|
||||
it('识别 Go 文件', () => {
|
||||
expect(getLanguageId('main.go')).toBe('go');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Rust', () => {
|
||||
it('识别 Rust 文件', () => {
|
||||
expect(getLanguageId('main.rs')).toBe('rust');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Java', () => {
|
||||
it('识别 Java 文件', () => {
|
||||
expect(getLanguageId('Main.java')).toBe('java');
|
||||
});
|
||||
});
|
||||
|
||||
describe('C/C++', () => {
|
||||
it('识别 C 文件', () => {
|
||||
expect(getLanguageId('main.c')).toBe('c');
|
||||
expect(getLanguageId('header.h')).toBe('c');
|
||||
});
|
||||
|
||||
it('识别 C++ 文件', () => {
|
||||
expect(getLanguageId('main.cpp')).toBe('cpp');
|
||||
expect(getLanguageId('main.cc')).toBe('cpp');
|
||||
expect(getLanguageId('main.cxx')).toBe('cpp');
|
||||
expect(getLanguageId('header.hpp')).toBe('cpp');
|
||||
expect(getLanguageId('header.hh')).toBe('cpp');
|
||||
expect(getLanguageId('header.hxx')).toBe('cpp');
|
||||
});
|
||||
});
|
||||
|
||||
describe('其他语言', () => {
|
||||
it('识别 C# 文件', () => {
|
||||
expect(getLanguageId('Program.cs')).toBe('csharp');
|
||||
});
|
||||
|
||||
it('识别 PHP 文件', () => {
|
||||
expect(getLanguageId('index.php')).toBe('php');
|
||||
});
|
||||
|
||||
it('识别 Ruby 文件', () => {
|
||||
expect(getLanguageId('app.rb')).toBe('ruby');
|
||||
expect(getLanguageId('task.rake')).toBe('ruby');
|
||||
});
|
||||
|
||||
it('识别 Swift 文件', () => {
|
||||
expect(getLanguageId('app.swift')).toBe('swift');
|
||||
});
|
||||
|
||||
it('识别 Kotlin 文件', () => {
|
||||
expect(getLanguageId('Main.kt')).toBe('kotlin');
|
||||
expect(getLanguageId('script.kts')).toBe('kotlin');
|
||||
});
|
||||
|
||||
it('识别 Scala 文件', () => {
|
||||
expect(getLanguageId('Main.scala')).toBe('scala');
|
||||
expect(getLanguageId('script.sc')).toBe('scala');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Web 技术', () => {
|
||||
it('识别 HTML 文件', () => {
|
||||
expect(getLanguageId('index.html')).toBe('html');
|
||||
expect(getLanguageId('page.htm')).toBe('html');
|
||||
});
|
||||
|
||||
it('识别 CSS 文件', () => {
|
||||
expect(getLanguageId('style.css')).toBe('css');
|
||||
expect(getLanguageId('style.scss')).toBe('scss');
|
||||
expect(getLanguageId('style.less')).toBe('less');
|
||||
});
|
||||
|
||||
it('识别框架文件', () => {
|
||||
expect(getLanguageId('App.vue')).toBe('vue');
|
||||
expect(getLanguageId('App.svelte')).toBe('svelte');
|
||||
});
|
||||
});
|
||||
|
||||
describe('数据格式', () => {
|
||||
it('识别 JSON 文件', () => {
|
||||
expect(getLanguageId('config.json')).toBe('json');
|
||||
});
|
||||
|
||||
it('识别 YAML 文件', () => {
|
||||
expect(getLanguageId('config.yaml')).toBe('yaml');
|
||||
expect(getLanguageId('config.yml')).toBe('yaml');
|
||||
});
|
||||
|
||||
it('识别 Markdown 文件', () => {
|
||||
expect(getLanguageId('README.md')).toBe('markdown');
|
||||
expect(getLanguageId('docs.markdown')).toBe('markdown');
|
||||
});
|
||||
});
|
||||
|
||||
describe('边缘情况', () => {
|
||||
it('处理完整路径', () => {
|
||||
expect(getLanguageId('/path/to/file.ts')).toBe('typescript');
|
||||
expect(getLanguageId('./relative/path/file.py')).toBe('python');
|
||||
});
|
||||
|
||||
it('处理大写扩展名', () => {
|
||||
expect(getLanguageId('file.TS')).toBe('typescript');
|
||||
expect(getLanguageId('file.JS')).toBe('javascript');
|
||||
expect(getLanguageId('file.PY')).toBe('python');
|
||||
});
|
||||
|
||||
it('未知扩展名返回 undefined', () => {
|
||||
expect(getLanguageId('file.xyz')).toBeUndefined();
|
||||
expect(getLanguageId('file.unknown')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('无扩展名返回 undefined', () => {
|
||||
expect(getLanguageId('Makefile')).toBeUndefined();
|
||||
expect(getLanguageId('Dockerfile')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('处理多点文件名', () => {
|
||||
expect(getLanguageId('file.test.ts')).toBe('typescript');
|
||||
expect(getLanguageId('app.module.js')).toBe('javascript');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('isLanguageSupported - 检查语言支持', () => {
|
||||
it('支持的语言返回 true', () => {
|
||||
expect(isLanguageSupported('file.ts')).toBe(true);
|
||||
expect(isLanguageSupported('file.py')).toBe(true);
|
||||
expect(isLanguageSupported('file.go')).toBe(true);
|
||||
});
|
||||
|
||||
it('不支持的语言返回 false', () => {
|
||||
expect(isLanguageSupported('file.xyz')).toBe(false);
|
||||
expect(isLanguageSupported('Makefile')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getSupportedExtensions - 获取支持的扩展名', () => {
|
||||
it('返回非空数组', () => {
|
||||
const extensions = getSupportedExtensions();
|
||||
expect(Array.isArray(extensions)).toBe(true);
|
||||
expect(extensions.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('包含常见扩展名', () => {
|
||||
const extensions = getSupportedExtensions();
|
||||
expect(extensions).toContain('.ts');
|
||||
expect(extensions).toContain('.js');
|
||||
expect(extensions).toContain('.py');
|
||||
expect(extensions).toContain('.go');
|
||||
});
|
||||
|
||||
it('所有扩展名以点开头', () => {
|
||||
const extensions = getSupportedExtensions();
|
||||
for (const ext of extensions) {
|
||||
expect(ext.startsWith('.')).toBe(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,195 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import {
|
||||
getServerConfig,
|
||||
hasServerConfig,
|
||||
getSupportedLanguages,
|
||||
getAllServerConfigs,
|
||||
getUniqueServers,
|
||||
} from '../../../src/lsp/server.js';
|
||||
|
||||
describe('LSP Server - 语言服务器配置', () => {
|
||||
describe('getServerConfig - 获取服务器配置', () => {
|
||||
it('返回 TypeScript 配置', () => {
|
||||
const config = getServerConfig('typescript');
|
||||
|
||||
expect(config).toBeDefined();
|
||||
expect(config?.command).toBe('typescript-language-server');
|
||||
expect(config?.args).toContain('--stdio');
|
||||
expect(config?.displayName).toBe('TypeScript');
|
||||
});
|
||||
|
||||
it('返回 JavaScript 配置(共用 TypeScript)', () => {
|
||||
const config = getServerConfig('javascript');
|
||||
|
||||
expect(config).toBeDefined();
|
||||
expect(config?.command).toBe('typescript-language-server');
|
||||
});
|
||||
|
||||
it('返回 Python 配置', () => {
|
||||
const config = getServerConfig('python');
|
||||
|
||||
expect(config).toBeDefined();
|
||||
expect(config?.command).toBe('pyright-langserver');
|
||||
expect(config?.install.npm).toBe('pyright');
|
||||
});
|
||||
|
||||
it('返回 Go 配置', () => {
|
||||
const config = getServerConfig('go');
|
||||
|
||||
expect(config).toBeDefined();
|
||||
expect(config?.command).toBe('gopls');
|
||||
expect(config?.install.go).toContain('gopls');
|
||||
});
|
||||
|
||||
it('返回 Rust 配置', () => {
|
||||
const config = getServerConfig('rust');
|
||||
|
||||
expect(config).toBeDefined();
|
||||
expect(config?.command).toBe('rust-analyzer');
|
||||
expect(config?.install.rustup).toBe('rust-analyzer');
|
||||
});
|
||||
|
||||
it('返回 C/C++ 配置', () => {
|
||||
const cConfig = getServerConfig('c');
|
||||
const cppConfig = getServerConfig('cpp');
|
||||
|
||||
expect(cConfig?.command).toBe('clangd');
|
||||
expect(cppConfig?.command).toBe('clangd');
|
||||
});
|
||||
|
||||
it('返回 Vue 配置', () => {
|
||||
const config = getServerConfig('vue');
|
||||
|
||||
expect(config).toBeDefined();
|
||||
expect(config?.command).toBe('vue-language-server');
|
||||
});
|
||||
|
||||
it('返回 HTML/CSS/JSON 配置', () => {
|
||||
expect(getServerConfig('html')?.command).toBe('vscode-html-language-server');
|
||||
expect(getServerConfig('css')?.command).toBe('vscode-css-language-server');
|
||||
expect(getServerConfig('json')?.command).toBe('vscode-json-language-server');
|
||||
});
|
||||
|
||||
it('不支持的语言返回 undefined', () => {
|
||||
const config = getServerConfig('unknown' as any);
|
||||
|
||||
expect(config).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('hasServerConfig - 检查服务器配置', () => {
|
||||
it('已配置的语言返回 true', () => {
|
||||
expect(hasServerConfig('typescript')).toBe(true);
|
||||
expect(hasServerConfig('python')).toBe(true);
|
||||
expect(hasServerConfig('go')).toBe(true);
|
||||
expect(hasServerConfig('rust')).toBe(true);
|
||||
});
|
||||
|
||||
it('未配置的语言返回 false', () => {
|
||||
expect(hasServerConfig('unknown' as any)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getSupportedLanguages - 获取支持的语言', () => {
|
||||
it('返回所有支持的语言 ID', () => {
|
||||
const languages = getSupportedLanguages();
|
||||
|
||||
expect(Array.isArray(languages)).toBe(true);
|
||||
expect(languages.length).toBeGreaterThan(0);
|
||||
expect(languages).toContain('typescript');
|
||||
expect(languages).toContain('javascript');
|
||||
expect(languages).toContain('python');
|
||||
expect(languages).toContain('go');
|
||||
expect(languages).toContain('rust');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getAllServerConfigs - 获取所有配置', () => {
|
||||
it('返回所有服务器配置对象', () => {
|
||||
const configs = getAllServerConfigs();
|
||||
|
||||
expect(typeof configs).toBe('object');
|
||||
expect(configs.typescript).toBeDefined();
|
||||
expect(configs.python).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getUniqueServers - 获取唯一服务器列表', () => {
|
||||
it('返回去重后的服务器列表', () => {
|
||||
const servers = getUniqueServers();
|
||||
|
||||
expect(Array.isArray(servers)).toBe(true);
|
||||
|
||||
// typescript-language-server 被多个语言共用
|
||||
const tsServer = servers.find((s) => s.id === 'typescript-language-server');
|
||||
expect(tsServer).toBeDefined();
|
||||
expect(tsServer?.languages).toContain('typescript');
|
||||
expect(tsServer?.languages).toContain('javascript');
|
||||
});
|
||||
|
||||
it('每个服务器包含必要字段', () => {
|
||||
const servers = getUniqueServers();
|
||||
|
||||
for (const server of servers) {
|
||||
expect(server.id).toBeDefined();
|
||||
expect(server.config).toBeDefined();
|
||||
expect(server.languages).toBeDefined();
|
||||
expect(Array.isArray(server.languages)).toBe(true);
|
||||
expect(server.languages.length).toBeGreaterThan(0);
|
||||
}
|
||||
});
|
||||
|
||||
it('clangd 被 C 和 C++ 共用', () => {
|
||||
const servers = getUniqueServers();
|
||||
const clangd = servers.find((s) => s.id === 'clangd');
|
||||
|
||||
expect(clangd).toBeDefined();
|
||||
expect(clangd?.languages).toContain('c');
|
||||
expect(clangd?.languages).toContain('cpp');
|
||||
});
|
||||
|
||||
it('vscode-css-language-server 被 CSS/SCSS/Less 共用', () => {
|
||||
const servers = getUniqueServers();
|
||||
const cssServer = servers.find((s) => s.id === 'vscode-css-language-server');
|
||||
|
||||
expect(cssServer).toBeDefined();
|
||||
expect(cssServer?.languages).toContain('css');
|
||||
expect(cssServer?.languages).toContain('scss');
|
||||
expect(cssServer?.languages).toContain('less');
|
||||
});
|
||||
});
|
||||
|
||||
describe('安装配置', () => {
|
||||
it('TypeScript 服务器有 npm 安装配置', () => {
|
||||
const config = getServerConfig('typescript');
|
||||
|
||||
expect(config?.install.npm).toContain('typescript-language-server');
|
||||
});
|
||||
|
||||
it('Python 服务器有多种安装方式', () => {
|
||||
const config = getServerConfig('python');
|
||||
|
||||
expect(config?.install.npm).toBe('pyright');
|
||||
expect(config?.install.pip).toBe('pyright');
|
||||
});
|
||||
|
||||
it('Go 服务器有 go install 配置', () => {
|
||||
const config = getServerConfig('go');
|
||||
|
||||
expect(config?.install.go).toContain('gopls');
|
||||
});
|
||||
|
||||
it('Rust 服务器有 rustup 和 brew 安装配置', () => {
|
||||
const config = getServerConfig('rust');
|
||||
|
||||
expect(config?.install.rustup).toBe('rust-analyzer');
|
||||
expect(config?.install.brew).toBe('rust-analyzer');
|
||||
});
|
||||
|
||||
it('Ruby 服务器有 gem 安装配置', () => {
|
||||
const config = getServerConfig('ruby');
|
||||
|
||||
expect(config?.install.gem).toBe('solargraph');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,351 @@
|
||||
/**
|
||||
* MCP 配置加载和验证测试
|
||||
*/
|
||||
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import {
|
||||
loadMCPConfig,
|
||||
validateMCPConfig,
|
||||
normalizeMCPConfig,
|
||||
getEnabledServers,
|
||||
isToolEnabled,
|
||||
resolveEnvVariables,
|
||||
resolveEnvInObject,
|
||||
} from '../../../src/mcp/config.js';
|
||||
import type { MCPConfig } from '../../../src/mcp/types.js';
|
||||
|
||||
// Mock fs 模块
|
||||
vi.mock('fs');
|
||||
vi.mock('path', async () => {
|
||||
const actual = await vi.importActual('path');
|
||||
return {
|
||||
...actual,
|
||||
extname: (actual as typeof import('path')).extname,
|
||||
};
|
||||
});
|
||||
|
||||
describe('MCP Config', () => {
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
// 默认不存在配置文件
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe('resolveEnvVariables', () => {
|
||||
it('应该解析环境变量引用', () => {
|
||||
process.env.TEST_VAR = 'test_value';
|
||||
const result = resolveEnvVariables('{env:TEST_VAR}');
|
||||
expect(result).toBe('test_value');
|
||||
delete process.env.TEST_VAR;
|
||||
});
|
||||
|
||||
it('应该处理多个环境变量', () => {
|
||||
process.env.VAR1 = 'value1';
|
||||
process.env.VAR2 = 'value2';
|
||||
const result = resolveEnvVariables('{env:VAR1}/{env:VAR2}');
|
||||
expect(result).toBe('value1/value2');
|
||||
delete process.env.VAR1;
|
||||
delete process.env.VAR2;
|
||||
});
|
||||
|
||||
it('应该对未设置的变量返回空字符串', () => {
|
||||
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
const result = resolveEnvVariables('{env:NONEXISTENT_VAR}');
|
||||
expect(result).toBe('');
|
||||
expect(warnSpy).toHaveBeenCalled();
|
||||
warnSpy.mockRestore();
|
||||
});
|
||||
|
||||
it('应该保留普通字符串不变', () => {
|
||||
const result = resolveEnvVariables('plain string');
|
||||
expect(result).toBe('plain string');
|
||||
});
|
||||
});
|
||||
|
||||
describe('resolveEnvInObject', () => {
|
||||
it('应该递归解析对象中的环境变量', () => {
|
||||
process.env.TEST_KEY = 'secret';
|
||||
const obj = {
|
||||
key: '{env:TEST_KEY}',
|
||||
nested: {
|
||||
value: '{env:TEST_KEY}',
|
||||
},
|
||||
array: ['{env:TEST_KEY}'],
|
||||
};
|
||||
const result = resolveEnvInObject(obj);
|
||||
expect(result).toEqual({
|
||||
key: 'secret',
|
||||
nested: {
|
||||
value: 'secret',
|
||||
},
|
||||
array: ['secret'],
|
||||
});
|
||||
delete process.env.TEST_KEY;
|
||||
});
|
||||
|
||||
it('应该保留非字符串值不变', () => {
|
||||
const obj = {
|
||||
number: 123,
|
||||
boolean: true,
|
||||
null: null,
|
||||
};
|
||||
const result = resolveEnvInObject(obj);
|
||||
expect(result).toEqual(obj);
|
||||
});
|
||||
});
|
||||
|
||||
describe('validateMCPConfig', () => {
|
||||
it('应该接受空配置', () => {
|
||||
const errors = validateMCPConfig({});
|
||||
expect(errors).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('应该接受有效的本地服务器配置', () => {
|
||||
const config: MCPConfig = {
|
||||
mcp: {
|
||||
filesystem: {
|
||||
type: 'local',
|
||||
command: ['npx', '-y', '@anthropic-ai/mcp-server-filesystem'],
|
||||
},
|
||||
},
|
||||
};
|
||||
const errors = validateMCPConfig(config);
|
||||
expect(errors).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('应该接受有效的远程服务器配置', () => {
|
||||
const config: MCPConfig = {
|
||||
mcp: {
|
||||
remote: {
|
||||
type: 'remote',
|
||||
url: 'https://mcp.example.com',
|
||||
},
|
||||
},
|
||||
};
|
||||
const errors = validateMCPConfig(config);
|
||||
expect(errors).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('应该拒绝无效的服务器名称', () => {
|
||||
const config: MCPConfig = {
|
||||
mcp: {
|
||||
'123invalid': {
|
||||
type: 'local',
|
||||
command: ['echo'],
|
||||
},
|
||||
},
|
||||
};
|
||||
const errors = validateMCPConfig(config);
|
||||
expect(errors.length).toBeGreaterThan(0);
|
||||
expect(errors[0]).toContain('服务器名称');
|
||||
});
|
||||
|
||||
it('应该拒绝缺少 command 的本地服务器', () => {
|
||||
const config: MCPConfig = {
|
||||
mcp: {
|
||||
server: {
|
||||
type: 'local',
|
||||
command: [],
|
||||
},
|
||||
},
|
||||
};
|
||||
const errors = validateMCPConfig(config);
|
||||
expect(errors.length).toBeGreaterThan(0);
|
||||
expect(errors[0]).toContain('command');
|
||||
});
|
||||
|
||||
it('应该拒绝无效的 URL', () => {
|
||||
const config: MCPConfig = {
|
||||
mcp: {
|
||||
remote: {
|
||||
type: 'remote',
|
||||
url: 'invalid-url',
|
||||
},
|
||||
},
|
||||
};
|
||||
const errors = validateMCPConfig(config);
|
||||
expect(errors.length).toBeGreaterThan(0);
|
||||
expect(errors[0]).toContain('url');
|
||||
});
|
||||
|
||||
it('应该验证 tools 配置', () => {
|
||||
const config: MCPConfig = {
|
||||
mcp: {},
|
||||
tools: {
|
||||
'valid-tool': true,
|
||||
'invalid-tool': 'not-boolean' as unknown as boolean,
|
||||
},
|
||||
};
|
||||
const errors = validateMCPConfig(config);
|
||||
expect(errors.length).toBeGreaterThan(0);
|
||||
expect(errors[0]).toContain('布尔值');
|
||||
});
|
||||
});
|
||||
|
||||
describe('normalizeMCPConfig', () => {
|
||||
it('应该添加默认值', () => {
|
||||
const config: MCPConfig = {
|
||||
mcp: {
|
||||
server: {
|
||||
type: 'local',
|
||||
command: ['echo'],
|
||||
},
|
||||
},
|
||||
};
|
||||
const normalized = normalizeMCPConfig(config);
|
||||
expect(normalized.mcp?.server.enabled).toBe(true);
|
||||
expect(normalized.mcp?.server.timeout).toBe(30000);
|
||||
});
|
||||
|
||||
it('应该保留显式设置的值', () => {
|
||||
const config: MCPConfig = {
|
||||
mcp: {
|
||||
server: {
|
||||
type: 'local',
|
||||
command: ['echo'],
|
||||
enabled: false,
|
||||
timeout: 5000,
|
||||
},
|
||||
},
|
||||
};
|
||||
const normalized = normalizeMCPConfig(config);
|
||||
expect(normalized.mcp?.server.enabled).toBe(false);
|
||||
expect(normalized.mcp?.server.timeout).toBe(5000);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getEnabledServers', () => {
|
||||
it('应该返回启用的服务器', () => {
|
||||
const config: MCPConfig = {
|
||||
mcp: {
|
||||
enabled: {
|
||||
type: 'local',
|
||||
command: ['echo'],
|
||||
enabled: true,
|
||||
},
|
||||
disabled: {
|
||||
type: 'local',
|
||||
command: ['echo'],
|
||||
enabled: false,
|
||||
},
|
||||
default: {
|
||||
type: 'local',
|
||||
command: ['echo'],
|
||||
},
|
||||
},
|
||||
};
|
||||
const servers = getEnabledServers(config);
|
||||
expect(servers).toHaveLength(2);
|
||||
expect(servers.map((s) => s.name)).toContain('enabled');
|
||||
expect(servers.map((s) => s.name)).toContain('default');
|
||||
expect(servers.map((s) => s.name)).not.toContain('disabled');
|
||||
});
|
||||
|
||||
it('应该处理空配置', () => {
|
||||
const servers = getEnabledServers({});
|
||||
expect(servers).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isToolEnabled', () => {
|
||||
it('没有配置时默认启用', () => {
|
||||
expect(isToolEnabled('any-tool')).toBe(true);
|
||||
expect(isToolEnabled('any-tool', undefined)).toBe(true);
|
||||
});
|
||||
|
||||
it('应该精确匹配工具名', () => {
|
||||
const config = {
|
||||
'server-tool': false,
|
||||
'server-other': true,
|
||||
};
|
||||
expect(isToolEnabled('server-tool', config)).toBe(false);
|
||||
expect(isToolEnabled('server-other', config)).toBe(true);
|
||||
});
|
||||
|
||||
it('应该支持通配符匹配', () => {
|
||||
const config = {
|
||||
'server-*': false,
|
||||
};
|
||||
expect(isToolEnabled('server-tool1', config)).toBe(false);
|
||||
expect(isToolEnabled('server-tool2', config)).toBe(false);
|
||||
expect(isToolEnabled('other-tool', config)).toBe(true);
|
||||
});
|
||||
|
||||
it('精确匹配应优先于通配符', () => {
|
||||
const config = {
|
||||
'server-*': false,
|
||||
'server-special': true,
|
||||
};
|
||||
expect(isToolEnabled('server-special', config)).toBe(true);
|
||||
expect(isToolEnabled('server-other', config)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadMCPConfig', () => {
|
||||
it('没有配置文件时返回空配置', () => {
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
const config = loadMCPConfig('/test/dir');
|
||||
expect(config).toEqual({});
|
||||
});
|
||||
|
||||
it('应该加载 JSON 配置文件', () => {
|
||||
const testConfig = {
|
||||
mcp: {
|
||||
server: {
|
||||
type: 'local',
|
||||
command: ['echo'],
|
||||
},
|
||||
},
|
||||
};
|
||||
vi.mocked(fs.existsSync).mockImplementation((filePath) => {
|
||||
return String(filePath).endsWith('config.json');
|
||||
});
|
||||
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify(testConfig));
|
||||
|
||||
const config = loadMCPConfig('/test/dir');
|
||||
expect(config.mcp).toBeDefined();
|
||||
});
|
||||
|
||||
it('应该合并用户级和项目级配置', () => {
|
||||
const userConfig = {
|
||||
mcp: {
|
||||
userServer: {
|
||||
type: 'local',
|
||||
command: ['user-cmd'],
|
||||
},
|
||||
},
|
||||
};
|
||||
const projectConfig = {
|
||||
mcp: {
|
||||
projectServer: {
|
||||
type: 'local',
|
||||
command: ['project-cmd'],
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
let callCount = 0;
|
||||
vi.mocked(fs.existsSync).mockImplementation((filePath) => {
|
||||
return (
|
||||
String(filePath).includes('.ai-assist') &&
|
||||
String(filePath).endsWith('config.json')
|
||||
);
|
||||
});
|
||||
vi.mocked(fs.readFileSync).mockImplementation(() => {
|
||||
callCount++;
|
||||
// 第一次调用返回用户配置,第二次返回项目配置
|
||||
return JSON.stringify(callCount === 1 ? userConfig : projectConfig);
|
||||
});
|
||||
|
||||
const config = loadMCPConfig('/test/dir');
|
||||
expect(config.mcp?.userServer).toBeDefined();
|
||||
expect(config.mcp?.projectServer).toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,157 @@
|
||||
/**
|
||||
* MCP Manager 测试
|
||||
* 测试不需要实际连接的功能
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, afterEach } from 'vitest';
|
||||
import { MCPManager, getMCPManager, resetMCPManager } from '../../../src/mcp/manager.js';
|
||||
import type { MCPConfig } from '../../../src/mcp/types.js';
|
||||
|
||||
describe('MCPManager', () => {
|
||||
afterEach(async () => {
|
||||
resetMCPManager();
|
||||
});
|
||||
|
||||
describe('基础状态', () => {
|
||||
it('初始状态应该是未初始化', () => {
|
||||
const manager = new MCPManager();
|
||||
expect(manager.isInitialized()).toBe(false);
|
||||
});
|
||||
|
||||
it('初始时没有工具', () => {
|
||||
const manager = new MCPManager();
|
||||
expect(manager.getTools()).toEqual([]);
|
||||
});
|
||||
|
||||
it('初始时没有服务器状态', () => {
|
||||
const manager = new MCPManager();
|
||||
expect(manager.getServerStatuses()).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('配置处理', () => {
|
||||
it('空配置不应该连接任何服务器', async () => {
|
||||
const manager = new MCPManager();
|
||||
await manager.initialize({});
|
||||
expect(manager.isInitialized()).toBe(true);
|
||||
expect(manager.getTools()).toEqual([]);
|
||||
});
|
||||
|
||||
it('禁用的服务器应该显示在状态中', async () => {
|
||||
const manager = new MCPManager();
|
||||
const config: MCPConfig = {
|
||||
mcp: {
|
||||
disabled: {
|
||||
type: 'local',
|
||||
command: ['echo'],
|
||||
enabled: false,
|
||||
},
|
||||
},
|
||||
};
|
||||
await manager.initialize(config);
|
||||
const statuses = manager.getServerStatuses();
|
||||
expect(statuses).toHaveLength(1);
|
||||
expect(statuses[0].name).toBe('disabled');
|
||||
expect(statuses[0].status).toBe('disabled');
|
||||
});
|
||||
});
|
||||
|
||||
describe('工具名解析', () => {
|
||||
it('无效的工具名格式应返回错误', async () => {
|
||||
const manager = new MCPManager();
|
||||
await manager.initialize({});
|
||||
|
||||
const result = await manager.callTool('invalidname', {});
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.isError).toBe(true);
|
||||
expect((result.content[0] as { text: string }).text).toContain('Invalid tool name');
|
||||
});
|
||||
|
||||
it('服务器未连接时应返回错误', async () => {
|
||||
const manager = new MCPManager();
|
||||
await manager.initialize({});
|
||||
|
||||
const result = await manager.callTool('server-tool', {});
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.isError).toBe(true);
|
||||
expect((result.content[0] as { text: string }).text).toContain('not connected');
|
||||
});
|
||||
});
|
||||
|
||||
describe('reconnect', () => {
|
||||
it('服务器不存在时应抛出错误', async () => {
|
||||
const manager = new MCPManager();
|
||||
await manager.initialize({ mcp: {} });
|
||||
|
||||
await expect(manager.reconnect('nonexistent')).rejects.toThrow('Server not found');
|
||||
});
|
||||
});
|
||||
|
||||
describe('setServerEnabled', () => {
|
||||
it('服务器不存在时应抛出错误', async () => {
|
||||
const manager = new MCPManager();
|
||||
await manager.initialize({ mcp: {} });
|
||||
|
||||
await expect(manager.setServerEnabled('nonexistent', true)).rejects.toThrow(
|
||||
'Server not found'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('shutdown', () => {
|
||||
it('空初始化后可以安全关闭', async () => {
|
||||
const manager = new MCPManager();
|
||||
await manager.initialize({});
|
||||
await manager.shutdown();
|
||||
expect(manager.isInitialized()).toBe(false);
|
||||
});
|
||||
|
||||
it('未初始化时可以安全关闭', async () => {
|
||||
const manager = new MCPManager();
|
||||
await manager.shutdown();
|
||||
expect(manager.isInitialized()).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('事件发射', () => {
|
||||
it('应该是 EventEmitter 实例', () => {
|
||||
const manager = new MCPManager();
|
||||
expect(typeof manager.on).toBe('function');
|
||||
expect(typeof manager.emit).toBe('function');
|
||||
});
|
||||
|
||||
it('应该支持事件监听', () => {
|
||||
const manager = new MCPManager();
|
||||
const callback = vi.fn();
|
||||
manager.on('tools:changed', callback);
|
||||
manager.emit('tools:changed');
|
||||
expect(callback).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getMCPManager / resetMCPManager', () => {
|
||||
afterEach(() => {
|
||||
resetMCPManager();
|
||||
});
|
||||
|
||||
it('应该返回单例实例', () => {
|
||||
const manager1 = getMCPManager();
|
||||
const manager2 = getMCPManager();
|
||||
expect(manager1).toBe(manager2);
|
||||
});
|
||||
|
||||
it('重置后应该返回新实例', () => {
|
||||
const manager1 = getMCPManager();
|
||||
resetMCPManager();
|
||||
const manager2 = getMCPManager();
|
||||
expect(manager1).not.toBe(manager2);
|
||||
});
|
||||
|
||||
it('重置的实例应该是未初始化状态', () => {
|
||||
const manager1 = getMCPManager();
|
||||
resetMCPManager();
|
||||
const manager2 = getMCPManager();
|
||||
expect(manager2.isInitialized()).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,289 @@
|
||||
/**
|
||||
* MCP 工具适配器测试
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { MCPToolAdapter, createMCPToolAdapter } from '../../../src/mcp/tool-adapter.js';
|
||||
import type { MCPTool, MCPManager, MCPToolCallResult } from '../../../src/mcp/types.js';
|
||||
|
||||
// 创建 mock MCPManager
|
||||
function createMockManager(): MCPManager {
|
||||
return {
|
||||
callTool: vi.fn(),
|
||||
getTools: vi.fn().mockReturnValue([]),
|
||||
getServerStatuses: vi.fn().mockReturnValue([]),
|
||||
initialize: vi.fn(),
|
||||
shutdown: vi.fn(),
|
||||
on: vi.fn(),
|
||||
emit: vi.fn(),
|
||||
} as unknown as MCPManager;
|
||||
}
|
||||
|
||||
describe('MCPToolAdapter', () => {
|
||||
let mockManager: MCPManager;
|
||||
let adapter: MCPToolAdapter;
|
||||
|
||||
beforeEach(() => {
|
||||
mockManager = createMockManager();
|
||||
adapter = new MCPToolAdapter(mockManager);
|
||||
});
|
||||
|
||||
describe('adaptToInternalTool', () => {
|
||||
it('应该将 MCP 工具转换为内部格式', () => {
|
||||
const mcpTool: MCPTool = {
|
||||
server: 'filesystem',
|
||||
name: 'filesystem-read_file',
|
||||
originalName: 'read_file',
|
||||
description: 'Read a file from the filesystem',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
path: {
|
||||
type: 'string',
|
||||
description: 'Path to the file',
|
||||
},
|
||||
},
|
||||
required: ['path'],
|
||||
},
|
||||
};
|
||||
|
||||
const internalTool = adapter.adaptToInternalTool(mcpTool);
|
||||
|
||||
expect(internalTool.name).toBe('filesystem-read_file');
|
||||
expect(internalTool.description).toBe('Read a file from the filesystem');
|
||||
expect(internalTool.parameters).toHaveProperty('path');
|
||||
expect(internalTool.parameters.path.type).toBe('string');
|
||||
expect(internalTool.parameters.path.required).toBe(true);
|
||||
expect(internalTool.metadata.category).toBe('agent');
|
||||
expect(internalTool.metadata.keywords).toContain('mcp');
|
||||
expect(internalTool.metadata.keywords).toContain('filesystem');
|
||||
});
|
||||
|
||||
it('应该正确转换参数类型', () => {
|
||||
const mcpTool: MCPTool = {
|
||||
server: 'test',
|
||||
name: 'test-tool',
|
||||
originalName: 'tool',
|
||||
description: 'Test tool',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
stringParam: { type: 'string' },
|
||||
numberParam: { type: 'number' },
|
||||
integerParam: { type: 'integer' },
|
||||
booleanParam: { type: 'boolean' },
|
||||
arrayParam: { type: 'array' },
|
||||
objectParam: { type: 'object' },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const internalTool = adapter.adaptToInternalTool(mcpTool);
|
||||
|
||||
expect(internalTool.parameters.stringParam.type).toBe('string');
|
||||
expect(internalTool.parameters.numberParam.type).toBe('number');
|
||||
expect(internalTool.parameters.integerParam.type).toBe('number');
|
||||
expect(internalTool.parameters.booleanParam.type).toBe('boolean');
|
||||
expect(internalTool.parameters.arrayParam.type).toBe('array');
|
||||
expect(internalTool.parameters.objectParam.type).toBe('object');
|
||||
});
|
||||
|
||||
it('应该处理联合类型', () => {
|
||||
const mcpTool: MCPTool = {
|
||||
server: 'test',
|
||||
name: 'test-tool',
|
||||
originalName: 'tool',
|
||||
description: 'Test tool',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
nullable: { type: ['string', 'null'] },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const internalTool = adapter.adaptToInternalTool(mcpTool);
|
||||
expect(internalTool.parameters.nullable.type).toBe('string');
|
||||
});
|
||||
|
||||
it('应该处理空 inputSchema', () => {
|
||||
const mcpTool: MCPTool = {
|
||||
server: 'test',
|
||||
name: 'test-tool',
|
||||
originalName: 'tool',
|
||||
description: 'Test tool',
|
||||
inputSchema: {},
|
||||
};
|
||||
|
||||
const internalTool = adapter.adaptToInternalTool(mcpTool);
|
||||
expect(internalTool.parameters).toEqual({});
|
||||
});
|
||||
|
||||
it('execute 应该调用 manager.callTool', async () => {
|
||||
const mcpTool: MCPTool = {
|
||||
server: 'test',
|
||||
name: 'test-tool',
|
||||
originalName: 'tool',
|
||||
description: 'Test tool',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
arg: { type: 'string' },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const mockResult: MCPToolCallResult = {
|
||||
success: true,
|
||||
content: [{ type: 'text', text: 'result' }],
|
||||
isError: false,
|
||||
};
|
||||
vi.mocked(mockManager.callTool).mockResolvedValue(mockResult);
|
||||
|
||||
const internalTool = adapter.adaptToInternalTool(mcpTool);
|
||||
const result = await internalTool.execute({ arg: 'value' });
|
||||
|
||||
expect(mockManager.callTool).toHaveBeenCalledWith('test-tool', {
|
||||
arg: 'value',
|
||||
});
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toBe('result');
|
||||
});
|
||||
|
||||
it('execute 应该处理错误结果', async () => {
|
||||
const mcpTool: MCPTool = {
|
||||
server: 'test',
|
||||
name: 'test-tool',
|
||||
originalName: 'tool',
|
||||
description: 'Test tool',
|
||||
inputSchema: {},
|
||||
};
|
||||
|
||||
const mockResult: MCPToolCallResult = {
|
||||
success: false,
|
||||
content: [{ type: 'text', text: 'error message' }],
|
||||
isError: true,
|
||||
};
|
||||
vi.mocked(mockManager.callTool).mockResolvedValue(mockResult);
|
||||
|
||||
const internalTool = adapter.adaptToInternalTool(mcpTool);
|
||||
const result = await internalTool.execute({});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBe('error message');
|
||||
});
|
||||
});
|
||||
|
||||
describe('adaptTools', () => {
|
||||
it('应该批量转换工具', () => {
|
||||
const mcpTools: MCPTool[] = [
|
||||
{
|
||||
server: 'server1',
|
||||
name: 'server1-tool1',
|
||||
originalName: 'tool1',
|
||||
description: 'Tool 1',
|
||||
inputSchema: {},
|
||||
},
|
||||
{
|
||||
server: 'server2',
|
||||
name: 'server2-tool2',
|
||||
originalName: 'tool2',
|
||||
description: 'Tool 2',
|
||||
inputSchema: {},
|
||||
},
|
||||
];
|
||||
|
||||
const internalTools = adapter.adaptTools(mcpTools);
|
||||
|
||||
expect(internalTools).toHaveLength(2);
|
||||
expect(internalTools[0].name).toBe('server1-tool1');
|
||||
expect(internalTools[1].name).toBe('server2-tool2');
|
||||
});
|
||||
});
|
||||
|
||||
describe('createMCPToolAdapter', () => {
|
||||
it('应该创建适配器实例', () => {
|
||||
const adapter = createMCPToolAdapter(mockManager);
|
||||
expect(adapter).toBeInstanceOf(MCPToolAdapter);
|
||||
});
|
||||
});
|
||||
|
||||
describe('内容转换', () => {
|
||||
it('应该处理图片内容', async () => {
|
||||
const mcpTool: MCPTool = {
|
||||
server: 'test',
|
||||
name: 'test-tool',
|
||||
originalName: 'tool',
|
||||
description: 'Test tool',
|
||||
inputSchema: {},
|
||||
};
|
||||
|
||||
const mockResult: MCPToolCallResult = {
|
||||
success: true,
|
||||
content: [{ type: 'image', data: 'base64data', mimeType: 'image/png' }],
|
||||
isError: false,
|
||||
};
|
||||
vi.mocked(mockManager.callTool).mockResolvedValue(mockResult);
|
||||
|
||||
const internalTool = adapter.adaptToInternalTool(mcpTool);
|
||||
const result = await internalTool.execute({});
|
||||
|
||||
expect(result.output).toBe('[Image: image/png]');
|
||||
});
|
||||
|
||||
it('应该处理资源内容', async () => {
|
||||
const mcpTool: MCPTool = {
|
||||
server: 'test',
|
||||
name: 'test-tool',
|
||||
originalName: 'tool',
|
||||
description: 'Test tool',
|
||||
inputSchema: {},
|
||||
};
|
||||
|
||||
const mockResult: MCPToolCallResult = {
|
||||
success: true,
|
||||
content: [
|
||||
{
|
||||
type: 'resource',
|
||||
resource: {
|
||||
uri: 'file:///test.txt',
|
||||
text: 'file content',
|
||||
},
|
||||
},
|
||||
],
|
||||
isError: false,
|
||||
};
|
||||
vi.mocked(mockManager.callTool).mockResolvedValue(mockResult);
|
||||
|
||||
const internalTool = adapter.adaptToInternalTool(mcpTool);
|
||||
const result = await internalTool.execute({});
|
||||
|
||||
expect(result.output).toBe('file content');
|
||||
});
|
||||
|
||||
it('应该处理混合内容', async () => {
|
||||
const mcpTool: MCPTool = {
|
||||
server: 'test',
|
||||
name: 'test-tool',
|
||||
originalName: 'tool',
|
||||
description: 'Test tool',
|
||||
inputSchema: {},
|
||||
};
|
||||
|
||||
const mockResult: MCPToolCallResult = {
|
||||
success: true,
|
||||
content: [
|
||||
{ type: 'text', text: 'line 1' },
|
||||
{ type: 'text', text: 'line 2' },
|
||||
],
|
||||
isError: false,
|
||||
};
|
||||
vi.mocked(mockManager.callTool).mockResolvedValue(mockResult);
|
||||
|
||||
const internalTool = adapter.adaptToInternalTool(mcpTool);
|
||||
const result = await internalTool.execute({});
|
||||
|
||||
expect(result.output).toBe('line 1\nline 2');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,288 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { BashPermissionChecker } from '../../../src/permission/checkers/bash.js';
|
||||
import type { PermissionDecision, PermissionContext } from '../../../src/permission/types.js';
|
||||
|
||||
// Mock fs 和 path 以避免实际文件操作
|
||||
vi.mock('fs', () => ({
|
||||
existsSync: vi.fn(() => false),
|
||||
readFileSync: vi.fn(),
|
||||
writeFileSync: vi.fn(),
|
||||
mkdirSync: vi.fn(),
|
||||
}));
|
||||
|
||||
describe('BashPermissionChecker - Bash 权限检查器', () => {
|
||||
let checker: BashPermissionChecker;
|
||||
const testProjectRoot = '/test/project';
|
||||
|
||||
beforeEach(() => {
|
||||
checker = new BashPermissionChecker(testProjectRoot);
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('默认配置', () => {
|
||||
it('加载默认配置', () => {
|
||||
const config = checker.getConfig();
|
||||
|
||||
expect(config.rules).toBeDefined();
|
||||
expect(config.rules.length).toBeGreaterThan(0);
|
||||
expect(config.default).toBe('ask');
|
||||
expect(config.externalDirectory).toBe('ask');
|
||||
});
|
||||
|
||||
it('默认规则包含安全命令', () => {
|
||||
const config = checker.getConfig();
|
||||
|
||||
// 检查一些默认允许的规则
|
||||
const allowRules = config.rules.filter(r => r.action === 'allow');
|
||||
const patterns = allowRules.map(r => r.pattern);
|
||||
|
||||
expect(patterns.some(p => p.startsWith('ls'))).toBe(true);
|
||||
expect(patterns.some(p => p.startsWith('cat'))).toBe(true);
|
||||
expect(patterns.some(p => p.startsWith('git status'))).toBe(true);
|
||||
});
|
||||
|
||||
it('默认规则包含危险命令', () => {
|
||||
const config = checker.getConfig();
|
||||
|
||||
const denyRules = config.rules.filter(r => r.action === 'deny');
|
||||
const patterns = denyRules.map(r => r.pattern);
|
||||
|
||||
expect(patterns.some(p => p.includes('rm -rf'))).toBe(true);
|
||||
expect(patterns.some(p => p.includes('sudo'))).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('安全命令检查(默认允许)', () => {
|
||||
const safeCommands = [
|
||||
'ls -la',
|
||||
'cat file.txt',
|
||||
'head -n 10 log.txt',
|
||||
'tail -f server.log',
|
||||
'grep pattern file.txt',
|
||||
'find . -name "*.js"',
|
||||
'echo hello',
|
||||
'pwd',
|
||||
'which node',
|
||||
'git status',
|
||||
'git log --oneline',
|
||||
'git diff HEAD',
|
||||
];
|
||||
|
||||
for (const command of safeCommands) {
|
||||
it(`允许安全命令: ${command}`, async () => {
|
||||
const result = await checker.check({
|
||||
command,
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
expect(result.allowed).toBe(true);
|
||||
expect(result.action).toBe('allow');
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe('危险命令检查(默认拒绝)', () => {
|
||||
// 这些命令精确匹配默认拒绝规则
|
||||
const denyCommands = [
|
||||
'sudo rm file',
|
||||
];
|
||||
|
||||
for (const command of denyCommands) {
|
||||
it(`拒绝危险命令: ${command}`, async () => {
|
||||
const result = await checker.check({
|
||||
command,
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
expect(result.allowed).toBe(false);
|
||||
expect(result.action).toBe('deny');
|
||||
});
|
||||
}
|
||||
|
||||
// 这些命令可能匹配 ask 规则或默认行为
|
||||
const askCommands = [
|
||||
'rm -rf /',
|
||||
'rm -rf /*',
|
||||
'chmod 777 /',
|
||||
];
|
||||
|
||||
for (const command of askCommands) {
|
||||
it(`危险命令需要确认或拒绝: ${command}`, async () => {
|
||||
const result = await checker.check({
|
||||
command,
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
// 这些命令应该不允许直接执行
|
||||
expect(result.allowed).toBe(false);
|
||||
// 可能是 deny 或 ask(取决于具体规则匹配)
|
||||
expect(['deny', 'ask']).toContain(result.action);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe('需要确认的命令', () => {
|
||||
const askCommands = [
|
||||
'git push origin main',
|
||||
'git commit -m "test"',
|
||||
'git checkout feature',
|
||||
'npm install lodash',
|
||||
];
|
||||
|
||||
for (const command of askCommands) {
|
||||
it(`需要确认: ${command}`, async () => {
|
||||
// 不设置回调,应该返回 ask
|
||||
const result = await checker.check({
|
||||
command,
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
expect(result.action).toBe('ask');
|
||||
expect(result.needsConfirmation).toBe(true);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe('回调处理', () => {
|
||||
it('用户允许时返回允许', async () => {
|
||||
const mockCallback = vi.fn().mockResolvedValue({
|
||||
allow: true,
|
||||
remember: false,
|
||||
} as PermissionDecision);
|
||||
|
||||
checker.setAskCallback(mockCallback);
|
||||
|
||||
const result = await checker.check({
|
||||
command: 'git push origin main',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
expect(result.allowed).toBe(true);
|
||||
expect(mockCallback).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('用户拒绝时返回拒绝', async () => {
|
||||
const mockCallback = vi.fn().mockResolvedValue({
|
||||
allow: false,
|
||||
remember: false,
|
||||
} as PermissionDecision);
|
||||
|
||||
checker.setAskCallback(mockCallback);
|
||||
|
||||
const result = await checker.check({
|
||||
command: 'git push origin main',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
expect(result.allowed).toBe(false);
|
||||
expect(result.action).toBe('deny');
|
||||
});
|
||||
|
||||
it('remember=true 时记住决定', async () => {
|
||||
const mockCallback = vi.fn().mockResolvedValue({
|
||||
allow: true,
|
||||
remember: true,
|
||||
} as PermissionDecision);
|
||||
|
||||
checker.setAskCallback(mockCallback);
|
||||
|
||||
// 第一次调用
|
||||
await checker.check({
|
||||
command: 'git push origin main',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
// 第二次调用应该不再询问
|
||||
const result = await checker.check({
|
||||
command: 'git commit -m "test"',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
// 记住的是整个模式,第二次可能仍需询问(取决于实现)
|
||||
expect(result.allowed).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('会话权限管理', () => {
|
||||
it('清除会话权限', async () => {
|
||||
const mockCallback = vi.fn().mockResolvedValue({
|
||||
allow: true,
|
||||
remember: true,
|
||||
} as PermissionDecision);
|
||||
|
||||
checker.setAskCallback(mockCallback);
|
||||
|
||||
// 第一次调用
|
||||
await checker.check({
|
||||
command: 'git push origin main',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
// 清除会话权限
|
||||
checker.clearSessionPermissions();
|
||||
|
||||
// 再次调用应该重新询问
|
||||
await checker.check({
|
||||
command: 'git push origin main',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
// 应该被调用两次
|
||||
expect(mockCallback).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('规则管理', () => {
|
||||
it('添加新规则', () => {
|
||||
checker.addRule({
|
||||
pattern: 'custom-cmd *',
|
||||
action: 'allow',
|
||||
});
|
||||
|
||||
const config = checker.getConfig();
|
||||
const hasRule = config.rules.some(r => r.pattern === 'custom-cmd *');
|
||||
expect(hasRule).toBe(true);
|
||||
});
|
||||
|
||||
it('更新已有规则', () => {
|
||||
// 添加规则
|
||||
checker.addRule({
|
||||
pattern: 'test-cmd',
|
||||
action: 'allow',
|
||||
});
|
||||
|
||||
// 更新规则
|
||||
checker.addRule({
|
||||
pattern: 'test-cmd',
|
||||
action: 'deny',
|
||||
});
|
||||
|
||||
const config = checker.getConfig();
|
||||
const rule = config.rules.find(r => r.pattern === 'test-cmd');
|
||||
expect(rule?.action).toBe('deny');
|
||||
});
|
||||
});
|
||||
|
||||
describe('项目目录检查', () => {
|
||||
it('识别项目内路径', async () => {
|
||||
const result = await checker.check({
|
||||
command: 'cat ./src/index.ts',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
// 项目内路径应该正常检查
|
||||
expect(result).toBeDefined();
|
||||
});
|
||||
|
||||
it('识别项目外路径', async () => {
|
||||
// 访问项目外的绝对路径
|
||||
const result = await checker.check({
|
||||
command: 'cat /etc/passwd',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
// 外部路径可能需要确认或拒绝
|
||||
expect(result).toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,163 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { parseCommandSimple } from '../../../src/permission/bash-parser.js';
|
||||
|
||||
// 注意:parseBashCommand 需要初始化 tree-sitter wasm,在单元测试中使用 parseCommandSimple
|
||||
|
||||
describe('parseCommandSimple - 简单命令解析', () => {
|
||||
describe('基本命令解析', () => {
|
||||
it('解析单个命令', () => {
|
||||
const result = parseCommandSimple('ls');
|
||||
|
||||
expect(result.name).toBe('ls');
|
||||
expect(result.subcommand).toBeUndefined();
|
||||
expect(result.args).toEqual([]);
|
||||
expect(result.text).toBe('ls');
|
||||
});
|
||||
|
||||
it('解析带子命令的命令', () => {
|
||||
const result = parseCommandSimple('git status');
|
||||
|
||||
expect(result.name).toBe('git');
|
||||
expect(result.subcommand).toBe('status');
|
||||
expect(result.args).toEqual([]);
|
||||
});
|
||||
|
||||
it('解析带参数的命令', () => {
|
||||
const result = parseCommandSimple('git commit -m "message"');
|
||||
|
||||
expect(result.name).toBe('git');
|
||||
expect(result.subcommand).toBe('commit');
|
||||
expect(result.args).toContain('-m');
|
||||
});
|
||||
|
||||
it('解析带 flag 前缀的子命令', () => {
|
||||
const result = parseCommandSimple('rm -rf node_modules');
|
||||
|
||||
expect(result.name).toBe('rm');
|
||||
// -rf 以 - 开头,所以 node_modules 是第一个非 flag 参数
|
||||
expect(result.subcommand).toBe('node_modules');
|
||||
expect(result.args).toContain('-rf');
|
||||
});
|
||||
});
|
||||
|
||||
describe('flag 和参数分离', () => {
|
||||
it('短 flag 放入 args', () => {
|
||||
const result = parseCommandSimple('ls -la');
|
||||
|
||||
expect(result.name).toBe('ls');
|
||||
expect(result.subcommand).toBeUndefined();
|
||||
expect(result.args).toContain('-la');
|
||||
});
|
||||
|
||||
it('长 flag 放入 args', () => {
|
||||
const result = parseCommandSimple('npm install --save-dev');
|
||||
|
||||
expect(result.name).toBe('npm');
|
||||
expect(result.subcommand).toBe('install');
|
||||
expect(result.args).toContain('--save-dev');
|
||||
});
|
||||
|
||||
it('混合 flag 和参数', () => {
|
||||
const result = parseCommandSimple('git checkout -b feature-branch');
|
||||
|
||||
expect(result.name).toBe('git');
|
||||
expect(result.subcommand).toBe('checkout');
|
||||
expect(result.args).toContain('-b');
|
||||
expect(result.args).toContain('feature-branch');
|
||||
});
|
||||
});
|
||||
|
||||
describe('空白处理', () => {
|
||||
it('处理前后空格', () => {
|
||||
const result = parseCommandSimple(' git status ');
|
||||
|
||||
expect(result.name).toBe('git');
|
||||
expect(result.subcommand).toBe('status');
|
||||
});
|
||||
|
||||
it('处理多个空格分隔', () => {
|
||||
const result = parseCommandSimple('git status -v');
|
||||
|
||||
expect(result.name).toBe('git');
|
||||
expect(result.subcommand).toBe('status');
|
||||
expect(result.args).toContain('-v');
|
||||
});
|
||||
|
||||
it('空字符串返回空命令', () => {
|
||||
const result = parseCommandSimple('');
|
||||
|
||||
expect(result.name).toBe('');
|
||||
expect(result.subcommand).toBeUndefined();
|
||||
expect(result.args).toEqual([]);
|
||||
});
|
||||
|
||||
it('只有空格返回空命令', () => {
|
||||
const result = parseCommandSimple(' ');
|
||||
|
||||
expect(result.name).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('复杂命令', () => {
|
||||
it('解析 git push 命令', () => {
|
||||
const result = parseCommandSimple('git push origin main --force');
|
||||
|
||||
expect(result.name).toBe('git');
|
||||
expect(result.subcommand).toBe('push');
|
||||
expect(result.args).toContain('origin');
|
||||
expect(result.args).toContain('main');
|
||||
expect(result.args).toContain('--force');
|
||||
});
|
||||
|
||||
it('解析 npm 命令', () => {
|
||||
const result = parseCommandSimple('npm run build -- --watch');
|
||||
|
||||
expect(result.name).toBe('npm');
|
||||
expect(result.subcommand).toBe('run');
|
||||
expect(result.args).toContain('build');
|
||||
});
|
||||
|
||||
it('解析 docker 命令', () => {
|
||||
const result = parseCommandSimple('docker run -d -p 8080:80 nginx');
|
||||
|
||||
expect(result.name).toBe('docker');
|
||||
expect(result.subcommand).toBe('run');
|
||||
expect(result.args).toContain('-d');
|
||||
expect(result.args).toContain('-p');
|
||||
});
|
||||
|
||||
it('解析管道前的命令', () => {
|
||||
// 简单解析不处理管道,只作为普通参数
|
||||
const result = parseCommandSimple('cat file.txt');
|
||||
|
||||
expect(result.name).toBe('cat');
|
||||
expect(result.subcommand).toBe('file.txt');
|
||||
});
|
||||
});
|
||||
|
||||
describe('保留原始文本', () => {
|
||||
it('text 字段保留原始命令', () => {
|
||||
const original = 'git commit -m "fix bug"';
|
||||
const result = parseCommandSimple(original);
|
||||
|
||||
expect(result.text).toBe(original);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('ParsedCommand 结构', () => {
|
||||
it('包含所有必要字段', () => {
|
||||
const result = parseCommandSimple('git push');
|
||||
|
||||
expect(result).toHaveProperty('name');
|
||||
expect(result).toHaveProperty('subcommand');
|
||||
expect(result).toHaveProperty('args');
|
||||
expect(result).toHaveProperty('text');
|
||||
});
|
||||
|
||||
it('args 始终是数组', () => {
|
||||
const result = parseCommandSimple('ls');
|
||||
|
||||
expect(Array.isArray(result.args)).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,322 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { FilePermissionChecker } from '../../../src/permission/checkers/file.js';
|
||||
import type { PermissionDecision, FilePermissionContext } from '../../../src/permission/types.js';
|
||||
|
||||
// Mock fs 以避免实际文件操作
|
||||
vi.mock('fs', () => ({
|
||||
existsSync: vi.fn(() => false),
|
||||
readFileSync: vi.fn(),
|
||||
writeFileSync: vi.fn(),
|
||||
mkdirSync: vi.fn(),
|
||||
}));
|
||||
|
||||
// Mock file-prompt
|
||||
vi.mock('../../../src/permission/file-prompt.js', () => ({
|
||||
promptFilePermission: vi.fn().mockResolvedValue({ allow: true, remember: false }),
|
||||
}));
|
||||
|
||||
describe('FilePermissionChecker - 文件权限检查器', () => {
|
||||
let checker: FilePermissionChecker;
|
||||
const testProjectRoot = '/test/project';
|
||||
|
||||
beforeEach(() => {
|
||||
checker = new FilePermissionChecker(testProjectRoot);
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('默认配置', () => {
|
||||
it('加载默认配置', () => {
|
||||
const config = checker.getConfig();
|
||||
|
||||
expect(config.operations).toBeDefined();
|
||||
expect(config.sensitivePaths).toBeDefined();
|
||||
expect(config.externalDirectory).toBe('ask');
|
||||
});
|
||||
|
||||
it('读操作默认允许', () => {
|
||||
const config = checker.getConfig();
|
||||
|
||||
expect(config.operations.read).toBe('allow');
|
||||
expect(config.operations.list).toBe('allow');
|
||||
expect(config.operations.search).toBe('allow');
|
||||
expect(config.operations.grep).toBe('allow');
|
||||
expect(config.operations.info).toBe('allow');
|
||||
});
|
||||
|
||||
it('写操作默认需要确认', () => {
|
||||
const config = checker.getConfig();
|
||||
|
||||
expect(config.operations.write).toBe('ask');
|
||||
expect(config.operations.edit).toBe('ask');
|
||||
expect(config.operations.move).toBe('ask');
|
||||
expect(config.operations.copy).toBe('ask');
|
||||
expect(config.operations.delete).toBe('ask');
|
||||
expect(config.operations.mkdir).toBe('ask');
|
||||
});
|
||||
});
|
||||
|
||||
describe('读操作权限', () => {
|
||||
const readOperations: FilePermissionContext['operation'][] = [
|
||||
'read',
|
||||
'list',
|
||||
'search',
|
||||
'grep',
|
||||
'info',
|
||||
];
|
||||
|
||||
for (const operation of readOperations) {
|
||||
it(`${operation} 操作在项目内默认允许`, async () => {
|
||||
const result = await checker.checkFilePermission({
|
||||
operation,
|
||||
path: './src/index.ts',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
expect(result.allowed).toBe(true);
|
||||
expect(result.action).toBe('allow');
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe('写操作权限', () => {
|
||||
const writeOperations: FilePermissionContext['operation'][] = [
|
||||
'write',
|
||||
'edit',
|
||||
'move',
|
||||
'copy',
|
||||
'delete',
|
||||
'mkdir',
|
||||
];
|
||||
|
||||
for (const operation of writeOperations) {
|
||||
it(`${operation} 操作无回调时需要确认`, async () => {
|
||||
const result = await checker.checkFilePermission({
|
||||
operation,
|
||||
path: './src/new-file.ts',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
expect(result.action).toBe('ask');
|
||||
expect(result.needsConfirmation).toBe(true);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe('敏感路径检查', () => {
|
||||
it('系统路径拒绝访问', async () => {
|
||||
const sensitivePaths = [
|
||||
'/etc/passwd',
|
||||
'/usr/bin/node',
|
||||
'/bin/sh',
|
||||
'/var/log/syslog',
|
||||
];
|
||||
|
||||
for (const testPath of sensitivePaths) {
|
||||
const result = await checker.checkFilePermission({
|
||||
operation: 'read',
|
||||
path: testPath,
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
expect(result.allowed).toBe(false);
|
||||
expect(result.action).toBe('deny');
|
||||
}
|
||||
});
|
||||
|
||||
it('用户敏感文件需要确认', async () => {
|
||||
const sensitivePaths = [
|
||||
'~/.ssh/id_rsa',
|
||||
'~/.aws/credentials',
|
||||
'./.env',
|
||||
];
|
||||
|
||||
for (const testPath of sensitivePaths) {
|
||||
const result = await checker.checkFilePermission({
|
||||
operation: 'read',
|
||||
path: testPath,
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
// 敏感路径会触发 ask
|
||||
expect(result.action === 'ask' || result.action === 'deny').toBe(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('外部目录访问', () => {
|
||||
it('项目外路径需要确认', async () => {
|
||||
const result = await checker.checkFilePermission({
|
||||
operation: 'read',
|
||||
path: '/home/other/file.txt',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
// 外部目录会触发 ask 或 deny
|
||||
expect(result.action === 'ask' || result.action === 'deny').toBe(true);
|
||||
});
|
||||
|
||||
it('项目内路径正常检查', async () => {
|
||||
const result = await checker.checkFilePermission({
|
||||
operation: 'read',
|
||||
path: './src/index.ts',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
expect(result.allowed).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('波浪号展开', () => {
|
||||
it('展开 ~ 为 home 目录', async () => {
|
||||
const result = await checker.checkFilePermission({
|
||||
operation: 'read',
|
||||
path: '~/projects/file.txt',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
// 外部路径会触发 ask
|
||||
expect(result).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('回调处理', () => {
|
||||
it('用户允许时返回允许', async () => {
|
||||
const mockCallback = vi.fn().mockResolvedValue({
|
||||
allow: true,
|
||||
remember: false,
|
||||
} as PermissionDecision);
|
||||
|
||||
checker.setAskCallback(mockCallback);
|
||||
|
||||
const result = await checker.checkFilePermission({
|
||||
operation: 'write',
|
||||
path: './src/new-file.ts',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
expect(result.allowed).toBe(true);
|
||||
});
|
||||
|
||||
it('用户拒绝时返回拒绝', async () => {
|
||||
const mockCallback = vi.fn().mockResolvedValue({
|
||||
allow: false,
|
||||
remember: false,
|
||||
} as PermissionDecision);
|
||||
|
||||
checker.setAskCallback(mockCallback);
|
||||
|
||||
const result = await checker.checkFilePermission({
|
||||
operation: 'delete',
|
||||
path: './src/file.ts',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
expect(result.allowed).toBe(false);
|
||||
expect(result.action).toBe('deny');
|
||||
});
|
||||
});
|
||||
|
||||
describe('会话权限管理', () => {
|
||||
it('记住允许决定', async () => {
|
||||
const mockCallback = vi.fn().mockResolvedValue({
|
||||
allow: true,
|
||||
remember: true,
|
||||
} as PermissionDecision);
|
||||
|
||||
checker.setAskCallback(mockCallback);
|
||||
|
||||
// 第一次调用
|
||||
await checker.checkFilePermission({
|
||||
operation: 'write',
|
||||
path: './src/test.ts',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
// 第二次调用同一操作和路径
|
||||
const result = await checker.checkFilePermission({
|
||||
operation: 'write',
|
||||
path: './src/test.ts',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
expect(result.allowed).toBe(true);
|
||||
// 第二次不应该调用回调
|
||||
expect(mockCallback).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('记住拒绝决定', async () => {
|
||||
const mockCallback = vi.fn().mockResolvedValue({
|
||||
allow: false,
|
||||
remember: true,
|
||||
} as PermissionDecision);
|
||||
|
||||
checker.setAskCallback(mockCallback);
|
||||
|
||||
// 第一次调用
|
||||
await checker.checkFilePermission({
|
||||
operation: 'delete',
|
||||
path: './src/important.ts',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
// 第二次调用
|
||||
const result = await checker.checkFilePermission({
|
||||
operation: 'delete',
|
||||
path: './src/important.ts',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
expect(result.allowed).toBe(false);
|
||||
expect(mockCallback).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('清除会话权限后重新询问', async () => {
|
||||
const mockCallback = vi.fn().mockResolvedValue({
|
||||
allow: true,
|
||||
remember: true,
|
||||
} as PermissionDecision);
|
||||
|
||||
checker.setAskCallback(mockCallback);
|
||||
|
||||
// 第一次调用
|
||||
await checker.checkFilePermission({
|
||||
operation: 'write',
|
||||
path: './src/test.ts',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
// 清除权限
|
||||
checker.clearSessionPermissions();
|
||||
|
||||
// 再次调用
|
||||
await checker.checkFilePermission({
|
||||
operation: 'write',
|
||||
path: './src/test.ts',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
// 应该调用两次
|
||||
expect(mockCallback).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('check 接口(兼容 PermissionChecker)', () => {
|
||||
it('解析 read 操作', async () => {
|
||||
const result = await checker.check({
|
||||
command: 'read ./src/index.ts',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
expect(result.allowed).toBe(true);
|
||||
});
|
||||
|
||||
it('解析 write 操作', async () => {
|
||||
const result = await checker.check({
|
||||
command: 'write ./src/new.ts',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
expect(result.action).toBe('ask');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,399 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import {
|
||||
promptFileWrite,
|
||||
promptFileEdit,
|
||||
promptFilePermission,
|
||||
} from '../../../src/permission/file-prompt.js';
|
||||
import type { FilePermissionContext } from '../../../src/permission/types.js';
|
||||
|
||||
// Mock readline
|
||||
vi.mock('readline', () => ({
|
||||
createInterface: vi.fn(() => ({
|
||||
question: vi.fn(),
|
||||
close: vi.fn(),
|
||||
})),
|
||||
}));
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
readFile: vi.fn(),
|
||||
}));
|
||||
|
||||
// Mock chalk
|
||||
vi.mock('chalk', () => ({
|
||||
default: {
|
||||
yellow: (s: string) => s,
|
||||
cyan: (s: string) => s,
|
||||
white: (s: string) => s,
|
||||
gray: (s: string) => s,
|
||||
red: (s: string) => s,
|
||||
green: (s: string) => s,
|
||||
},
|
||||
}));
|
||||
|
||||
// Mock diff utils
|
||||
vi.mock('../../../src/utils/diff.js', () => ({
|
||||
computeDiff: vi.fn(() => ({
|
||||
isNew: false,
|
||||
oldContent: 'old',
|
||||
newContent: 'new',
|
||||
hunks: [],
|
||||
})),
|
||||
formatDiff: vi.fn(() => 'diff output'),
|
||||
countChanges: vi.fn(() => ({ additions: 5, deletions: 3 })),
|
||||
formatEditDiff: vi.fn(() => 'edit diff output'),
|
||||
}));
|
||||
|
||||
import * as readline from 'readline';
|
||||
import * as fs from 'fs/promises';
|
||||
import { computeDiff, countChanges } from '../../../src/utils/diff.js';
|
||||
|
||||
describe('File Prompt - 文件操作提示', () => {
|
||||
let consoleLogSpy: ReturnType<typeof vi.spyOn>;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
consoleLogSpy.mockRestore();
|
||||
});
|
||||
|
||||
describe('promptFileWrite - 文件写入提示', () => {
|
||||
const baseContext: FilePermissionContext = {
|
||||
operation: 'write',
|
||||
path: '/test/file.ts',
|
||||
workdir: '/test',
|
||||
toolName: 'write_file',
|
||||
newContent: 'new content',
|
||||
};
|
||||
|
||||
it('无内容时使用简单确认', async () => {
|
||||
const ctx: FilePermissionContext = {
|
||||
...baseContext,
|
||||
newContent: undefined,
|
||||
};
|
||||
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('y')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
const result = await promptFileWrite(ctx);
|
||||
|
||||
expect(result.allow).toBe(true);
|
||||
});
|
||||
|
||||
it('内容相同时直接允许', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('new content');
|
||||
|
||||
const result = await promptFileWrite(baseContext);
|
||||
|
||||
expect(result).toEqual({ allow: true, remember: false });
|
||||
});
|
||||
|
||||
it('新文件显示新增行数', async () => {
|
||||
vi.mocked(fs.readFile).mockRejectedValue(new Error('ENOENT'));
|
||||
vi.mocked(computeDiff).mockReturnValue({
|
||||
isNew: true,
|
||||
oldContent: null,
|
||||
newContent: 'new content',
|
||||
hunks: [],
|
||||
} as any);
|
||||
vi.mocked(countChanges).mockReturnValue({ additions: 10, deletions: 0 });
|
||||
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('y')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
await promptFileWrite(baseContext);
|
||||
|
||||
const calls = consoleLogSpy.mock.calls.flat().join('\n');
|
||||
expect(calls).toContain('新文件');
|
||||
expect(calls).toContain('+10 行');
|
||||
});
|
||||
|
||||
it('修改文件显示增删行数', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('old content');
|
||||
vi.mocked(computeDiff).mockReturnValue({
|
||||
isNew: false,
|
||||
oldContent: 'old content',
|
||||
newContent: 'new content',
|
||||
hunks: [],
|
||||
} as any);
|
||||
vi.mocked(countChanges).mockReturnValue({ additions: 5, deletions: 3 });
|
||||
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('y')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
await promptFileWrite(baseContext);
|
||||
|
||||
const calls = consoleLogSpy.mock.calls.flat().join('\n');
|
||||
expect(calls).toContain('+5 行');
|
||||
expect(calls).toContain('-3 行');
|
||||
});
|
||||
|
||||
it('用户输入 y 返回允许不记住', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('old content');
|
||||
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('y')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
const result = await promptFileWrite(baseContext);
|
||||
|
||||
expect(result).toEqual({ allow: true, remember: false });
|
||||
});
|
||||
|
||||
it('用户输入 Y 返回允许并记住', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('old content');
|
||||
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('Y')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
const result = await promptFileWrite(baseContext);
|
||||
|
||||
expect(result).toEqual({ allow: true, remember: true });
|
||||
});
|
||||
|
||||
it('用户输入 n 返回拒绝不记住', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('old content');
|
||||
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('n')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
const result = await promptFileWrite(baseContext);
|
||||
|
||||
expect(result).toEqual({ allow: false, remember: false });
|
||||
});
|
||||
|
||||
it('用户输入 N 返回拒绝并记住', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('old content');
|
||||
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('N')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
const result = await promptFileWrite(baseContext);
|
||||
|
||||
expect(result).toEqual({ allow: false, remember: true });
|
||||
});
|
||||
|
||||
it('无效输入默认拒绝', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('old content');
|
||||
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('invalid')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
const result = await promptFileWrite(baseContext);
|
||||
|
||||
expect(result).toEqual({ allow: false, remember: false });
|
||||
});
|
||||
|
||||
it('超长 diff 被截断', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('old content');
|
||||
// 模拟超过 50 行的 diff
|
||||
const longDiff = Array(100).fill('line').join('\n');
|
||||
const { formatDiff } = await import('../../../src/utils/diff.js');
|
||||
vi.mocked(formatDiff).mockReturnValue(longDiff);
|
||||
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('y')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
await promptFileWrite(baseContext);
|
||||
|
||||
const calls = consoleLogSpy.mock.calls.flat().join('\n');
|
||||
expect(calls).toContain('省略');
|
||||
});
|
||||
});
|
||||
|
||||
describe('promptFileEdit - 文件编辑提示', () => {
|
||||
const baseContext: FilePermissionContext = {
|
||||
operation: 'edit',
|
||||
path: '/test/file.ts',
|
||||
workdir: '/test',
|
||||
toolName: 'edit_file',
|
||||
oldContent: 'old text',
|
||||
newContent: 'new text',
|
||||
};
|
||||
|
||||
it('无内容时使用简单确认', async () => {
|
||||
const ctx: FilePermissionContext = {
|
||||
...baseContext,
|
||||
oldContent: undefined,
|
||||
newContent: undefined,
|
||||
};
|
||||
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('y')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
const result = await promptFileEdit(ctx);
|
||||
|
||||
expect(result.allow).toBe(true);
|
||||
});
|
||||
|
||||
it('显示编辑 diff', async () => {
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('y')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
await promptFileEdit(baseContext);
|
||||
|
||||
const calls = consoleLogSpy.mock.calls.flat().join('\n');
|
||||
expect(calls).toContain('文件编辑预览');
|
||||
expect(calls).toContain('/test/file.ts');
|
||||
});
|
||||
|
||||
it('用户确认后返回决定', async () => {
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('Y')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
const result = await promptFileEdit(baseContext);
|
||||
|
||||
expect(result).toEqual({ allow: true, remember: true });
|
||||
});
|
||||
});
|
||||
|
||||
describe('promptFilePermission - 统一入口', () => {
|
||||
it('write 操作调用 promptFileWrite', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('same content');
|
||||
|
||||
const ctx: FilePermissionContext = {
|
||||
operation: 'write',
|
||||
path: '/test/file.ts',
|
||||
workdir: '/test',
|
||||
toolName: 'write_file',
|
||||
newContent: 'same content',
|
||||
};
|
||||
|
||||
const result = await promptFilePermission(ctx);
|
||||
|
||||
// 内容相同直接允许
|
||||
expect(result).toEqual({ allow: true, remember: false });
|
||||
});
|
||||
|
||||
it('edit 操作调用 promptFileEdit', async () => {
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('y')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
const ctx: FilePermissionContext = {
|
||||
operation: 'edit',
|
||||
path: '/test/file.ts',
|
||||
workdir: '/test',
|
||||
toolName: 'edit_file',
|
||||
oldContent: 'old',
|
||||
newContent: 'new',
|
||||
};
|
||||
|
||||
await promptFilePermission(ctx);
|
||||
|
||||
const calls = consoleLogSpy.mock.calls.flat().join('\n');
|
||||
expect(calls).toContain('文件编辑预览');
|
||||
});
|
||||
|
||||
it('其他操作使用简单确认', async () => {
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('y')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
const ctx: FilePermissionContext = {
|
||||
operation: 'delete',
|
||||
path: '/test/file.ts',
|
||||
workdir: '/test',
|
||||
toolName: 'delete_file',
|
||||
};
|
||||
|
||||
await promptFilePermission(ctx);
|
||||
|
||||
const calls = consoleLogSpy.mock.calls.flat().join('\n');
|
||||
expect(calls).toContain('文件操作确认');
|
||||
});
|
||||
});
|
||||
|
||||
describe('确认选项显示', () => {
|
||||
it('显示所有选项', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('old content');
|
||||
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('y')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
await promptFileWrite({
|
||||
operation: 'write',
|
||||
path: '/test/file.ts',
|
||||
workdir: '/test',
|
||||
toolName: 'write_file',
|
||||
newContent: 'new content',
|
||||
});
|
||||
|
||||
const calls = consoleLogSpy.mock.calls.flat().join('\n');
|
||||
expect(calls).toContain('[y]');
|
||||
expect(calls).toContain('[Y]');
|
||||
expect(calls).toContain('[n]');
|
||||
expect(calls).toContain('[N]');
|
||||
expect(calls).toContain('确认执行');
|
||||
expect(calls).toContain('拒绝执行');
|
||||
expect(calls).toContain('记住');
|
||||
});
|
||||
});
|
||||
|
||||
describe('输入处理', () => {
|
||||
it('输入被 trim', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('old content');
|
||||
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback(' y ')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
const result = await promptFileWrite({
|
||||
operation: 'write',
|
||||
path: '/test/file.ts',
|
||||
workdir: '/test',
|
||||
toolName: 'write_file',
|
||||
newContent: 'new content',
|
||||
});
|
||||
|
||||
expect(result).toEqual({ allow: true, remember: false });
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,297 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { GitPermissionChecker } from '../../../src/permission/checkers/git.js';
|
||||
import type { GitPermissionContext, PermissionDecision } from '../../../src/permission/types.js';
|
||||
|
||||
describe('GitPermissionChecker - Git 权限检查器', () => {
|
||||
let checker: GitPermissionChecker;
|
||||
|
||||
beforeEach(() => {
|
||||
checker = new GitPermissionChecker();
|
||||
});
|
||||
|
||||
describe('读操作(默认允许)', () => {
|
||||
const readOperations: GitPermissionContext['operation'][] = [
|
||||
'status',
|
||||
'diff',
|
||||
'log',
|
||||
'branch_list',
|
||||
'show',
|
||||
];
|
||||
|
||||
for (const operation of readOperations) {
|
||||
it(`${operation} 默认允许`, async () => {
|
||||
const result = await checker.checkGitPermission({ operation });
|
||||
|
||||
expect(result.allowed).toBe(true);
|
||||
expect(result.action).toBe('allow');
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe('写操作(默认询问)', () => {
|
||||
const writeOperations: GitPermissionContext['operation'][] = [
|
||||
'add',
|
||||
'commit',
|
||||
'push',
|
||||
'pull',
|
||||
'checkout',
|
||||
'branch_create',
|
||||
'branch_delete',
|
||||
'stash',
|
||||
'stash_pop',
|
||||
'merge',
|
||||
'rebase',
|
||||
];
|
||||
|
||||
for (const operation of writeOperations) {
|
||||
it(`${operation} 无回调时需要确认`, async () => {
|
||||
const result = await checker.checkGitPermission({ operation });
|
||||
|
||||
expect(result.action).toBe('ask');
|
||||
expect(result.needsConfirmation).toBe(true);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe('危险操作', () => {
|
||||
it('reset 总是危险操作', async () => {
|
||||
const result = await checker.checkGitPermission({ operation: 'reset' });
|
||||
|
||||
expect(result.action).toBe('ask');
|
||||
expect(result.needsConfirmation).toBe(true);
|
||||
});
|
||||
|
||||
it('push --force 是危险操作', async () => {
|
||||
const result = await checker.checkGitPermission({
|
||||
operation: 'push',
|
||||
force: true,
|
||||
});
|
||||
|
||||
expect(result.action).toBe('ask');
|
||||
});
|
||||
|
||||
it('checkout --force 是危险操作', async () => {
|
||||
const result = await checker.checkGitPermission({
|
||||
operation: 'checkout',
|
||||
force: true,
|
||||
});
|
||||
|
||||
expect(result.action).toBe('ask');
|
||||
});
|
||||
|
||||
it('rebase --force 是危险操作', async () => {
|
||||
const result = await checker.checkGitPermission({
|
||||
operation: 'rebase',
|
||||
force: true,
|
||||
});
|
||||
|
||||
expect(result.action).toBe('ask');
|
||||
});
|
||||
});
|
||||
|
||||
describe('回调处理', () => {
|
||||
it('用户允许时返回允许', async () => {
|
||||
const mockCallback = vi.fn().mockResolvedValue({
|
||||
allow: true,
|
||||
remember: false,
|
||||
} as PermissionDecision);
|
||||
|
||||
checker.setAskCallback(mockCallback);
|
||||
|
||||
const result = await checker.checkGitPermission({ operation: 'commit' });
|
||||
|
||||
expect(result.allowed).toBe(true);
|
||||
expect(mockCallback).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('用户拒绝时返回拒绝', async () => {
|
||||
const mockCallback = vi.fn().mockResolvedValue({
|
||||
allow: false,
|
||||
remember: false,
|
||||
} as PermissionDecision);
|
||||
|
||||
checker.setAskCallback(mockCallback);
|
||||
|
||||
const result = await checker.checkGitPermission({ operation: 'push' });
|
||||
|
||||
expect(result.allowed).toBe(false);
|
||||
expect(result.action).toBe('deny');
|
||||
});
|
||||
|
||||
it('remember=true 时记住决定', async () => {
|
||||
const mockCallback = vi.fn().mockResolvedValue({
|
||||
allow: true,
|
||||
remember: true,
|
||||
} as PermissionDecision);
|
||||
|
||||
checker.setAskCallback(mockCallback);
|
||||
|
||||
// 第一次调用
|
||||
await checker.checkGitPermission({ operation: 'commit' });
|
||||
|
||||
// 第二次调用不应该再询问
|
||||
const secondResult = await checker.checkGitPermission({ operation: 'push' });
|
||||
|
||||
expect(secondResult.allowed).toBe(true);
|
||||
// 第二次不需要回调(因为记住了写操作的权限)
|
||||
expect(mockCallback).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('会话权限管理', () => {
|
||||
it('清除会话权限后重新询问', async () => {
|
||||
const mockCallback = vi.fn().mockResolvedValue({
|
||||
allow: true,
|
||||
remember: true,
|
||||
} as PermissionDecision);
|
||||
|
||||
checker.setAskCallback(mockCallback);
|
||||
|
||||
// 第一次调用并记住
|
||||
await checker.checkGitPermission({ operation: 'commit' });
|
||||
|
||||
// 清除会话权限
|
||||
checker.clearSessionPermissions();
|
||||
|
||||
// 再次调用应该重新询问
|
||||
await checker.checkGitPermission({ operation: 'commit' });
|
||||
|
||||
expect(mockCallback).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('拒绝决定也被记住', async () => {
|
||||
const mockCallback = vi.fn().mockResolvedValue({
|
||||
allow: false,
|
||||
remember: true,
|
||||
} as PermissionDecision);
|
||||
|
||||
checker.setAskCallback(mockCallback);
|
||||
|
||||
// 第一次拒绝并记住
|
||||
await checker.checkGitPermission({ operation: 'push' });
|
||||
|
||||
// 第二次直接拒绝
|
||||
const result = await checker.checkGitPermission({ operation: 'commit' });
|
||||
|
||||
expect(result.allowed).toBe(false);
|
||||
expect(mockCallback).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('配置管理', () => {
|
||||
it('获取默认配置', () => {
|
||||
const config = checker.getConfig();
|
||||
|
||||
expect(config.readOperations).toBe('allow');
|
||||
expect(config.writeOperations).toBe('ask');
|
||||
expect(config.dangerousOperations).toBe('ask');
|
||||
});
|
||||
|
||||
it('更新配置', () => {
|
||||
checker.setConfig({ writeOperations: 'allow' });
|
||||
|
||||
const config = checker.getConfig();
|
||||
expect(config.writeOperations).toBe('allow');
|
||||
});
|
||||
|
||||
it('配置更改后影响权限检查', async () => {
|
||||
checker.setConfig({ writeOperations: 'allow' });
|
||||
|
||||
const result = await checker.checkGitPermission({ operation: 'commit' });
|
||||
|
||||
expect(result.allowed).toBe(true);
|
||||
});
|
||||
|
||||
it('配置危险操作为拒绝', async () => {
|
||||
checker.setConfig({ dangerousOperations: 'deny' });
|
||||
|
||||
const result = await checker.checkGitPermission({ operation: 'reset' });
|
||||
|
||||
expect(result.allowed).toBe(false);
|
||||
expect(result.action).toBe('deny');
|
||||
});
|
||||
});
|
||||
|
||||
describe('操作描述生成', () => {
|
||||
it('带 target 的操作描述', async () => {
|
||||
checker.setAskCallback(vi.fn().mockResolvedValue({ allow: true, remember: false }));
|
||||
|
||||
await checker.checkGitPermission({
|
||||
operation: 'checkout',
|
||||
target: 'feature-branch',
|
||||
});
|
||||
|
||||
// 验证回调收到正确的描述
|
||||
const callArg = vi.mocked(checker['askCallback']!).mock.calls[0][0];
|
||||
expect(callArg.command).toContain('feature-branch');
|
||||
});
|
||||
|
||||
it('带 remote 的操作描述', async () => {
|
||||
checker.setAskCallback(vi.fn().mockResolvedValue({ allow: true, remember: false }));
|
||||
|
||||
await checker.checkGitPermission({
|
||||
operation: 'push',
|
||||
remote: 'origin',
|
||||
});
|
||||
|
||||
const callArg = vi.mocked(checker['askCallback']!).mock.calls[0][0];
|
||||
expect(callArg.command).toContain('origin');
|
||||
});
|
||||
|
||||
it('带 commit message 的操作描述(截断)', async () => {
|
||||
checker.setAskCallback(vi.fn().mockResolvedValue({ allow: true, remember: false }));
|
||||
|
||||
const longMessage = 'a'.repeat(100);
|
||||
await checker.checkGitPermission({
|
||||
operation: 'commit',
|
||||
message: longMessage,
|
||||
});
|
||||
|
||||
const callArg = vi.mocked(checker['askCallback']!).mock.calls[0][0];
|
||||
expect(callArg.command).toContain('...');
|
||||
expect(callArg.command.length).toBeLessThan(longMessage.length + 50);
|
||||
});
|
||||
|
||||
it('--force 显示在描述中', async () => {
|
||||
checker.setAskCallback(vi.fn().mockResolvedValue({ allow: true, remember: false }));
|
||||
|
||||
await checker.checkGitPermission({
|
||||
operation: 'push',
|
||||
force: true,
|
||||
});
|
||||
|
||||
const callArg = vi.mocked(checker['askCallback']!).mock.calls[0][0];
|
||||
expect(callArg.command).toContain('--force');
|
||||
});
|
||||
});
|
||||
|
||||
describe('check 接口(从命令解析)', () => {
|
||||
it('解析 git status', async () => {
|
||||
const result = await checker.check({
|
||||
command: 'git status',
|
||||
workdir: '/test',
|
||||
});
|
||||
|
||||
expect(result.allowed).toBe(true);
|
||||
});
|
||||
|
||||
it('解析 git_commit', async () => {
|
||||
const result = await checker.check({
|
||||
command: 'git_commit',
|
||||
workdir: '/test',
|
||||
});
|
||||
|
||||
expect(result.action).toBe('ask');
|
||||
});
|
||||
|
||||
it('无法解析的命令返回拒绝', async () => {
|
||||
const result = await checker.check({
|
||||
command: 'invalid command',
|
||||
workdir: '/test',
|
||||
});
|
||||
|
||||
expect(result.allowed).toBe(false);
|
||||
expect(result.reason).toContain('无法解析');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,222 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import {
|
||||
PermissionManager,
|
||||
getPermissionManager,
|
||||
resetPermissionManager,
|
||||
} from '../../../src/permission/manager.js';
|
||||
import type { PermissionDecision, PermissionContext } from '../../../src/permission/types.js';
|
||||
|
||||
// Mock 检查器以避免文件系统操作
|
||||
vi.mock('fs', () => ({
|
||||
existsSync: vi.fn(() => false),
|
||||
readFileSync: vi.fn(),
|
||||
writeFileSync: vi.fn(),
|
||||
mkdirSync: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('../../../src/permission/file-prompt.js', () => ({
|
||||
promptFilePermission: vi.fn().mockResolvedValue({ allow: true, remember: false }),
|
||||
}));
|
||||
|
||||
describe('PermissionManager - 权限管理器', () => {
|
||||
let manager: PermissionManager;
|
||||
const testProjectRoot = '/test/project';
|
||||
|
||||
beforeEach(() => {
|
||||
manager = new PermissionManager(testProjectRoot);
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('初始化', () => {
|
||||
it('创建时注册默认检查器', () => {
|
||||
// 应该包含 bash, file, web, git 检查器
|
||||
expect(manager.getChecker('bash')).toBeDefined();
|
||||
expect(manager.getChecker('file')).toBeDefined();
|
||||
expect(manager.getChecker('web')).toBeDefined();
|
||||
expect(manager.getChecker('git')).toBeDefined();
|
||||
});
|
||||
|
||||
it('未注册的检查器返回 undefined', () => {
|
||||
expect(manager.getChecker('non-existent')).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('registerChecker', () => {
|
||||
it('注册自定义检查器', () => {
|
||||
const customChecker = {
|
||||
name: 'custom',
|
||||
check: vi.fn().mockResolvedValue({ allowed: true, action: 'allow' }),
|
||||
clearSessionPermissions: vi.fn(),
|
||||
};
|
||||
|
||||
manager.registerChecker(customChecker);
|
||||
|
||||
expect(manager.getChecker('custom')).toBe(customChecker);
|
||||
});
|
||||
|
||||
it('覆盖已有检查器', () => {
|
||||
const newBashChecker = {
|
||||
name: 'bash',
|
||||
check: vi.fn().mockResolvedValue({ allowed: false, action: 'deny' }),
|
||||
clearSessionPermissions: vi.fn(),
|
||||
};
|
||||
|
||||
manager.registerChecker(newBashChecker);
|
||||
|
||||
expect(manager.getChecker('bash')).toBe(newBashChecker);
|
||||
});
|
||||
});
|
||||
|
||||
describe('setAskCallback', () => {
|
||||
it('设置回调传递给所有支持回调的检查器', () => {
|
||||
const callback = vi.fn().mockResolvedValue({
|
||||
allow: true,
|
||||
remember: false,
|
||||
} as PermissionDecision);
|
||||
|
||||
manager.setAskCallback(callback);
|
||||
|
||||
// 验证回调被设置(通过间接方式)
|
||||
expect(callback).not.toHaveBeenCalled(); // 设置时不调用
|
||||
});
|
||||
});
|
||||
|
||||
describe('checkPermission', () => {
|
||||
it('使用指定检查器检查权限', async () => {
|
||||
const result = await manager.checkPermission('bash', {
|
||||
command: 'ls -la',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
expect(result.allowed).toBe(true);
|
||||
});
|
||||
|
||||
it('未注册的检查器返回 ask', async () => {
|
||||
const result = await manager.checkPermission('non-existent', {
|
||||
command: 'some command',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
expect(result.action).toBe('ask');
|
||||
expect(result.needsConfirmation).toBe(true);
|
||||
expect(result.reason).toContain('未找到检查器');
|
||||
});
|
||||
});
|
||||
|
||||
describe('checkBashPermission - 便捷方法', () => {
|
||||
it('检查安全命令', async () => {
|
||||
const result = await manager.checkBashPermission({
|
||||
command: 'git status',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
expect(result.allowed).toBe(true);
|
||||
});
|
||||
|
||||
it('检查危险命令', async () => {
|
||||
const result = await manager.checkBashPermission({
|
||||
command: 'rm -rf /',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
expect(result.allowed).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('checkFilePermission - 便捷方法', () => {
|
||||
it('检查读操作', async () => {
|
||||
const result = await manager.checkFilePermission({
|
||||
operation: 'read',
|
||||
path: './src/index.ts',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
expect(result.allowed).toBe(true);
|
||||
});
|
||||
|
||||
it('检查写操作需要确认', async () => {
|
||||
const result = await manager.checkFilePermission({
|
||||
operation: 'write',
|
||||
path: './src/new-file.ts',
|
||||
workdir: testProjectRoot,
|
||||
});
|
||||
|
||||
expect(result.action).toBe('ask');
|
||||
});
|
||||
});
|
||||
|
||||
describe('checkGitPermission - 便捷方法', () => {
|
||||
it('检查读操作', async () => {
|
||||
const result = await manager.checkGitPermission({
|
||||
operation: 'status',
|
||||
});
|
||||
|
||||
expect(result.allowed).toBe(true);
|
||||
});
|
||||
|
||||
it('检查写操作需要确认', async () => {
|
||||
const result = await manager.checkGitPermission({
|
||||
operation: 'push',
|
||||
});
|
||||
|
||||
expect(result.action).toBe('ask');
|
||||
});
|
||||
});
|
||||
|
||||
describe('checkWebPermission - 便捷方法', () => {
|
||||
it('检查网页访问', async () => {
|
||||
const result = await manager.checkWebPermission({
|
||||
operation: 'fetch',
|
||||
url: 'https://example.com',
|
||||
});
|
||||
|
||||
// Web 检查器的默认行为
|
||||
expect(result).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('会话权限管理', () => {
|
||||
it('clearAllSessionPermissions 清除所有检查器的权限', () => {
|
||||
// 调用方法不应该抛出错误
|
||||
expect(() => manager.clearAllSessionPermissions()).not.toThrow();
|
||||
});
|
||||
|
||||
it('clearSessionPermissions 清除指定检查器的权限', () => {
|
||||
expect(() => manager.clearSessionPermissions('bash')).not.toThrow();
|
||||
expect(() => manager.clearSessionPermissions('file')).not.toThrow();
|
||||
expect(() => manager.clearSessionPermissions('git')).not.toThrow();
|
||||
});
|
||||
|
||||
it('清除不存在的检查器权限不报错', () => {
|
||||
expect(() => manager.clearSessionPermissions('non-existent')).not.toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('全局实例管理', () => {
|
||||
beforeEach(() => {
|
||||
resetPermissionManager();
|
||||
});
|
||||
|
||||
it('getPermissionManager 返回单例', () => {
|
||||
const manager1 = getPermissionManager('/test/project');
|
||||
const manager2 = getPermissionManager();
|
||||
|
||||
expect(manager1).toBe(manager2);
|
||||
});
|
||||
|
||||
it('resetPermissionManager 重置单例', () => {
|
||||
const manager1 = getPermissionManager('/test/project');
|
||||
resetPermissionManager();
|
||||
const manager2 = getPermissionManager('/test/project');
|
||||
|
||||
expect(manager1).not.toBe(manager2);
|
||||
});
|
||||
|
||||
it('首次创建使用指定的 projectRoot', () => {
|
||||
const manager = getPermissionManager('/custom/root');
|
||||
|
||||
// 验证管理器已创建
|
||||
expect(manager).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,226 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { promptPermission, showPermissionDenied, showPermissionAllowed } from '../../../src/permission/prompt.js';
|
||||
import type { PermissionContext } from '../../../src/permission/types.js';
|
||||
|
||||
// Mock readline
|
||||
vi.mock('readline', () => ({
|
||||
createInterface: vi.fn(() => ({
|
||||
question: vi.fn(),
|
||||
close: vi.fn(),
|
||||
})),
|
||||
}));
|
||||
|
||||
// Mock chalk
|
||||
vi.mock('chalk', () => ({
|
||||
default: {
|
||||
yellow: (s: string) => s,
|
||||
cyan: (s: string) => s,
|
||||
white: (s: string) => s,
|
||||
gray: (s: string) => s,
|
||||
red: (s: string) => s,
|
||||
green: (s: string) => s,
|
||||
},
|
||||
}));
|
||||
|
||||
import * as readline from 'readline';
|
||||
|
||||
describe('Permission Prompt - 权限提示模块', () => {
|
||||
let consoleLogSpy: ReturnType<typeof vi.spyOn>;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
consoleLogSpy.mockRestore();
|
||||
});
|
||||
|
||||
describe('showPermissionDenied - 显示权限被拒绝', () => {
|
||||
it('显示命令和原因', () => {
|
||||
showPermissionDenied('rm -rf /', '危险命令');
|
||||
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('权限被拒绝'));
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('rm -rf /'));
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('危险命令'));
|
||||
});
|
||||
|
||||
it('输出包含空行', () => {
|
||||
showPermissionDenied('test', 'reason');
|
||||
|
||||
// 第一个和最后一个调用是空行
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('showPermissionAllowed - 显示权限允许', () => {
|
||||
it('显示执行的命令', () => {
|
||||
showPermissionAllowed('npm install');
|
||||
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('执行'));
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('npm install'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('promptPermission - 交互式权限提示', () => {
|
||||
const mockContext: PermissionContext = {
|
||||
command: 'git push',
|
||||
workdir: '/project',
|
||||
toolName: 'bash',
|
||||
};
|
||||
|
||||
it('用户输入 y 返回允许不记住', async () => {
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('y')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
const result = await promptPermission(mockContext);
|
||||
|
||||
expect(result).toEqual({ allow: true, remember: false });
|
||||
expect(mockRl.close).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('用户输入 Y 返回允许并记住', async () => {
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('Y')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
const result = await promptPermission(mockContext);
|
||||
|
||||
expect(result).toEqual({ allow: true, remember: true });
|
||||
});
|
||||
|
||||
it('用户输入 n 返回拒绝不记住', async () => {
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('n')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
const result = await promptPermission(mockContext);
|
||||
|
||||
expect(result).toEqual({ allow: false, remember: false });
|
||||
});
|
||||
|
||||
it('用户输入 N 返回拒绝并记住', async () => {
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('N')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
const result = await promptPermission(mockContext);
|
||||
|
||||
expect(result).toEqual({ allow: false, remember: true });
|
||||
});
|
||||
|
||||
it('无效输入默认为拒绝', async () => {
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('invalid')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
const result = await promptPermission(mockContext);
|
||||
|
||||
expect(result).toEqual({ allow: false, remember: false });
|
||||
});
|
||||
|
||||
it('空输入默认为拒绝', async () => {
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
const result = await promptPermission(mockContext);
|
||||
|
||||
expect(result).toEqual({ allow: false, remember: false });
|
||||
});
|
||||
|
||||
it('带空格的输入会被 trim', async () => {
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback(' y ')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
const result = await promptPermission(mockContext);
|
||||
|
||||
expect(result).toEqual({ allow: true, remember: false });
|
||||
});
|
||||
|
||||
it('显示命令和工作目录', async () => {
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('y')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
await promptPermission(mockContext);
|
||||
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('git push'));
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('/project'));
|
||||
});
|
||||
|
||||
it('显示外部路径警告', async () => {
|
||||
const contextWithExternal: PermissionContext = {
|
||||
...mockContext,
|
||||
externalPaths: ['/etc/passwd', '/root/.ssh'],
|
||||
};
|
||||
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('n')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
await promptPermission(contextWithExternal);
|
||||
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('项目目录外的路径'));
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('/etc/passwd'));
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('/root/.ssh'));
|
||||
});
|
||||
|
||||
it('显示匹配模式', async () => {
|
||||
const contextWithPatterns: PermissionContext = {
|
||||
...mockContext,
|
||||
patterns: ['*.js', '*.ts'],
|
||||
};
|
||||
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('y')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
await promptPermission(contextWithPatterns);
|
||||
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('*.js'));
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('*.ts'));
|
||||
});
|
||||
|
||||
it('不显示空的外部路径', async () => {
|
||||
const contextEmptyExternal: PermissionContext = {
|
||||
...mockContext,
|
||||
externalPaths: [],
|
||||
};
|
||||
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('y')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
await promptPermission(contextEmptyExternal);
|
||||
|
||||
// 不应该显示外部路径相关的警告
|
||||
const calls = consoleLogSpy.mock.calls.flat().join('\n');
|
||||
expect(calls).not.toContain('项目目录外的路径');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,185 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { WebPermissionChecker } from '../../../src/permission/checkers/web.js';
|
||||
|
||||
describe('WebPermissionChecker - Web 权限检查器', () => {
|
||||
let checker: WebPermissionChecker;
|
||||
|
||||
beforeEach(() => {
|
||||
checker = new WebPermissionChecker();
|
||||
});
|
||||
|
||||
describe('构造和基本属性', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(checker.name).toBe('web');
|
||||
});
|
||||
|
||||
it('默认配置为 ask', () => {
|
||||
const config = checker.getConfig();
|
||||
expect(config.default).toBe('ask');
|
||||
});
|
||||
|
||||
it('默认允许高级搜索', () => {
|
||||
const config = checker.getConfig();
|
||||
expect(config.allowAdvancedSearch).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('checkWebPermission - 检查 Web 权限', () => {
|
||||
it('默认策略为 allow 时允许', async () => {
|
||||
checker.setConfig({ default: 'allow' });
|
||||
|
||||
const result = await checker.checkWebPermission({ query: 'test search' });
|
||||
|
||||
expect(result.allowed).toBe(true);
|
||||
expect(result.action).toBe('allow');
|
||||
});
|
||||
|
||||
it('默认策略为 deny 时拒绝', async () => {
|
||||
checker.setConfig({ default: 'deny' });
|
||||
|
||||
const result = await checker.checkWebPermission({ query: 'test search' });
|
||||
|
||||
expect(result.allowed).toBe(false);
|
||||
expect(result.action).toBe('deny');
|
||||
});
|
||||
|
||||
it('默认策略为 ask 时需要确认', async () => {
|
||||
checker.setConfig({ default: 'ask' });
|
||||
|
||||
const result = await checker.checkWebPermission({ query: 'test search' });
|
||||
|
||||
expect(result.allowed).toBe(false);
|
||||
expect(result.action).toBe('ask');
|
||||
expect(result.needsConfirmation).toBe(true);
|
||||
});
|
||||
|
||||
it('不允许高级搜索时拒绝', async () => {
|
||||
checker.setConfig({ allowAdvancedSearch: false });
|
||||
|
||||
const result = await checker.checkWebPermission({
|
||||
query: 'test search',
|
||||
searchDepth: 'advanced',
|
||||
});
|
||||
|
||||
expect(result.allowed).toBe(false);
|
||||
expect(result.reason).toContain('不允许深度搜索');
|
||||
});
|
||||
|
||||
it('主题不在允许列表时拒绝', async () => {
|
||||
checker.setConfig({ allowedTopics: ['general', 'news'] });
|
||||
|
||||
const result = await checker.checkWebPermission({
|
||||
query: 'test search',
|
||||
topic: 'finance',
|
||||
});
|
||||
|
||||
expect(result.allowed).toBe(false);
|
||||
expect(result.reason).toContain('不允许搜索主题');
|
||||
});
|
||||
|
||||
it('主题在允许列表时通过', async () => {
|
||||
checker.setConfig({ default: 'allow', allowedTopics: ['general', 'news'] });
|
||||
|
||||
const result = await checker.checkWebPermission({
|
||||
query: 'test search',
|
||||
topic: 'news',
|
||||
});
|
||||
|
||||
expect(result.allowed).toBe(true);
|
||||
});
|
||||
|
||||
it('空主题列表允许所有主题', async () => {
|
||||
checker.setConfig({ default: 'allow', allowedTopics: [] });
|
||||
|
||||
const result = await checker.checkWebPermission({
|
||||
query: 'test search',
|
||||
topic: 'any-topic',
|
||||
});
|
||||
|
||||
expect(result.allowed).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('会话权限管理', () => {
|
||||
it('会话允许后不再询问', async () => {
|
||||
const mockCallback = vi.fn().mockResolvedValue({ allow: true, remember: true });
|
||||
checker.setAskCallback(mockCallback);
|
||||
|
||||
// 第一次调用,触发回调
|
||||
const result1 = await checker.checkWebPermission({ query: 'test1' });
|
||||
expect(result1.allowed).toBe(true);
|
||||
expect(mockCallback).toHaveBeenCalledTimes(1);
|
||||
|
||||
// 第二次调用,使用会话权限
|
||||
const result2 = await checker.checkWebPermission({ query: 'test2' });
|
||||
expect(result2.allowed).toBe(true);
|
||||
expect(mockCallback).toHaveBeenCalledTimes(1); // 不再调用
|
||||
});
|
||||
|
||||
it('会话拒绝后不再询问', async () => {
|
||||
const mockCallback = vi.fn().mockResolvedValue({ allow: false, remember: true });
|
||||
checker.setAskCallback(mockCallback);
|
||||
|
||||
// 第一次调用
|
||||
const result1 = await checker.checkWebPermission({ query: 'test1' });
|
||||
expect(result1.allowed).toBe(false);
|
||||
|
||||
// 第二次调用
|
||||
const result2 = await checker.checkWebPermission({ query: 'test2' });
|
||||
expect(result2.allowed).toBe(false);
|
||||
expect(mockCallback).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('不记住权限时每次询问', async () => {
|
||||
const mockCallback = vi.fn().mockResolvedValue({ allow: true, remember: false });
|
||||
checker.setAskCallback(mockCallback);
|
||||
|
||||
await checker.checkWebPermission({ query: 'test1' });
|
||||
await checker.checkWebPermission({ query: 'test2' });
|
||||
|
||||
expect(mockCallback).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('清除会话权限后重新询问', async () => {
|
||||
const mockCallback = vi.fn().mockResolvedValue({ allow: true, remember: true });
|
||||
checker.setAskCallback(mockCallback);
|
||||
|
||||
await checker.checkWebPermission({ query: 'test1' });
|
||||
checker.clearSessionPermissions();
|
||||
await checker.checkWebPermission({ query: 'test2' });
|
||||
|
||||
expect(mockCallback).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('check - 通用接口', () => {
|
||||
it('从 command 中提取查询', async () => {
|
||||
checker.setConfig({ default: 'allow' });
|
||||
|
||||
const result = await checker.check({
|
||||
command: 'web_search: test query',
|
||||
workdir: '/test',
|
||||
});
|
||||
|
||||
expect(result.allowed).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('配置管理', () => {
|
||||
it('getConfig 返回配置副本', () => {
|
||||
const config1 = checker.getConfig();
|
||||
config1.default = 'deny';
|
||||
|
||||
const config2 = checker.getConfig();
|
||||
expect(config2.default).toBe('ask'); // 原配置不变
|
||||
});
|
||||
|
||||
it('setConfig 部分更新配置', () => {
|
||||
checker.setConfig({ allowAdvancedSearch: false });
|
||||
|
||||
const config = checker.getConfig();
|
||||
expect(config.allowAdvancedSearch).toBe(false);
|
||||
expect(config.default).toBe('ask'); // 其他配置不变
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,156 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { matchPattern, matchRules, parseCommand, generateAskPattern } from '../../../src/permission/wildcard.js';
|
||||
|
||||
describe('matchPattern - 通配符模式匹配', () => {
|
||||
describe('* 通配符', () => {
|
||||
it('匹配任意字符', () => {
|
||||
expect(matchPattern('git diff', 'git diff*')).toBe(true);
|
||||
expect(matchPattern('git diff --staged', 'git diff*')).toBe(true);
|
||||
expect(matchPattern('git diff HEAD~1', 'git diff*')).toBe(true);
|
||||
});
|
||||
|
||||
it('不匹配不同前缀', () => {
|
||||
expect(matchPattern('git status', 'git diff*')).toBe(false);
|
||||
expect(matchPattern('git pull', 'git diff*')).toBe(false);
|
||||
});
|
||||
|
||||
it('匹配危险命令模式 rm -rf*', () => {
|
||||
expect(matchPattern('rm -rf /', 'rm -rf*')).toBe(true);
|
||||
expect(matchPattern('rm -rf /home', 'rm -rf*')).toBe(true);
|
||||
expect(matchPattern('rm -rf .', 'rm -rf*')).toBe(true);
|
||||
});
|
||||
|
||||
it('rm 普通命令不匹配 rm -rf*', () => {
|
||||
expect(matchPattern('rm file.txt', 'rm -rf*')).toBe(false);
|
||||
expect(matchPattern('rm -r dir', 'rm -rf*')).toBe(false);
|
||||
});
|
||||
|
||||
it('中间位置的通配符', () => {
|
||||
expect(matchPattern('git push origin main', 'git push * main')).toBe(true);
|
||||
expect(matchPattern('git push upstream main', 'git push * main')).toBe(true);
|
||||
});
|
||||
|
||||
it('多个通配符', () => {
|
||||
expect(matchPattern('git push origin main', 'git * origin *')).toBe(true);
|
||||
expect(matchPattern('git pull origin main', 'git * origin *')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('? 通配符', () => {
|
||||
it('匹配单个字符', () => {
|
||||
expect(matchPattern('ls -a', 'ls -?')).toBe(true);
|
||||
expect(matchPattern('ls -l', 'ls -?')).toBe(true);
|
||||
});
|
||||
|
||||
it('不匹配多个字符', () => {
|
||||
expect(matchPattern('ls -la', 'ls -?')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('精确匹配', () => {
|
||||
it('完全相同的字符串', () => {
|
||||
expect(matchPattern('ls', 'ls')).toBe(true);
|
||||
expect(matchPattern('pwd', 'pwd')).toBe(true);
|
||||
});
|
||||
|
||||
it('不匹配带参数的命令', () => {
|
||||
expect(matchPattern('ls -la', 'ls')).toBe(false);
|
||||
expect(matchPattern('pwd /home', 'pwd')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('大小写不敏感', () => {
|
||||
it('匹配不同大小写', () => {
|
||||
expect(matchPattern('GIT DIFF', 'git diff*')).toBe(true);
|
||||
expect(matchPattern('Git Diff', 'git diff*')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('特殊字符转义', () => {
|
||||
it('正确处理点号', () => {
|
||||
expect(matchPattern('file.txt', 'file.txt')).toBe(true);
|
||||
expect(matchPattern('fileatxt', 'file.txt')).toBe(false);
|
||||
});
|
||||
|
||||
it('正确处理括号', () => {
|
||||
expect(matchPattern('echo (test)', 'echo (test)')).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('matchRules - 规则匹配', () => {
|
||||
const rules = [
|
||||
{ pattern: 'ls *', action: 'allow' as const },
|
||||
{ pattern: 'rm -rf*', action: 'deny' as const },
|
||||
{ pattern: 'git *', action: 'ask' as const },
|
||||
];
|
||||
|
||||
it('匹配 allow 规则', () => {
|
||||
const result = matchRules('ls -la', rules, 'ask');
|
||||
expect(result.action).toBe('allow');
|
||||
});
|
||||
|
||||
it('匹配 deny 规则', () => {
|
||||
const result = matchRules('rm -rf /', rules, 'ask');
|
||||
expect(result.action).toBe('deny');
|
||||
});
|
||||
|
||||
it('匹配 ask 规则', () => {
|
||||
const result = matchRules('git push', rules, 'deny');
|
||||
expect(result.action).toBe('ask');
|
||||
});
|
||||
|
||||
it('无匹配时返回默认动作', () => {
|
||||
const result = matchRules('npm install', rules, 'ask');
|
||||
expect(result.action).toBe('ask');
|
||||
});
|
||||
|
||||
it('返回匹配的模式', () => {
|
||||
const result = matchRules('rm -rf /home', rules, 'ask');
|
||||
expect(result.matchedPattern).toBe('rm -rf*');
|
||||
});
|
||||
|
||||
it('空规则列表返回默认动作', () => {
|
||||
const result = matchRules('any command', [], 'allow');
|
||||
expect(result.action).toBe('allow');
|
||||
});
|
||||
});
|
||||
|
||||
describe('parseCommand - 命令解析', () => {
|
||||
it('解析简单命令', () => {
|
||||
const result = parseCommand('ls');
|
||||
expect(result.head).toBe('ls');
|
||||
expect(result.sub).toBeUndefined();
|
||||
});
|
||||
|
||||
it('解析带子命令的命令', () => {
|
||||
const result = parseCommand('git push');
|
||||
expect(result.head).toBe('git');
|
||||
expect(result.sub).toBe('push');
|
||||
});
|
||||
|
||||
it('解析带参数的命令', () => {
|
||||
const result = parseCommand('git push origin main');
|
||||
expect(result.head).toBe('git');
|
||||
expect(result.sub).toBe('push');
|
||||
expect(result.args).toContain('origin');
|
||||
expect(result.args).toContain('main');
|
||||
});
|
||||
});
|
||||
|
||||
describe('generateAskPattern - 生成询问模式', () => {
|
||||
it('简单命令生成 cmd *', () => {
|
||||
const pattern = generateAskPattern('ls -la');
|
||||
expect(pattern).toBe('ls *');
|
||||
});
|
||||
|
||||
it('带子命令生成 cmd sub *', () => {
|
||||
const pattern = generateAskPattern('git push origin');
|
||||
expect(pattern).toBe('git push *');
|
||||
});
|
||||
|
||||
it('npm install 生成 npm install *', () => {
|
||||
const pattern = generateAskPattern('npm install lodash');
|
||||
expect(pattern).toBe('npm install *');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,468 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { SessionManager } from '../../../src/session/manager.js';
|
||||
import { SessionStorage } from '../../../src/session/storage.js';
|
||||
import type { SessionData, Todo } from '../../../src/session/types.js';
|
||||
import type { ModelMessage } from 'ai';
|
||||
|
||||
// Mock SessionStorage
|
||||
class MockSessionStorage extends SessionStorage {
|
||||
private mockCurrentSession: SessionData | null = null;
|
||||
private mockSessions: Map<string, SessionData> = new Map();
|
||||
|
||||
constructor() {
|
||||
super('/tmp/test-sessions');
|
||||
}
|
||||
|
||||
async ensureDir(): Promise<void> {
|
||||
// no-op for testing
|
||||
}
|
||||
|
||||
generateSessionId(): string {
|
||||
return `test-${Date.now()}-${Math.random().toString(36).substring(2, 6)}`;
|
||||
}
|
||||
|
||||
async saveCurrentSession(session: SessionData): Promise<void> {
|
||||
this.mockCurrentSession = { ...session, updatedAt: new Date().toISOString() };
|
||||
}
|
||||
|
||||
async loadCurrentSession(): Promise<SessionData | null> {
|
||||
return this.mockCurrentSession;
|
||||
}
|
||||
|
||||
async archiveCurrentSession(): Promise<void> {
|
||||
if (this.mockCurrentSession) {
|
||||
this.mockSessions.set(this.mockCurrentSession.id, { ...this.mockCurrentSession });
|
||||
this.mockCurrentSession = null;
|
||||
}
|
||||
}
|
||||
|
||||
async clearCurrentSession(): Promise<void> {
|
||||
this.mockCurrentSession = null;
|
||||
}
|
||||
|
||||
async listSessions(): Promise<{ id: string; title: string; workdir: string; messageCount: number; createdAt: string; updatedAt: string }[]> {
|
||||
return Array.from(this.mockSessions.values()).map((s) => ({
|
||||
id: s.id,
|
||||
title: s.title || `Session ${s.id}`,
|
||||
workdir: s.workdir,
|
||||
messageCount: s.messages.length,
|
||||
createdAt: s.createdAt,
|
||||
updatedAt: s.updatedAt,
|
||||
}));
|
||||
}
|
||||
|
||||
async loadSession(sessionId: string): Promise<SessionData | null> {
|
||||
return this.mockSessions.get(sessionId) || null;
|
||||
}
|
||||
|
||||
async saveSession(session: SessionData): Promise<void> {
|
||||
this.mockSessions.set(session.id, { ...session, updatedAt: new Date().toISOString() });
|
||||
}
|
||||
|
||||
async deleteSession(sessionId: string): Promise<boolean> {
|
||||
return this.mockSessions.delete(sessionId);
|
||||
}
|
||||
|
||||
async cleanupOldSessions(keepCount: number = 50): Promise<number> {
|
||||
const sessions = await this.listSessions();
|
||||
if (sessions.length <= keepCount) return 0;
|
||||
const toDelete = sessions.slice(keepCount);
|
||||
let count = 0;
|
||||
for (const s of toDelete) {
|
||||
if (await this.deleteSession(s.id)) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// Helper methods for testing
|
||||
_setCurrentSession(session: SessionData | null): void {
|
||||
this.mockCurrentSession = session;
|
||||
}
|
||||
|
||||
_addSession(session: SessionData): void {
|
||||
this.mockSessions.set(session.id, session);
|
||||
}
|
||||
|
||||
_clear(): void {
|
||||
this.mockCurrentSession = null;
|
||||
this.mockSessions.clear();
|
||||
}
|
||||
}
|
||||
|
||||
describe('SessionManager - 会话管理器', () => {
|
||||
let storage: MockSessionStorage;
|
||||
let manager: SessionManager;
|
||||
|
||||
beforeEach(() => {
|
||||
storage = new MockSessionStorage();
|
||||
manager = new SessionManager(storage);
|
||||
vi.useFakeTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
manager.stopAutoSave();
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
describe('init - 初始化', () => {
|
||||
it('无现有会话时创建新会话', async () => {
|
||||
const session = await manager.init('/test/workdir');
|
||||
|
||||
expect(session).toBeDefined();
|
||||
expect(session.id).toBeDefined();
|
||||
expect(session.workdir).toBe('/test/workdir');
|
||||
expect(session.messages).toHaveLength(0);
|
||||
expect(session.discoveredTools).toHaveLength(0);
|
||||
expect(session.todos).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('同一工作目录恢复现有会话', async () => {
|
||||
const existingSession: SessionData = {
|
||||
id: 'existing-session',
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
workdir: '/test/workdir',
|
||||
messages: [{ role: 'user', content: 'Hello' }],
|
||||
discoveredTools: ['tool1'],
|
||||
todos: [],
|
||||
};
|
||||
storage._setCurrentSession(existingSession);
|
||||
|
||||
const session = await manager.init('/test/workdir');
|
||||
|
||||
expect(session.id).toBe('existing-session');
|
||||
expect(session.messages).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('不同工作目录创建新会话并归档旧会话', async () => {
|
||||
const existingSession: SessionData = {
|
||||
id: 'old-session',
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
workdir: '/old/workdir',
|
||||
messages: [{ role: 'user', content: 'Old message' }],
|
||||
discoveredTools: [],
|
||||
todos: [],
|
||||
};
|
||||
storage._setCurrentSession(existingSession);
|
||||
|
||||
const session = await manager.init('/new/workdir');
|
||||
|
||||
expect(session.id).not.toBe('old-session');
|
||||
expect(session.workdir).toBe('/new/workdir');
|
||||
// 旧会话应该被归档
|
||||
const sessions = await storage.listSessions();
|
||||
expect(sessions.some((s) => s.id === 'old-session')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('消息管理', () => {
|
||||
beforeEach(async () => {
|
||||
await manager.init('/test/workdir');
|
||||
});
|
||||
|
||||
it('添加单条消息', async () => {
|
||||
const message: ModelMessage = { role: 'user', content: 'Test message' };
|
||||
await manager.addMessage(message);
|
||||
|
||||
const messages = manager.getMessages();
|
||||
expect(messages).toHaveLength(1);
|
||||
expect(messages[0].content).toBe('Test message');
|
||||
});
|
||||
|
||||
it('批量设置消息', async () => {
|
||||
const messages: ModelMessage[] = [
|
||||
{ role: 'user', content: 'Message 1' },
|
||||
{ role: 'assistant', content: 'Response 1' },
|
||||
{ role: 'user', content: 'Message 2' },
|
||||
];
|
||||
await manager.setMessages(messages);
|
||||
|
||||
expect(manager.getMessages()).toHaveLength(3);
|
||||
});
|
||||
|
||||
it('无当前会话时添加消息不报错', async () => {
|
||||
const newManager = new SessionManager(new MockSessionStorage());
|
||||
// 不调用 init,直接添加消息
|
||||
await newManager.addMessage({ role: 'user', content: 'Test' });
|
||||
expect(newManager.getMessages()).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('工具发现管理', () => {
|
||||
beforeEach(async () => {
|
||||
await manager.init('/test/workdir');
|
||||
});
|
||||
|
||||
it('设置已发现的工具', async () => {
|
||||
await manager.setDiscoveredTools(['tool1', 'tool2', 'tool3']);
|
||||
|
||||
expect(manager.getDiscoveredTools()).toEqual(['tool1', 'tool2', 'tool3']);
|
||||
});
|
||||
|
||||
it('更新已发现的工具', async () => {
|
||||
await manager.setDiscoveredTools(['tool1']);
|
||||
await manager.setDiscoveredTools(['tool1', 'tool2']);
|
||||
|
||||
expect(manager.getDiscoveredTools()).toEqual(['tool1', 'tool2']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('待办事项管理', () => {
|
||||
beforeEach(async () => {
|
||||
await manager.init('/test/workdir');
|
||||
});
|
||||
|
||||
it('设置待办事项', async () => {
|
||||
const todos: Todo[] = [
|
||||
{
|
||||
id: '1',
|
||||
content: 'Task 1',
|
||||
status: 'pending',
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
},
|
||||
{
|
||||
id: '2',
|
||||
content: 'Task 2',
|
||||
status: 'in_progress',
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
},
|
||||
];
|
||||
await manager.setTodos(todos);
|
||||
|
||||
expect(manager.getTodos()).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('无当前会话时返回空数组', () => {
|
||||
const newManager = new SessionManager(new MockSessionStorage());
|
||||
expect(newManager.getTodos()).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('newSession - 创建新会话', () => {
|
||||
beforeEach(async () => {
|
||||
await manager.init('/test/workdir');
|
||||
await manager.addMessage({ role: 'user', content: 'Test' });
|
||||
});
|
||||
|
||||
it('创建新会话并归档旧会话', async () => {
|
||||
const oldSessionId = manager.getSessionId();
|
||||
const newSession = await manager.newSession();
|
||||
|
||||
expect(newSession.id).not.toBe(oldSessionId);
|
||||
expect(newSession.messages).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('使用指定工作目录创建新会话', async () => {
|
||||
const newSession = await manager.newSession('/new/workdir');
|
||||
|
||||
expect(newSession.workdir).toBe('/new/workdir');
|
||||
});
|
||||
|
||||
it('空消息会话不归档', async () => {
|
||||
const emptySession = await manager.newSession('/empty/workdir');
|
||||
const anotherSession = await manager.newSession('/another/workdir');
|
||||
|
||||
// 空会话不应该被归档
|
||||
const sessions = await manager.listSessions();
|
||||
expect(sessions.every((s) => s.id !== emptySession.id)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('子会话管理', () => {
|
||||
beforeEach(async () => {
|
||||
await manager.init('/test/workdir');
|
||||
});
|
||||
|
||||
it('创建子会话', () => {
|
||||
const parentId = manager.getSessionId()!;
|
||||
const childSession = manager.createChildSession(parentId, 'explore', 'Search task');
|
||||
|
||||
expect(childSession.parentId).toBe(parentId);
|
||||
expect(childSession.agentName).toBe('explore');
|
||||
expect(childSession.title).toBe('Search task');
|
||||
expect(childSession.workdir).toBe('/test/workdir');
|
||||
});
|
||||
|
||||
it('子会话使用默认标题', () => {
|
||||
const parentId = manager.getSessionId()!;
|
||||
const childSession = manager.createChildSession(parentId, 'code-reviewer');
|
||||
|
||||
expect(childSession.title).toBe('子任务 (@code-reviewer)');
|
||||
});
|
||||
|
||||
it('保存子会话', async () => {
|
||||
const parentId = manager.getSessionId()!;
|
||||
const childSession = manager.createChildSession(parentId, 'explore');
|
||||
childSession.messages.push({ role: 'user', content: 'Explore task' });
|
||||
|
||||
await manager.saveChildSession(childSession);
|
||||
|
||||
const saved = await storage.loadSession(childSession.id);
|
||||
expect(saved).not.toBeNull();
|
||||
expect(saved?.parentId).toBe(parentId);
|
||||
});
|
||||
});
|
||||
|
||||
describe('会话恢复', () => {
|
||||
let archivedSessionId: string;
|
||||
|
||||
beforeEach(async () => {
|
||||
// 清理之前的状态
|
||||
storage._clear();
|
||||
// 创建并归档一个会话
|
||||
await manager.init('/test/workdir');
|
||||
await manager.addMessage({ role: 'user', content: 'Archived message' });
|
||||
archivedSessionId = manager.getSessionId()!;
|
||||
await manager.newSession('/another/workdir');
|
||||
});
|
||||
|
||||
it('恢复历史会话', async () => {
|
||||
const restored = await manager.restoreSession(archivedSessionId);
|
||||
|
||||
expect(restored).not.toBeNull();
|
||||
expect(restored?.id).toBe(archivedSessionId);
|
||||
expect(manager.getMessages()).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('恢复不存在的会话返回 null', async () => {
|
||||
const result = await manager.restoreSession('non-existent-id');
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('会话列表和删除', () => {
|
||||
beforeEach(async () => {
|
||||
// 清理之前的状态
|
||||
storage._clear();
|
||||
// 创建第一个会话
|
||||
await manager.init('/workdir0');
|
||||
await manager.addMessage({ role: 'user', content: 'Message 0' });
|
||||
// 创建后续会话(使用 newSession 避免 init 的额外归档)
|
||||
for (let i = 1; i <= 2; i++) {
|
||||
await manager.newSession(`/workdir${i}`);
|
||||
await manager.addMessage({ role: 'user', content: `Message ${i}` });
|
||||
}
|
||||
// 最后归档当前会话
|
||||
await manager.newSession('/final');
|
||||
});
|
||||
|
||||
it('列出历史会话', async () => {
|
||||
const sessions = await manager.listSessions();
|
||||
expect(sessions.length).toBe(3);
|
||||
});
|
||||
|
||||
it('删除历史会话', async () => {
|
||||
const sessions = await manager.listSessions();
|
||||
const toDelete = sessions[0].id;
|
||||
|
||||
const result = await manager.deleteSession(toDelete);
|
||||
|
||||
expect(result).toBe(true);
|
||||
const remaining = await manager.listSessions();
|
||||
expect(remaining.length).toBe(2);
|
||||
});
|
||||
|
||||
it('删除不存在的会话返回 false', async () => {
|
||||
const result = await manager.deleteSession('non-existent');
|
||||
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getSessionId', () => {
|
||||
it('返回当前会话 ID', async () => {
|
||||
await manager.init('/test/workdir');
|
||||
const id = manager.getSessionId();
|
||||
|
||||
expect(id).toBeDefined();
|
||||
expect(typeof id).toBe('string');
|
||||
});
|
||||
|
||||
it('无会话时返回 undefined', () => {
|
||||
const newManager = new SessionManager(new MockSessionStorage());
|
||||
expect(newManager.getSessionId()).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('自动保存', () => {
|
||||
it('自动保存每 30 秒执行', async () => {
|
||||
await manager.init('/test/workdir');
|
||||
const saveSpy = vi.spyOn(manager, 'save');
|
||||
|
||||
// 前进 30 秒
|
||||
await vi.advanceTimersByTimeAsync(30000);
|
||||
|
||||
expect(saveSpy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('stopAutoSave 停止自动保存', async () => {
|
||||
await manager.init('/test/workdir');
|
||||
const saveSpy = vi.spyOn(manager, 'save');
|
||||
|
||||
manager.stopAutoSave();
|
||||
await vi.advanceTimersByTimeAsync(60000);
|
||||
|
||||
// 只有 init 时调用了一次
|
||||
expect(saveSpy).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('close - 关闭管理器', () => {
|
||||
it('关闭时保存并停止自动保存', async () => {
|
||||
await manager.init('/test/workdir');
|
||||
const saveSpy = vi.spyOn(manager, 'save');
|
||||
const stopSpy = vi.spyOn(manager, 'stopAutoSave');
|
||||
|
||||
await manager.close();
|
||||
|
||||
expect(saveSpy).toHaveBeenCalled();
|
||||
expect(stopSpy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('cleanup - 清理旧会话', () => {
|
||||
beforeEach(async () => {
|
||||
// 清理之前的状态
|
||||
storage._clear();
|
||||
// 创建第一个会话
|
||||
await manager.init('/workdir0');
|
||||
await manager.addMessage({ role: 'user', content: 'Message 0' });
|
||||
// 创建 9 个后续会话(使用 newSession 避免 init 的额外归档)
|
||||
for (let i = 1; i <= 9; i++) {
|
||||
await manager.newSession(`/workdir${i}`);
|
||||
await manager.addMessage({ role: 'user', content: `Message ${i}` });
|
||||
}
|
||||
// 最后归档当前会话
|
||||
await manager.newSession('/final');
|
||||
});
|
||||
|
||||
it('清理保留指定数量的会话', async () => {
|
||||
const deleted = await manager.cleanup(5);
|
||||
|
||||
expect(deleted).toBe(5);
|
||||
const remaining = await manager.listSessions();
|
||||
expect(remaining.length).toBe(5);
|
||||
});
|
||||
|
||||
it('会话数量不足时不清理', async () => {
|
||||
const deleted = await manager.cleanup(20);
|
||||
|
||||
expect(deleted).toBe(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('SessionStorage - 会话存储', () => {
|
||||
it('generateSessionId 生成唯一 ID', () => {
|
||||
const storage = new SessionStorage('/tmp/test');
|
||||
const id1 = storage.generateSessionId();
|
||||
const id2 = storage.generateSessionId();
|
||||
|
||||
expect(id1).not.toBe(id2);
|
||||
expect(id1).toMatch(/^\d{4}-\d{2}-\d{2}_[a-z0-9]+$/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,414 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
mkdir: vi.fn().mockResolvedValue(undefined),
|
||||
writeFile: vi.fn().mockResolvedValue(undefined),
|
||||
readFile: vi.fn().mockResolvedValue('{}'),
|
||||
readdir: vi.fn().mockResolvedValue([]),
|
||||
unlink: vi.fn().mockResolvedValue(undefined),
|
||||
stat: vi.fn().mockResolvedValue({ isDirectory: () => false }),
|
||||
}));
|
||||
|
||||
// Mock os
|
||||
vi.mock('os', () => ({
|
||||
homedir: vi.fn(() => '/home/testuser'),
|
||||
}));
|
||||
|
||||
import { SessionStorage } from '../../../src/session/storage.js';
|
||||
import * as fs from 'fs/promises';
|
||||
|
||||
describe('SessionStorage - 会话存储', () => {
|
||||
let storage: SessionStorage;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
storage = new SessionStorage('/test/storage');
|
||||
});
|
||||
|
||||
describe('构造函数', () => {
|
||||
it('使用提供的存储目录', () => {
|
||||
const s = new SessionStorage('/custom/path');
|
||||
expect(s.getStorageDir()).toBe('/custom/path');
|
||||
});
|
||||
|
||||
it('默认使用 XDG 规范路径', () => {
|
||||
const originalEnv = process.env.XDG_DATA_HOME;
|
||||
process.env.XDG_DATA_HOME = '/xdg/data';
|
||||
|
||||
const s = new SessionStorage();
|
||||
expect(s.getStorageDir()).toBe('/xdg/data/ai-assist');
|
||||
|
||||
process.env.XDG_DATA_HOME = originalEnv;
|
||||
});
|
||||
|
||||
it('无 XDG 环境变量使用 home 目录', () => {
|
||||
const originalEnv = process.env.XDG_DATA_HOME;
|
||||
delete process.env.XDG_DATA_HOME;
|
||||
|
||||
const s = new SessionStorage();
|
||||
expect(s.getStorageDir()).toContain('.local/share/ai-assist');
|
||||
|
||||
process.env.XDG_DATA_HOME = originalEnv;
|
||||
});
|
||||
});
|
||||
|
||||
describe('generateSessionId - 生成会话 ID', () => {
|
||||
it('生成包含日期的会话 ID', () => {
|
||||
const id = storage.generateSessionId();
|
||||
|
||||
// 格式: YYYY-MM-DD_xxxxxx
|
||||
expect(id).toMatch(/^\d{4}-\d{2}-\d{2}_[a-z0-9]{6}$/);
|
||||
});
|
||||
|
||||
it('生成唯一的会话 ID', () => {
|
||||
const ids = new Set();
|
||||
for (let i = 0; i < 100; i++) {
|
||||
ids.add(storage.generateSessionId());
|
||||
}
|
||||
expect(ids.size).toBe(100);
|
||||
});
|
||||
});
|
||||
|
||||
describe('ensureDir - 确保目录存在', () => {
|
||||
it('创建会话目录', async () => {
|
||||
await storage.ensureDir();
|
||||
|
||||
expect(fs.mkdir).toHaveBeenCalledWith(
|
||||
expect.stringContaining('sessions'),
|
||||
{ recursive: true }
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('saveCurrentSession - 保存当前会话', () => {
|
||||
it('保存会话数据', async () => {
|
||||
const session = {
|
||||
id: 'test-session',
|
||||
workdir: '/test',
|
||||
messages: [{ role: 'user', content: 'hello' }],
|
||||
createdAt: '2024-01-01T00:00:00Z',
|
||||
updatedAt: '2024-01-01T00:00:00Z',
|
||||
};
|
||||
|
||||
await storage.saveCurrentSession(session as any);
|
||||
|
||||
expect(fs.mkdir).toHaveBeenCalled();
|
||||
expect(fs.writeFile).toHaveBeenCalledWith(
|
||||
expect.stringContaining('current-session.json'),
|
||||
expect.any(String),
|
||||
'utf-8'
|
||||
);
|
||||
});
|
||||
|
||||
it('更新 updatedAt 时间戳', async () => {
|
||||
const session = {
|
||||
id: 'test-session',
|
||||
workdir: '/test',
|
||||
messages: [],
|
||||
createdAt: '2024-01-01T00:00:00Z',
|
||||
updatedAt: '2024-01-01T00:00:00Z',
|
||||
};
|
||||
|
||||
await storage.saveCurrentSession(session as any);
|
||||
|
||||
const writeCall = vi.mocked(fs.writeFile).mock.calls[0];
|
||||
const savedData = JSON.parse(writeCall[1] as string);
|
||||
expect(new Date(savedData.updatedAt).getTime()).toBeGreaterThan(
|
||||
new Date('2024-01-01T00:00:00Z').getTime()
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadCurrentSession - 加载当前会话', () => {
|
||||
it('成功加载会话', async () => {
|
||||
const sessionData = {
|
||||
id: 'test-session',
|
||||
workdir: '/test',
|
||||
messages: [],
|
||||
createdAt: '2024-01-01T00:00:00Z',
|
||||
updatedAt: '2024-01-01T00:00:00Z',
|
||||
};
|
||||
vi.mocked(fs.readFile).mockResolvedValueOnce(JSON.stringify(sessionData));
|
||||
|
||||
const session = await storage.loadCurrentSession();
|
||||
|
||||
expect(session).toEqual(sessionData);
|
||||
});
|
||||
|
||||
it('文件不存在返回 null', async () => {
|
||||
vi.mocked(fs.readFile).mockRejectedValueOnce(new Error('ENOENT'));
|
||||
|
||||
const session = await storage.loadCurrentSession();
|
||||
|
||||
expect(session).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('archiveCurrentSession - 归档当前会话', () => {
|
||||
it('归档有消息的会话', async () => {
|
||||
const sessionData = {
|
||||
id: 'test-session',
|
||||
workdir: '/test',
|
||||
messages: [{ role: 'user', content: 'test' }],
|
||||
createdAt: '2024-01-01T00:00:00Z',
|
||||
updatedAt: '2024-01-01T00:00:00Z',
|
||||
};
|
||||
vi.mocked(fs.readFile).mockResolvedValueOnce(JSON.stringify(sessionData));
|
||||
|
||||
await storage.archiveCurrentSession();
|
||||
|
||||
expect(fs.writeFile).toHaveBeenCalledWith(
|
||||
expect.stringContaining('test-session.json'),
|
||||
expect.any(String),
|
||||
'utf-8'
|
||||
);
|
||||
});
|
||||
|
||||
it('空会话不归档', async () => {
|
||||
const sessionData = {
|
||||
id: 'test-session',
|
||||
workdir: '/test',
|
||||
messages: [],
|
||||
createdAt: '2024-01-01T00:00:00Z',
|
||||
updatedAt: '2024-01-01T00:00:00Z',
|
||||
};
|
||||
vi.mocked(fs.readFile).mockResolvedValueOnce(JSON.stringify(sessionData));
|
||||
|
||||
await storage.archiveCurrentSession();
|
||||
|
||||
// writeFile 不应该被调用(只有 ensureDir 的 mkdir)
|
||||
expect(fs.writeFile).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('无当前会话不操作', async () => {
|
||||
vi.mocked(fs.readFile).mockRejectedValueOnce(new Error('ENOENT'));
|
||||
|
||||
await storage.archiveCurrentSession();
|
||||
|
||||
expect(fs.writeFile).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('clearCurrentSession - 清除当前会话', () => {
|
||||
it('删除当前会话文件', async () => {
|
||||
await storage.clearCurrentSession();
|
||||
|
||||
expect(fs.unlink).toHaveBeenCalledWith(
|
||||
expect.stringContaining('current-session.json')
|
||||
);
|
||||
});
|
||||
|
||||
it('文件不存在不报错', async () => {
|
||||
vi.mocked(fs.unlink).mockRejectedValueOnce(new Error('ENOENT'));
|
||||
|
||||
await expect(storage.clearCurrentSession()).resolves.not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('listSessions - 列出历史会话', () => {
|
||||
it('返回会话摘要列表', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValueOnce(['session1.json', 'session2.json'] as any);
|
||||
|
||||
const session1 = {
|
||||
id: 'session1',
|
||||
workdir: '/test1',
|
||||
messages: [{ role: 'user', content: '第一条消息' }],
|
||||
createdAt: '2024-01-01T00:00:00Z',
|
||||
updatedAt: '2024-01-02T00:00:00Z',
|
||||
};
|
||||
const session2 = {
|
||||
id: 'session2',
|
||||
workdir: '/test2',
|
||||
messages: [],
|
||||
createdAt: '2024-01-03T00:00:00Z',
|
||||
updatedAt: '2024-01-04T00:00:00Z',
|
||||
};
|
||||
|
||||
vi.mocked(fs.readFile)
|
||||
.mockResolvedValueOnce(JSON.stringify(session1))
|
||||
.mockResolvedValueOnce(JSON.stringify(session2));
|
||||
|
||||
const sessions = await storage.listSessions();
|
||||
|
||||
expect(sessions).toHaveLength(2);
|
||||
// 按更新时间降序
|
||||
expect(sessions[0].id).toBe('session2');
|
||||
expect(sessions[1].id).toBe('session1');
|
||||
});
|
||||
|
||||
it('生成会话标题', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValueOnce(['session1.json'] as any);
|
||||
|
||||
const session = {
|
||||
id: 'session1',
|
||||
workdir: '/test',
|
||||
messages: [{ role: 'user', content: '这是第一条用户消息' }],
|
||||
createdAt: '2024-01-01T00:00:00Z',
|
||||
updatedAt: '2024-01-01T00:00:00Z',
|
||||
};
|
||||
vi.mocked(fs.readFile).mockResolvedValueOnce(JSON.stringify(session));
|
||||
|
||||
const sessions = await storage.listSessions();
|
||||
|
||||
expect(sessions[0].title).toBe('这是第一条用户消息');
|
||||
});
|
||||
|
||||
it('长标题截断', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValueOnce(['session1.json'] as any);
|
||||
|
||||
const longContent = 'a'.repeat(100);
|
||||
const session = {
|
||||
id: 'session1',
|
||||
workdir: '/test',
|
||||
messages: [{ role: 'user', content: longContent }],
|
||||
createdAt: '2024-01-01T00:00:00Z',
|
||||
updatedAt: '2024-01-01T00:00:00Z',
|
||||
};
|
||||
vi.mocked(fs.readFile).mockResolvedValueOnce(JSON.stringify(session));
|
||||
|
||||
const sessions = await storage.listSessions();
|
||||
|
||||
expect(sessions[0].title.length).toBeLessThanOrEqual(53); // 50 + '...'
|
||||
});
|
||||
|
||||
it('跳过非 JSON 文件', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValueOnce(['session.json', 'readme.txt'] as any);
|
||||
|
||||
const session = {
|
||||
id: 'session',
|
||||
workdir: '/test',
|
||||
messages: [],
|
||||
createdAt: '2024-01-01T00:00:00Z',
|
||||
updatedAt: '2024-01-01T00:00:00Z',
|
||||
};
|
||||
vi.mocked(fs.readFile).mockResolvedValueOnce(JSON.stringify(session));
|
||||
|
||||
const sessions = await storage.listSessions();
|
||||
|
||||
expect(sessions).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('跳过无法解析的文件', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValueOnce(['session.json'] as any);
|
||||
vi.mocked(fs.readFile).mockResolvedValueOnce('invalid json');
|
||||
|
||||
const sessions = await storage.listSessions();
|
||||
|
||||
expect(sessions).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadSession - 加载指定会话', () => {
|
||||
it('成功加载会话', async () => {
|
||||
const sessionData = {
|
||||
id: 'session-123',
|
||||
workdir: '/test',
|
||||
messages: [],
|
||||
createdAt: '2024-01-01T00:00:00Z',
|
||||
updatedAt: '2024-01-01T00:00:00Z',
|
||||
};
|
||||
vi.mocked(fs.readFile).mockResolvedValueOnce(JSON.stringify(sessionData));
|
||||
|
||||
const session = await storage.loadSession('session-123');
|
||||
|
||||
expect(session).toEqual(sessionData);
|
||||
expect(fs.readFile).toHaveBeenCalledWith(
|
||||
expect.stringContaining('session-123.json'),
|
||||
'utf-8'
|
||||
);
|
||||
});
|
||||
|
||||
it('会话不存在返回 null', async () => {
|
||||
vi.mocked(fs.readFile).mockRejectedValueOnce(new Error('ENOENT'));
|
||||
|
||||
const session = await storage.loadSession('nonexistent');
|
||||
|
||||
expect(session).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('saveSession - 保存指定会话', () => {
|
||||
it('保存会话到文件', async () => {
|
||||
const session = {
|
||||
id: 'child-session',
|
||||
workdir: '/test',
|
||||
messages: [{ role: 'assistant', content: 'response' }],
|
||||
createdAt: '2024-01-01T00:00:00Z',
|
||||
updatedAt: '2024-01-01T00:00:00Z',
|
||||
};
|
||||
|
||||
await storage.saveSession(session as any);
|
||||
|
||||
expect(fs.writeFile).toHaveBeenCalledWith(
|
||||
expect.stringContaining('child-session.json'),
|
||||
expect.any(String),
|
||||
'utf-8'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('deleteSession - 删除会话', () => {
|
||||
it('成功删除返回 true', async () => {
|
||||
const result = await storage.deleteSession('session-123');
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(fs.unlink).toHaveBeenCalledWith(
|
||||
expect.stringContaining('session-123.json')
|
||||
);
|
||||
});
|
||||
|
||||
it('删除失败返回 false', async () => {
|
||||
vi.mocked(fs.unlink).mockRejectedValueOnce(new Error('ENOENT'));
|
||||
|
||||
const result = await storage.deleteSession('nonexistent');
|
||||
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('cleanupOldSessions - 清理旧会话', () => {
|
||||
it('删除超出保留数量的会话', async () => {
|
||||
// Mock listSessions 返回 3 个会话
|
||||
vi.mocked(fs.readdir).mockResolvedValueOnce([
|
||||
'session1.json',
|
||||
'session2.json',
|
||||
'session3.json',
|
||||
] as any);
|
||||
|
||||
const sessions = [
|
||||
{ id: 'session3', updatedAt: '2024-01-03' },
|
||||
{ id: 'session2', updatedAt: '2024-01-02' },
|
||||
{ id: 'session1', updatedAt: '2024-01-01' },
|
||||
];
|
||||
|
||||
vi.mocked(fs.readFile)
|
||||
.mockResolvedValueOnce(JSON.stringify({ ...sessions[0], messages: [], workdir: '/', createdAt: '2024-01-01' }))
|
||||
.mockResolvedValueOnce(JSON.stringify({ ...sessions[1], messages: [], workdir: '/', createdAt: '2024-01-01' }))
|
||||
.mockResolvedValueOnce(JSON.stringify({ ...sessions[2], messages: [], workdir: '/', createdAt: '2024-01-01' }));
|
||||
|
||||
const deletedCount = await storage.cleanupOldSessions(2);
|
||||
|
||||
expect(deletedCount).toBe(1);
|
||||
expect(fs.unlink).toHaveBeenCalledWith(
|
||||
expect.stringContaining('session1.json')
|
||||
);
|
||||
});
|
||||
|
||||
it('会话数量不超过保留数量不删除', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValueOnce(['session1.json'] as any);
|
||||
vi.mocked(fs.readFile).mockResolvedValueOnce(JSON.stringify({
|
||||
id: 'session1',
|
||||
messages: [],
|
||||
workdir: '/',
|
||||
createdAt: '2024-01-01',
|
||||
updatedAt: '2024-01-01',
|
||||
}));
|
||||
|
||||
const deletedCount = await storage.cleanupOldSessions(5);
|
||||
|
||||
expect(deletedCount).toBe(0);
|
||||
expect(fs.unlink).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,272 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import {
|
||||
builtinSkills,
|
||||
codeReviewSkill,
|
||||
explainCodeSkill,
|
||||
generateDocsSkill,
|
||||
generateTestsSkill,
|
||||
refactorSuggestSkill,
|
||||
fixBugSkill,
|
||||
gitCommitSkill,
|
||||
apiDesignSkill,
|
||||
} from '../../../../src/skills/builtin/index.js';
|
||||
|
||||
describe('Builtin Skills - 内置技能', () => {
|
||||
describe('builtinSkills 数组', () => {
|
||||
it('包含所有内置 Skills', () => {
|
||||
expect(builtinSkills).toHaveLength(8);
|
||||
});
|
||||
|
||||
it('所有 Skills 都是启用状态', () => {
|
||||
builtinSkills.forEach((skill) => {
|
||||
expect(skill.enabled).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
it('所有 Skills 都有必要字段', () => {
|
||||
builtinSkills.forEach((skill) => {
|
||||
expect(skill.name).toBeDefined();
|
||||
expect(skill.displayName).toBeDefined();
|
||||
expect(skill.description).toBeDefined();
|
||||
expect(skill.promptTemplate).toBeDefined();
|
||||
expect(skill.parameters).toBeDefined();
|
||||
expect(skill.category).toBeDefined();
|
||||
expect(skill.source).toBe('builtin');
|
||||
});
|
||||
});
|
||||
|
||||
it('所有 Skills 都有关键词', () => {
|
||||
builtinSkills.forEach((skill) => {
|
||||
expect(skill.keywords).toBeDefined();
|
||||
expect(skill.keywords!.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
it('Skills 名称唯一', () => {
|
||||
const names = builtinSkills.map((s) => s.name);
|
||||
const uniqueNames = new Set(names);
|
||||
expect(uniqueNames.size).toBe(names.length);
|
||||
});
|
||||
});
|
||||
|
||||
describe('codeReviewSkill - 代码审查', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(codeReviewSkill.name).toBe('code-review');
|
||||
expect(codeReviewSkill.displayName).toBe('代码审查');
|
||||
});
|
||||
|
||||
it('属于 development 分类', () => {
|
||||
expect(codeReviewSkill.category).toBe('development');
|
||||
});
|
||||
|
||||
it('code 参数是必需的', () => {
|
||||
expect(codeReviewSkill.parameters.code.required).toBe(true);
|
||||
});
|
||||
|
||||
it('focus 参数是可选的', () => {
|
||||
expect(codeReviewSkill.parameters.focus.required).toBe(false);
|
||||
});
|
||||
|
||||
it('模板包含审查相关内容', () => {
|
||||
expect(codeReviewSkill.promptTemplate).toContain('审查');
|
||||
expect(codeReviewSkill.promptTemplate).toContain('{{code}}');
|
||||
});
|
||||
|
||||
it('关键词包含相关词汇', () => {
|
||||
expect(codeReviewSkill.keywords).toContain('review');
|
||||
expect(codeReviewSkill.keywords).toContain('审查');
|
||||
});
|
||||
});
|
||||
|
||||
describe('explainCodeSkill - 代码解释', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(explainCodeSkill.name).toBe('explain-code');
|
||||
expect(explainCodeSkill.displayName).toBe('代码解释');
|
||||
});
|
||||
|
||||
it('属于 development 分类', () => {
|
||||
expect(explainCodeSkill.category).toBe('development');
|
||||
});
|
||||
|
||||
it('code 参数是必需的', () => {
|
||||
expect(explainCodeSkill.parameters.code.required).toBe(true);
|
||||
});
|
||||
|
||||
it('level 参数有枚举值', () => {
|
||||
expect(explainCodeSkill.parameters.level.enum).toContain('beginner');
|
||||
expect(explainCodeSkill.parameters.level.enum).toContain('intermediate');
|
||||
expect(explainCodeSkill.parameters.level.enum).toContain('advanced');
|
||||
});
|
||||
|
||||
it('level 参数有默认值', () => {
|
||||
expect(explainCodeSkill.parameters.level.default).toBe('intermediate');
|
||||
});
|
||||
});
|
||||
|
||||
describe('generateDocsSkill - 文档生成', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(generateDocsSkill.name).toBe('generate-docs');
|
||||
expect(generateDocsSkill.displayName).toBe('文档生成');
|
||||
});
|
||||
|
||||
it('属于 documentation 分类', () => {
|
||||
expect(generateDocsSkill.category).toBe('documentation');
|
||||
});
|
||||
|
||||
it('type 参数有多种文档类型', () => {
|
||||
expect(generateDocsSkill.parameters.type.enum).toContain('JSDoc');
|
||||
expect(generateDocsSkill.parameters.type.enum).toContain('TSDoc');
|
||||
expect(generateDocsSkill.parameters.type.enum).toContain('README');
|
||||
});
|
||||
|
||||
it('type 参数有默认值', () => {
|
||||
expect(generateDocsSkill.parameters.type.default).toBe('文档注释');
|
||||
});
|
||||
});
|
||||
|
||||
describe('generateTestsSkill - 测试生成', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(generateTestsSkill.name).toBe('generate-tests');
|
||||
expect(generateTestsSkill.displayName).toBe('测试生成');
|
||||
});
|
||||
|
||||
it('属于 testing 分类', () => {
|
||||
expect(generateTestsSkill.category).toBe('testing');
|
||||
});
|
||||
|
||||
it('framework 参数有多个框架选项', () => {
|
||||
expect(generateTestsSkill.parameters.framework.enum).toContain('vitest');
|
||||
expect(generateTestsSkill.parameters.framework.enum).toContain('jest');
|
||||
expect(generateTestsSkill.parameters.framework.enum).toContain('mocha');
|
||||
expect(generateTestsSkill.parameters.framework.enum).toContain('pytest');
|
||||
});
|
||||
|
||||
it('framework 默认为 vitest', () => {
|
||||
expect(generateTestsSkill.parameters.framework.default).toBe('vitest');
|
||||
});
|
||||
});
|
||||
|
||||
describe('refactorSuggestSkill - 重构建议', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(refactorSuggestSkill.name).toBe('refactor-suggest');
|
||||
expect(refactorSuggestSkill.displayName).toBe('重构建议');
|
||||
});
|
||||
|
||||
it('属于 development 分类', () => {
|
||||
expect(refactorSuggestSkill.category).toBe('development');
|
||||
});
|
||||
|
||||
it('goal 参数是可选的', () => {
|
||||
expect(refactorSuggestSkill.parameters.goal.required).toBe(false);
|
||||
});
|
||||
|
||||
it('模板包含重构相关内容', () => {
|
||||
expect(refactorSuggestSkill.promptTemplate).toContain('重构');
|
||||
expect(refactorSuggestSkill.promptTemplate).toContain('{{code}}');
|
||||
});
|
||||
});
|
||||
|
||||
describe('fixBugSkill - Bug 修复', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(fixBugSkill.name).toBe('fix-bug');
|
||||
expect(fixBugSkill.displayName).toBe('Bug 修复');
|
||||
});
|
||||
|
||||
it('属于 debugging 分类', () => {
|
||||
expect(fixBugSkill.category).toBe('debugging');
|
||||
});
|
||||
|
||||
it('有 error 和 context 可选参数', () => {
|
||||
expect(fixBugSkill.parameters.error.required).toBe(false);
|
||||
expect(fixBugSkill.parameters.context.required).toBe(false);
|
||||
});
|
||||
|
||||
it('模板支持错误信息和上下文', () => {
|
||||
expect(fixBugSkill.promptTemplate).toContain('{{#if error}}');
|
||||
expect(fixBugSkill.promptTemplate).toContain('{{#if context}}');
|
||||
});
|
||||
});
|
||||
|
||||
describe('gitCommitSkill - Git Commit', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(gitCommitSkill.name).toBe('git-commit');
|
||||
expect(gitCommitSkill.displayName).toBe('Git Commit');
|
||||
});
|
||||
|
||||
it('属于 git 分类', () => {
|
||||
expect(gitCommitSkill.category).toBe('git');
|
||||
});
|
||||
|
||||
it('diff 参数是必需的', () => {
|
||||
expect(gitCommitSkill.parameters.diff.required).toBe(true);
|
||||
});
|
||||
|
||||
it('type 参数有 conventional commit 类型', () => {
|
||||
expect(gitCommitSkill.parameters.type.enum).toContain('feat');
|
||||
expect(gitCommitSkill.parameters.type.enum).toContain('fix');
|
||||
expect(gitCommitSkill.parameters.type.enum).toContain('docs');
|
||||
expect(gitCommitSkill.parameters.type.enum).toContain('refactor');
|
||||
});
|
||||
|
||||
it('模板提到 Conventional Commits', () => {
|
||||
expect(gitCommitSkill.promptTemplate).toContain('Conventional Commits');
|
||||
});
|
||||
});
|
||||
|
||||
describe('apiDesignSkill - API 设计', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(apiDesignSkill.name).toBe('api-design');
|
||||
expect(apiDesignSkill.displayName).toBe('API 设计');
|
||||
});
|
||||
|
||||
it('属于 architecture 分类', () => {
|
||||
expect(apiDesignSkill.category).toBe('architecture');
|
||||
});
|
||||
|
||||
it('requirement 参数是必需的', () => {
|
||||
expect(apiDesignSkill.parameters.requirement.required).toBe(true);
|
||||
});
|
||||
|
||||
it('constraints 参数是可选的', () => {
|
||||
expect(apiDesignSkill.parameters.constraints.required).toBe(false);
|
||||
});
|
||||
|
||||
it('模板提到 RESTful API', () => {
|
||||
expect(apiDesignSkill.promptTemplate).toContain('RESTful API');
|
||||
});
|
||||
});
|
||||
|
||||
describe('参数类型验证', () => {
|
||||
it('所有必需参数都是 string 类型', () => {
|
||||
builtinSkills.forEach((skill) => {
|
||||
Object.entries(skill.parameters).forEach(([, param]) => {
|
||||
if (param.required) {
|
||||
expect(param.type).toBe('string');
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('所有参数都有描述', () => {
|
||||
builtinSkills.forEach((skill) => {
|
||||
Object.entries(skill.parameters).forEach(([, param]) => {
|
||||
expect(param.description).toBeDefined();
|
||||
expect(param.description.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('分类覆盖', () => {
|
||||
it('覆盖多个分类', () => {
|
||||
const categories = new Set(builtinSkills.map((s) => s.category));
|
||||
expect(categories.size).toBeGreaterThan(3);
|
||||
expect(categories).toContain('development');
|
||||
expect(categories).toContain('documentation');
|
||||
expect(categories).toContain('testing');
|
||||
expect(categories).toContain('debugging');
|
||||
expect(categories).toContain('git');
|
||||
expect(categories).toContain('architecture');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,193 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { SkillLoader } from '../../../src/skills/loader.js';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
access: vi.fn(),
|
||||
readdir: vi.fn(),
|
||||
readFile: vi.fn(),
|
||||
}));
|
||||
|
||||
import * as fs from 'fs/promises';
|
||||
|
||||
describe('SkillLoader - Skill 加载器', () => {
|
||||
let loader: SkillLoader;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
loader = new SkillLoader();
|
||||
});
|
||||
|
||||
describe('loadFromFile - 从文件加载', () => {
|
||||
it('加载 YAML 格式的 Skill', async () => {
|
||||
const yamlContent = `
|
||||
skill:
|
||||
name: test-skill
|
||||
description: 测试 Skill
|
||||
promptTemplate: "提示模板 {{param}}"
|
||||
parameters:
|
||||
param:
|
||||
type: string
|
||||
required: true
|
||||
description: 参数描述
|
||||
`;
|
||||
|
||||
vi.mocked(fs.readFile).mockResolvedValue(yamlContent);
|
||||
|
||||
const skill = await loader.loadFromFile('/test/skill.yaml', 'user');
|
||||
|
||||
expect(skill).not.toBeNull();
|
||||
expect(skill?.name).toBe('test-skill');
|
||||
expect(skill?.description).toBe('测试 Skill');
|
||||
expect(skill?.source).toBe('user');
|
||||
expect(skill?.parameters?.param.required).toBe(true);
|
||||
});
|
||||
|
||||
it('加载 JSON 格式的 Skill', async () => {
|
||||
const jsonContent = JSON.stringify({
|
||||
skill: {
|
||||
name: 'json-skill',
|
||||
description: 'JSON Skill',
|
||||
promptTemplate: '提示',
|
||||
},
|
||||
});
|
||||
|
||||
vi.mocked(fs.readFile).mockResolvedValue(jsonContent);
|
||||
|
||||
const skill = await loader.loadFromFile('/test/skill.json', 'project');
|
||||
|
||||
expect(skill).not.toBeNull();
|
||||
expect(skill?.name).toBe('json-skill');
|
||||
expect(skill?.source).toBe('project');
|
||||
});
|
||||
|
||||
it('加载 Markdown 格式的 Skill(带 frontmatter)', async () => {
|
||||
const mdContent = `---
|
||||
name: md-skill
|
||||
description: Markdown Skill
|
||||
parameters:
|
||||
code:
|
||||
type: string
|
||||
required: true
|
||||
description: 代码
|
||||
---
|
||||
|
||||
# 代码审查
|
||||
|
||||
请审查以下代码:
|
||||
|
||||
{{code}}
|
||||
`;
|
||||
|
||||
vi.mocked(fs.readFile).mockResolvedValue(mdContent);
|
||||
|
||||
const skill = await loader.loadFromFile('/test/skill.md', 'user');
|
||||
|
||||
expect(skill).not.toBeNull();
|
||||
expect(skill?.name).toBe('md-skill');
|
||||
expect(skill?.description).toBe('Markdown Skill');
|
||||
expect(skill?.promptTemplate).toContain('{{code}}');
|
||||
expect(skill?.promptTemplate).toContain('代码审查');
|
||||
});
|
||||
|
||||
it('加载 Markdown 格式的 Skill(无 frontmatter)', async () => {
|
||||
const mdContent = `# 简单提示
|
||||
|
||||
这是一个简单的提示模板。
|
||||
|
||||
{{input}}
|
||||
`;
|
||||
|
||||
vi.mocked(fs.readFile).mockResolvedValue(mdContent);
|
||||
|
||||
const skill = await loader.loadFromFile('/test/simple.md', 'user');
|
||||
|
||||
expect(skill).not.toBeNull();
|
||||
expect(skill?.name).toBe('simple'); // 从文件名获取
|
||||
expect(skill?.promptTemplate).toContain('简单提示');
|
||||
});
|
||||
|
||||
it('缺少必需字段时返回 null', async () => {
|
||||
const yamlContent = `
|
||||
skill:
|
||||
description: 没有 name 和 promptTemplate
|
||||
`;
|
||||
|
||||
vi.mocked(fs.readFile).mockResolvedValue(yamlContent);
|
||||
|
||||
const skill = await loader.loadFromFile('/test/invalid.yaml', 'user');
|
||||
|
||||
expect(skill).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadFromDirectory - 从目录加载', () => {
|
||||
it('目录不存在时返回空数组', async () => {
|
||||
vi.mocked(fs.access).mockRejectedValue(new Error('Not found'));
|
||||
|
||||
const skills = await loader.loadFromDirectory('/non-existent', 'user');
|
||||
|
||||
expect(skills).toEqual([]);
|
||||
});
|
||||
|
||||
it('加载目录中的所有 Skill 文件', async () => {
|
||||
vi.mocked(fs.access).mockResolvedValue(undefined);
|
||||
vi.mocked(fs.readdir).mockResolvedValue([
|
||||
{ name: 'skill1.yaml', isFile: () => true, isDirectory: () => false },
|
||||
{ name: 'skill2.json', isFile: () => true, isDirectory: () => false },
|
||||
{ name: 'readme.txt', isFile: () => true, isDirectory: () => false }, // 不支持的格式
|
||||
] as any);
|
||||
|
||||
vi.mocked(fs.readFile)
|
||||
.mockResolvedValueOnce(`skill:\n name: skill1\n description: 1\n promptTemplate: t1`)
|
||||
.mockResolvedValueOnce(JSON.stringify({ skill: { name: 'skill2', description: '2', promptTemplate: 't2' } }));
|
||||
|
||||
const skills = await loader.loadFromDirectory('/test', 'user');
|
||||
|
||||
expect(skills.length).toBe(2);
|
||||
expect(skills.map((s) => s.name)).toContain('skill1');
|
||||
expect(skills.map((s) => s.name)).toContain('skill2');
|
||||
});
|
||||
|
||||
it('递归加载子目录', async () => {
|
||||
vi.mocked(fs.access).mockResolvedValue(undefined);
|
||||
vi.mocked(fs.readdir)
|
||||
.mockResolvedValueOnce([
|
||||
{ name: 'subdir', isFile: () => false, isDirectory: () => true },
|
||||
{ name: 'root.yaml', isFile: () => true, isDirectory: () => false },
|
||||
] as any)
|
||||
.mockResolvedValueOnce([
|
||||
{ name: 'sub.yaml', isFile: () => true, isDirectory: () => false },
|
||||
] as any);
|
||||
|
||||
vi.mocked(fs.readFile)
|
||||
.mockResolvedValueOnce(`skill:\n name: root\n description: root\n promptTemplate: root`)
|
||||
.mockResolvedValueOnce(`skill:\n name: sub\n description: sub\n promptTemplate: sub`);
|
||||
|
||||
const skills = await loader.loadFromDirectory('/test', 'user');
|
||||
|
||||
expect(skills.length).toBe(2);
|
||||
expect(skills.map((s) => s.name)).toContain('root');
|
||||
expect(skills.map((s) => s.name)).toContain('sub');
|
||||
});
|
||||
});
|
||||
|
||||
describe('目录路径', () => {
|
||||
it('getUserSkillsDir 返回用户 Skills 目录', () => {
|
||||
const originalHome = process.env.HOME;
|
||||
process.env.HOME = '/home/testuser';
|
||||
|
||||
const dir = loader.getUserSkillsDir();
|
||||
|
||||
expect(dir).toBe('/home/testuser/.config/ai-terminal/skills');
|
||||
|
||||
process.env.HOME = originalHome;
|
||||
});
|
||||
|
||||
it('getProjectSkillsDir 返回项目 Skills 目录', () => {
|
||||
const dir = loader.getProjectSkillsDir('/workspace');
|
||||
|
||||
expect(dir).toBe('/workspace/.ai-terminal/skills');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,407 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import {
|
||||
SkillRegistry,
|
||||
getSkillRegistry,
|
||||
resetSkillRegistry,
|
||||
} from '../../../src/skills/registry.js';
|
||||
import type { Skill } from '../../../src/skills/types.js';
|
||||
|
||||
// Mock loader to prevent file system access
|
||||
vi.mock('../../../src/skills/loader.js', () => ({
|
||||
skillLoader: {
|
||||
loadFromDirectory: vi.fn().mockResolvedValue([]),
|
||||
getUserSkillsDir: vi.fn().mockReturnValue('/mock/user/skills'),
|
||||
getProjectSkillsDir: vi.fn().mockReturnValue('/mock/project/skills'),
|
||||
},
|
||||
}));
|
||||
|
||||
// Mock builtin skills
|
||||
vi.mock('../../../src/skills/builtin/index.js', () => ({
|
||||
builtinSkills: [
|
||||
{
|
||||
name: 'test-builtin',
|
||||
description: '内置测试 Skill',
|
||||
promptTemplate: '测试提示: {{param1}}',
|
||||
parameters: {
|
||||
param1: { type: 'string', required: true, description: '参数1' },
|
||||
},
|
||||
source: 'builtin',
|
||||
enabled: true,
|
||||
},
|
||||
],
|
||||
}));
|
||||
|
||||
describe('SkillRegistry - Skill 注册表', () => {
|
||||
let registry: SkillRegistry;
|
||||
|
||||
beforeEach(() => {
|
||||
resetSkillRegistry();
|
||||
registry = new SkillRegistry({ autoLoad: false });
|
||||
});
|
||||
|
||||
describe('register - 注册', () => {
|
||||
it('成功注册 Skill', () => {
|
||||
const skill: Skill = {
|
||||
name: 'test-skill',
|
||||
description: '测试 Skill',
|
||||
promptTemplate: '测试提示',
|
||||
source: 'user',
|
||||
};
|
||||
|
||||
registry.register(skill);
|
||||
expect(registry.has('test-skill')).toBe(true);
|
||||
});
|
||||
|
||||
it('高优先级 Skill 可以覆盖低优先级', () => {
|
||||
const builtinSkill: Skill = {
|
||||
name: 'same-name',
|
||||
description: '内置版本',
|
||||
promptTemplate: '内置提示',
|
||||
source: 'builtin',
|
||||
};
|
||||
|
||||
const projectSkill: Skill = {
|
||||
name: 'same-name',
|
||||
description: '项目版本',
|
||||
promptTemplate: '项目提示',
|
||||
source: 'project',
|
||||
};
|
||||
|
||||
registry.register(builtinSkill);
|
||||
registry.register(projectSkill);
|
||||
|
||||
const result = registry.get('same-name');
|
||||
expect(result?.source).toBe('project');
|
||||
});
|
||||
|
||||
it('低优先级 Skill 不能覆盖高优先级', () => {
|
||||
const projectSkill: Skill = {
|
||||
name: 'same-name',
|
||||
description: '项目版本',
|
||||
promptTemplate: '项目提示',
|
||||
source: 'project',
|
||||
};
|
||||
|
||||
const builtinSkill: Skill = {
|
||||
name: 'same-name',
|
||||
description: '内置版本',
|
||||
promptTemplate: '内置提示',
|
||||
source: 'builtin',
|
||||
};
|
||||
|
||||
registry.register(projectSkill);
|
||||
registry.register(builtinSkill);
|
||||
|
||||
const result = registry.get('same-name');
|
||||
expect(result?.source).toBe('project');
|
||||
});
|
||||
});
|
||||
|
||||
describe('get - 获取', () => {
|
||||
it('获取存在的 Skill', () => {
|
||||
const skill: Skill = {
|
||||
name: 'test-skill',
|
||||
description: '测试',
|
||||
promptTemplate: '提示',
|
||||
source: 'user',
|
||||
};
|
||||
|
||||
registry.register(skill);
|
||||
const result = registry.get('test-skill');
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.name).toBe('test-skill');
|
||||
});
|
||||
|
||||
it('获取不存在的 Skill 返回 undefined', () => {
|
||||
const result = registry.get('non-existent');
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('search - 搜索', () => {
|
||||
beforeEach(() => {
|
||||
const skills: Skill[] = [
|
||||
{
|
||||
name: 'code-review',
|
||||
description: '代码审查',
|
||||
promptTemplate: '审查代码',
|
||||
keywords: ['review', 'quality'],
|
||||
category: 'development',
|
||||
source: 'builtin',
|
||||
enabled: true,
|
||||
},
|
||||
{
|
||||
name: 'generate-tests',
|
||||
description: '生成测试',
|
||||
promptTemplate: '生成测试代码',
|
||||
keywords: ['test', 'unit'],
|
||||
category: 'testing',
|
||||
source: 'builtin',
|
||||
enabled: true,
|
||||
},
|
||||
{
|
||||
name: 'explain-code',
|
||||
description: '解释代码功能',
|
||||
promptTemplate: '解释代码',
|
||||
category: 'development',
|
||||
source: 'builtin',
|
||||
enabled: true,
|
||||
},
|
||||
];
|
||||
|
||||
for (const skill of skills) {
|
||||
registry.register(skill);
|
||||
}
|
||||
});
|
||||
|
||||
it('按名称精确匹配', () => {
|
||||
const results = registry.search('code-review');
|
||||
expect(results.length).toBeGreaterThan(0);
|
||||
expect(results[0].skill.name).toBe('code-review');
|
||||
expect(results[0].score).toBe(100);
|
||||
});
|
||||
|
||||
it('按名称前缀匹配', () => {
|
||||
const results = registry.search('code');
|
||||
expect(results.length).toBe(2); // code-review, explain-code
|
||||
});
|
||||
|
||||
it('按描述匹配', () => {
|
||||
const results = registry.search('审查');
|
||||
expect(results.length).toBeGreaterThan(0);
|
||||
expect(results[0].skill.name).toBe('code-review');
|
||||
});
|
||||
|
||||
it('按关键词匹配', () => {
|
||||
const results = registry.search('quality');
|
||||
expect(results.length).toBeGreaterThan(0);
|
||||
expect(results[0].skill.name).toBe('code-review');
|
||||
});
|
||||
|
||||
it('按分类匹配', () => {
|
||||
const results = registry.search('testing');
|
||||
expect(results.length).toBeGreaterThan(0);
|
||||
expect(results[0].skill.name).toBe('generate-tests');
|
||||
});
|
||||
|
||||
it('限制结果数量', () => {
|
||||
const results = registry.search('code', 1);
|
||||
expect(results.length).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('renderPrompt - 渲染模板', () => {
|
||||
it('成功渲染模板', () => {
|
||||
const skill: Skill = {
|
||||
name: 'test',
|
||||
description: '测试',
|
||||
promptTemplate: '你好 {{name}},请{{action}}',
|
||||
source: 'user',
|
||||
};
|
||||
|
||||
const result = registry.renderPrompt(skill, {
|
||||
name: '世界',
|
||||
action: '测试',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.prompt).toBe('你好 世界,请测试');
|
||||
});
|
||||
|
||||
it('缺少必需参数时返回错误', () => {
|
||||
const skill: Skill = {
|
||||
name: 'test',
|
||||
description: '测试',
|
||||
promptTemplate: '{{param}}',
|
||||
parameters: {
|
||||
param: { type: 'string', required: true, description: '必需参数' },
|
||||
},
|
||||
source: 'user',
|
||||
};
|
||||
|
||||
const result = registry.renderPrompt(skill, {});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('缺少必需参数');
|
||||
});
|
||||
|
||||
it('使用默认值', () => {
|
||||
const skill: Skill = {
|
||||
name: 'test',
|
||||
description: '测试',
|
||||
promptTemplate: '值: {{param}}',
|
||||
parameters: {
|
||||
param: {
|
||||
type: 'string',
|
||||
required: false,
|
||||
default: '默认值',
|
||||
description: '可选参数',
|
||||
},
|
||||
},
|
||||
source: 'user',
|
||||
};
|
||||
|
||||
const result = registry.renderPrompt(skill, {});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.prompt).toBe('值: 默认值');
|
||||
});
|
||||
|
||||
it('保留未匹配的变量', () => {
|
||||
const skill: Skill = {
|
||||
name: 'test',
|
||||
description: '测试',
|
||||
promptTemplate: '{{known}} 和 {{unknown}}',
|
||||
source: 'user',
|
||||
};
|
||||
|
||||
const result = registry.renderPrompt(skill, { known: '已知' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.prompt).toBe('已知 和 {{unknown}}');
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功执行 Skill', () => {
|
||||
const skill: Skill = {
|
||||
name: 'test',
|
||||
description: '测试',
|
||||
promptTemplate: '提示: {{param}}',
|
||||
source: 'user',
|
||||
enabled: true,
|
||||
};
|
||||
|
||||
registry.register(skill);
|
||||
const result = registry.execute('test', { param: '值' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.prompt).toBe('提示: 值');
|
||||
});
|
||||
|
||||
it('Skill 不存在时返回错误', () => {
|
||||
const result = registry.execute('non-existent', {});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('不存在');
|
||||
});
|
||||
|
||||
it('Skill 禁用时返回错误', () => {
|
||||
const skill: Skill = {
|
||||
name: 'disabled',
|
||||
description: '禁用的 Skill',
|
||||
promptTemplate: '提示',
|
||||
source: 'user',
|
||||
enabled: false,
|
||||
};
|
||||
|
||||
registry.register(skill);
|
||||
const result = registry.execute('disabled', {});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('禁用');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getByCategory - 按分类获取', () => {
|
||||
it('返回指定分类的 Skills', () => {
|
||||
const skills: Skill[] = [
|
||||
{
|
||||
name: 'skill1',
|
||||
description: '1',
|
||||
promptTemplate: '1',
|
||||
category: 'dev',
|
||||
source: 'builtin',
|
||||
enabled: true,
|
||||
},
|
||||
{
|
||||
name: 'skill2',
|
||||
description: '2',
|
||||
promptTemplate: '2',
|
||||
category: 'dev',
|
||||
source: 'builtin',
|
||||
enabled: true,
|
||||
},
|
||||
{
|
||||
name: 'skill3',
|
||||
description: '3',
|
||||
promptTemplate: '3',
|
||||
category: 'test',
|
||||
source: 'builtin',
|
||||
enabled: true,
|
||||
},
|
||||
];
|
||||
|
||||
for (const skill of skills) {
|
||||
registry.register(skill);
|
||||
}
|
||||
|
||||
const devSkills = registry.getByCategory('dev');
|
||||
expect(devSkills.length).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getStats - 统计信息', () => {
|
||||
it('返回正确的统计信息', () => {
|
||||
const skills: Skill[] = [
|
||||
{
|
||||
name: 'builtin1',
|
||||
description: '1',
|
||||
promptTemplate: '1',
|
||||
category: 'dev',
|
||||
source: 'builtin',
|
||||
enabled: true,
|
||||
},
|
||||
{
|
||||
name: 'user1',
|
||||
description: '2',
|
||||
promptTemplate: '2',
|
||||
category: 'dev',
|
||||
source: 'user',
|
||||
enabled: true,
|
||||
},
|
||||
{
|
||||
name: 'disabled1',
|
||||
description: '3',
|
||||
promptTemplate: '3',
|
||||
source: 'builtin',
|
||||
enabled: false,
|
||||
},
|
||||
];
|
||||
|
||||
for (const skill of skills) {
|
||||
registry.register(skill);
|
||||
}
|
||||
|
||||
const stats = registry.getStats();
|
||||
|
||||
expect(stats.total).toBe(3);
|
||||
expect(stats.enabled).toBe(2);
|
||||
expect(stats.bySource.builtin).toBe(2);
|
||||
expect(stats.bySource.user).toBe(1);
|
||||
expect(stats.byCategory.dev).toBe(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getSkillRegistry - 全局注册表', () => {
|
||||
beforeEach(() => {
|
||||
resetSkillRegistry();
|
||||
});
|
||||
|
||||
it('返回单例实例', () => {
|
||||
const registry1 = getSkillRegistry();
|
||||
const registry2 = getSkillRegistry();
|
||||
|
||||
expect(registry1).toBe(registry2);
|
||||
});
|
||||
|
||||
it('resetSkillRegistry 重置实例', () => {
|
||||
const registry1 = getSkillRegistry();
|
||||
resetSkillRegistry();
|
||||
const registry2 = getSkillRegistry();
|
||||
|
||||
expect(registry1).not.toBe(registry2);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,192 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
stat: vi.fn(),
|
||||
copyFile: vi.fn().mockResolvedValue(undefined),
|
||||
mkdir: vi.fn().mockResolvedValue(undefined),
|
||||
readdir: vi.fn().mockResolvedValue([]),
|
||||
}));
|
||||
|
||||
// 可变状态
|
||||
let mockCheckResults: Array<{
|
||||
allowed: boolean;
|
||||
action?: string;
|
||||
reason?: string;
|
||||
needsConfirmation?: boolean;
|
||||
}> = [];
|
||||
let checkCallIndex = 0;
|
||||
|
||||
// Mock permission manager
|
||||
vi.mock('../../../../src/permission/index.js', () => ({
|
||||
getPermissionManager: () => ({
|
||||
checkFilePermission: vi.fn(async () => {
|
||||
const result = mockCheckResults[checkCallIndex] || { allowed: true };
|
||||
checkCallIndex++;
|
||||
return result;
|
||||
}),
|
||||
}),
|
||||
}));
|
||||
|
||||
// Mock loadDescription
|
||||
vi.mock('../../../../src/tools/load_description.js', () => ({
|
||||
loadDescription: vi.fn(() => '复制文件'),
|
||||
}));
|
||||
|
||||
import { copyFileTool } from '../../../../src/tools/filesystem/copy_file.js';
|
||||
import * as fs from 'fs/promises';
|
||||
|
||||
describe('copyFileTool - 扩展测试', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockCheckResults = [{ allowed: true }, { allowed: true }];
|
||||
checkCallIndex = 0;
|
||||
// 重置 mock 默认值
|
||||
vi.mocked(fs.stat).mockReset();
|
||||
vi.mocked(fs.copyFile).mockReset().mockResolvedValue(undefined);
|
||||
vi.mocked(fs.mkdir).mockReset().mockResolvedValue(undefined);
|
||||
vi.mocked(fs.readdir).mockReset().mockResolvedValue([]);
|
||||
});
|
||||
|
||||
describe('递归复制目录', () => {
|
||||
it('递归复制包含文件的目录', async () => {
|
||||
vi.mocked(fs.stat)
|
||||
.mockResolvedValueOnce({ isDirectory: () => true } as any)
|
||||
.mockRejectedValueOnce(new Error('ENOENT'))
|
||||
.mockResolvedValueOnce({ isDirectory: () => false } as any);
|
||||
|
||||
vi.mocked(fs.readdir).mockResolvedValueOnce(['file1.txt'] as any);
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: 'src_dir',
|
||||
destination: 'dest_dir',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(fs.mkdir).toHaveBeenCalled();
|
||||
expect(fs.copyFile).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('递归复制包含子目录的目录', async () => {
|
||||
// 调用顺序:
|
||||
// 1. execute: stat(source) - 检查源是否存在
|
||||
// 2. execute: stat(dest) - 检查目标(ENOENT)
|
||||
// 3. copyRecursive: stat(source) - 判断是否是目录
|
||||
// 4. copyRecursive: stat(source/subdir) - 递归判断子目录
|
||||
vi.mocked(fs.stat)
|
||||
.mockResolvedValueOnce({ isDirectory: () => true } as any) // source 存在且是目录
|
||||
.mockRejectedValueOnce(new Error('ENOENT')) // dest 不存在
|
||||
.mockResolvedValueOnce({ isDirectory: () => true } as any) // copyRecursive(source)
|
||||
.mockResolvedValueOnce({ isDirectory: () => true } as any); // copyRecursive(source/subdir)
|
||||
|
||||
vi.mocked(fs.readdir)
|
||||
.mockResolvedValueOnce(['subdir'] as any) // 第一层目录
|
||||
.mockResolvedValueOnce([] as any); // 子目录为空
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: 'src_dir',
|
||||
destination: 'dest_dir',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(fs.mkdir).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('目标位置权限', () => {
|
||||
it('目标位置需要确认时返回错误', async () => {
|
||||
mockCheckResults = [
|
||||
{ allowed: true },
|
||||
{ allowed: false, action: 'ask', needsConfirmation: true, reason: '首次复制到此位置' },
|
||||
];
|
||||
|
||||
vi.mocked(fs.stat).mockResolvedValueOnce({ isDirectory: () => false } as any);
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: 'src.txt',
|
||||
destination: '/new/location/dest.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
expect(result.error).toContain('首次复制到此位置');
|
||||
});
|
||||
});
|
||||
|
||||
describe('绝对路径处理', () => {
|
||||
it('源和目标都是绝对路径', async () => {
|
||||
vi.mocked(fs.stat)
|
||||
.mockResolvedValueOnce({ isDirectory: () => false } as any)
|
||||
.mockRejectedValueOnce(new Error('ENOENT'));
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: '/absolute/source/file.txt',
|
||||
destination: '/absolute/destination/file.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('/absolute/source/file.txt');
|
||||
expect(result.output).toContain('/absolute/destination/file.txt');
|
||||
});
|
||||
});
|
||||
|
||||
describe('权限检查细节', () => {
|
||||
it('源文件权限被拒绝(无原因)', async () => {
|
||||
mockCheckResults = [
|
||||
{ allowed: false, action: 'deny' },
|
||||
];
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: 'src.txt',
|
||||
destination: 'dest.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('不允许读取此文件');
|
||||
});
|
||||
|
||||
it('源文件需要确认(无原因)', async () => {
|
||||
mockCheckResults = [
|
||||
{ allowed: false, action: 'ask', needsConfirmation: true },
|
||||
];
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: 'src.txt',
|
||||
destination: 'dest.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要权限确认');
|
||||
});
|
||||
|
||||
it('目标权限被拒绝(无原因)', async () => {
|
||||
mockCheckResults = [
|
||||
{ allowed: true },
|
||||
{ allowed: false, action: 'deny' },
|
||||
];
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: 'src.txt',
|
||||
destination: 'dest.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('不允许复制到此位置');
|
||||
});
|
||||
|
||||
it('目标需要确认(无原因)', async () => {
|
||||
mockCheckResults = [
|
||||
{ allowed: true },
|
||||
{ allowed: false, action: 'ask', needsConfirmation: true },
|
||||
];
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: 'src.txt',
|
||||
destination: 'dest.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要权限确认');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,173 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
stat: vi.fn(),
|
||||
copyFile: vi.fn().mockResolvedValue(undefined),
|
||||
mkdir: vi.fn().mockResolvedValue(undefined),
|
||||
readdir: vi.fn().mockResolvedValue([]),
|
||||
}));
|
||||
|
||||
// Mock permission manager
|
||||
vi.mock('../../../../src/permission/index.js', () => ({
|
||||
getPermissionManager: vi.fn(() => ({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
}),
|
||||
})),
|
||||
}));
|
||||
|
||||
// Mock loadDescription
|
||||
vi.mock('../../../../src/tools/load_description.js', () => ({
|
||||
loadDescription: vi.fn(() => '复制文件'),
|
||||
}));
|
||||
|
||||
import { copyFileTool } from '../../../../src/tools/filesystem/copy_file.js';
|
||||
import * as fs from 'fs/promises';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('copyFileTool - 文件复制工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(fs.stat).mockResolvedValue({
|
||||
isDirectory: () => false,
|
||||
isFile: () => true,
|
||||
} as any);
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(copyFileTool.name).toBe('copy_file');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(copyFileTool.metadata.category).toBe('filesystem');
|
||||
expect(copyFileTool.metadata.keywords).toContain('copy');
|
||||
expect(copyFileTool.metadata.keywords).toContain('cp');
|
||||
});
|
||||
|
||||
it('定义了必需参数', () => {
|
||||
expect(copyFileTool.parameters.source.required).toBe(true);
|
||||
expect(copyFileTool.parameters.destination.required).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功复制文件', async () => {
|
||||
// 第一次调用检查源文件,第二次调用检查目标是否是目录
|
||||
vi.mocked(fs.stat)
|
||||
.mockResolvedValueOnce({ isDirectory: () => false } as any)
|
||||
.mockRejectedValueOnce(new Error('ENOENT')); // 目标不存在
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: 'src.txt',
|
||||
destination: 'dest.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('已复制');
|
||||
expect(fs.copyFile).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('复制到已存在的目录', async () => {
|
||||
vi.mocked(fs.stat)
|
||||
.mockResolvedValueOnce({ isDirectory: () => false } as any) // 源文件
|
||||
.mockResolvedValueOnce({ isDirectory: () => true } as any); // 目标是目录
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: 'file.txt',
|
||||
destination: '/target/dir',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('file.txt');
|
||||
});
|
||||
|
||||
it('递归复制目录', async () => {
|
||||
vi.mocked(fs.stat)
|
||||
.mockResolvedValueOnce({ isDirectory: () => true } as any) // 源是目录
|
||||
.mockRejectedValueOnce(new Error('ENOENT')); // 目标不存在
|
||||
|
||||
vi.mocked(fs.readdir).mockResolvedValueOnce([]);
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: 'src_dir',
|
||||
destination: 'dest_dir',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(fs.mkdir).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('源文件读取权限被拒绝', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '不允许读取',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: '/etc/passwd',
|
||||
destination: 'copy.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('权限被拒绝');
|
||||
});
|
||||
|
||||
it('目标位置写入权限被拒绝', async () => {
|
||||
const mockCheck = vi.fn()
|
||||
.mockResolvedValueOnce({ allowed: true }) // 读取权限
|
||||
.mockResolvedValueOnce({ allowed: false, action: 'deny', reason: '不允许写入' }); // 复制权限
|
||||
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: mockCheck,
|
||||
} as any);
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: 'src.txt',
|
||||
destination: '/protected/dest.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('权限被拒绝');
|
||||
});
|
||||
|
||||
it('源文件需要确认', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'ask',
|
||||
needsConfirmation: true,
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: '/sensitive/file',
|
||||
destination: 'dest.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
});
|
||||
|
||||
it('源文件不存在返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({ allowed: true }),
|
||||
} as any);
|
||||
|
||||
vi.mocked(fs.stat).mockRejectedValue(new Error('ENOENT: no such file'));
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: 'nonexistent.txt',
|
||||
destination: 'dest.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('ENOENT');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,156 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
stat: vi.fn(),
|
||||
mkdir: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
// Mock permission manager
|
||||
vi.mock('../../../../src/permission/index.js', () => ({
|
||||
getPermissionManager: vi.fn(() => ({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
}),
|
||||
})),
|
||||
}));
|
||||
|
||||
// Mock loadDescription
|
||||
vi.mock('../../../../src/tools/load_description.js', () => ({
|
||||
loadDescription: vi.fn(() => '创建目录'),
|
||||
}));
|
||||
|
||||
import { createDirectoryTool } from '../../../../src/tools/filesystem/create_directory.js';
|
||||
import * as fs from 'fs/promises';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('createDirectoryTool - 创建目录工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
// 默认目录不存在
|
||||
vi.mocked(fs.stat).mockRejectedValue(new Error('ENOENT'));
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(createDirectoryTool.name).toBe('create_directory');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(createDirectoryTool.metadata.category).toBe('filesystem');
|
||||
expect(createDirectoryTool.metadata.keywords).toContain('create');
|
||||
expect(createDirectoryTool.metadata.keywords).toContain('directory');
|
||||
expect(createDirectoryTool.metadata.keywords).toContain('mkdir');
|
||||
});
|
||||
|
||||
it('定义了必需的 path 参数', () => {
|
||||
expect(createDirectoryTool.parameters.path.required).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功创建目录', async () => {
|
||||
const result = await createDirectoryTool.execute({ path: 'new_dir' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('已创建目录');
|
||||
expect(fs.mkdir).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
{ recursive: true }
|
||||
);
|
||||
});
|
||||
|
||||
it('目录已存在返回成功', async () => {
|
||||
vi.mocked(fs.stat).mockResolvedValue({
|
||||
isDirectory: () => true,
|
||||
} as any);
|
||||
|
||||
const result = await createDirectoryTool.execute({ path: 'existing_dir' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('目录已存在');
|
||||
expect(fs.mkdir).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('路径是文件返回错误', async () => {
|
||||
vi.mocked(fs.stat).mockResolvedValue({
|
||||
isDirectory: () => false,
|
||||
} as any);
|
||||
|
||||
const result = await createDirectoryTool.execute({ path: 'file.txt' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('路径已存在且不是目录');
|
||||
});
|
||||
|
||||
it('权限被拒绝时返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '不允许创建目录',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await createDirectoryTool.execute({ path: '/protected/dir' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('权限被拒绝');
|
||||
});
|
||||
|
||||
it('需要确认时返回提示', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'ask',
|
||||
needsConfirmation: true,
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await createDirectoryTool.execute({ path: 'new_dir' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
});
|
||||
|
||||
it('创建嵌套目录', async () => {
|
||||
// 确保权限检查通过
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({ allowed: true }),
|
||||
} as any);
|
||||
|
||||
const result = await createDirectoryTool.execute({ path: 'a/b/c/d' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(fs.mkdir).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
{ recursive: true }
|
||||
);
|
||||
});
|
||||
|
||||
it('传递正确参数给权限检查', async () => {
|
||||
const mockCheck = vi.fn().mockResolvedValue({ allowed: true });
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: mockCheck,
|
||||
} as any);
|
||||
|
||||
await createDirectoryTool.execute({ path: 'test_dir' });
|
||||
|
||||
expect(mockCheck).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
operation: 'mkdir',
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('处理创建错误', async () => {
|
||||
vi.mocked(fs.mkdir).mockRejectedValue(new Error('Permission denied'));
|
||||
|
||||
const result = await createDirectoryTool.execute({ path: 'new_dir' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('Permission denied');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,173 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
stat: vi.fn(),
|
||||
unlink: vi.fn().mockResolvedValue(undefined),
|
||||
rmdir: vi.fn().mockResolvedValue(undefined),
|
||||
rm: vi.fn().mockResolvedValue(undefined),
|
||||
readdir: vi.fn().mockResolvedValue([]),
|
||||
}));
|
||||
|
||||
// Mock permission manager
|
||||
vi.mock('../../../../src/permission/index.js', () => ({
|
||||
getPermissionManager: vi.fn(() => ({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
}),
|
||||
})),
|
||||
}));
|
||||
|
||||
// Mock loadDescription
|
||||
vi.mock('../../../../src/tools/load_description.js', () => ({
|
||||
loadDescription: vi.fn(() => '删除文件'),
|
||||
}));
|
||||
|
||||
import { deleteFileTool } from '../../../../src/tools/filesystem/delete_file.js';
|
||||
import * as fs from 'fs/promises';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('deleteFileTool - 文件删除工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(deleteFileTool.name).toBe('delete_file');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(deleteFileTool.metadata.category).toBe('filesystem');
|
||||
expect(deleteFileTool.metadata.keywords).toContain('delete');
|
||||
expect(deleteFileTool.metadata.keywords).toContain('remove');
|
||||
expect(deleteFileTool.metadata.keywords).toContain('rm');
|
||||
});
|
||||
|
||||
it('定义了必需的 path 参数', () => {
|
||||
expect(deleteFileTool.parameters.path.required).toBe(true);
|
||||
});
|
||||
|
||||
it('定义了可选的 recursive 参数', () => {
|
||||
expect(deleteFileTool.parameters.recursive.required).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功删除文件', async () => {
|
||||
vi.mocked(fs.stat).mockResolvedValue({
|
||||
isDirectory: () => false,
|
||||
} as any);
|
||||
|
||||
const result = await deleteFileTool.execute({ path: 'file.txt' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('已删除文件');
|
||||
expect(fs.unlink).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('删除空目录', async () => {
|
||||
vi.mocked(fs.stat).mockResolvedValue({
|
||||
isDirectory: () => true,
|
||||
} as any);
|
||||
vi.mocked(fs.readdir).mockResolvedValue([]);
|
||||
|
||||
const result = await deleteFileTool.execute({ path: 'empty_dir' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('已删除目录');
|
||||
expect(fs.rmdir).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('非空目录无 recursive 返回错误', async () => {
|
||||
vi.mocked(fs.stat).mockResolvedValue({
|
||||
isDirectory: () => true,
|
||||
} as any);
|
||||
vi.mocked(fs.readdir).mockResolvedValue(['file1.txt', 'file2.txt'] as any);
|
||||
|
||||
const result = await deleteFileTool.execute({ path: 'nonempty_dir' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('目录不为空');
|
||||
expect(result.error).toContain('recursive: true');
|
||||
});
|
||||
|
||||
it('递归删除非空目录', async () => {
|
||||
vi.mocked(fs.stat).mockResolvedValue({
|
||||
isDirectory: () => true,
|
||||
} as any);
|
||||
|
||||
const result = await deleteFileTool.execute({
|
||||
path: 'nonempty_dir',
|
||||
recursive: true,
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('已删除目录');
|
||||
expect(fs.rm).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
{ recursive: true }
|
||||
);
|
||||
});
|
||||
|
||||
it('权限被拒绝时返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '不允许删除',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await deleteFileTool.execute({ path: '/protected/file' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('权限被拒绝');
|
||||
});
|
||||
|
||||
it('需要确认时返回提示', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'ask',
|
||||
needsConfirmation: true,
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await deleteFileTool.execute({ path: 'important.txt' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
});
|
||||
|
||||
it('文件不存在返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({ allowed: true }),
|
||||
} as any);
|
||||
|
||||
vi.mocked(fs.stat).mockRejectedValue(new Error('ENOENT'));
|
||||
|
||||
const result = await deleteFileTool.execute({ path: 'nonexistent.txt' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('ENOENT');
|
||||
});
|
||||
|
||||
it('传递正确参数给权限检查', async () => {
|
||||
const mockCheck = vi.fn().mockResolvedValue({ allowed: true });
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: mockCheck,
|
||||
} as any);
|
||||
vi.mocked(fs.stat).mockResolvedValue({ isDirectory: () => false } as any);
|
||||
|
||||
await deleteFileTool.execute({ path: 'test.txt' });
|
||||
|
||||
expect(mockCheck).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
operation: 'delete',
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,206 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// Mock fs/promises - still needed by editors module
|
||||
vi.mock('fs/promises', () => ({
|
||||
readFile: vi.fn(),
|
||||
writeFile: vi.fn().mockResolvedValue(undefined),
|
||||
access: vi.fn().mockResolvedValue(undefined),
|
||||
mkdir: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
// Mock permission manager
|
||||
vi.mock('../../../../src/permission/index.js', () => ({
|
||||
getPermissionManager: vi.fn(() => ({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
}),
|
||||
})),
|
||||
}));
|
||||
|
||||
// Mock loadDescription
|
||||
vi.mock('../../../../src/tools/load_description.js', () => ({
|
||||
loadDescription: vi.fn(() => '编辑文件'),
|
||||
}));
|
||||
|
||||
// Mock LSP
|
||||
vi.mock('../../../../src/lsp/index.js', () => ({
|
||||
touchFile: vi.fn().mockResolvedValue(false),
|
||||
getFormattedFileDiagnostics: vi.fn().mockResolvedValue(null),
|
||||
isLanguageSupported: vi.fn().mockReturnValue(false),
|
||||
}));
|
||||
|
||||
import { editFileTool } from '../../../../src/tools/filesystem/edit_file.js';
|
||||
import * as fs from 'fs/promises';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
import { isLanguageSupported, touchFile, getFormattedFileDiagnostics } from '../../../../src/lsp/index.js';
|
||||
|
||||
describe('editFileTool - 文件编辑工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(fs.readFile).mockResolvedValue('original content here');
|
||||
vi.mocked(fs.access).mockResolvedValue(undefined);
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(editFileTool.name).toBe('edit_file');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(editFileTool.metadata.category).toBe('filesystem');
|
||||
expect(editFileTool.metadata.keywords).toContain('edit');
|
||||
expect(editFileTool.metadata.keywords).toContain('replace');
|
||||
});
|
||||
|
||||
it('定义了必需参数', () => {
|
||||
expect(editFileTool.parameters.path.required).toBe(true);
|
||||
expect(editFileTool.parameters.old_string.required).toBe(true);
|
||||
expect(editFileTool.parameters.new_string.required).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功编辑文件', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('hello world');
|
||||
|
||||
const result = await editFileTool.execute({
|
||||
path: 'test.txt',
|
||||
old_string: 'world',
|
||||
new_string: 'universe',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('文件已编辑');
|
||||
expect(fs.writeFile).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
'hello universe',
|
||||
'utf-8'
|
||||
);
|
||||
});
|
||||
|
||||
it('old_string 不存在返回错误', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('hello world');
|
||||
|
||||
const result = await editFileTool.execute({
|
||||
path: 'test.txt',
|
||||
old_string: 'notfound',
|
||||
new_string: 'replacement',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('未找到要替换的字符串');
|
||||
});
|
||||
|
||||
it('old_string 多次出现返回错误', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('hello hello hello');
|
||||
|
||||
const result = await editFileTool.execute({
|
||||
path: 'test.txt',
|
||||
old_string: 'hello',
|
||||
new_string: 'hi',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('3 处匹配');
|
||||
expect(result.error).toContain('必须唯一');
|
||||
});
|
||||
|
||||
it('权限被拒绝时返回错误', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('content');
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '不允许编辑',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await editFileTool.execute({
|
||||
path: 'test.txt',
|
||||
old_string: 'content',
|
||||
new_string: 'new',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('权限被拒绝');
|
||||
});
|
||||
|
||||
it('需要确认时返回提示', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('content');
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'ask',
|
||||
needsConfirmation: true,
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await editFileTool.execute({
|
||||
path: 'test.txt',
|
||||
old_string: 'content',
|
||||
new_string: 'new',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
});
|
||||
|
||||
it('文件不存在返回错误', async () => {
|
||||
vi.mocked(fs.readFile).mockRejectedValue(new Error('ENOENT: no such file'));
|
||||
vi.mocked(fs.access).mockRejectedValue(new Error('ENOENT: no such file'));
|
||||
|
||||
const result = await editFileTool.execute({
|
||||
path: 'nonexistent.txt',
|
||||
old_string: 'text',
|
||||
new_string: 'new',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
// The error message may vary, just check it failed
|
||||
expect(result.error).toBeTruthy();
|
||||
});
|
||||
|
||||
it('支持 LSP 时获取诊断信息', async () => {
|
||||
// 确保权限检查通过
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({ allowed: true }),
|
||||
} as any);
|
||||
vi.mocked(fs.readFile).mockResolvedValue('const x = 1');
|
||||
vi.mocked(isLanguageSupported).mockReturnValue(true);
|
||||
vi.mocked(touchFile).mockResolvedValue(false);
|
||||
vi.mocked(getFormattedFileDiagnostics).mockResolvedValue('\n错误: 类型不匹配');
|
||||
|
||||
const result = await editFileTool.execute({
|
||||
path: 'test.ts',
|
||||
old_string: 'const x = 1',
|
||||
new_string: 'const x: string = 1',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('代码检查发现问题');
|
||||
});
|
||||
|
||||
it('传递正确参数给权限检查', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('old text');
|
||||
const mockCheck = vi.fn().mockResolvedValue({ allowed: true });
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: mockCheck,
|
||||
} as any);
|
||||
|
||||
await editFileTool.execute({
|
||||
path: 'test.txt',
|
||||
old_string: 'old text',
|
||||
new_string: 'new text',
|
||||
});
|
||||
|
||||
expect(mockCheck).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
operation: 'edit',
|
||||
oldContent: 'old text',
|
||||
newContent: 'new text',
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,188 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
stat: vi.fn(),
|
||||
readlink: vi.fn(),
|
||||
readdir: vi.fn(),
|
||||
}));
|
||||
|
||||
// Mock permission manager
|
||||
vi.mock('../../../../src/permission/index.js', () => ({
|
||||
getPermissionManager: vi.fn(() => ({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
}),
|
||||
})),
|
||||
}));
|
||||
|
||||
// Mock loadDescription
|
||||
vi.mock('../../../../src/tools/load_description.js', () => ({
|
||||
loadDescription: vi.fn(() => '获取文件信息'),
|
||||
}));
|
||||
|
||||
import { getFileInfoTool } from '../../../../src/tools/filesystem/get_file_info.js';
|
||||
import * as fs from 'fs/promises';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('getFileInfoTool - 获取文件信息工具', () => {
|
||||
const mockStats = {
|
||||
isDirectory: () => false,
|
||||
isFile: () => true,
|
||||
isSymbolicLink: () => false,
|
||||
size: 1024,
|
||||
mode: 0o100644,
|
||||
birthtime: new Date('2024-01-01'),
|
||||
mtime: new Date('2024-01-15'),
|
||||
atime: new Date('2024-01-20'),
|
||||
ino: 12345,
|
||||
nlink: 1,
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(fs.stat).mockResolvedValue(mockStats as any);
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(getFileInfoTool.name).toBe('get_file_info');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(getFileInfoTool.metadata.category).toBe('filesystem');
|
||||
expect(getFileInfoTool.metadata.keywords).toContain('file');
|
||||
expect(getFileInfoTool.metadata.keywords).toContain('info');
|
||||
expect(getFileInfoTool.metadata.keywords).toContain('stat');
|
||||
});
|
||||
|
||||
it('定义了必需的 path 参数', () => {
|
||||
expect(getFileInfoTool.parameters.path.required).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功获取文件信息', async () => {
|
||||
const result = await getFileInfoTool.execute({ path: 'test.txt' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('路径:');
|
||||
expect(result.output).toContain('类型: 文件');
|
||||
expect(result.output).toContain('大小:');
|
||||
expect(result.output).toContain('权限:');
|
||||
expect(result.output).toContain('创建时间:');
|
||||
expect(result.output).toContain('修改时间:');
|
||||
expect(result.output).toContain('inode:');
|
||||
});
|
||||
|
||||
it('正确显示目录信息', async () => {
|
||||
vi.mocked(fs.stat).mockResolvedValue({
|
||||
...mockStats,
|
||||
isDirectory: () => true,
|
||||
isFile: () => false,
|
||||
} as any);
|
||||
vi.mocked(fs.readdir).mockResolvedValue(['file1', 'file2', 'dir1'] as any);
|
||||
|
||||
const result = await getFileInfoTool.execute({ path: 'test_dir' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('类型: 目录');
|
||||
expect(result.output).toContain('子项数量: 3');
|
||||
});
|
||||
|
||||
it('正确显示符号链接信息', async () => {
|
||||
vi.mocked(fs.stat).mockResolvedValue({
|
||||
...mockStats,
|
||||
isSymbolicLink: () => true,
|
||||
isFile: () => false,
|
||||
} as any);
|
||||
vi.mocked(fs.readlink).mockResolvedValue('/real/path');
|
||||
|
||||
const result = await getFileInfoTool.execute({ path: 'link' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('类型: 符号链接');
|
||||
expect(result.output).toContain('链接目标: /real/path');
|
||||
});
|
||||
|
||||
it('正确格式化文件大小', async () => {
|
||||
// 测试不同大小
|
||||
const sizes = [
|
||||
{ size: 500, expected: 'B' },
|
||||
{ size: 1024, expected: 'KB' },
|
||||
{ size: 1024 * 1024, expected: 'MB' },
|
||||
{ size: 1024 * 1024 * 1024, expected: 'GB' },
|
||||
];
|
||||
|
||||
for (const { size, expected } of sizes) {
|
||||
vi.mocked(fs.stat).mockResolvedValue({
|
||||
...mockStats,
|
||||
size,
|
||||
} as any);
|
||||
|
||||
const result = await getFileInfoTool.execute({ path: 'test.txt' });
|
||||
|
||||
expect(result.output).toContain(expected);
|
||||
}
|
||||
});
|
||||
|
||||
it('权限被拒绝时返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '不允许获取信息',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await getFileInfoTool.execute({ path: '/protected/file' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('权限被拒绝');
|
||||
});
|
||||
|
||||
it('需要确认时返回提示', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'ask',
|
||||
needsConfirmation: true,
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await getFileInfoTool.execute({ path: 'file.txt' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
});
|
||||
|
||||
it('文件不存在返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({ allowed: true }),
|
||||
} as any);
|
||||
|
||||
vi.mocked(fs.stat).mockRejectedValue(new Error('ENOENT'));
|
||||
|
||||
const result = await getFileInfoTool.execute({ path: 'nonexistent.txt' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('ENOENT');
|
||||
});
|
||||
|
||||
it('传递正确参数给权限检查', async () => {
|
||||
const mockCheck = vi.fn().mockResolvedValue({ allowed: true });
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: mockCheck,
|
||||
} as any);
|
||||
|
||||
await getFileInfoTool.execute({ path: 'test.txt' });
|
||||
|
||||
expect(mockCheck).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
operation: 'info',
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,225 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
readdir: vi.fn(),
|
||||
readFile: vi.fn(),
|
||||
}));
|
||||
|
||||
// Mock permission manager
|
||||
vi.mock('../../../../src/permission/index.js', () => ({
|
||||
getPermissionManager: vi.fn(() => ({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
}),
|
||||
})),
|
||||
}));
|
||||
|
||||
// Mock loadDescription
|
||||
vi.mock('../../../../src/tools/load_description.js', () => ({
|
||||
loadDescription: vi.fn(() => '在文件内容中搜索文本'),
|
||||
}));
|
||||
|
||||
import { grepContentTool } from '../../../../src/tools/filesystem/grep_content.js';
|
||||
import * as fs from 'fs/promises';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('grepContentTool - 内容搜索工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(grepContentTool.name).toBe('grep_content');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(grepContentTool.metadata.category).toBe('filesystem');
|
||||
expect(grepContentTool.metadata.keywords).toContain('grep');
|
||||
expect(grepContentTool.metadata.keywords).toContain('search');
|
||||
expect(grepContentTool.metadata.keywords).toContain('content');
|
||||
});
|
||||
|
||||
it('定义了必需参数', () => {
|
||||
expect(grepContentTool.parameters.directory.required).toBe(true);
|
||||
expect(grepContentTool.parameters.pattern.required).toBe(true);
|
||||
});
|
||||
|
||||
it('定义了可选参数', () => {
|
||||
expect(grepContentTool.parameters.file_pattern.required).toBe(false);
|
||||
expect(grepContentTool.parameters.max_results.required).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功搜索并返回匹配结果', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValue([
|
||||
{ name: 'test.ts', isDirectory: () => false, isFile: () => true },
|
||||
] as any);
|
||||
vi.mocked(fs.readFile).mockResolvedValue('const hello = "world";\nconst foo = "bar";');
|
||||
|
||||
const result = await grepContentTool.execute({
|
||||
directory: '.',
|
||||
pattern: 'hello',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('找到');
|
||||
expect(result.output).toContain('hello');
|
||||
});
|
||||
|
||||
it('没有匹配时返回提示', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValue([
|
||||
{ name: 'test.ts', isDirectory: () => false, isFile: () => true },
|
||||
] as any);
|
||||
vi.mocked(fs.readFile).mockResolvedValue('const foo = "bar";');
|
||||
|
||||
const result = await grepContentTool.execute({
|
||||
directory: '.',
|
||||
pattern: 'notfound',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('没有找到匹配的内容');
|
||||
});
|
||||
|
||||
it('按文件模式过滤', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValue([
|
||||
{ name: 'test.ts', isDirectory: () => false, isFile: () => true },
|
||||
{ name: 'test.js', isDirectory: () => false, isFile: () => true },
|
||||
] as any);
|
||||
vi.mocked(fs.readFile).mockResolvedValue('const hello = "world";');
|
||||
|
||||
const result = await grepContentTool.execute({
|
||||
directory: '.',
|
||||
pattern: 'hello',
|
||||
file_pattern: '*.ts',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
// 只搜索 .ts 文件
|
||||
expect(fs.readFile).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('限制最大结果数', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValue([
|
||||
{ name: 'test.ts', isDirectory: () => false, isFile: () => true },
|
||||
] as any);
|
||||
// 多行匹配内容
|
||||
vi.mocked(fs.readFile).mockResolvedValue(
|
||||
'hello1\nhello2\nhello3\nhello4\nhello5'
|
||||
);
|
||||
|
||||
const result = await grepContentTool.execute({
|
||||
directory: '.',
|
||||
pattern: 'hello',
|
||||
max_results: 2,
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('已达上限');
|
||||
});
|
||||
|
||||
it('跳过隐藏文件和 node_modules', async () => {
|
||||
// 第一次调用返回根目录内容,第二次调用返回 src 目录内容
|
||||
vi.mocked(fs.readdir)
|
||||
.mockResolvedValueOnce([
|
||||
{ name: '.hidden', isDirectory: () => true, isFile: () => false },
|
||||
{ name: 'node_modules', isDirectory: () => true, isFile: () => false },
|
||||
{ name: 'src', isDirectory: () => true, isFile: () => false },
|
||||
] as any)
|
||||
.mockResolvedValueOnce([
|
||||
{ name: 'index.ts', isDirectory: () => false, isFile: () => true },
|
||||
] as any);
|
||||
vi.mocked(fs.readFile).mockResolvedValue('const test = 1;');
|
||||
|
||||
await grepContentTool.execute({
|
||||
directory: '.',
|
||||
pattern: 'test',
|
||||
});
|
||||
|
||||
// 不应该进入隐藏目录或 node_modules,只进入 src
|
||||
expect(fs.readdir).toHaveBeenCalledTimes(2); // 根目录 + src
|
||||
});
|
||||
|
||||
it('权限被拒绝时返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '不允许搜索',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await grepContentTool.execute({
|
||||
directory: '/protected',
|
||||
pattern: 'test',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('权限被拒绝');
|
||||
});
|
||||
|
||||
it('需要确认时返回提示', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'ask',
|
||||
needsConfirmation: true,
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await grepContentTool.execute({
|
||||
directory: '.',
|
||||
pattern: 'test',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
});
|
||||
|
||||
it('递归搜索子目录', async () => {
|
||||
// 恢复权限检查
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({ allowed: true }),
|
||||
} as any);
|
||||
vi.mocked(fs.readdir)
|
||||
.mockResolvedValueOnce([
|
||||
{ name: 'src', isDirectory: () => true, isFile: () => false },
|
||||
] as any)
|
||||
.mockResolvedValueOnce([
|
||||
{ name: 'index.ts', isDirectory: () => false, isFile: () => true },
|
||||
] as any);
|
||||
vi.mocked(fs.readFile).mockResolvedValue('const test = 1;');
|
||||
|
||||
const result = await grepContentTool.execute({
|
||||
directory: '.',
|
||||
pattern: 'test',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(fs.readdir).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('传递正确参数给权限检查', async () => {
|
||||
const mockCheck = vi.fn().mockResolvedValue({ allowed: true });
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: mockCheck,
|
||||
} as any);
|
||||
vi.mocked(fs.readdir).mockResolvedValue([]);
|
||||
|
||||
await grepContentTool.execute({
|
||||
directory: 'src',
|
||||
pattern: 'test',
|
||||
});
|
||||
|
||||
expect(mockCheck).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
operation: 'grep',
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,143 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { listDirTool } from '../../../../src/tools/filesystem/list_directory.js';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
readdir: vi.fn(),
|
||||
}));
|
||||
|
||||
// Mock permission manager
|
||||
vi.mock('../../../../src/permission/index.js', () => ({
|
||||
getPermissionManager: vi.fn(() => ({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
}),
|
||||
})),
|
||||
}));
|
||||
|
||||
// Mock loadDescription
|
||||
vi.mock('../../../../src/tools/load_description.js', () => ({
|
||||
loadDescription: vi.fn(() => '列出目录内容'),
|
||||
}));
|
||||
|
||||
import * as fs from 'fs/promises';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('listDirTool - 列出目录工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(listDirTool.name).toBe('list_directory');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(listDirTool.metadata.category).toBe('filesystem');
|
||||
expect(listDirTool.metadata.keywords).toContain('list');
|
||||
expect(listDirTool.metadata.keywords).toContain('directory');
|
||||
expect(listDirTool.metadata.keywords).toContain('ls');
|
||||
});
|
||||
|
||||
it('定义了必需的 path 参数', () => {
|
||||
expect(listDirTool.parameters.path).toBeDefined();
|
||||
expect(listDirTool.parameters.path.required).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功列出目录内容', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValue([
|
||||
{ name: 'file1.txt', isDirectory: () => false },
|
||||
{ name: 'folder', isDirectory: () => true },
|
||||
{ name: 'file2.js', isDirectory: () => false },
|
||||
] as any);
|
||||
|
||||
const result = await listDirTool.execute({ path: './' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('file1.txt');
|
||||
expect(result.output).toContain('folder');
|
||||
expect(result.output).toContain('file2.js');
|
||||
});
|
||||
|
||||
it('使用正确的图标区分文件和目录', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValue([
|
||||
{ name: 'file.txt', isDirectory: () => false },
|
||||
{ name: 'folder', isDirectory: () => true },
|
||||
] as any);
|
||||
|
||||
const result = await listDirTool.execute({ path: './' });
|
||||
|
||||
expect(result.output).toMatch(/📄.*file\.txt/);
|
||||
expect(result.output).toMatch(/📁.*folder/);
|
||||
});
|
||||
|
||||
it('空目录显示提示', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValue([]);
|
||||
|
||||
const result = await listDirTool.execute({ path: './' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toBe('(空目录)');
|
||||
});
|
||||
|
||||
it('权限被拒绝时返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '不允许列出此目录',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await listDirTool.execute({ path: '/etc' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('权限被拒绝');
|
||||
});
|
||||
|
||||
it('需要确认时返回提示', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'ask',
|
||||
needsConfirmation: true,
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await listDirTool.execute({ path: '/home/user' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
});
|
||||
|
||||
it('目录不存在时返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
}),
|
||||
} as any);
|
||||
vi.mocked(fs.readdir).mockRejectedValue(new Error('ENOENT: no such directory'));
|
||||
|
||||
const result = await listDirTool.execute({ path: './nonexistent' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('ENOENT');
|
||||
});
|
||||
|
||||
it('使用 withFileTypes 选项调用 readdir', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValue([]);
|
||||
|
||||
await listDirTool.execute({ path: './' });
|
||||
|
||||
expect(fs.readdir).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
{ withFileTypes: true }
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,171 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
access: vi.fn().mockResolvedValue(undefined),
|
||||
stat: vi.fn(),
|
||||
rename: vi.fn().mockResolvedValue(undefined),
|
||||
mkdir: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
// Mock permission manager
|
||||
vi.mock('../../../../src/permission/index.js', () => ({
|
||||
getPermissionManager: vi.fn(() => ({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
}),
|
||||
})),
|
||||
}));
|
||||
|
||||
// Mock loadDescription
|
||||
vi.mock('../../../../src/tools/load_description.js', () => ({
|
||||
loadDescription: vi.fn(() => '移动文件'),
|
||||
}));
|
||||
|
||||
import { moveFileTool } from '../../../../src/tools/filesystem/move_file.js';
|
||||
import * as fs from 'fs/promises';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('moveFileTool - 文件移动工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(moveFileTool.name).toBe('move_file');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(moveFileTool.metadata.category).toBe('filesystem');
|
||||
expect(moveFileTool.metadata.keywords).toContain('move');
|
||||
expect(moveFileTool.metadata.keywords).toContain('rename');
|
||||
expect(moveFileTool.metadata.keywords).toContain('mv');
|
||||
});
|
||||
|
||||
it('定义了必需参数', () => {
|
||||
expect(moveFileTool.parameters.source.required).toBe(true);
|
||||
expect(moveFileTool.parameters.destination.required).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功移动文件', async () => {
|
||||
vi.mocked(fs.stat).mockRejectedValue(new Error('ENOENT')); // 目标不存在
|
||||
|
||||
const result = await moveFileTool.execute({
|
||||
source: 'old.txt',
|
||||
destination: 'new.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('已移动');
|
||||
expect(fs.rename).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('移动到已存在的目录', async () => {
|
||||
vi.mocked(fs.stat).mockResolvedValue({ isDirectory: () => true } as any);
|
||||
|
||||
const result = await moveFileTool.execute({
|
||||
source: 'file.txt',
|
||||
destination: '/target/dir',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('file.txt');
|
||||
});
|
||||
|
||||
it('源文件移动权限被拒绝', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '不允许移动',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await moveFileTool.execute({
|
||||
source: '/protected/file',
|
||||
destination: 'dest.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('权限被拒绝');
|
||||
});
|
||||
|
||||
it('目标位置写入权限被拒绝', async () => {
|
||||
const mockCheck = vi.fn()
|
||||
.mockResolvedValueOnce({ allowed: true }) // 移动权限
|
||||
.mockResolvedValueOnce({ allowed: false, action: 'deny', reason: '不允许写入' }); // 写入权限
|
||||
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: mockCheck,
|
||||
} as any);
|
||||
|
||||
const result = await moveFileTool.execute({
|
||||
source: 'src.txt',
|
||||
destination: '/protected/dest.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('权限被拒绝');
|
||||
});
|
||||
|
||||
it('需要确认时返回提示', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'ask',
|
||||
needsConfirmation: true,
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await moveFileTool.execute({
|
||||
source: 'file.txt',
|
||||
destination: 'new.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
});
|
||||
|
||||
it('源文件不存在返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({ allowed: true }),
|
||||
} as any);
|
||||
|
||||
vi.mocked(fs.access).mockRejectedValue(new Error('ENOENT'));
|
||||
|
||||
const result = await moveFileTool.execute({
|
||||
source: 'nonexistent.txt',
|
||||
destination: 'dest.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('ENOENT');
|
||||
});
|
||||
|
||||
it('创建目标目录', async () => {
|
||||
// 确保权限检查通过
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({ allowed: true }),
|
||||
} as any);
|
||||
// 源文件存在
|
||||
vi.mocked(fs.access).mockResolvedValue(undefined);
|
||||
// 目标不存在
|
||||
vi.mocked(fs.stat).mockRejectedValue(new Error('ENOENT'));
|
||||
|
||||
const result = await moveFileTool.execute({
|
||||
source: 'file.txt',
|
||||
destination: '/new/path/file.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(fs.mkdir).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
{ recursive: true }
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,142 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { readFileTool } from '../../../../src/tools/filesystem/read_file.js';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
readFile: vi.fn(),
|
||||
}));
|
||||
|
||||
// Mock permission manager
|
||||
vi.mock('../../../../src/permission/index.js', () => ({
|
||||
getPermissionManager: vi.fn(() => ({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
}),
|
||||
})),
|
||||
}));
|
||||
|
||||
// Mock loadDescription
|
||||
vi.mock('../../../../src/tools/load_description.js', () => ({
|
||||
loadDescription: vi.fn(() => '读取文件内容'),
|
||||
}));
|
||||
|
||||
import * as fs from 'fs/promises';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('readFileTool - 读取文件工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(readFileTool.name).toBe('read_file');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(readFileTool.metadata.category).toBe('filesystem');
|
||||
expect(readFileTool.metadata.keywords).toContain('read');
|
||||
expect(readFileTool.metadata.keywords).toContain('file');
|
||||
});
|
||||
|
||||
it('定义了必需的 path 参数', () => {
|
||||
expect(readFileTool.parameters.path).toBeDefined();
|
||||
expect(readFileTool.parameters.path.required).toBe(true);
|
||||
expect(readFileTool.parameters.path.type).toBe('string');
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功读取文件', async () => {
|
||||
const mockContent = 'Hello, World!';
|
||||
vi.mocked(fs.readFile).mockResolvedValue(mockContent);
|
||||
|
||||
const result = await readFileTool.execute({ path: './test.txt' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toBe(mockContent);
|
||||
});
|
||||
|
||||
it('处理绝对路径', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('content');
|
||||
|
||||
await readFileTool.execute({ path: '/absolute/path/file.txt' });
|
||||
|
||||
expect(fs.readFile).toHaveBeenCalledWith('/absolute/path/file.txt', 'utf-8');
|
||||
});
|
||||
|
||||
it('处理相对路径', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('content');
|
||||
|
||||
await readFileTool.execute({ path: './relative/file.txt' });
|
||||
|
||||
// 应该解析为绝对路径
|
||||
expect(fs.readFile).toHaveBeenCalled();
|
||||
const calledPath = vi.mocked(fs.readFile).mock.calls[0][0] as string;
|
||||
expect(calledPath.endsWith('relative/file.txt')).toBe(true);
|
||||
});
|
||||
|
||||
it('权限被拒绝时返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '不允许读取此文件',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await readFileTool.execute({ path: '/etc/passwd' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('权限被拒绝');
|
||||
});
|
||||
|
||||
it('需要确认时返回提示', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'ask',
|
||||
needsConfirmation: true,
|
||||
reason: '需要确认',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await readFileTool.execute({ path: './sensitive.txt' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
});
|
||||
|
||||
it('文件不存在时返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
}),
|
||||
} as any);
|
||||
vi.mocked(fs.readFile).mockRejectedValue(new Error('ENOENT: no such file'));
|
||||
|
||||
const result = await readFileTool.execute({ path: './nonexistent.txt' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('ENOENT');
|
||||
});
|
||||
|
||||
it('读取大文件', async () => {
|
||||
const largeContent = 'x'.repeat(10000);
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
}),
|
||||
} as any);
|
||||
vi.mocked(fs.readFile).mockResolvedValue(largeContent);
|
||||
|
||||
const result = await readFileTool.execute({ path: './large.txt' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output.length).toBe(10000);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,199 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
readdir: vi.fn(),
|
||||
}));
|
||||
|
||||
// Mock permission manager
|
||||
vi.mock('../../../../src/permission/index.js', () => ({
|
||||
getPermissionManager: vi.fn(() => ({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
}),
|
||||
})),
|
||||
}));
|
||||
|
||||
// Mock loadDescription
|
||||
vi.mock('../../../../src/tools/load_description.js', () => ({
|
||||
loadDescription: vi.fn(() => '按文件名搜索文件'),
|
||||
}));
|
||||
|
||||
import { searchFilesTool } from '../../../../src/tools/filesystem/search_files.js';
|
||||
import * as fs from 'fs/promises';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('searchFilesTool - 文件搜索工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(searchFilesTool.name).toBe('search_files');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(searchFilesTool.metadata.category).toBe('filesystem');
|
||||
expect(searchFilesTool.metadata.keywords).toContain('search');
|
||||
expect(searchFilesTool.metadata.keywords).toContain('find');
|
||||
expect(searchFilesTool.metadata.keywords).toContain('glob');
|
||||
});
|
||||
|
||||
it('定义了必需参数', () => {
|
||||
expect(searchFilesTool.parameters.directory.required).toBe(true);
|
||||
expect(searchFilesTool.parameters.pattern.required).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功搜索并返回匹配文件', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValue([
|
||||
{ name: 'test.ts', isDirectory: () => false, isFile: () => true },
|
||||
{ name: 'test.js', isDirectory: () => false, isFile: () => true },
|
||||
] as any);
|
||||
|
||||
const result = await searchFilesTool.execute({
|
||||
directory: '.',
|
||||
pattern: '*.ts',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('test.ts');
|
||||
expect(result.output).not.toContain('test.js');
|
||||
});
|
||||
|
||||
it('没有匹配时返回提示', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValue([
|
||||
{ name: 'test.js', isDirectory: () => false, isFile: () => true },
|
||||
] as any);
|
||||
|
||||
const result = await searchFilesTool.execute({
|
||||
directory: '.',
|
||||
pattern: '*.tsx',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('没有找到匹配的文件');
|
||||
});
|
||||
|
||||
it('递归搜索子目录', async () => {
|
||||
vi.mocked(fs.readdir)
|
||||
.mockResolvedValueOnce([
|
||||
{ name: 'src', isDirectory: () => true, isFile: () => false },
|
||||
{ name: 'index.ts', isDirectory: () => false, isFile: () => true },
|
||||
] as any)
|
||||
.mockResolvedValueOnce([
|
||||
{ name: 'app.ts', isDirectory: () => false, isFile: () => true },
|
||||
] as any);
|
||||
|
||||
const result = await searchFilesTool.execute({
|
||||
directory: '.',
|
||||
pattern: '*.ts',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('index.ts');
|
||||
expect(result.output).toContain('app.ts');
|
||||
});
|
||||
|
||||
it('跳过隐藏文件和 node_modules', async () => {
|
||||
// 第一次调用返回根目录内容,第二次调用返回 src 目录内容
|
||||
vi.mocked(fs.readdir)
|
||||
.mockResolvedValueOnce([
|
||||
{ name: '.git', isDirectory: () => true, isFile: () => false },
|
||||
{ name: 'node_modules', isDirectory: () => true, isFile: () => false },
|
||||
{ name: 'src', isDirectory: () => true, isFile: () => false },
|
||||
] as any)
|
||||
.mockResolvedValueOnce([
|
||||
{ name: 'index.ts', isDirectory: () => false, isFile: () => true },
|
||||
] as any);
|
||||
|
||||
await searchFilesTool.execute({
|
||||
directory: '.',
|
||||
pattern: '*',
|
||||
});
|
||||
|
||||
// 不应该进入隐藏目录或 node_modules,只进入 src
|
||||
expect(fs.readdir).toHaveBeenCalledTimes(2); // 根目录 + src
|
||||
});
|
||||
|
||||
it('权限被拒绝时返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '不允许搜索',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await searchFilesTool.execute({
|
||||
directory: '/protected',
|
||||
pattern: '*',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('权限被拒绝');
|
||||
});
|
||||
|
||||
it('需要确认时返回提示', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'ask',
|
||||
needsConfirmation: true,
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await searchFilesTool.execute({
|
||||
directory: '.',
|
||||
pattern: '*',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
});
|
||||
|
||||
it('支持 glob 模式匹配', async () => {
|
||||
// 恢复权限检查
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({ allowed: true }),
|
||||
} as any);
|
||||
vi.mocked(fs.readdir).mockResolvedValue([
|
||||
{ name: 'component.tsx', isDirectory: () => false, isFile: () => true },
|
||||
{ name: 'helper.ts', isDirectory: () => false, isFile: () => true },
|
||||
{ name: 'style.css', isDirectory: () => false, isFile: () => true },
|
||||
] as any);
|
||||
|
||||
// *.tsx 模式会匹配 component.tsx
|
||||
const result = await searchFilesTool.execute({
|
||||
directory: '.',
|
||||
pattern: '*.tsx',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('component.tsx');
|
||||
expect(result.output).not.toContain('style.css');
|
||||
});
|
||||
|
||||
it('传递正确参数给权限检查', async () => {
|
||||
const mockCheck = vi.fn().mockResolvedValue({ allowed: true });
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: mockCheck,
|
||||
} as any);
|
||||
vi.mocked(fs.readdir).mockResolvedValue([]);
|
||||
|
||||
await searchFilesTool.execute({
|
||||
directory: 'src',
|
||||
pattern: '*.ts',
|
||||
});
|
||||
|
||||
expect(mockCheck).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
operation: 'search',
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,262 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { writeFileTool } from '../../../../src/tools/filesystem/write_file.js';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
mkdir: vi.fn().mockResolvedValue(undefined),
|
||||
writeFile: vi.fn().mockResolvedValue(undefined),
|
||||
readFile: vi.fn().mockRejectedValue(new Error('ENOENT: file not found')),
|
||||
}));
|
||||
|
||||
// Mock permission manager
|
||||
const mockCheckFilePermission = vi.fn();
|
||||
vi.mock('../../../../src/permission/index.js', () => ({
|
||||
getPermissionManager: vi.fn(() => ({
|
||||
checkFilePermission: mockCheckFilePermission,
|
||||
})),
|
||||
}));
|
||||
|
||||
// Mock loadDescription
|
||||
vi.mock('../../../../src/tools/load_description.js', () => ({
|
||||
loadDescription: vi.fn(() => '写入文件内容'),
|
||||
}));
|
||||
|
||||
// Mock LSP
|
||||
const mockTouchFile = vi.fn();
|
||||
const mockGetFormattedFileDiagnostics = vi.fn();
|
||||
const mockIsLanguageSupported = vi.fn();
|
||||
|
||||
vi.mock('../../../../src/lsp/index.js', () => ({
|
||||
touchFile: (...args: unknown[]) => mockTouchFile(...args),
|
||||
getFormattedFileDiagnostics: (...args: unknown[]) => mockGetFormattedFileDiagnostics(...args),
|
||||
isLanguageSupported: (...args: unknown[]) => mockIsLanguageSupported(...args),
|
||||
}));
|
||||
|
||||
import * as fs from 'fs/promises';
|
||||
|
||||
describe('writeFileTool - 写入文件工具扩展测试', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
// 重置所有 mock 返回值
|
||||
vi.mocked(fs.mkdir).mockResolvedValue(undefined);
|
||||
vi.mocked(fs.writeFile).mockResolvedValue(undefined);
|
||||
mockCheckFilePermission.mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
});
|
||||
mockIsLanguageSupported.mockReturnValue(false);
|
||||
});
|
||||
|
||||
describe('LSP 集成', () => {
|
||||
it('支持的语言触发 LSP 诊断', async () => {
|
||||
mockIsLanguageSupported.mockReturnValue(true);
|
||||
mockTouchFile.mockResolvedValue(false);
|
||||
mockGetFormattedFileDiagnostics.mockResolvedValue(null);
|
||||
|
||||
const result = await writeFileTool.execute({
|
||||
path: './test.ts',
|
||||
content: 'const x = 1;',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(mockTouchFile).toHaveBeenCalled();
|
||||
expect(mockGetFormattedFileDiagnostics).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('首次启动等待更长时间', async () => {
|
||||
mockIsLanguageSupported.mockReturnValue(true);
|
||||
mockTouchFile.mockResolvedValue(true); // 首次启动返回 true
|
||||
mockGetFormattedFileDiagnostics.mockResolvedValue(null);
|
||||
|
||||
const startTime = Date.now();
|
||||
await writeFileTool.execute({
|
||||
path: './test.ts',
|
||||
content: 'const x = 1;',
|
||||
});
|
||||
const elapsed = Date.now() - startTime;
|
||||
|
||||
// 首次启动应该等待更长时间(2000ms vs 300ms)
|
||||
expect(elapsed).toBeGreaterThanOrEqual(1900);
|
||||
});
|
||||
|
||||
it('有诊断问题时在输出中显示', async () => {
|
||||
mockIsLanguageSupported.mockReturnValue(true);
|
||||
mockTouchFile.mockResolvedValue(false);
|
||||
mockGetFormattedFileDiagnostics.mockResolvedValue(
|
||||
'\n<file_diagnostics>\n1:1 ERROR: Type error\n</file_diagnostics>'
|
||||
);
|
||||
|
||||
const result = await writeFileTool.execute({
|
||||
path: './test.ts',
|
||||
content: 'const x: string = 1;',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('代码检查发现问题');
|
||||
expect(result.output).toContain('file_diagnostics');
|
||||
});
|
||||
|
||||
it('LSP 错误不影响主流程', async () => {
|
||||
mockIsLanguageSupported.mockReturnValue(true);
|
||||
mockTouchFile.mockRejectedValue(new Error('LSP error'));
|
||||
|
||||
const result = await writeFileTool.execute({
|
||||
path: './test.ts',
|
||||
content: 'const x = 1;',
|
||||
});
|
||||
|
||||
// 即使 LSP 失败,文件写入仍然成功
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('文件已写入');
|
||||
});
|
||||
|
||||
it('不支持的语言不触发 LSP', async () => {
|
||||
mockIsLanguageSupported.mockReturnValue(false);
|
||||
|
||||
await writeFileTool.execute({
|
||||
path: './test.txt',
|
||||
content: 'plain text',
|
||||
});
|
||||
|
||||
expect(mockTouchFile).not.toHaveBeenCalled();
|
||||
expect(mockGetFormattedFileDiagnostics).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('路径处理', () => {
|
||||
it('相对路径转换为绝对路径', async () => {
|
||||
await writeFileTool.execute({
|
||||
path: './relative/path/file.txt',
|
||||
content: 'content',
|
||||
});
|
||||
|
||||
expect(fs.mkdir).toHaveBeenCalledWith(
|
||||
expect.stringMatching(/relative\/path$/),
|
||||
{ recursive: true }
|
||||
);
|
||||
});
|
||||
|
||||
it('绝对路径保持不变', async () => {
|
||||
await writeFileTool.execute({
|
||||
path: '/absolute/path/file.txt',
|
||||
content: 'content',
|
||||
});
|
||||
|
||||
expect(fs.writeFile).toHaveBeenCalledWith(
|
||||
'/absolute/path/file.txt',
|
||||
'content',
|
||||
'utf-8'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('权限检查详情', () => {
|
||||
it('权限检查包含正确的上下文', async () => {
|
||||
await writeFileTool.execute({
|
||||
path: './test.txt',
|
||||
content: 'file content',
|
||||
});
|
||||
|
||||
expect(mockCheckFilePermission).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
operation: 'write',
|
||||
newContent: 'file content',
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('被拒绝时返回具体原因', async () => {
|
||||
mockCheckFilePermission.mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '系统文件不允许修改',
|
||||
});
|
||||
|
||||
const result = await writeFileTool.execute({
|
||||
path: '/etc/passwd',
|
||||
content: 'malicious',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('权限被拒绝');
|
||||
expect(result.error).toContain('系统文件不允许修改');
|
||||
});
|
||||
|
||||
it('需要确认时返回具体原因', async () => {
|
||||
mockCheckFilePermission.mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'ask',
|
||||
needsConfirmation: true,
|
||||
reason: '首次写入此文件',
|
||||
});
|
||||
|
||||
const result = await writeFileTool.execute({
|
||||
path: './new-file.txt',
|
||||
content: 'content',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
expect(result.error).toContain('首次写入此文件');
|
||||
});
|
||||
});
|
||||
|
||||
describe('错误处理', () => {
|
||||
it('文件系统错误返回错误信息', async () => {
|
||||
vi.mocked(fs.writeFile).mockRejectedValue(new Error('EACCES: permission denied'));
|
||||
|
||||
const result = await writeFileTool.execute({
|
||||
path: './test.txt',
|
||||
content: 'content',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('EACCES');
|
||||
});
|
||||
|
||||
it('目录创建失败返回错误信息', async () => {
|
||||
vi.mocked(fs.mkdir).mockImplementationOnce(() => {
|
||||
return Promise.reject(new Error('mkdir failed'));
|
||||
});
|
||||
|
||||
const result = await writeFileTool.execute({
|
||||
path: './deep/nested/file.txt',
|
||||
content: 'content',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
// The error is now wrapped by the editors module
|
||||
expect(result.error).toBeTruthy();
|
||||
});
|
||||
|
||||
it('非 Error 对象也能正确处理', async () => {
|
||||
vi.mocked(fs.writeFile).mockRejectedValue('String error');
|
||||
|
||||
const result = await writeFileTool.execute({
|
||||
path: './test.txt',
|
||||
content: 'content',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('String error');
|
||||
});
|
||||
});
|
||||
|
||||
describe('工具元数据', () => {
|
||||
it('包含完整的元数据', () => {
|
||||
expect(writeFileTool.metadata.category).toBe('filesystem');
|
||||
expect(writeFileTool.metadata.keywords).toContain('write');
|
||||
expect(writeFileTool.metadata.keywords).toContain('save');
|
||||
expect(writeFileTool.metadata.keywords).toContain('create');
|
||||
expect(writeFileTool.metadata.keywords).toContain('写入');
|
||||
expect(writeFileTool.metadata.deferLoading).toBe(false);
|
||||
});
|
||||
|
||||
it('参数定义正确', () => {
|
||||
expect(writeFileTool.parameters.path.required).toBe(true);
|
||||
expect(writeFileTool.parameters.content.required).toBe(true);
|
||||
expect(writeFileTool.parameters.path.type).toBe('string');
|
||||
expect(writeFileTool.parameters.content.type).toBe('string');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,158 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { writeFileTool } from '../../../../src/tools/filesystem/write_file.js';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
mkdir: vi.fn().mockResolvedValue(undefined),
|
||||
writeFile: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
// Mock permission manager
|
||||
vi.mock('../../../../src/permission/index.js', () => ({
|
||||
getPermissionManager: vi.fn(() => ({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
}),
|
||||
})),
|
||||
}));
|
||||
|
||||
// Mock loadDescription
|
||||
vi.mock('../../../../src/tools/load_description.js', () => ({
|
||||
loadDescription: vi.fn(() => '写入文件内容'),
|
||||
}));
|
||||
|
||||
// Mock LSP
|
||||
vi.mock('../../../../src/lsp/index.js', () => ({
|
||||
touchFile: vi.fn().mockResolvedValue(false),
|
||||
getFormattedFileDiagnostics: vi.fn().mockResolvedValue(null),
|
||||
isLanguageSupported: vi.fn().mockReturnValue(false),
|
||||
}));
|
||||
|
||||
import * as fs from 'fs/promises';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('writeFileTool - 写入文件工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(writeFileTool.name).toBe('write_file');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(writeFileTool.metadata.category).toBe('filesystem');
|
||||
expect(writeFileTool.metadata.keywords).toContain('write');
|
||||
expect(writeFileTool.metadata.keywords).toContain('save');
|
||||
});
|
||||
|
||||
it('定义了必需的参数', () => {
|
||||
expect(writeFileTool.parameters.path).toBeDefined();
|
||||
expect(writeFileTool.parameters.path.required).toBe(true);
|
||||
expect(writeFileTool.parameters.content).toBeDefined();
|
||||
expect(writeFileTool.parameters.content.required).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功写入文件', async () => {
|
||||
const result = await writeFileTool.execute({
|
||||
path: './test.txt',
|
||||
content: 'Hello, World!',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('文件已写入');
|
||||
});
|
||||
|
||||
it('创建必要的目录', async () => {
|
||||
await writeFileTool.execute({
|
||||
path: './deep/nested/file.txt',
|
||||
content: 'content',
|
||||
});
|
||||
|
||||
expect(fs.mkdir).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
{ recursive: true }
|
||||
);
|
||||
});
|
||||
|
||||
it('权限被拒绝时返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '不允许写入此文件',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await writeFileTool.execute({
|
||||
path: '/etc/passwd',
|
||||
content: 'malicious',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('权限被拒绝');
|
||||
});
|
||||
|
||||
it('需要确认时返回提示', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'ask',
|
||||
needsConfirmation: true,
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await writeFileTool.execute({
|
||||
path: './new-file.txt',
|
||||
content: 'content',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
});
|
||||
|
||||
it('写入失败时返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
}),
|
||||
} as any);
|
||||
vi.mocked(fs.writeFile).mockRejectedValue(new Error('Write failed'));
|
||||
|
||||
const result = await writeFileTool.execute({
|
||||
path: './test.txt',
|
||||
content: 'content',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('Write failed');
|
||||
});
|
||||
|
||||
it('传递 newContent 给权限检查', async () => {
|
||||
const mockCheck = vi.fn().mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
});
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: mockCheck,
|
||||
} as any);
|
||||
|
||||
await writeFileTool.execute({
|
||||
path: './test.txt',
|
||||
content: 'new content',
|
||||
});
|
||||
|
||||
expect(mockCheck).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
operation: 'write',
|
||||
newContent: 'new content',
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,160 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// 定义一个可控的 mock
|
||||
let mockExecAsyncResult: { stdout: string; stderr: string } | Error = {
|
||||
stdout: '',
|
||||
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 add 命令'),
|
||||
}));
|
||||
|
||||
import { gitAddTool } from '../../../../src/tools/git/git_add.js';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('gitAddTool - Git Add 工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockExecAsyncResult = {
|
||||
stdout: '',
|
||||
stderr: '',
|
||||
};
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(gitAddTool.name).toBe('git_add');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(gitAddTool.metadata.category).toBe('git');
|
||||
expect(gitAddTool.metadata.keywords).toContain('add');
|
||||
expect(gitAddTool.metadata.keywords).toContain('stage');
|
||||
});
|
||||
|
||||
it('定义了可选参数', () => {
|
||||
expect(gitAddTool.parameters.files.required).toBe(false);
|
||||
expect(gitAddTool.parameters.all.required).toBe(false);
|
||||
expect(gitAddTool.parameters.update.required).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('暂存所有文件 (all: true)', async () => {
|
||||
mockExecAsyncResult = { stdout: '', stderr: '' };
|
||||
|
||||
const result = await gitAddTool.execute({ all: true });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('文件已暂存');
|
||||
});
|
||||
|
||||
it('暂存指定文件', async () => {
|
||||
mockExecAsyncResult = { stdout: '', stderr: '' };
|
||||
|
||||
const result = await gitAddTool.execute({
|
||||
files: ['file1.txt', 'file2.txt'],
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('暂存单个文件(字符串)', async () => {
|
||||
mockExecAsyncResult = { stdout: '', stderr: '' };
|
||||
|
||||
const result = await gitAddTool.execute({ files: 'single.txt' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('使用 update 选项', async () => {
|
||||
mockExecAsyncResult = { stdout: '', stderr: '' };
|
||||
|
||||
const result = await gitAddTool.execute({ update: true });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('无参数返回错误', async () => {
|
||||
const result = await gitAddTool.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 gitAddTool.execute({ all: true });
|
||||
|
||||
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 gitAddTool.execute({ all: true });
|
||||
|
||||
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('Command failed'),
|
||||
{ stdout: '', stderr: 'fatal: not a git repository', message: 'Command failed' }
|
||||
);
|
||||
|
||||
const result = await gitAddTool.execute({ all: true });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('not a git repository');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,240 @@
|
||||
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 模拟异步
|
||||
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 { gitBranchTool } from '../../../../src/tools/git/git_branch.js';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('gitBranchTool - Git 分支工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockExec.mockReturnValue({ stdout: '* main\n develop', stderr: '' });
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(gitBranchTool.name).toBe('git_branch');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(gitBranchTool.metadata.category).toBe('git');
|
||||
expect(gitBranchTool.metadata.keywords).toContain('branch');
|
||||
expect(gitBranchTool.metadata.keywords).toContain('create');
|
||||
expect(gitBranchTool.metadata.keywords).toContain('delete');
|
||||
});
|
||||
|
||||
it('定义了可选参数', () => {
|
||||
expect(gitBranchTool.parameters.action.required).toBe(false);
|
||||
expect(gitBranchTool.parameters.name.required).toBe(false);
|
||||
expect(gitBranchTool.parameters.new_name.required).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('列出分支(默认操作)', async () => {
|
||||
const result = await gitBranchTool.execute({});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
// 源代码使用 -v 参数,输出可能包含 main 或在空结果时返回空字符串
|
||||
expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('git branch'));
|
||||
expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('-v'));
|
||||
});
|
||||
|
||||
it('创建新分支', async () => {
|
||||
mockExec.mockReturnValue({ stdout: '', stderr: '' });
|
||||
|
||||
const result = await gitBranchTool.execute({
|
||||
action: 'create',
|
||||
name: 'feature/new-branch',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('git branch feature/new-branch'));
|
||||
});
|
||||
|
||||
it('删除分支', async () => {
|
||||
mockExec.mockReturnValue({ stdout: '', stderr: '' });
|
||||
|
||||
const result = await gitBranchTool.execute({
|
||||
action: 'delete',
|
||||
name: 'old-branch',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('git branch -d old-branch'));
|
||||
});
|
||||
|
||||
it('强制删除分支', async () => {
|
||||
mockExec.mockReturnValue({ stdout: '', stderr: '' });
|
||||
|
||||
const result = await gitBranchTool.execute({
|
||||
action: 'delete',
|
||||
name: 'unmerged-branch',
|
||||
force: true,
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('git branch -D unmerged-branch'));
|
||||
});
|
||||
|
||||
it('重命名分支', async () => {
|
||||
mockExec.mockReturnValue({ stdout: '', stderr: '' });
|
||||
|
||||
const result = await gitBranchTool.execute({
|
||||
action: 'rename',
|
||||
name: 'old-name',
|
||||
new_name: 'new-name',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('git branch -m old-name new-name'));
|
||||
});
|
||||
|
||||
it('显示远程分支', async () => {
|
||||
mockExec.mockReturnValue({ stdout: 'origin/main\norigin/develop', stderr: '' });
|
||||
|
||||
const result = await gitBranchTool.execute({
|
||||
action: 'list',
|
||||
remote: true,
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('git branch -r'));
|
||||
});
|
||||
|
||||
it('显示所有分支', async () => {
|
||||
const result = await gitBranchTool.execute({
|
||||
action: 'list',
|
||||
all: true,
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('git branch -a'));
|
||||
});
|
||||
|
||||
it('创建分支缺少名称返回错误', async () => {
|
||||
const result = await gitBranchTool.execute({
|
||||
action: 'create',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要提供分支名称');
|
||||
});
|
||||
|
||||
it('删除分支缺少名称返回错误', async () => {
|
||||
const result = await gitBranchTool.execute({
|
||||
action: 'delete',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要提供分支名称');
|
||||
});
|
||||
|
||||
it('重命名缺少参数返回错误', async () => {
|
||||
const result = await gitBranchTool.execute({
|
||||
action: 'rename',
|
||||
name: 'old-name',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('新名称');
|
||||
});
|
||||
|
||||
it('未知操作返回错误', async () => {
|
||||
const result = await gitBranchTool.execute({
|
||||
action: 'unknown',
|
||||
});
|
||||
|
||||
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 gitBranchTool.execute({
|
||||
action: 'create',
|
||||
name: '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 gitBranchTool.execute({
|
||||
action: 'delete',
|
||||
name: 'branch',
|
||||
});
|
||||
|
||||
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 gitBranchTool.execute({});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('not a git repository');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,192 @@
|
||||
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('需要用户确认');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,275 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// 定义可控的 mock 变量
|
||||
let mockExecAsyncResult: { stdout: string; stderr: string } | Error = {
|
||||
stdout: '[main abc1234] Test commit\n 1 file changed, 1 insertion(+)',
|
||||
stderr: '',
|
||||
};
|
||||
|
||||
let mockPermissionResult = {
|
||||
allowed: true,
|
||||
action: 'allow' as const,
|
||||
reason: undefined as string | undefined,
|
||||
needsConfirmation: false,
|
||||
};
|
||||
|
||||
// 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(async () => mockPermissionResult),
|
||||
})),
|
||||
}));
|
||||
|
||||
// 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\n 1 file changed, 1 insertion(+)',
|
||||
stderr: '',
|
||||
};
|
||||
mockPermissionResult = {
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
reason: undefined,
|
||||
needsConfirmation: false,
|
||||
};
|
||||
});
|
||||
|
||||
describe('基本提交', () => {
|
||||
it('成功提交变更', async () => {
|
||||
const result = await gitCommitTool.execute({
|
||||
message: 'Test commit message',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('Test commit');
|
||||
});
|
||||
|
||||
it('没有 message 返回错误', async () => {
|
||||
const result = await gitCommitTool.execute({});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('提交信息是必填的');
|
||||
});
|
||||
|
||||
it('amend 模式无 message 允许', async () => {
|
||||
const result = await gitCommitTool.execute({
|
||||
amend: true,
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('提交选项', () => {
|
||||
it('使用 -a 选项暂存所有变更', async () => {
|
||||
const result = await gitCommitTool.execute({
|
||||
message: 'Auto stage commit',
|
||||
all: true,
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('amend 带 message', async () => {
|
||||
const result = await gitCommitTool.execute({
|
||||
message: 'Updated message',
|
||||
amend: true,
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('转义 message 中的引号', async () => {
|
||||
const result = await gitCommitTool.execute({
|
||||
message: 'Message with "quotes"',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('权限检查', () => {
|
||||
it('权限拒绝返回错误', async () => {
|
||||
mockPermissionResult = {
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '不允许提交到此仓库',
|
||||
needsConfirmation: false,
|
||||
};
|
||||
|
||||
const result = await gitCommitTool.execute({
|
||||
message: 'Test commit',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('权限被拒绝');
|
||||
expect(result.error).toContain('不允许提交到此仓库');
|
||||
});
|
||||
|
||||
it('需要确认时返回提示', async () => {
|
||||
mockPermissionResult = {
|
||||
allowed: false,
|
||||
action: 'ask',
|
||||
reason: '首次提交',
|
||||
needsConfirmation: true,
|
||||
};
|
||||
|
||||
const result = await gitCommitTool.execute({
|
||||
message: 'First commit',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
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: 'Check context',
|
||||
});
|
||||
|
||||
expect(mockCheck).toHaveBeenCalledWith({
|
||||
operation: 'commit',
|
||||
message: 'Check context',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('错误处理', () => {
|
||||
it('没有变更可提交', async () => {
|
||||
mockExecAsyncResult = Object.assign(
|
||||
new Error('nothing to commit, working tree clean'),
|
||||
{ stdout: '', stderr: '', message: 'nothing to commit, working tree clean' }
|
||||
);
|
||||
|
||||
const result = await gitCommitTool.execute({
|
||||
message: 'Empty commit',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('没有变更需要提交');
|
||||
expect(result.error).toContain('git_add');
|
||||
});
|
||||
|
||||
it('stderr 包含 nothing to commit', async () => {
|
||||
mockExecAsyncResult = Object.assign(
|
||||
new Error('Command failed'),
|
||||
{ stdout: '', stderr: 'nothing to commit', message: 'Command failed' }
|
||||
);
|
||||
|
||||
const result = await gitCommitTool.execute({
|
||||
message: 'Empty commit',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('没有变更需要提交');
|
||||
});
|
||||
|
||||
it('其他 Git 错误', async () => {
|
||||
mockExecAsyncResult = Object.assign(
|
||||
new Error('fatal: not a git repository'),
|
||||
{ stdout: '', stderr: 'fatal: not a git repository', message: 'fatal: not a git repository' }
|
||||
);
|
||||
|
||||
const result = await gitCommitTool.execute({
|
||||
message: 'Test commit',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('not a git repository');
|
||||
});
|
||||
|
||||
it('保留 stdout 在错误中', async () => {
|
||||
mockExecAsyncResult = Object.assign(
|
||||
new Error('error'),
|
||||
{ stdout: 'some output', stderr: 'error message', message: 'error' }
|
||||
);
|
||||
|
||||
const result = await gitCommitTool.execute({
|
||||
message: 'Test',
|
||||
});
|
||||
|
||||
expect(result.output).toBe('some output');
|
||||
expect(result.error).toBe('error message');
|
||||
});
|
||||
});
|
||||
|
||||
describe('工具元数据', () => {
|
||||
it('包含正确的名称', () => {
|
||||
expect(gitCommitTool.name).toBe('git_commit');
|
||||
});
|
||||
|
||||
it('包含正确的类别', () => {
|
||||
expect(gitCommitTool.metadata.category).toBe('git');
|
||||
});
|
||||
|
||||
it('包含关键词', () => {
|
||||
expect(gitCommitTool.metadata.keywords).toContain('git');
|
||||
expect(gitCommitTool.metadata.keywords).toContain('commit');
|
||||
expect(gitCommitTool.metadata.keywords).toContain('提交');
|
||||
});
|
||||
|
||||
it('参数定义正确', () => {
|
||||
expect(gitCommitTool.parameters.message.required).toBe(true);
|
||||
expect(gitCommitTool.parameters.amend.required).toBe(false);
|
||||
expect(gitCommitTool.parameters.all.required).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('输出格式', () => {
|
||||
it('包含 stdout', async () => {
|
||||
mockExecAsyncResult = {
|
||||
stdout: 'commit output',
|
||||
stderr: '',
|
||||
};
|
||||
|
||||
const result = await gitCommitTool.execute({ message: 'test' });
|
||||
|
||||
expect(result.output).toBe('commit output');
|
||||
});
|
||||
|
||||
it('包含 stdout 和 stderr', async () => {
|
||||
mockExecAsyncResult = {
|
||||
stdout: 'commit output',
|
||||
stderr: 'warning message',
|
||||
};
|
||||
|
||||
const result = await gitCommitTool.execute({ message: 'test' });
|
||||
|
||||
expect(result.output).toContain('commit output');
|
||||
expect(result.output).toContain('warning message');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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',
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,172 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// 定义一个可控的 mock
|
||||
let mockExecAsyncResult: { stdout: string; stderr: string } | Error = {
|
||||
stdout: '',
|
||||
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 diff 命令'),
|
||||
}));
|
||||
|
||||
import { gitDiffTool } from '../../../../src/tools/git/git_diff.js';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('gitDiffTool - Git Diff 工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockExecAsyncResult = {
|
||||
stdout: 'diff --git a/file.txt b/file.txt\n-old\n+new',
|
||||
stderr: '',
|
||||
};
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(gitDiffTool.name).toBe('git_diff');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(gitDiffTool.metadata.category).toBe('git');
|
||||
expect(gitDiffTool.metadata.keywords).toContain('diff');
|
||||
expect(gitDiffTool.metadata.keywords).toContain('compare');
|
||||
});
|
||||
|
||||
it('定义了可选参数', () => {
|
||||
expect(gitDiffTool.parameters.path.required).toBe(false);
|
||||
expect(gitDiffTool.parameters.staged.required).toBe(false);
|
||||
expect(gitDiffTool.parameters.commit.required).toBe(false);
|
||||
expect(gitDiffTool.parameters.stat.required).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功获取差异', async () => {
|
||||
const result = await gitDiffTool.execute({});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('diff');
|
||||
});
|
||||
|
||||
it('无差异时显示提示', async () => {
|
||||
mockExecAsyncResult = { stdout: '', stderr: '' };
|
||||
|
||||
const result = await gitDiffTool.execute({});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('无差异');
|
||||
});
|
||||
|
||||
it('显示暂存区差异', async () => {
|
||||
mockExecAsyncResult = { stdout: 'staged changes', stderr: '' };
|
||||
|
||||
const result = await gitDiffTool.execute({ staged: true });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('staged changes');
|
||||
});
|
||||
|
||||
it('与指定提交对比', async () => {
|
||||
mockExecAsyncResult = { stdout: 'commit diff', stderr: '' };
|
||||
|
||||
const result = await gitDiffTool.execute({ commit: 'HEAD~1' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('指定文件路径', async () => {
|
||||
mockExecAsyncResult = { stdout: 'file diff', stderr: '' };
|
||||
|
||||
const result = await gitDiffTool.execute({ path: 'src/file.ts' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('仅显示统计信息', async () => {
|
||||
mockExecAsyncResult = {
|
||||
stdout: ' file.txt | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)',
|
||||
stderr: '',
|
||||
};
|
||||
|
||||
const result = await gitDiffTool.execute({ stat: true });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('file changed');
|
||||
});
|
||||
|
||||
it('权限被拒绝时返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkGitPermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '操作不被允许',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await gitDiffTool.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 gitDiffTool.execute({});
|
||||
|
||||
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('Command failed'),
|
||||
{ stdout: '', stderr: 'fatal: not a git repository', message: 'Command failed' }
|
||||
);
|
||||
|
||||
const result = await gitDiffTool.execute({});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('not a git repository');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,173 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// 定义一个可控的 mock
|
||||
let mockExecAsyncResult: { stdout: string; stderr: string } | Error = {
|
||||
stdout: 'abc123 Fix bug (John, 2 days ago)\ndef456 Add feature (Jane, 3 days ago)',
|
||||
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 { gitLogTool } from '../../../../src/tools/git/git_log.js';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('gitLogTool - Git Log 工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockExecAsyncResult = {
|
||||
stdout: 'abc123 Fix bug (John, 2 days ago)\ndef456 Add feature (Jane, 3 days ago)',
|
||||
stderr: '',
|
||||
};
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(gitLogTool.name).toBe('git_log');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(gitLogTool.metadata.category).toBe('git');
|
||||
expect(gitLogTool.metadata.keywords).toContain('log');
|
||||
expect(gitLogTool.metadata.keywords).toContain('history');
|
||||
expect(gitLogTool.metadata.keywords).toContain('commit');
|
||||
});
|
||||
|
||||
it('所有参数都是可选的', () => {
|
||||
expect(gitLogTool.parameters.limit.required).toBe(false);
|
||||
expect(gitLogTool.parameters.oneline.required).toBe(false);
|
||||
expect(gitLogTool.parameters.file.required).toBe(false);
|
||||
expect(gitLogTool.parameters.author.required).toBe(false);
|
||||
expect(gitLogTool.parameters.since.required).toBe(false);
|
||||
expect(gitLogTool.parameters.graph.required).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功获取提交历史', async () => {
|
||||
const result = await gitLogTool.execute({});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('abc123');
|
||||
expect(result.output).toContain('Fix bug');
|
||||
});
|
||||
|
||||
it('使用自定义 limit', async () => {
|
||||
const result = await gitLogTool.execute({ limit: 5 });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('使用 oneline 格式', async () => {
|
||||
const result = await gitLogTool.execute({ oneline: true });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('显示分支图', async () => {
|
||||
const result = await gitLogTool.execute({ graph: true });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('按作者筛选', async () => {
|
||||
const result = await gitLogTool.execute({ author: 'John' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('按日期筛选', async () => {
|
||||
const result = await gitLogTool.execute({ since: '2024-01-01' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('查看指定文件的历史', async () => {
|
||||
const result = await gitLogTool.execute({ file: 'src/index.ts' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('没有提交记录时返回提示', async () => {
|
||||
mockExecAsyncResult = { stdout: '', stderr: '' };
|
||||
|
||||
const result = await gitLogTool.execute({});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('无提交记录');
|
||||
});
|
||||
|
||||
it('权限被拒绝时返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkGitPermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '不允许查看历史',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await gitLogTool.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 gitLogTool.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);
|
||||
mockExecAsyncResult = Object.assign(
|
||||
new Error('Command failed'),
|
||||
{ stdout: '', stderr: 'fatal: not a git repository' }
|
||||
);
|
||||
|
||||
const result = await gitLogTool.execute({});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('not a git repository');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,163 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// 定义一个可控的 mock
|
||||
let mockExecAsyncResult: { stdout: string; stderr: string } | Error = {
|
||||
stdout: 'Already up to date.',
|
||||
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(() => '从远程仓库拉取更新'),
|
||||
}));
|
||||
|
||||
import { gitPullTool } from '../../../../src/tools/git/git_pull.js';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('gitPullTool - Git Pull 工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockExecAsyncResult = {
|
||||
stdout: 'Already up to date.',
|
||||
stderr: '',
|
||||
};
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(gitPullTool.name).toBe('git_pull');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(gitPullTool.metadata.category).toBe('git');
|
||||
expect(gitPullTool.metadata.keywords).toContain('pull');
|
||||
expect(gitPullTool.metadata.keywords).toContain('fetch');
|
||||
});
|
||||
|
||||
it('所有参数都是可选的', () => {
|
||||
expect(gitPullTool.parameters.remote.required).toBe(false);
|
||||
expect(gitPullTool.parameters.branch.required).toBe(false);
|
||||
expect(gitPullTool.parameters.rebase.required).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功拉取更新', async () => {
|
||||
const result = await gitPullTool.execute({});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('Already up to date');
|
||||
});
|
||||
|
||||
it('指定远程仓库和分支', async () => {
|
||||
const result = await gitPullTool.execute({
|
||||
remote: 'upstream',
|
||||
branch: 'develop',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('使用 rebase 模式', async () => {
|
||||
const result = await gitPullTool.execute({ rebase: true });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('合并冲突时返回友好错误', async () => {
|
||||
mockExecAsyncResult = Object.assign(
|
||||
new Error('Command failed'),
|
||||
{ stdout: '', stderr: 'CONFLICT (content): Merge conflict in file.txt' }
|
||||
);
|
||||
|
||||
const result = await gitPullTool.execute({});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('合并冲突');
|
||||
expect(result.error).toContain('手动解决');
|
||||
});
|
||||
|
||||
it('本地变更时返回友好错误', async () => {
|
||||
mockExecAsyncResult = Object.assign(
|
||||
new Error('Command failed'),
|
||||
{ stdout: '', stderr: 'error: Your local changes would be overwritten by merge' }
|
||||
);
|
||||
|
||||
const result = await gitPullTool.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 gitPullTool.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 gitPullTool.execute({});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
});
|
||||
|
||||
it('Git 命令失败返回错误', async () => {
|
||||
// 恢复权限检查(因为之前的测试可能修改了 mock)
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkGitPermission: vi.fn().mockResolvedValue({ allowed: true }),
|
||||
} as any);
|
||||
mockExecAsyncResult = Object.assign(
|
||||
new Error('Command failed'),
|
||||
{ stdout: '', stderr: 'fatal: not a git repository' }
|
||||
);
|
||||
|
||||
const result = await gitPullTool.execute({});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('not a git repository');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,195 @@
|
||||
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');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,224 @@
|
||||
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 { gitStashTool } from '../../../../src/tools/git/git_stash.js';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('gitStashTool - Git Stash 工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockExec.mockReturnValue({ stdout: 'Saved working directory', stderr: '' });
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(gitStashTool.name).toBe('git_stash');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(gitStashTool.metadata.category).toBe('git');
|
||||
expect(gitStashTool.metadata.keywords).toContain('stash');
|
||||
expect(gitStashTool.metadata.keywords).toContain('save');
|
||||
});
|
||||
|
||||
it('所有参数都是可选的', () => {
|
||||
expect(gitStashTool.parameters.action.required).toBe(false);
|
||||
expect(gitStashTool.parameters.message.required).toBe(false);
|
||||
expect(gitStashTool.parameters.index.required).toBe(false);
|
||||
expect(gitStashTool.parameters.include_untracked.required).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('默认 push 操作暂存变更', async () => {
|
||||
const result = await gitStashTool.execute({});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('git stash push'));
|
||||
});
|
||||
|
||||
it('带消息暂存', async () => {
|
||||
const result = await gitStashTool.execute({
|
||||
message: 'WIP: feature',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('-m'));
|
||||
});
|
||||
|
||||
it('包含未跟踪文件', async () => {
|
||||
const result = await gitStashTool.execute({
|
||||
include_untracked: true,
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('-u'));
|
||||
});
|
||||
|
||||
it('列出暂存', async () => {
|
||||
mockExec.mockReturnValue({
|
||||
stdout: 'stash@{0}: WIP on main\nstash@{1}: feature',
|
||||
stderr: '',
|
||||
});
|
||||
|
||||
const result = await gitStashTool.execute({ action: 'list' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('git stash list'));
|
||||
});
|
||||
|
||||
it('恢复暂存 (pop)', async () => {
|
||||
mockExec.mockReturnValue({ stdout: '', stderr: '' });
|
||||
|
||||
const result = await gitStashTool.execute({ action: 'pop' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('暂存已恢复并删除');
|
||||
expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('git stash pop'));
|
||||
});
|
||||
|
||||
it('应用暂存 (apply)', async () => {
|
||||
mockExec.mockReturnValue({ stdout: '', stderr: '' });
|
||||
|
||||
const result = await gitStashTool.execute({ action: 'apply' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('暂存已恢复');
|
||||
expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('git stash apply'));
|
||||
});
|
||||
|
||||
it('删除暂存 (drop)', async () => {
|
||||
mockExec.mockReturnValue({ stdout: '', stderr: '' });
|
||||
|
||||
const result = await gitStashTool.execute({ action: 'drop', index: 1 });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('git stash drop stash@{1}'));
|
||||
});
|
||||
|
||||
it('清除所有暂存', async () => {
|
||||
mockExec.mockReturnValue({ stdout: '', stderr: '' });
|
||||
|
||||
const result = await gitStashTool.execute({ action: 'clear' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('所有暂存已清除');
|
||||
expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('git stash clear'));
|
||||
});
|
||||
|
||||
it('显示暂存内容', async () => {
|
||||
mockExec.mockReturnValue({ stdout: 'diff --git a/file.ts', stderr: '' });
|
||||
|
||||
const result = await gitStashTool.execute({ action: 'show' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('git stash show -p'));
|
||||
});
|
||||
|
||||
it('未知操作返回错误', async () => {
|
||||
const result = await gitStashTool.execute({ action: 'unknown' });
|
||||
|
||||
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: 'No local changes to save',
|
||||
});
|
||||
|
||||
const result = await gitStashTool.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: 'CONFLICT (content): Merge conflict',
|
||||
});
|
||||
|
||||
const result = await gitStashTool.execute({ action: 'pop' });
|
||||
|
||||
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 gitStashTool.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 gitStashTool.execute({});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,139 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// 定义一个可控的 mock
|
||||
let mockExecAsyncResult: { stdout: string; stderr: string } | Error = {
|
||||
stdout: 'On branch main\nnothing to commit',
|
||||
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 { gitStatusTool } from '../../../../src/tools/git/git_status.js';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('gitStatusTool - Git 状态工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockExecAsyncResult = {
|
||||
stdout: 'On branch main\nnothing to commit',
|
||||
stderr: '',
|
||||
};
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(gitStatusTool.name).toBe('git_status');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(gitStatusTool.metadata.category).toBe('git');
|
||||
expect(gitStatusTool.metadata.keywords).toContain('git');
|
||||
expect(gitStatusTool.metadata.keywords).toContain('status');
|
||||
});
|
||||
|
||||
it('定义了可选参数', () => {
|
||||
expect(gitStatusTool.parameters.short).toBeDefined();
|
||||
expect(gitStatusTool.parameters.short.required).toBe(false);
|
||||
expect(gitStatusTool.parameters.branch).toBeDefined();
|
||||
expect(gitStatusTool.parameters.branch.required).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功获取状态', async () => {
|
||||
const result = await gitStatusTool.execute({});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('On branch main');
|
||||
});
|
||||
|
||||
it('权限被拒绝时返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkGitPermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '操作不被允许',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await gitStatusTool.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 gitStatusTool.execute({});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
});
|
||||
|
||||
it('非 git 仓库返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkGitPermission: vi.fn().mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
}),
|
||||
} as any);
|
||||
mockExecAsyncResult = Object.assign(
|
||||
new Error('Command failed'),
|
||||
{ stderr: 'fatal: not a git repository', stdout: '' }
|
||||
);
|
||||
|
||||
const result = await gitStatusTool.execute({});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('not a git repository');
|
||||
});
|
||||
|
||||
it('包含 stderr 输出', async () => {
|
||||
mockExecAsyncResult = {
|
||||
stdout: 'main output',
|
||||
stderr: 'warning message',
|
||||
};
|
||||
|
||||
const result = await gitStatusTool.execute({});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('main output');
|
||||
expect(result.output).toContain('warning message');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,205 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
|
||||
// Mock fs
|
||||
vi.mock('fs', () => ({
|
||||
readFileSync: vi.fn(),
|
||||
}));
|
||||
|
||||
import * as fs from 'fs';
|
||||
import { loadDescription } from '../../../src/tools/load_description.js';
|
||||
|
||||
describe('loadDescription', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('带分类的工具', () => {
|
||||
it('加载 shell 类工具描述', () => {
|
||||
vi.mocked(fs.readFileSync).mockReturnValue(' Bash 工具描述 ');
|
||||
|
||||
const result = loadDescription('bash');
|
||||
|
||||
expect(result).toBe('Bash 工具描述');
|
||||
expect(fs.readFileSync).toHaveBeenCalledWith(
|
||||
expect.stringContaining('descriptions/shell/bash.txt'),
|
||||
'utf-8'
|
||||
);
|
||||
});
|
||||
|
||||
it('加载 filesystem 类工具描述', () => {
|
||||
vi.mocked(fs.readFileSync).mockReturnValue('读取文件描述');
|
||||
|
||||
const result = loadDescription('read_file');
|
||||
|
||||
expect(result).toBe('读取文件描述');
|
||||
expect(fs.readFileSync).toHaveBeenCalledWith(
|
||||
expect.stringContaining('descriptions/filesystem/read_file.txt'),
|
||||
'utf-8'
|
||||
);
|
||||
});
|
||||
|
||||
it('加载 write_file 描述', () => {
|
||||
vi.mocked(fs.readFileSync).mockReturnValue('写入文件描述');
|
||||
|
||||
loadDescription('write_file');
|
||||
|
||||
expect(fs.readFileSync).toHaveBeenCalledWith(
|
||||
expect.stringContaining('descriptions/filesystem/write_file.txt'),
|
||||
'utf-8'
|
||||
);
|
||||
});
|
||||
|
||||
it('加载 edit_file 描述', () => {
|
||||
vi.mocked(fs.readFileSync).mockReturnValue('编辑文件描述');
|
||||
|
||||
loadDescription('edit_file');
|
||||
|
||||
expect(fs.readFileSync).toHaveBeenCalledWith(
|
||||
expect.stringContaining('descriptions/filesystem/edit_file.txt'),
|
||||
'utf-8'
|
||||
);
|
||||
});
|
||||
|
||||
it('加载 git 类工具描述', () => {
|
||||
vi.mocked(fs.readFileSync).mockReturnValue('Git status 描述');
|
||||
|
||||
loadDescription('git_status');
|
||||
|
||||
expect(fs.readFileSync).toHaveBeenCalledWith(
|
||||
expect.stringContaining('descriptions/git/git_status.txt'),
|
||||
'utf-8'
|
||||
);
|
||||
});
|
||||
|
||||
it('加载 web 类工具描述', () => {
|
||||
vi.mocked(fs.readFileSync).mockReturnValue('Web search 描述');
|
||||
|
||||
loadDescription('web_search');
|
||||
|
||||
expect(fs.readFileSync).toHaveBeenCalledWith(
|
||||
expect.stringContaining('descriptions/web/web_search.txt'),
|
||||
'utf-8'
|
||||
);
|
||||
});
|
||||
|
||||
it('加载 todo 类工具描述', () => {
|
||||
vi.mocked(fs.readFileSync).mockReturnValue('Todo read 描述');
|
||||
|
||||
loadDescription('todo_read');
|
||||
|
||||
expect(fs.readFileSync).toHaveBeenCalledWith(
|
||||
expect.stringContaining('descriptions/todo/todo_read.txt'),
|
||||
'utf-8'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('不带分类的工具', () => {
|
||||
it('未知工具使用根目录', () => {
|
||||
vi.mocked(fs.readFileSync).mockReturnValue('未知工具描述');
|
||||
|
||||
loadDescription('unknown_tool');
|
||||
|
||||
expect(fs.readFileSync).toHaveBeenCalledWith(
|
||||
expect.stringContaining('descriptions/unknown_tool.txt'),
|
||||
'utf-8'
|
||||
);
|
||||
// 确保不包含子目录
|
||||
expect(fs.readFileSync).not.toHaveBeenCalledWith(
|
||||
expect.stringContaining('descriptions/filesystem/unknown_tool.txt'),
|
||||
'utf-8'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('错误处理', () => {
|
||||
it('文件不存在时抛出错误', () => {
|
||||
vi.mocked(fs.readFileSync).mockImplementation(() => {
|
||||
throw new Error('ENOENT');
|
||||
});
|
||||
|
||||
expect(() => loadDescription('nonexistent')).toThrow('无法加载工具描述文件');
|
||||
});
|
||||
|
||||
it('错误信息包含文件路径', () => {
|
||||
vi.mocked(fs.readFileSync).mockImplementation(() => {
|
||||
throw new Error('ENOENT');
|
||||
});
|
||||
|
||||
expect(() => loadDescription('some_tool')).toThrow('some_tool.txt');
|
||||
});
|
||||
});
|
||||
|
||||
describe('内容处理', () => {
|
||||
it('去除前后空白', () => {
|
||||
vi.mocked(fs.readFileSync).mockReturnValue('\n\n 内容 \n\n');
|
||||
|
||||
const result = loadDescription('bash');
|
||||
|
||||
expect(result).toBe('内容');
|
||||
});
|
||||
|
||||
it('保留中间空白', () => {
|
||||
vi.mocked(fs.readFileSync).mockReturnValue('第一行\n第二行\n第三行');
|
||||
|
||||
const result = loadDescription('bash');
|
||||
|
||||
expect(result).toBe('第一行\n第二行\n第三行');
|
||||
});
|
||||
|
||||
it('空文件返回空字符串', () => {
|
||||
vi.mocked(fs.readFileSync).mockReturnValue(' ');
|
||||
|
||||
const result = loadDescription('bash');
|
||||
|
||||
expect(result).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('所有映射的工具', () => {
|
||||
const toolCategories = [
|
||||
// shell
|
||||
{ tool: 'bash', category: 'shell' },
|
||||
// filesystem
|
||||
{ tool: 'read_file', category: 'filesystem' },
|
||||
{ tool: 'write_file', category: 'filesystem' },
|
||||
{ tool: 'edit_file', category: 'filesystem' },
|
||||
{ tool: 'list_directory', category: 'filesystem' },
|
||||
{ tool: 'create_directory', category: 'filesystem' },
|
||||
{ tool: 'search_files', category: 'filesystem' },
|
||||
{ tool: 'grep_content', category: 'filesystem' },
|
||||
{ tool: 'get_file_info', category: 'filesystem' },
|
||||
{ tool: 'move_file', category: 'filesystem' },
|
||||
{ tool: 'copy_file', category: 'filesystem' },
|
||||
{ tool: 'delete_file', category: 'filesystem' },
|
||||
// web
|
||||
{ tool: 'web_search', category: 'web' },
|
||||
{ tool: 'web_extract', category: 'web' },
|
||||
// git
|
||||
{ tool: 'git_status', category: 'git' },
|
||||
{ tool: 'git_diff', category: 'git' },
|
||||
{ tool: 'git_log', category: 'git' },
|
||||
{ tool: 'git_branch', category: 'git' },
|
||||
{ tool: 'git_add', category: 'git' },
|
||||
{ tool: 'git_commit', category: 'git' },
|
||||
{ tool: 'git_push', category: 'git' },
|
||||
{ tool: 'git_pull', category: 'git' },
|
||||
{ tool: 'git_checkout', category: 'git' },
|
||||
{ tool: 'git_stash', category: 'git' },
|
||||
// todo
|
||||
{ tool: 'todo_read', category: 'todo' },
|
||||
{ tool: 'todo_write', category: 'todo' },
|
||||
];
|
||||
|
||||
it.each(toolCategories)('$tool 映射到 $category 目录', ({ tool, category }) => {
|
||||
vi.mocked(fs.readFileSync).mockReturnValue('描述');
|
||||
|
||||
loadDescription(tool);
|
||||
|
||||
expect(fs.readFileSync).toHaveBeenCalledWith(
|
||||
expect.stringContaining(`descriptions/${category}/${tool}.txt`),
|
||||
'utf-8'
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,360 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import { ToolRegistry } from '../../../src/tools/registry.js';
|
||||
import type { ToolWithMetadata, ToolMetadata, ToolCategory } from '../../../src/tools/types.js';
|
||||
|
||||
// 创建 mock 工具
|
||||
function createMockToolWithMetadata(
|
||||
name: string,
|
||||
options: Partial<{
|
||||
category: ToolCategory;
|
||||
deferLoading: boolean;
|
||||
keywords: string[];
|
||||
description: string;
|
||||
}> = {}
|
||||
): ToolWithMetadata {
|
||||
const metadata: ToolMetadata = {
|
||||
name,
|
||||
category: options.category ?? 'core',
|
||||
description: options.description ?? `Mock tool: ${name}`,
|
||||
keywords: options.keywords ?? [name],
|
||||
deferLoading: options.deferLoading ?? false,
|
||||
};
|
||||
|
||||
return {
|
||||
name,
|
||||
description: metadata.description,
|
||||
parameters: { type: 'object', properties: {}, required: [] },
|
||||
execute: async () => ({ success: true, output: `executed ${name}` }),
|
||||
metadata,
|
||||
};
|
||||
}
|
||||
|
||||
describe('ToolRegistry - 工具注册表', () => {
|
||||
let registry: ToolRegistry;
|
||||
|
||||
beforeEach(() => {
|
||||
registry = new ToolRegistry();
|
||||
});
|
||||
|
||||
describe('register / registerAll', () => {
|
||||
it('注册单个工具', () => {
|
||||
const tool = createMockToolWithMetadata('test_tool');
|
||||
registry.register(tool);
|
||||
|
||||
expect(registry.has('test_tool')).toBe(true);
|
||||
expect(registry.size).toBe(1);
|
||||
});
|
||||
|
||||
it('批量注册工具', () => {
|
||||
const tools = [
|
||||
createMockToolWithMetadata('tool_a'),
|
||||
createMockToolWithMetadata('tool_b'),
|
||||
createMockToolWithMetadata('tool_c'),
|
||||
];
|
||||
registry.registerAll(tools);
|
||||
|
||||
expect(registry.size).toBe(3);
|
||||
expect(registry.has('tool_a')).toBe(true);
|
||||
expect(registry.has('tool_b')).toBe(true);
|
||||
expect(registry.has('tool_c')).toBe(true);
|
||||
});
|
||||
|
||||
it('同名工具覆盖注册', () => {
|
||||
const tool1 = createMockToolWithMetadata('test_tool', { description: 'version 1' });
|
||||
const tool2 = createMockToolWithMetadata('test_tool', { description: 'version 2' });
|
||||
|
||||
registry.register(tool1);
|
||||
registry.register(tool2);
|
||||
|
||||
expect(registry.size).toBe(1);
|
||||
const retrieved = registry.getTool('test_tool');
|
||||
expect(retrieved?.description).toBe('version 2');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getTool / getTools', () => {
|
||||
beforeEach(() => {
|
||||
registry.registerAll([
|
||||
createMockToolWithMetadata('read_file'),
|
||||
createMockToolWithMetadata('write_file'),
|
||||
createMockToolWithMetadata('bash'),
|
||||
]);
|
||||
});
|
||||
|
||||
it('获取存在的工具', () => {
|
||||
const tool = registry.getTool('read_file');
|
||||
expect(tool).toBeDefined();
|
||||
expect(tool?.name).toBe('read_file');
|
||||
});
|
||||
|
||||
it('获取不存在的工具返回 undefined', () => {
|
||||
const tool = registry.getTool('non_existent');
|
||||
expect(tool).toBeUndefined();
|
||||
});
|
||||
|
||||
it('批量获取工具', () => {
|
||||
const tools = registry.getTools(['read_file', 'bash']);
|
||||
expect(tools).toHaveLength(2);
|
||||
expect(tools.map((t) => t.name)).toContain('read_file');
|
||||
expect(tools.map((t) => t.name)).toContain('bash');
|
||||
});
|
||||
|
||||
it('批量获取时跳过不存在的工具', () => {
|
||||
const tools = registry.getTools(['read_file', 'non_existent', 'bash']);
|
||||
expect(tools).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('批量获取空列表返回空数组', () => {
|
||||
const tools = registry.getTools([]);
|
||||
expect(tools).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getCoreTools - 核心工具(非延迟加载)', () => {
|
||||
it('只返回 deferLoading=false 的工具', () => {
|
||||
registry.registerAll([
|
||||
createMockToolWithMetadata('core_tool_1', { deferLoading: false }),
|
||||
createMockToolWithMetadata('core_tool_2', { deferLoading: false }),
|
||||
createMockToolWithMetadata('deferred_tool', { deferLoading: true }),
|
||||
]);
|
||||
|
||||
const coreTools = registry.getCoreTools();
|
||||
|
||||
expect(coreTools).toHaveLength(2);
|
||||
expect(coreTools.map((t) => t.name)).toContain('core_tool_1');
|
||||
expect(coreTools.map((t) => t.name)).toContain('core_tool_2');
|
||||
expect(coreTools.map((t) => t.name)).not.toContain('deferred_tool');
|
||||
});
|
||||
|
||||
it('所有工具都是延迟加载时返回空数组', () => {
|
||||
registry.registerAll([
|
||||
createMockToolWithMetadata('tool_1', { deferLoading: true }),
|
||||
createMockToolWithMetadata('tool_2', { deferLoading: true }),
|
||||
]);
|
||||
|
||||
const coreTools = registry.getCoreTools();
|
||||
expect(coreTools).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('默认 deferLoading=false 作为核心工具', () => {
|
||||
registry.register(createMockToolWithMetadata('default_tool'));
|
||||
const coreTools = registry.getCoreTools();
|
||||
expect(coreTools).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getAllTools / getAllMetadata', () => {
|
||||
beforeEach(() => {
|
||||
registry.registerAll([
|
||||
createMockToolWithMetadata('tool_a', { category: 'filesystem', deferLoading: false }),
|
||||
createMockToolWithMetadata('tool_b', { category: 'shell', deferLoading: true }),
|
||||
createMockToolWithMetadata('tool_c', { category: 'core', deferLoading: false }),
|
||||
]);
|
||||
});
|
||||
|
||||
it('获取所有工具', () => {
|
||||
const allTools = registry.getAllTools();
|
||||
expect(allTools).toHaveLength(3);
|
||||
});
|
||||
|
||||
it('获取所有元数据', () => {
|
||||
const metadata = registry.getAllMetadata();
|
||||
|
||||
expect(metadata).toHaveLength(3);
|
||||
expect(metadata.find((m) => m.name === 'tool_a')?.category).toBe('filesystem');
|
||||
expect(metadata.find((m) => m.name === 'tool_b')?.deferLoading).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('has / size', () => {
|
||||
it('空注册表', () => {
|
||||
expect(registry.size).toBe(0);
|
||||
expect(registry.has('any')).toBe(false);
|
||||
});
|
||||
|
||||
it('注册后正确检测', () => {
|
||||
registry.register(createMockToolWithMetadata('test'));
|
||||
|
||||
expect(registry.size).toBe(1);
|
||||
expect(registry.has('test')).toBe(true);
|
||||
expect(registry.has('other')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('search - 工具搜索', () => {
|
||||
// 注意:searchTools 只搜索 deferLoading=true 的工具
|
||||
beforeEach(() => {
|
||||
registry.registerAll([
|
||||
createMockToolWithMetadata('read_file', {
|
||||
category: 'filesystem',
|
||||
keywords: ['read', 'file', 'open', 'cat'],
|
||||
description: '读取文件内容',
|
||||
deferLoading: true, // 必须为 true 才能被搜索
|
||||
}),
|
||||
createMockToolWithMetadata('write_file', {
|
||||
category: 'filesystem',
|
||||
keywords: ['write', 'file', 'save', 'create'],
|
||||
description: '写入文件内容',
|
||||
deferLoading: true,
|
||||
}),
|
||||
createMockToolWithMetadata('bash', {
|
||||
category: 'shell',
|
||||
keywords: ['bash', 'shell', 'command', 'execute', 'run'],
|
||||
description: '执行 bash 命令',
|
||||
deferLoading: true,
|
||||
}),
|
||||
createMockToolWithMetadata('glob', {
|
||||
category: 'filesystem',
|
||||
keywords: ['glob', 'pattern', 'find', 'search', 'file'],
|
||||
description: '搜索匹配模式的文件',
|
||||
deferLoading: true,
|
||||
}),
|
||||
]);
|
||||
});
|
||||
|
||||
it('按关键词搜索', () => {
|
||||
const results = registry.search('file');
|
||||
|
||||
expect(results.length).toBeGreaterThan(0);
|
||||
// file 相关工具应该排在前面
|
||||
const fileTools = results.filter((r) => r.name.includes('file') || r.category === 'filesystem');
|
||||
expect(fileTools.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('限制返回结果数量', () => {
|
||||
const results = registry.search('file', 2);
|
||||
expect(results.length).toBeLessThanOrEqual(2);
|
||||
});
|
||||
|
||||
it('搜索 shell 相关', () => {
|
||||
const results = registry.search('shell');
|
||||
|
||||
expect(results.length).toBeGreaterThan(0);
|
||||
expect(results.some((r) => r.name === 'bash')).toBe(true);
|
||||
});
|
||||
|
||||
it('无匹配时返回空数组或低分结果', () => {
|
||||
const results = registry.search('xyznonexistent');
|
||||
// 可能返回空数组或低分结果,取决于搜索实现
|
||||
expect(Array.isArray(results)).toBe(true);
|
||||
});
|
||||
|
||||
it('只搜索 deferLoading=true 的工具', () => {
|
||||
// 创建新的注册表
|
||||
const testRegistry = new ToolRegistry();
|
||||
testRegistry.registerAll([
|
||||
createMockToolWithMetadata('core_tool', {
|
||||
keywords: ['test'],
|
||||
deferLoading: false, // 核心工具,不被搜索
|
||||
}),
|
||||
createMockToolWithMetadata('deferred_tool', {
|
||||
keywords: ['test'],
|
||||
deferLoading: true, // 延迟加载,可被搜索
|
||||
}),
|
||||
]);
|
||||
|
||||
const results = testRegistry.search('test');
|
||||
|
||||
expect(results).toHaveLength(1);
|
||||
expect(results[0].name).toBe('deferred_tool');
|
||||
});
|
||||
});
|
||||
|
||||
describe('toBasicTool - 转换为基础工具类型', () => {
|
||||
it('转换后只包含基础属性', () => {
|
||||
const toolWithMeta = createMockToolWithMetadata('test', {
|
||||
category: 'filesystem',
|
||||
deferLoading: true,
|
||||
keywords: ['test', 'mock'],
|
||||
});
|
||||
registry.register(toolWithMeta);
|
||||
|
||||
const basicTool = registry.getTool('test');
|
||||
|
||||
expect(basicTool).toBeDefined();
|
||||
expect(basicTool).toHaveProperty('name');
|
||||
expect(basicTool).toHaveProperty('description');
|
||||
expect(basicTool).toHaveProperty('parameters');
|
||||
expect(basicTool).toHaveProperty('execute');
|
||||
// 不应该包含 metadata
|
||||
expect(basicTool).not.toHaveProperty('metadata');
|
||||
});
|
||||
|
||||
it('execute 函数正常工作', async () => {
|
||||
registry.register(createMockToolWithMetadata('test'));
|
||||
const tool = registry.getTool('test');
|
||||
|
||||
const result = await tool?.execute({});
|
||||
|
||||
expect(result).toEqual({ success: true, output: 'executed test' });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('ToolRegistry 实际使用场景', () => {
|
||||
let registry: ToolRegistry;
|
||||
|
||||
beforeEach(() => {
|
||||
registry = new ToolRegistry();
|
||||
});
|
||||
|
||||
it('模拟工具发现流程', () => {
|
||||
// 注册一批工具,部分为核心工具,部分为延迟加载
|
||||
registry.registerAll([
|
||||
createMockToolWithMetadata('tool_search', {
|
||||
deferLoading: false,
|
||||
category: 'core',
|
||||
keywords: ['search', 'discover', 'find', 'tool'],
|
||||
}),
|
||||
createMockToolWithMetadata('read_file', {
|
||||
deferLoading: false,
|
||||
category: 'filesystem',
|
||||
keywords: ['read', 'file'],
|
||||
}),
|
||||
createMockToolWithMetadata('advanced_git', {
|
||||
deferLoading: true,
|
||||
category: 'git',
|
||||
keywords: ['git', 'version', 'control'],
|
||||
}),
|
||||
createMockToolWithMetadata('database_query', {
|
||||
deferLoading: true,
|
||||
category: 'database',
|
||||
keywords: ['database', 'sql', 'query'],
|
||||
}),
|
||||
]);
|
||||
|
||||
// 1. 获取核心工具(会话开始时)
|
||||
const coreTools = registry.getCoreTools();
|
||||
expect(coreTools).toHaveLength(2);
|
||||
expect(coreTools.map((t) => t.name)).toContain('tool_search');
|
||||
|
||||
// 2. 搜索工具
|
||||
const gitResults = registry.search('git');
|
||||
expect(gitResults.some((r) => r.name === 'advanced_git')).toBe(true);
|
||||
|
||||
// 3. 按需加载发现的工具
|
||||
const discoveredTools = registry.getTools(['advanced_git']);
|
||||
expect(discoveredTools).toHaveLength(1);
|
||||
expect(discoveredTools[0].name).toBe('advanced_git');
|
||||
});
|
||||
|
||||
it('模拟多分类工具注册', () => {
|
||||
const categories: ToolCategory[] = ['core', 'filesystem', 'shell', 'git', 'web'];
|
||||
|
||||
for (const category of categories) {
|
||||
registry.register(
|
||||
createMockToolWithMetadata(`${category}_tool`, {
|
||||
category,
|
||||
keywords: [category],
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
const metadata = registry.getAllMetadata();
|
||||
|
||||
expect(metadata).toHaveLength(5);
|
||||
for (const category of categories) {
|
||||
expect(metadata.some((m) => m.category === category)).toBe(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,278 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { searchTools } from '../../../src/tools/search.js';
|
||||
import type { ToolMetadata, ToolCategory } from '../../../src/tools/types.js';
|
||||
|
||||
// 创建测试用的工具元数据
|
||||
function createToolMetadata(
|
||||
name: string,
|
||||
options: Partial<{
|
||||
category: ToolCategory;
|
||||
description: string;
|
||||
keywords: string[];
|
||||
deferLoading: boolean;
|
||||
}> = {}
|
||||
): ToolMetadata {
|
||||
return {
|
||||
name,
|
||||
category: options.category ?? 'core',
|
||||
description: options.description ?? `Description for ${name}`,
|
||||
keywords: options.keywords ?? [name],
|
||||
deferLoading: options.deferLoading ?? true,
|
||||
};
|
||||
}
|
||||
|
||||
describe('searchTools - 工具搜索算法', () => {
|
||||
const testTools: ToolMetadata[] = [
|
||||
createToolMetadata('read_file', {
|
||||
category: 'filesystem',
|
||||
description: '读取文件内容',
|
||||
keywords: ['read', 'file', 'open', 'cat', '文件', '读取'],
|
||||
}),
|
||||
createToolMetadata('write_file', {
|
||||
category: 'filesystem',
|
||||
description: '写入文件内容',
|
||||
keywords: ['write', 'file', 'save', 'create', '文件', '写入'],
|
||||
}),
|
||||
createToolMetadata('bash', {
|
||||
category: 'shell',
|
||||
description: '执行 bash 命令',
|
||||
keywords: ['bash', 'shell', 'command', 'execute', 'run', '命令', '执行'],
|
||||
}),
|
||||
createToolMetadata('glob', {
|
||||
category: 'filesystem',
|
||||
description: '搜索匹配模式的文件',
|
||||
keywords: ['glob', 'pattern', 'find', 'search', 'file', '搜索', '模式'],
|
||||
}),
|
||||
createToolMetadata('grep', {
|
||||
category: 'filesystem',
|
||||
description: '在文件内容中搜索',
|
||||
keywords: ['grep', 'search', 'content', 'find', '搜索', '内容'],
|
||||
}),
|
||||
createToolMetadata('git_status', {
|
||||
category: 'git',
|
||||
description: '查看 Git 仓库状态',
|
||||
keywords: ['git', 'status', 'repository', '状态', '仓库'],
|
||||
}),
|
||||
createToolMetadata('git_commit', {
|
||||
category: 'git',
|
||||
description: '提交代码更改',
|
||||
keywords: ['git', 'commit', 'save', '提交', '保存'],
|
||||
}),
|
||||
];
|
||||
|
||||
describe('基础搜索功能', () => {
|
||||
it('按名称精确匹配得最高分', () => {
|
||||
const results = searchTools('bash', testTools);
|
||||
|
||||
expect(results.length).toBeGreaterThan(0);
|
||||
expect(results[0].name).toBe('bash');
|
||||
expect(results[0].score).toBeGreaterThanOrEqual(10); // 名称精确匹配 +10
|
||||
});
|
||||
|
||||
it('按名称包含匹配', () => {
|
||||
const results = searchTools('file', testTools);
|
||||
|
||||
expect(results.length).toBeGreaterThan(0);
|
||||
// read_file 和 write_file 都应该匹配
|
||||
const fileTools = results.filter((r) => r.name.includes('file'));
|
||||
expect(fileTools.length).toBe(2);
|
||||
});
|
||||
|
||||
it('按关键词精确匹配', () => {
|
||||
const results = searchTools('shell', testTools);
|
||||
|
||||
expect(results.length).toBeGreaterThan(0);
|
||||
expect(results[0].name).toBe('bash');
|
||||
});
|
||||
|
||||
it('按描述内容匹配', () => {
|
||||
const results = searchTools('仓库', testTools);
|
||||
|
||||
expect(results.length).toBeGreaterThan(0);
|
||||
expect(results.some((r) => r.name === 'git_status')).toBe(true);
|
||||
});
|
||||
|
||||
it('中文关键词搜索', () => {
|
||||
const results = searchTools('文件', testTools);
|
||||
|
||||
expect(results.length).toBeGreaterThan(0);
|
||||
// read_file 和 write_file 都有 '文件' 关键词
|
||||
expect(results.some((r) => r.name === 'read_file')).toBe(true);
|
||||
expect(results.some((r) => r.name === 'write_file')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('分词功能', () => {
|
||||
it('空格分隔的多词查询', () => {
|
||||
const results = searchTools('read file', testTools);
|
||||
|
||||
expect(results.length).toBeGreaterThan(0);
|
||||
// read_file 应该得到最高分(匹配 read 和 file)
|
||||
expect(results[0].name).toBe('read_file');
|
||||
});
|
||||
|
||||
it('逗号分隔的多词查询', () => {
|
||||
const results = searchTools('git,status', testTools);
|
||||
|
||||
expect(results.length).toBeGreaterThan(0);
|
||||
expect(results[0].name).toBe('git_status');
|
||||
});
|
||||
|
||||
it('中文逗号分隔', () => {
|
||||
const results = searchTools('读取,文件', testTools);
|
||||
|
||||
expect(results.length).toBeGreaterThan(0);
|
||||
expect(results[0].name).toBe('read_file');
|
||||
});
|
||||
|
||||
it('下划线分隔', () => {
|
||||
const results = searchTools('git_commit', testTools);
|
||||
|
||||
expect(results.length).toBeGreaterThan(0);
|
||||
expect(results[0].name).toBe('git_commit');
|
||||
});
|
||||
|
||||
it('连字符分隔', () => {
|
||||
const results = searchTools('read-write', testTools);
|
||||
|
||||
// 应该匹配到包含 read 或 write 关键词的工具
|
||||
expect(results.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('顿号分隔(中文)', () => {
|
||||
const results = searchTools('搜索、文件', testTools);
|
||||
|
||||
expect(results.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('评分规则', () => {
|
||||
it('名称精确匹配优先于包含匹配', () => {
|
||||
const tools: ToolMetadata[] = [
|
||||
createToolMetadata('bash', { keywords: ['shell'] }),
|
||||
createToolMetadata('bash_advanced', { keywords: ['advanced'] }), // 移除 bash 关键词,避免额外得分
|
||||
];
|
||||
|
||||
const results = searchTools('bash', tools);
|
||||
|
||||
expect(results[0].name).toBe('bash'); // 精确匹配得分更高
|
||||
});
|
||||
|
||||
it('关键词精确匹配优先于包含匹配', () => {
|
||||
const tools: ToolMetadata[] = [
|
||||
createToolMetadata('tool_a', { keywords: ['git'] }),
|
||||
createToolMetadata('tool_b', { keywords: ['github', 'gitlab'] }),
|
||||
];
|
||||
|
||||
const results = searchTools('git', tools);
|
||||
|
||||
expect(results[0].name).toBe('tool_a'); // 关键词精确匹配得分更高
|
||||
});
|
||||
|
||||
it('多词查询累加分数', () => {
|
||||
const results = searchTools('git commit save', testTools);
|
||||
|
||||
// git_commit 应该匹配 git, commit, save (在关键词中)
|
||||
expect(results[0].name).toBe('git_commit');
|
||||
});
|
||||
});
|
||||
|
||||
describe('结果限制', () => {
|
||||
it('默认返回最多 5 个结果', () => {
|
||||
const results = searchTools('file', testTools);
|
||||
|
||||
expect(results.length).toBeLessThanOrEqual(5);
|
||||
});
|
||||
|
||||
it('自定义限制结果数量', () => {
|
||||
const results = searchTools('file', testTools, 2);
|
||||
|
||||
expect(results.length).toBeLessThanOrEqual(2);
|
||||
});
|
||||
|
||||
it('limit 为 0 时返回空数组', () => {
|
||||
const results = searchTools('file', testTools, 0);
|
||||
|
||||
expect(results).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('只搜索延迟加载的工具', () => {
|
||||
it('跳过 deferLoading=false 的工具', () => {
|
||||
const tools: ToolMetadata[] = [
|
||||
createToolMetadata('core_tool', {
|
||||
keywords: ['test'],
|
||||
deferLoading: false,
|
||||
}),
|
||||
createToolMetadata('deferred_tool', {
|
||||
keywords: ['test'],
|
||||
deferLoading: true,
|
||||
}),
|
||||
];
|
||||
|
||||
const results = searchTools('test', tools);
|
||||
|
||||
expect(results).toHaveLength(1);
|
||||
expect(results[0].name).toBe('deferred_tool');
|
||||
});
|
||||
|
||||
it('全部为 deferLoading=false 时返回空数组', () => {
|
||||
const tools: ToolMetadata[] = [
|
||||
createToolMetadata('tool_a', { deferLoading: false }),
|
||||
createToolMetadata('tool_b', { deferLoading: false }),
|
||||
];
|
||||
|
||||
const results = searchTools('tool', tools);
|
||||
|
||||
expect(results).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('边界情况', () => {
|
||||
it('空查询返回空数组', () => {
|
||||
const results = searchTools('', testTools);
|
||||
expect(results).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('只有空格的查询返回空数组', () => {
|
||||
const results = searchTools(' ', testTools);
|
||||
expect(results).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('空工具列表返回空数组', () => {
|
||||
const results = searchTools('test', []);
|
||||
expect(results).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('无匹配时返回空数组', () => {
|
||||
const results = searchTools('xyznonexistent', testTools);
|
||||
expect(results).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('大小写不敏感', () => {
|
||||
const results = searchTools('BASH', testTools);
|
||||
|
||||
expect(results.length).toBeGreaterThan(0);
|
||||
expect(results[0].name).toBe('bash');
|
||||
});
|
||||
});
|
||||
|
||||
describe('搜索结果结构', () => {
|
||||
it('返回正确的结果结构', () => {
|
||||
const results = searchTools('bash', testTools);
|
||||
|
||||
expect(results[0]).toHaveProperty('name');
|
||||
expect(results[0]).toHaveProperty('description');
|
||||
expect(results[0]).toHaveProperty('category');
|
||||
expect(results[0]).toHaveProperty('score');
|
||||
});
|
||||
|
||||
it('结果按分数降序排列', () => {
|
||||
const results = searchTools('file', testTools);
|
||||
|
||||
for (let i = 1; i < results.length; i++) {
|
||||
expect(results[i - 1].score).toBeGreaterThanOrEqual(results[i].score);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,183 @@
|
||||
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',
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,205 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { skillTool, updateSkillDescription } from '../../../../src/tools/skill/skill.js';
|
||||
import { getSkillRegistry, resetSkillRegistry } from '../../../../src/skills/registry.js';
|
||||
import type { Skill } from '../../../../src/skills/types.js';
|
||||
|
||||
// Mock loader to prevent file system access
|
||||
vi.mock('../../../../src/skills/loader.js', () => ({
|
||||
skillLoader: {
|
||||
loadFromDirectory: vi.fn().mockResolvedValue([]),
|
||||
getUserSkillsDir: vi.fn().mockReturnValue('/mock/user/skills'),
|
||||
getProjectSkillsDir: vi.fn().mockReturnValue('/mock/project/skills'),
|
||||
},
|
||||
}));
|
||||
|
||||
// Mock builtin skills
|
||||
vi.mock('../../../../src/skills/builtin/index.js', () => ({
|
||||
builtinSkills: [
|
||||
{
|
||||
name: 'code-review',
|
||||
description: '代码审查',
|
||||
promptTemplate: '请审查以下代码:\n\n{{code}}\n\n重点:{{focus}}',
|
||||
parameters: {
|
||||
code: { type: 'string', required: true, description: '代码' },
|
||||
focus: { type: 'string', required: false, default: '代码质量', description: '审查重点' },
|
||||
},
|
||||
category: 'development',
|
||||
source: 'builtin',
|
||||
enabled: true,
|
||||
},
|
||||
{
|
||||
name: 'disabled-skill',
|
||||
description: '禁用的 Skill',
|
||||
promptTemplate: '不应该执行',
|
||||
source: 'builtin',
|
||||
enabled: false,
|
||||
},
|
||||
],
|
||||
}));
|
||||
|
||||
describe('skillTool - Skill 工具', () => {
|
||||
beforeEach(async () => {
|
||||
vi.clearAllMocks();
|
||||
resetSkillRegistry();
|
||||
// 初始化注册表
|
||||
const registry = getSkillRegistry();
|
||||
await registry.initialize();
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(skillTool.name).toBe('skill');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(skillTool.metadata.category).toBe('agent');
|
||||
expect(skillTool.metadata.keywords).toContain('skill');
|
||||
});
|
||||
|
||||
it('skill_name 参数是必须的', () => {
|
||||
expect(skillTool.parameters.skill_name.required).toBe(true);
|
||||
});
|
||||
|
||||
it('params 参数是可选的', () => {
|
||||
expect(skillTool.parameters.params.required).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功执行 Skill 并渲染模板', async () => {
|
||||
const result = await skillTool.execute({
|
||||
skill_name: 'code-review',
|
||||
params: {
|
||||
code: 'function add(a, b) { return a + b; }',
|
||||
focus: '性能',
|
||||
},
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('function add');
|
||||
expect(result.output).toContain('性能');
|
||||
expect(result.metadata?.skill).toBe('code-review');
|
||||
});
|
||||
|
||||
it('使用默认参数值', async () => {
|
||||
const result = await skillTool.execute({
|
||||
skill_name: 'code-review',
|
||||
params: {
|
||||
code: 'const x = 1;',
|
||||
},
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('代码质量'); // 默认值
|
||||
});
|
||||
|
||||
it('Skill 不存在时返回错误', async () => {
|
||||
const result = await skillTool.execute({
|
||||
skill_name: 'non-existent',
|
||||
params: {},
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('不存在');
|
||||
});
|
||||
|
||||
it('Skill 不存在时提供建议', async () => {
|
||||
const result = await skillTool.execute({
|
||||
skill_name: 'code-rev', // 类似 code-review
|
||||
params: {},
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('code-review'); // 建议
|
||||
});
|
||||
|
||||
it('缺少必需参数时返回错误', async () => {
|
||||
const result = await skillTool.execute({
|
||||
skill_name: 'code-review',
|
||||
params: {}, // 缺少 code 参数
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('缺少必需参数');
|
||||
});
|
||||
|
||||
it('禁用的 Skill 返回错误', async () => {
|
||||
const result = await skillTool.execute({
|
||||
skill_name: 'disabled-skill',
|
||||
params: {},
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('禁用');
|
||||
});
|
||||
|
||||
it('返回正确的 metadata', async () => {
|
||||
const result = await skillTool.execute({
|
||||
skill_name: 'code-review',
|
||||
params: { code: 'test' },
|
||||
});
|
||||
|
||||
expect(result.metadata?.skill).toBe('code-review');
|
||||
expect(result.metadata?.category).toBe('development');
|
||||
expect(result.metadata?.source).toBe('builtin');
|
||||
});
|
||||
});
|
||||
|
||||
describe('工具描述', () => {
|
||||
it('更新后描述包含可用 Skill 列表', () => {
|
||||
// beforeEach 中已初始化 registry,现在更新描述
|
||||
updateSkillDescription();
|
||||
|
||||
expect(skillTool.description).toContain('code-review');
|
||||
expect(skillTool.description).toContain('代码审查');
|
||||
});
|
||||
|
||||
it('更新后描述包含分类信息', () => {
|
||||
updateSkillDescription();
|
||||
expect(skillTool.description).toContain('[development]');
|
||||
});
|
||||
|
||||
it('更新后描述包含使用示例', () => {
|
||||
updateSkillDescription();
|
||||
expect(skillTool.description).toContain('使用示例');
|
||||
expect(skillTool.description).toContain('skill_name');
|
||||
});
|
||||
|
||||
it('禁用的 Skill 不在描述中', () => {
|
||||
updateSkillDescription();
|
||||
expect(skillTool.description).not.toContain('disabled-skill');
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateSkillDescription', () => {
|
||||
it('调用后描述被更新', async () => {
|
||||
updateSkillDescription();
|
||||
|
||||
// 描述应该包含当前注册的 Skill
|
||||
expect(skillTool.description).toContain('code-review');
|
||||
expect(typeof skillTool.description).toBe('string');
|
||||
});
|
||||
});
|
||||
|
||||
describe('边界情况', () => {
|
||||
it('params 为 undefined 时使用空对象', async () => {
|
||||
const result = await skillTool.execute({
|
||||
skill_name: 'code-review',
|
||||
// 不提供 params
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('缺少必需参数');
|
||||
});
|
||||
|
||||
it('skill_name 为空字符串时返回错误', async () => {
|
||||
const result = await skillTool.execute({
|
||||
skill_name: '',
|
||||
params: {},
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('不存在');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,185 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { skillSearchTool } from '../../../../src/tools/skill/skill_search.js';
|
||||
import { getSkillRegistry, resetSkillRegistry } from '../../../../src/skills/registry.js';
|
||||
|
||||
// Mock loader to prevent file system access
|
||||
vi.mock('../../../../src/skills/loader.js', () => ({
|
||||
skillLoader: {
|
||||
loadFromDirectory: vi.fn().mockResolvedValue([]),
|
||||
getUserSkillsDir: vi.fn().mockReturnValue('/mock/user/skills'),
|
||||
getProjectSkillsDir: vi.fn().mockReturnValue('/mock/project/skills'),
|
||||
},
|
||||
}));
|
||||
|
||||
// Mock builtin skills with various scenarios
|
||||
vi.mock('../../../../src/skills/builtin/index.js', () => ({
|
||||
builtinSkills: [
|
||||
{
|
||||
name: 'builtin-skill',
|
||||
description: '内置技能',
|
||||
promptTemplate: '内置',
|
||||
keywords: ['builtin'],
|
||||
category: 'development',
|
||||
source: 'builtin',
|
||||
enabled: true,
|
||||
},
|
||||
{
|
||||
name: 'user-skill',
|
||||
description: '用户定义的技能',
|
||||
promptTemplate: '用户',
|
||||
keywords: ['user', 'custom'],
|
||||
category: 'development',
|
||||
source: 'user', // 非 builtin 来源
|
||||
enabled: true,
|
||||
},
|
||||
{
|
||||
name: 'project-skill',
|
||||
description: '项目技能',
|
||||
promptTemplate: '项目',
|
||||
keywords: ['project'],
|
||||
source: 'project', // 非 builtin 来源,无分类
|
||||
enabled: true,
|
||||
// 无 category - 测试 uncategorized 分支
|
||||
},
|
||||
{
|
||||
name: 'skill-with-params',
|
||||
description: '带参数的技能',
|
||||
promptTemplate: '带参数',
|
||||
keywords: ['params'],
|
||||
category: 'tools',
|
||||
source: 'builtin',
|
||||
enabled: true,
|
||||
parameters: {
|
||||
file: { type: 'string', description: '文件路径', required: true },
|
||||
verbose: { type: 'boolean', description: '详细输出', required: false },
|
||||
},
|
||||
},
|
||||
],
|
||||
}));
|
||||
|
||||
describe('skillSearchTool - 扩展测试', () => {
|
||||
beforeEach(async () => {
|
||||
vi.clearAllMocks();
|
||||
resetSkillRegistry();
|
||||
const registry = getSkillRegistry();
|
||||
await registry.initialize();
|
||||
});
|
||||
|
||||
describe('list_all - 来源标记', () => {
|
||||
it('非 builtin 来源的 Skill 显示来源标记', async () => {
|
||||
const result = await skillSearchTool.execute({ list_all: true });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
// user-skill 和 project-skill 应该显示来源标记
|
||||
expect(result.output).toContain('[user]');
|
||||
expect(result.output).toContain('[project]');
|
||||
// builtin-skill 不应该显示 [builtin] 标记
|
||||
expect(result.output).not.toContain('[builtin]');
|
||||
});
|
||||
});
|
||||
|
||||
describe('list_all - 未分类的 Skills', () => {
|
||||
it('显示未分类的 Skills 在"其他"分组下', async () => {
|
||||
const result = await skillSearchTool.execute({ list_all: true });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
// project-skill 没有分类,应该出现在"其他"分组
|
||||
expect(result.output).toContain('## 其他');
|
||||
expect(result.output).toContain('project-skill');
|
||||
});
|
||||
});
|
||||
|
||||
describe('按分类筛选 - 参数显示', () => {
|
||||
it('显示 Skill 的参数列表', async () => {
|
||||
const result = await skillSearchTool.execute({ category: 'tools' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('skill-with-params');
|
||||
expect(result.output).toContain('参数:');
|
||||
expect(result.output).toContain('file');
|
||||
expect(result.output).toContain('verbose');
|
||||
});
|
||||
|
||||
it('无参数的 Skill 不显示参数行', async () => {
|
||||
const result = await skillSearchTool.execute({ category: 'development' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('builtin-skill');
|
||||
// 检查这个技能后面没有参数行
|
||||
const lines = result.output.split('\n');
|
||||
const builtinIndex = lines.findIndex((l) => l.includes('builtin-skill'));
|
||||
if (builtinIndex !== -1 && builtinIndex + 1 < lines.length) {
|
||||
// builtin-skill 没有参数,下一行不应该是"参数:"
|
||||
const nextLine = lines[builtinIndex + 1];
|
||||
expect(nextLine).not.toMatch(/^\s*参数:/);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('搜索结果 - 分类显示', () => {
|
||||
it('搜索结果包含分类信息', async () => {
|
||||
const result = await skillSearchTool.execute({ query: 'builtin' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('分类:');
|
||||
expect(result.output).toContain('development');
|
||||
});
|
||||
|
||||
it('无分类的 Skill 搜索结果不显示分类', async () => {
|
||||
const result = await skillSearchTool.execute({ query: 'project' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('project-skill');
|
||||
// project-skill 没有分类
|
||||
const lines = result.output.split('\n');
|
||||
const projectLine = lines.find((l) => l.includes('project-skill'));
|
||||
expect(projectLine).toBeDefined();
|
||||
// 该行不应包含"分类:"
|
||||
expect(projectLine).not.toContain('分类:');
|
||||
});
|
||||
});
|
||||
|
||||
describe('空注册表', () => {
|
||||
it('list_all 时注册表为空显示提示', async () => {
|
||||
// 重置并使用空注册表
|
||||
resetSkillRegistry();
|
||||
|
||||
// 需要重新 mock 为空
|
||||
vi.doMock('../../../../src/skills/builtin/index.js', () => ({
|
||||
builtinSkills: [],
|
||||
}));
|
||||
|
||||
// 这个测试比较复杂,因为 mock 已经在模块加载时确定
|
||||
// 跳过这个测试,因为需要更复杂的设置
|
||||
});
|
||||
});
|
||||
|
||||
describe('元数据', () => {
|
||||
it('list_all 返回统计元数据', async () => {
|
||||
const result = await skillSearchTool.execute({ list_all: true });
|
||||
|
||||
expect(result.metadata?.stats).toBeDefined();
|
||||
expect(result.metadata?.stats.total).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('按分类筛选返回分类和计数元数据', async () => {
|
||||
const result = await skillSearchTool.execute({ category: 'development' });
|
||||
|
||||
expect(result.metadata?.category).toBe('development');
|
||||
expect(result.metadata?.count).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('搜索返回查询和结果数元数据', async () => {
|
||||
const result = await skillSearchTool.execute({ query: 'skill' });
|
||||
|
||||
expect(result.metadata?.query).toBe('skill');
|
||||
expect(result.metadata?.resultCount).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('概览返回统计元数据', async () => {
|
||||
const result = await skillSearchTool.execute({});
|
||||
|
||||
expect(result.metadata?.stats).toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,122 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { skillSearchTool } from '../../../../src/tools/skill/skill_search.js';
|
||||
import { getSkillRegistry, resetSkillRegistry } from '../../../../src/skills/registry.js';
|
||||
|
||||
// Mock loader to prevent file system access
|
||||
vi.mock('../../../../src/skills/loader.js', () => ({
|
||||
skillLoader: {
|
||||
loadFromDirectory: vi.fn().mockResolvedValue([]),
|
||||
getUserSkillsDir: vi.fn().mockReturnValue('/mock/user/skills'),
|
||||
getProjectSkillsDir: vi.fn().mockReturnValue('/mock/project/skills'),
|
||||
},
|
||||
}));
|
||||
|
||||
// Mock builtin skills
|
||||
vi.mock('../../../../src/skills/builtin/index.js', () => ({
|
||||
builtinSkills: [
|
||||
{
|
||||
name: 'code-review',
|
||||
description: '代码审查',
|
||||
promptTemplate: '审查代码',
|
||||
keywords: ['review', 'quality'],
|
||||
category: 'development',
|
||||
source: 'builtin',
|
||||
enabled: true,
|
||||
},
|
||||
{
|
||||
name: 'generate-tests',
|
||||
description: '生成测试',
|
||||
promptTemplate: '生成测试',
|
||||
keywords: ['test', 'unit'],
|
||||
category: 'testing',
|
||||
source: 'builtin',
|
||||
enabled: true,
|
||||
},
|
||||
{
|
||||
name: 'explain-code',
|
||||
description: '解释代码功能',
|
||||
promptTemplate: '解释代码',
|
||||
category: 'development',
|
||||
source: 'builtin',
|
||||
enabled: true,
|
||||
},
|
||||
],
|
||||
}));
|
||||
|
||||
describe('skillSearchTool - Skill 搜索工具', () => {
|
||||
beforeEach(async () => {
|
||||
vi.clearAllMocks();
|
||||
resetSkillRegistry();
|
||||
const registry = getSkillRegistry();
|
||||
await registry.initialize();
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(skillSearchTool.name).toBe('skill_search');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(skillSearchTool.metadata.category).toBe('agent');
|
||||
expect(skillSearchTool.metadata.keywords).toContain('search');
|
||||
});
|
||||
|
||||
it('所有参数都是可选的', () => {
|
||||
expect(skillSearchTool.parameters.query.required).toBe(false);
|
||||
expect(skillSearchTool.parameters.category.required).toBe(false);
|
||||
expect(skillSearchTool.parameters.list_all.required).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('list_all 列出所有 Skills', async () => {
|
||||
const result = await skillSearchTool.execute({ list_all: true });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('code-review');
|
||||
expect(result.output).toContain('generate-tests');
|
||||
expect(result.output).toContain('explain-code');
|
||||
expect(result.output).toContain('development');
|
||||
expect(result.output).toContain('testing');
|
||||
});
|
||||
|
||||
it('按关键词搜索', async () => {
|
||||
const result = await skillSearchTool.execute({ query: 'review' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('code-review');
|
||||
expect(result.metadata?.query).toBe('review');
|
||||
});
|
||||
|
||||
it('按分类筛选', async () => {
|
||||
const result = await skillSearchTool.execute({ category: 'testing' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('generate-tests');
|
||||
expect(result.output).not.toContain('code-review');
|
||||
});
|
||||
|
||||
it('分类不存在时显示可用分类', async () => {
|
||||
const result = await skillSearchTool.execute({ category: 'non-existent' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('没有 Skills');
|
||||
expect(result.output).toContain('可用分类');
|
||||
});
|
||||
|
||||
it('搜索无结果时提示', async () => {
|
||||
const result = await skillSearchTool.execute({ query: 'xxxxxxxxx' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('没有找到');
|
||||
});
|
||||
|
||||
it('无参数时显示概览', async () => {
|
||||
const result = await skillSearchTool.execute({});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('概览');
|
||||
expect(result.output).toContain('使用方法');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,160 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { agentOutputTool } from '../../../../src/tools/task/agent_output.js';
|
||||
import { getAgentManager, resetAgentManager } from '../../../../src/agent/manager.js';
|
||||
|
||||
// Mock AgentExecutor - 使用延迟执行让测试更可控
|
||||
vi.mock('../../../../src/agent/executor.js', () => ({
|
||||
AgentExecutor: vi.fn().mockImplementation(() => ({
|
||||
execute: vi.fn().mockImplementation(async () => {
|
||||
await new Promise((r) => setTimeout(r, 50));
|
||||
return {
|
||||
success: true,
|
||||
text: '任务完成结果',
|
||||
steps: 3,
|
||||
sessionId: 'test-session',
|
||||
};
|
||||
}),
|
||||
})),
|
||||
}));
|
||||
|
||||
describe('agentOutputTool - Agent 输出工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
resetAgentManager();
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(agentOutputTool.name).toBe('agent_output');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(agentOutputTool.metadata.category).toBe('agent');
|
||||
expect(agentOutputTool.metadata.keywords).toContain('agent');
|
||||
expect(agentOutputTool.metadata.keywords).toContain('output');
|
||||
expect(agentOutputTool.metadata.keywords).toContain('background');
|
||||
});
|
||||
|
||||
it('agent_id 参数是必须的', () => {
|
||||
expect(agentOutputTool.parameters.agent_id.required).toBe(true);
|
||||
});
|
||||
|
||||
it('block 和 timeout 参数是可选的', () => {
|
||||
expect(agentOutputTool.parameters.block.required).toBe(false);
|
||||
expect(agentOutputTool.parameters.timeout.required).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('Agent 不存在时返回错误', async () => {
|
||||
const result = await agentOutputTool.execute({
|
||||
agent_id: 'non-existent-id',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('不存在');
|
||||
});
|
||||
|
||||
it('返回 Agent 的状态', async () => {
|
||||
const manager = getAgentManager();
|
||||
const agentId = await manager.runInBackground(
|
||||
{
|
||||
name: 'test-agent',
|
||||
description: '测试',
|
||||
mode: 'subagent',
|
||||
},
|
||||
'测试任务',
|
||||
'执行测试',
|
||||
{
|
||||
provider: 'anthropic',
|
||||
apiKey: 'test',
|
||||
model: 'test',
|
||||
maxTokens: 1000,
|
||||
systemPrompt: 'test',
|
||||
},
|
||||
{} as any,
|
||||
{ parentSessionId: 'parent', workdir: '/test' }
|
||||
);
|
||||
|
||||
// 立即查询(不阻塞)
|
||||
const result = await agentOutputTool.execute({
|
||||
agent_id: agentId,
|
||||
block: false,
|
||||
});
|
||||
|
||||
// 应该成功返回状态(可能是 running 或 completed)
|
||||
expect(result.output).toBeDefined();
|
||||
expect(result.metadata?.agentId).toBe(agentId);
|
||||
});
|
||||
|
||||
it('阻塞等待后返回结果', async () => {
|
||||
const manager = getAgentManager();
|
||||
const agentId = await manager.runInBackground(
|
||||
{
|
||||
name: 'test-agent',
|
||||
description: '测试',
|
||||
mode: 'subagent',
|
||||
},
|
||||
'测试任务',
|
||||
'执行测试',
|
||||
{
|
||||
provider: 'anthropic',
|
||||
apiKey: 'test',
|
||||
model: 'test',
|
||||
maxTokens: 1000,
|
||||
systemPrompt: 'test',
|
||||
},
|
||||
{} as any,
|
||||
{ parentSessionId: 'parent', workdir: '/test' }
|
||||
);
|
||||
|
||||
// 使用阻塞模式等待
|
||||
const result = await agentOutputTool.execute({
|
||||
agent_id: agentId,
|
||||
block: true,
|
||||
timeout: 5,
|
||||
});
|
||||
|
||||
// 等待后应该有确定的状态
|
||||
expect(result.metadata?.status).toBeDefined();
|
||||
expect(['running', 'completed', 'failed']).toContain(result.metadata?.status);
|
||||
});
|
||||
|
||||
it('返回包含正确字段的输出', async () => {
|
||||
const manager = getAgentManager();
|
||||
const agentId = await manager.runInBackground(
|
||||
{
|
||||
name: 'test-agent',
|
||||
description: '测试',
|
||||
mode: 'subagent',
|
||||
},
|
||||
'测试任务',
|
||||
'执行测试',
|
||||
{
|
||||
provider: 'anthropic',
|
||||
apiKey: 'test',
|
||||
model: 'test',
|
||||
maxTokens: 1000,
|
||||
systemPrompt: 'test',
|
||||
},
|
||||
{} as any,
|
||||
{ parentSessionId: 'parent', workdir: '/test' }
|
||||
);
|
||||
|
||||
// 等待完成
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
|
||||
const result = await agentOutputTool.execute({
|
||||
agent_id: agentId,
|
||||
block: false,
|
||||
});
|
||||
|
||||
// 检查返回了有效结果
|
||||
expect(result.output).toBeDefined();
|
||||
expect(result.metadata?.agentId).toBe(agentId);
|
||||
expect(result.metadata?.agentName).toBe('test-agent');
|
||||
// 状态应该是完成或失败(由于 mock,可能会失败)
|
||||
expect(['completed', 'failed']).toContain(result.metadata?.status);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,380 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// 可变状态
|
||||
let mockExecuteResult = {
|
||||
success: true,
|
||||
text: '任务完成',
|
||||
steps: 3,
|
||||
};
|
||||
let mockVisionConfig: { provider: string; apiKey: string; model: string; baseUrl?: string } | null = null;
|
||||
let mockRunInBackgroundResult = 'agent-123';
|
||||
|
||||
// Mock loadVisionConfig
|
||||
vi.mock('../../../../src/utils/config.js', () => ({
|
||||
loadVisionConfig: () => mockVisionConfig,
|
||||
}));
|
||||
|
||||
// Mock agent manager
|
||||
vi.mock('../../../../src/agent/manager.js', () => ({
|
||||
getAgentManager: () => ({
|
||||
runInBackground: vi.fn(async () => mockRunInBackgroundResult),
|
||||
}),
|
||||
}));
|
||||
|
||||
// Mock agent registry 和 AgentExecutor
|
||||
vi.mock('../../../../src/agent/index.js', () => ({
|
||||
agentRegistry: {
|
||||
listSubagents: vi.fn(() => [
|
||||
{ name: 'explore', description: '代码探索', mode: 'subagent' },
|
||||
{ name: 'vision', description: '图片分析', mode: 'subagent' },
|
||||
]),
|
||||
get: vi.fn(),
|
||||
},
|
||||
AgentExecutor: class {
|
||||
execute() {
|
||||
return Promise.resolve(mockExecuteResult);
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
// Mock tool registry
|
||||
vi.mock('../../../../src/tools/registry.js', () => ({
|
||||
toolRegistry: {},
|
||||
}));
|
||||
|
||||
// Mock session manager
|
||||
vi.mock('../../../../src/session/index.js', () => ({
|
||||
SessionManager: vi.fn(),
|
||||
}));
|
||||
|
||||
import { taskTool, initTaskContext, getTaskContext } from '../../../../src/tools/task/task.js';
|
||||
import { agentRegistry } from '../../../../src/agent/index.js';
|
||||
|
||||
describe('taskTool - 扩展测试', () => {
|
||||
const createMockSession = () => ({
|
||||
getSessionId: vi.fn(() => 'parent-session'),
|
||||
createChildSession: vi.fn(() => ({
|
||||
id: 'child-session',
|
||||
messages: [],
|
||||
})),
|
||||
saveChildSession: vi.fn(),
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
initTaskContext(null as any, null as any);
|
||||
mockExecuteResult = {
|
||||
success: true,
|
||||
text: '任务完成',
|
||||
steps: 3,
|
||||
};
|
||||
mockVisionConfig = null;
|
||||
mockRunInBackgroundResult = 'agent-123';
|
||||
});
|
||||
|
||||
describe('getTaskContext', () => {
|
||||
it('返回上下文', () => {
|
||||
initTaskContext({ model: 'test' } as any, { sessionManager: true } as any);
|
||||
expect(getTaskContext()).not.toBeNull();
|
||||
});
|
||||
|
||||
it('初始化 null 时返回包含 null 的对象', () => {
|
||||
initTaskContext(null as any, null as any);
|
||||
const ctx = getTaskContext();
|
||||
expect(ctx?.baseConfig).toBeNull();
|
||||
expect(ctx?.sessionManager).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Vision Agent 处理', () => {
|
||||
it('Vision Agent 无配置时返回错误', async () => {
|
||||
mockVisionConfig = null;
|
||||
const mockSession = createMockSession();
|
||||
initTaskContext({ model: 'test' } as any, mockSession as any);
|
||||
|
||||
vi.mocked(agentRegistry.get).mockReturnValue({
|
||||
name: 'vision',
|
||||
description: '图片分析 Agent',
|
||||
mode: 'subagent',
|
||||
prompt: '你是视觉助手',
|
||||
});
|
||||
|
||||
const result = await taskTool.execute({
|
||||
description: 'analyze image',
|
||||
prompt: '分析图片',
|
||||
subagent_type: 'vision',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('Vision Agent 需要配置');
|
||||
});
|
||||
|
||||
it('Vision Agent 有配置时成功执行', async () => {
|
||||
mockVisionConfig = {
|
||||
provider: 'openai',
|
||||
apiKey: 'vision-key',
|
||||
model: 'gpt-4o',
|
||||
};
|
||||
const mockSession = createMockSession();
|
||||
initTaskContext({ model: 'test' } as any, mockSession as any);
|
||||
|
||||
vi.mocked(agentRegistry.get).mockReturnValue({
|
||||
name: 'vision',
|
||||
description: '图片分析 Agent',
|
||||
mode: 'subagent',
|
||||
prompt: '你是视觉助手',
|
||||
});
|
||||
|
||||
const result = await taskTool.execute({
|
||||
description: 'analyze image',
|
||||
prompt: '分析图片',
|
||||
subagent_type: 'vision',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('模型选择', () => {
|
||||
it('无效模型选择返回错误', async () => {
|
||||
const mockSession = createMockSession();
|
||||
initTaskContext({ model: 'test' } as any, mockSession as any);
|
||||
|
||||
vi.mocked(agentRegistry.get).mockReturnValue({
|
||||
name: 'explore',
|
||||
description: '探索 Agent',
|
||||
mode: 'subagent',
|
||||
prompt: '你是探索助手',
|
||||
});
|
||||
|
||||
const result = await taskTool.execute({
|
||||
description: 'test',
|
||||
prompt: 'test',
|
||||
subagent_type: 'explore',
|
||||
model: 'invalid-model',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('无效的模型选择');
|
||||
expect(result.error).toContain('sonnet, opus, haiku');
|
||||
});
|
||||
|
||||
it('sonnet 模型选择有效', async () => {
|
||||
const mockSession = createMockSession();
|
||||
initTaskContext({ model: 'original-model' } as any, mockSession as any);
|
||||
|
||||
vi.mocked(agentRegistry.get).mockReturnValue({
|
||||
name: 'explore',
|
||||
description: '探索 Agent',
|
||||
mode: 'subagent',
|
||||
prompt: '你是探索助手',
|
||||
});
|
||||
|
||||
const result = await taskTool.execute({
|
||||
description: 'test',
|
||||
prompt: 'test',
|
||||
subagent_type: 'explore',
|
||||
model: 'sonnet',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('opus 模型选择有效', async () => {
|
||||
const mockSession = createMockSession();
|
||||
initTaskContext({ model: 'original-model' } as any, mockSession as any);
|
||||
|
||||
vi.mocked(agentRegistry.get).mockReturnValue({
|
||||
name: 'explore',
|
||||
description: '探索 Agent',
|
||||
mode: 'subagent',
|
||||
prompt: '你是探索助手',
|
||||
});
|
||||
|
||||
const result = await taskTool.execute({
|
||||
description: 'test',
|
||||
prompt: 'test',
|
||||
subagent_type: 'explore',
|
||||
model: 'opus',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('haiku 模型选择有效', async () => {
|
||||
const mockSession = createMockSession();
|
||||
initTaskContext({ model: 'original-model' } as any, mockSession as any);
|
||||
|
||||
vi.mocked(agentRegistry.get).mockReturnValue({
|
||||
name: 'explore',
|
||||
description: '探索 Agent',
|
||||
mode: 'subagent',
|
||||
prompt: '你是探索助手',
|
||||
});
|
||||
|
||||
const result = await taskTool.execute({
|
||||
description: 'test',
|
||||
prompt: 'test',
|
||||
subagent_type: 'explore',
|
||||
model: 'haiku',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('后台运行模式', () => {
|
||||
it('后台运行返回 agentId', async () => {
|
||||
const mockSession = createMockSession();
|
||||
initTaskContext({ model: 'test' } as any, mockSession as any);
|
||||
|
||||
vi.mocked(agentRegistry.get).mockReturnValue({
|
||||
name: 'explore',
|
||||
description: '探索 Agent',
|
||||
mode: 'subagent',
|
||||
prompt: '你是探索助手',
|
||||
});
|
||||
|
||||
const result = await taskTool.execute({
|
||||
description: 'background task',
|
||||
prompt: 'do something',
|
||||
subagent_type: 'explore',
|
||||
run_in_background: true,
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('agent-123');
|
||||
expect(result.output).toContain('后台启动');
|
||||
expect(result.metadata?.mode).toBe('background');
|
||||
});
|
||||
|
||||
it('sessionId 不存在时使用 standalone', async () => {
|
||||
const mockSession = {
|
||||
getSessionId: vi.fn(() => null),
|
||||
createChildSession: vi.fn(() => ({
|
||||
id: 'child-session',
|
||||
messages: [],
|
||||
})),
|
||||
saveChildSession: vi.fn(),
|
||||
};
|
||||
initTaskContext({ model: 'test' } as any, mockSession as any);
|
||||
|
||||
vi.mocked(agentRegistry.get).mockReturnValue({
|
||||
name: 'explore',
|
||||
description: '探索 Agent',
|
||||
mode: 'subagent',
|
||||
prompt: '你是探索助手',
|
||||
});
|
||||
|
||||
const result = await taskTool.execute({
|
||||
description: 'background task',
|
||||
prompt: 'do something',
|
||||
subagent_type: 'explore',
|
||||
run_in_background: true,
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('同步执行模式元数据', () => {
|
||||
it('同步模式返回正确 metadata', async () => {
|
||||
const mockSession = createMockSession();
|
||||
initTaskContext({ model: 'test' } as any, mockSession as any);
|
||||
|
||||
vi.mocked(agentRegistry.get).mockReturnValue({
|
||||
name: 'explore',
|
||||
description: '探索 Agent',
|
||||
mode: 'subagent',
|
||||
prompt: '你是探索助手',
|
||||
});
|
||||
|
||||
const result = await taskTool.execute({
|
||||
description: 'test',
|
||||
prompt: 'test',
|
||||
subagent_type: 'explore',
|
||||
});
|
||||
|
||||
expect(result.metadata?.mode).toBe('sync');
|
||||
});
|
||||
|
||||
it('失败时也返回 metadata', async () => {
|
||||
const mockSession = createMockSession();
|
||||
initTaskContext({ model: 'test' } as any, mockSession as any);
|
||||
|
||||
vi.mocked(agentRegistry.get).mockReturnValue({
|
||||
name: 'explore',
|
||||
description: '探索 Agent',
|
||||
mode: 'subagent',
|
||||
prompt: '你是探索助手',
|
||||
});
|
||||
|
||||
mockExecuteResult = {
|
||||
success: false,
|
||||
text: '',
|
||||
steps: 1,
|
||||
};
|
||||
|
||||
const result = await taskTool.execute({
|
||||
description: 'test',
|
||||
prompt: 'test',
|
||||
subagent_type: 'explore',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.metadata).toBeDefined();
|
||||
expect(result.metadata?.mode).toBe('sync');
|
||||
});
|
||||
|
||||
it('失败时没有 error 消息时使用默认消息', async () => {
|
||||
const mockSession = createMockSession();
|
||||
initTaskContext({ model: 'test' } as any, mockSession as any);
|
||||
|
||||
vi.mocked(agentRegistry.get).mockReturnValue({
|
||||
name: 'explore',
|
||||
description: '探索 Agent',
|
||||
mode: 'subagent',
|
||||
prompt: '你是探索助手',
|
||||
});
|
||||
|
||||
mockExecuteResult = {
|
||||
success: false,
|
||||
text: '',
|
||||
steps: 1,
|
||||
// 没有 error 字段
|
||||
} as any;
|
||||
|
||||
const result = await taskTool.execute({
|
||||
description: 'test',
|
||||
prompt: 'test',
|
||||
subagent_type: 'explore',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBe('子任务执行失败');
|
||||
});
|
||||
});
|
||||
|
||||
describe('图片传递', () => {
|
||||
it('同步模式传递图片', async () => {
|
||||
const mockSession = createMockSession();
|
||||
initTaskContext({ model: 'test' } as any, mockSession as any);
|
||||
|
||||
vi.mocked(agentRegistry.get).mockReturnValue({
|
||||
name: 'explore',
|
||||
description: '探索 Agent',
|
||||
mode: 'subagent',
|
||||
prompt: '你是探索助手',
|
||||
});
|
||||
|
||||
const result = await taskTool.execute({
|
||||
description: 'analyze images',
|
||||
prompt: 'describe these',
|
||||
subagent_type: 'explore',
|
||||
images: [{ data: 'base64data', mimeType: 'image/png' }],
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,307 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// 使用可变的引用对象来绕过 hoisting 问题
|
||||
const mockState = {
|
||||
execute: vi.fn(),
|
||||
};
|
||||
|
||||
// Mock agent registry 和 AgentExecutor
|
||||
vi.mock('../../../../src/agent/index.js', () => {
|
||||
// 在 mock 工厂内部定义类
|
||||
return {
|
||||
agentRegistry: {
|
||||
listSubagents: vi.fn(() => [
|
||||
{ name: 'explore', description: '代码探索', mode: 'subagent' },
|
||||
{ name: 'code-reviewer', description: '代码审查', mode: 'subagent' },
|
||||
]),
|
||||
get: vi.fn(),
|
||||
},
|
||||
AgentExecutor: class {
|
||||
execute(...args: any[]) {
|
||||
return mockState.execute(...args);
|
||||
}
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
// Mock tool registry
|
||||
vi.mock('../../../../src/tools/registry.js', () => ({
|
||||
toolRegistry: {},
|
||||
}));
|
||||
|
||||
// Mock session manager
|
||||
vi.mock('../../../../src/session/index.js', () => ({
|
||||
SessionManager: vi.fn(),
|
||||
}));
|
||||
|
||||
import { taskTool, initTaskContext, updateTaskDescription } from '../../../../src/tools/task/task.js';
|
||||
import { agentRegistry } from '../../../../src/agent/index.js';
|
||||
|
||||
describe('taskTool - Task 工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
// 重置上下文为 null
|
||||
initTaskContext(null as any, null as any);
|
||||
// 重置 mock
|
||||
mockState.execute.mockResolvedValue({
|
||||
success: true,
|
||||
text: '任务完成',
|
||||
steps: 3,
|
||||
});
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(taskTool.name).toBe('task');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(taskTool.metadata.category).toBe('agent');
|
||||
expect(taskTool.metadata.keywords).toContain('task');
|
||||
expect(taskTool.metadata.keywords).toContain('subagent');
|
||||
});
|
||||
|
||||
it('定义了必需的参数', () => {
|
||||
expect(taskTool.parameters.description.required).toBe(true);
|
||||
expect(taskTool.parameters.prompt.required).toBe(true);
|
||||
expect(taskTool.parameters.subagent_type.required).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('initTaskContext - 初始化上下文', () => {
|
||||
it('设置上下文不报错', () => {
|
||||
const mockConfig = { model: 'test' };
|
||||
const mockSession = {
|
||||
getSessionId: vi.fn(() => 'session-id'),
|
||||
createChildSession: vi.fn(() => ({ id: 'child', messages: [] })),
|
||||
saveChildSession: vi.fn(),
|
||||
};
|
||||
|
||||
expect(() => initTaskContext(mockConfig as any, mockSession as any)).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateTaskDescription - 更新描述', () => {
|
||||
it('更新工具描述', () => {
|
||||
vi.mocked(agentRegistry.listSubagents).mockReturnValue([
|
||||
{ name: 'explore', description: '代码探索', mode: 'subagent' },
|
||||
{ name: 'code-reviewer', description: '代码审查', mode: 'subagent' },
|
||||
]);
|
||||
|
||||
updateTaskDescription();
|
||||
|
||||
expect(taskTool.description).toContain('explore');
|
||||
expect(taskTool.description).toContain('code-reviewer');
|
||||
});
|
||||
|
||||
it('无子 Agent 时显示提示', () => {
|
||||
vi.mocked(agentRegistry.listSubagents).mockReturnValue([]);
|
||||
|
||||
updateTaskDescription();
|
||||
|
||||
expect(taskTool.description).toContain('没有可用');
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('未初始化上下文时返回错误', async () => {
|
||||
// 确保上下文为 null
|
||||
initTaskContext(null as any, null as any);
|
||||
vi.mocked(agentRegistry.get).mockReturnValue(undefined);
|
||||
|
||||
const result = await taskTool.execute({
|
||||
description: 'test task',
|
||||
prompt: 'do something',
|
||||
subagent_type: 'explore',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
// 可能是未初始化或未找到 Agent
|
||||
expect(result.error).toBeDefined();
|
||||
});
|
||||
|
||||
it('成功执行子任务', async () => {
|
||||
const mockSession = {
|
||||
getSessionId: vi.fn(() => 'parent-session'),
|
||||
createChildSession: vi.fn(() => ({
|
||||
id: 'child-session',
|
||||
messages: [],
|
||||
})),
|
||||
saveChildSession: vi.fn(),
|
||||
};
|
||||
|
||||
initTaskContext({ model: 'test' } as any, mockSession as any);
|
||||
|
||||
vi.mocked(agentRegistry.get).mockReturnValue({
|
||||
name: 'explore',
|
||||
description: '探索 Agent',
|
||||
mode: 'subagent',
|
||||
prompt: '你是探索助手',
|
||||
});
|
||||
|
||||
const result = await taskTool.execute({
|
||||
description: 'search code',
|
||||
prompt: 'find all API routes',
|
||||
subagent_type: 'explore',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('任务完成');
|
||||
});
|
||||
|
||||
it('未找到 Agent 时返回错误', async () => {
|
||||
const mockSession = {
|
||||
getSessionId: vi.fn(() => 'parent-session'),
|
||||
createChildSession: vi.fn(() => ({ id: 'child', messages: [] })),
|
||||
saveChildSession: vi.fn(),
|
||||
};
|
||||
|
||||
initTaskContext({ model: 'test' } as any, mockSession as any);
|
||||
|
||||
vi.mocked(agentRegistry.get).mockReturnValue(undefined);
|
||||
vi.mocked(agentRegistry.listSubagents).mockReturnValue([
|
||||
{ name: 'explore', description: '探索', mode: 'subagent' },
|
||||
]);
|
||||
|
||||
const result = await taskTool.execute({
|
||||
description: 'test',
|
||||
prompt: 'test',
|
||||
subagent_type: 'nonexistent',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('未找到 Agent');
|
||||
});
|
||||
|
||||
it('primary 模式 Agent 不能作为子任务', async () => {
|
||||
const mockSession = {
|
||||
getSessionId: vi.fn(() => 'parent-session'),
|
||||
createChildSession: vi.fn(() => ({ id: 'child', messages: [] })),
|
||||
saveChildSession: vi.fn(),
|
||||
};
|
||||
|
||||
initTaskContext({ model: 'test' } as any, mockSession as any);
|
||||
|
||||
vi.mocked(agentRegistry.get).mockReturnValue({
|
||||
name: 'primary-agent',
|
||||
description: '主 Agent',
|
||||
mode: 'primary',
|
||||
prompt: '你是主助手',
|
||||
});
|
||||
|
||||
const result = await taskTool.execute({
|
||||
description: 'test',
|
||||
prompt: 'test',
|
||||
subagent_type: 'primary-agent',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('primary 模式');
|
||||
});
|
||||
|
||||
it('子任务失败时返回错误', async () => {
|
||||
const mockSession = {
|
||||
getSessionId: vi.fn(() => 'parent-session'),
|
||||
createChildSession: vi.fn(() => ({ id: 'child', messages: [] })),
|
||||
saveChildSession: vi.fn(),
|
||||
};
|
||||
|
||||
initTaskContext({ model: 'test' } as any, mockSession as any);
|
||||
|
||||
vi.mocked(agentRegistry.get).mockReturnValue({
|
||||
name: 'explore',
|
||||
description: '探索 Agent',
|
||||
mode: 'subagent',
|
||||
prompt: '你是探索助手',
|
||||
});
|
||||
|
||||
mockState.execute.mockResolvedValue({
|
||||
success: false,
|
||||
text: '',
|
||||
error: '执行失败',
|
||||
steps: 1,
|
||||
});
|
||||
|
||||
const result = await taskTool.execute({
|
||||
description: 'test',
|
||||
prompt: 'test',
|
||||
subagent_type: 'explore',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('执行失败');
|
||||
});
|
||||
|
||||
it('返回元数据', async () => {
|
||||
const mockSession = {
|
||||
getSessionId: vi.fn(() => 'parent-session'),
|
||||
createChildSession: vi.fn(() => ({
|
||||
id: 'child-session-123',
|
||||
messages: [],
|
||||
})),
|
||||
saveChildSession: vi.fn(),
|
||||
};
|
||||
|
||||
initTaskContext({ model: 'test' } as any, mockSession as any);
|
||||
|
||||
vi.mocked(agentRegistry.get).mockReturnValue({
|
||||
name: 'explore',
|
||||
description: '探索 Agent',
|
||||
mode: 'subagent',
|
||||
prompt: '你是探索助手',
|
||||
});
|
||||
|
||||
mockState.execute.mockResolvedValue({
|
||||
success: true,
|
||||
text: '完成',
|
||||
steps: 5,
|
||||
});
|
||||
|
||||
const result = await taskTool.execute({
|
||||
description: 'test',
|
||||
prompt: 'test',
|
||||
subagent_type: 'explore',
|
||||
});
|
||||
|
||||
expect(result.metadata).toBeDefined();
|
||||
expect(result.metadata?.agent).toBe('explore');
|
||||
expect(result.metadata?.sessionId).toBe('child-session-123');
|
||||
expect(result.metadata?.steps).toBe(5);
|
||||
});
|
||||
|
||||
it('保存子会话', async () => {
|
||||
const saveChildSession = vi.fn();
|
||||
const mockSession = {
|
||||
getSessionId: vi.fn(() => 'parent-session'),
|
||||
createChildSession: vi.fn(() => ({
|
||||
id: 'child-session',
|
||||
messages: [],
|
||||
})),
|
||||
saveChildSession,
|
||||
};
|
||||
|
||||
initTaskContext({ model: 'test' } as any, mockSession as any);
|
||||
|
||||
vi.mocked(agentRegistry.get).mockReturnValue({
|
||||
name: 'explore',
|
||||
description: '探索 Agent',
|
||||
mode: 'subagent',
|
||||
prompt: '你是探索助手',
|
||||
});
|
||||
|
||||
mockState.execute.mockResolvedValue({
|
||||
success: true,
|
||||
text: '任务结果',
|
||||
steps: 2,
|
||||
});
|
||||
|
||||
await taskTool.execute({
|
||||
description: 'test',
|
||||
prompt: 'test prompt',
|
||||
subagent_type: 'explore',
|
||||
});
|
||||
|
||||
expect(saveChildSession).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,333 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import type { Todo, TodoStatus } from '../../../src/session/types.js';
|
||||
import type { SessionManager } from '../../../src/session/index.js';
|
||||
|
||||
// 需要单独导入 todoManager 因为它是单例
|
||||
// 每个测试创建新的实例来避免状态污染
|
||||
|
||||
// 简单的 TodoManager 测试类(复制逻辑以测试)
|
||||
class TestTodoManager {
|
||||
private sessionManager: SessionManager | null = null;
|
||||
|
||||
setSessionManager(manager: SessionManager): void {
|
||||
this.sessionManager = manager;
|
||||
}
|
||||
|
||||
getTodos(): Todo[] {
|
||||
if (!this.sessionManager) {
|
||||
return [];
|
||||
}
|
||||
return this.sessionManager.getTodos();
|
||||
}
|
||||
|
||||
async setTodos(todos: Todo[]): Promise<void> {
|
||||
if (!this.sessionManager) {
|
||||
return;
|
||||
}
|
||||
await this.sessionManager.setTodos(todos);
|
||||
}
|
||||
|
||||
async addTodo(content: string, status: TodoStatus = 'pending'): Promise<Todo> {
|
||||
const todos = this.getTodos();
|
||||
const now = new Date().toISOString();
|
||||
const newTodo: Todo = {
|
||||
id: this.generateId(),
|
||||
content,
|
||||
status,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
};
|
||||
todos.push(newTodo);
|
||||
await this.setTodos(todos);
|
||||
return newTodo;
|
||||
}
|
||||
|
||||
async updateTodoStatus(id: string, status: TodoStatus): Promise<boolean> {
|
||||
const todos = this.getTodos();
|
||||
const todo = todos.find((t) => t.id === id);
|
||||
if (!todo) return false;
|
||||
|
||||
todo.status = status;
|
||||
todo.updatedAt = new Date().toISOString();
|
||||
await this.setTodos(todos);
|
||||
return true;
|
||||
}
|
||||
|
||||
async deleteTodo(id: string): Promise<boolean> {
|
||||
const todos = this.getTodos();
|
||||
const index = todos.findIndex((t) => t.id === id);
|
||||
if (index === -1) return false;
|
||||
|
||||
todos.splice(index, 1);
|
||||
await this.setTodos(todos);
|
||||
return true;
|
||||
}
|
||||
|
||||
async clearTodos(): Promise<void> {
|
||||
await this.setTodos([]);
|
||||
}
|
||||
|
||||
private generateId(): string {
|
||||
return Math.random().toString(36).substring(2, 10);
|
||||
}
|
||||
|
||||
isInitialized(): boolean {
|
||||
return this.sessionManager !== null;
|
||||
}
|
||||
}
|
||||
|
||||
// Mock SessionManager
|
||||
function createMockSessionManager(): SessionManager {
|
||||
let todos: Todo[] = [];
|
||||
|
||||
return {
|
||||
getTodos: vi.fn(() => [...todos]),
|
||||
setTodos: vi.fn(async (newTodos: Todo[]) => {
|
||||
todos = [...newTodos];
|
||||
}),
|
||||
} as unknown as SessionManager;
|
||||
}
|
||||
|
||||
describe('TodoManager - Todo 管理器', () => {
|
||||
let todoManager: TestTodoManager;
|
||||
let mockSessionManager: SessionManager;
|
||||
|
||||
beforeEach(() => {
|
||||
todoManager = new TestTodoManager();
|
||||
mockSessionManager = createMockSessionManager();
|
||||
});
|
||||
|
||||
describe('初始化状态', () => {
|
||||
it('未设置 sessionManager 时 isInitialized 返回 false', () => {
|
||||
expect(todoManager.isInitialized()).toBe(false);
|
||||
});
|
||||
|
||||
it('设置 sessionManager 后 isInitialized 返回 true', () => {
|
||||
todoManager.setSessionManager(mockSessionManager);
|
||||
|
||||
expect(todoManager.isInitialized()).toBe(true);
|
||||
});
|
||||
|
||||
it('未初始化时 getTodos 返回空数组', () => {
|
||||
expect(todoManager.getTodos()).toEqual([]);
|
||||
});
|
||||
|
||||
it('未初始化时 setTodos 不报错', async () => {
|
||||
await expect(todoManager.setTodos([{
|
||||
id: '1',
|
||||
content: 'test',
|
||||
status: 'pending',
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
}])).resolves.not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getTodos', () => {
|
||||
beforeEach(() => {
|
||||
todoManager.setSessionManager(mockSessionManager);
|
||||
});
|
||||
|
||||
it('返回空数组当没有 todos', () => {
|
||||
const todos = todoManager.getTodos();
|
||||
|
||||
expect(todos).toEqual([]);
|
||||
});
|
||||
|
||||
it('返回 sessionManager 中的 todos', async () => {
|
||||
await todoManager.addTodo('Task 1');
|
||||
await todoManager.addTodo('Task 2');
|
||||
|
||||
const todos = todoManager.getTodos();
|
||||
|
||||
expect(todos).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('setTodos', () => {
|
||||
beforeEach(() => {
|
||||
todoManager.setSessionManager(mockSessionManager);
|
||||
});
|
||||
|
||||
it('设置 todos 列表', async () => {
|
||||
const todos: Todo[] = [
|
||||
{
|
||||
id: '1',
|
||||
content: 'Task 1',
|
||||
status: 'pending',
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
},
|
||||
];
|
||||
|
||||
await todoManager.setTodos(todos);
|
||||
|
||||
expect(todoManager.getTodos()).toHaveLength(1);
|
||||
expect(todoManager.getTodos()[0].content).toBe('Task 1');
|
||||
});
|
||||
|
||||
it('清空 todos', async () => {
|
||||
await todoManager.addTodo('Task 1');
|
||||
await todoManager.setTodos([]);
|
||||
|
||||
expect(todoManager.getTodos()).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('addTodo', () => {
|
||||
beforeEach(() => {
|
||||
todoManager.setSessionManager(mockSessionManager);
|
||||
});
|
||||
|
||||
it('添加新 todo 默认状态为 pending', async () => {
|
||||
const todo = await todoManager.addTodo('New task');
|
||||
|
||||
expect(todo.content).toBe('New task');
|
||||
expect(todo.status).toBe('pending');
|
||||
expect(todo.id).toBeDefined();
|
||||
expect(todo.createdAt).toBeDefined();
|
||||
expect(todo.updatedAt).toBeDefined();
|
||||
});
|
||||
|
||||
it('添加新 todo 指定状态', async () => {
|
||||
const todo = await todoManager.addTodo('In progress task', 'in_progress');
|
||||
|
||||
expect(todo.status).toBe('in_progress');
|
||||
});
|
||||
|
||||
it('添加的 todo 出现在列表中', async () => {
|
||||
await todoManager.addTodo('Task 1');
|
||||
await todoManager.addTodo('Task 2');
|
||||
|
||||
const todos = todoManager.getTodos();
|
||||
|
||||
expect(todos).toHaveLength(2);
|
||||
expect(todos.some(t => t.content === 'Task 1')).toBe(true);
|
||||
expect(todos.some(t => t.content === 'Task 2')).toBe(true);
|
||||
});
|
||||
|
||||
it('每个 todo 有唯一 ID', async () => {
|
||||
const todo1 = await todoManager.addTodo('Task 1');
|
||||
const todo2 = await todoManager.addTodo('Task 2');
|
||||
|
||||
expect(todo1.id).not.toBe(todo2.id);
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateTodoStatus', () => {
|
||||
beforeEach(() => {
|
||||
todoManager.setSessionManager(mockSessionManager);
|
||||
});
|
||||
|
||||
it('更新存在的 todo 状态', async () => {
|
||||
const todo = await todoManager.addTodo('Task');
|
||||
const result = await todoManager.updateTodoStatus(todo.id, 'completed');
|
||||
|
||||
expect(result).toBe(true);
|
||||
const updated = todoManager.getTodos().find(t => t.id === todo.id);
|
||||
expect(updated?.status).toBe('completed');
|
||||
});
|
||||
|
||||
it('更新不存在的 todo 返回 false', async () => {
|
||||
const result = await todoManager.updateTodoStatus('non-existent-id', 'completed');
|
||||
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it('更新状态时更新 updatedAt', async () => {
|
||||
const todo = await todoManager.addTodo('Task');
|
||||
const originalUpdatedAt = todo.updatedAt;
|
||||
|
||||
// 等待一小段时间确保时间戳不同
|
||||
await new Promise(resolve => setTimeout(resolve, 10));
|
||||
|
||||
await todoManager.updateTodoStatus(todo.id, 'in_progress');
|
||||
const updated = todoManager.getTodos().find(t => t.id === todo.id);
|
||||
|
||||
expect(updated?.updatedAt).not.toBe(originalUpdatedAt);
|
||||
});
|
||||
});
|
||||
|
||||
describe('deleteTodo', () => {
|
||||
beforeEach(() => {
|
||||
todoManager.setSessionManager(mockSessionManager);
|
||||
});
|
||||
|
||||
it('删除存在的 todo', async () => {
|
||||
const todo = await todoManager.addTodo('Task to delete');
|
||||
const result = await todoManager.deleteTodo(todo.id);
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(todoManager.getTodos().find(t => t.id === todo.id)).toBeUndefined();
|
||||
});
|
||||
|
||||
it('删除不存在的 todo 返回 false', async () => {
|
||||
const result = await todoManager.deleteTodo('non-existent-id');
|
||||
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it('删除后列表长度减少', async () => {
|
||||
await todoManager.addTodo('Task 1');
|
||||
const todo2 = await todoManager.addTodo('Task 2');
|
||||
await todoManager.addTodo('Task 3');
|
||||
|
||||
await todoManager.deleteTodo(todo2.id);
|
||||
|
||||
expect(todoManager.getTodos()).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('clearTodos', () => {
|
||||
beforeEach(() => {
|
||||
todoManager.setSessionManager(mockSessionManager);
|
||||
});
|
||||
|
||||
it('清空所有 todos', async () => {
|
||||
await todoManager.addTodo('Task 1');
|
||||
await todoManager.addTodo('Task 2');
|
||||
await todoManager.addTodo('Task 3');
|
||||
|
||||
await todoManager.clearTodos();
|
||||
|
||||
expect(todoManager.getTodos()).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('清空空列表不报错', async () => {
|
||||
await expect(todoManager.clearTodos()).resolves.not.toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Todo 状态流转', () => {
|
||||
let todoManager: TestTodoManager;
|
||||
let mockSessionManager: SessionManager;
|
||||
|
||||
beforeEach(() => {
|
||||
todoManager = new TestTodoManager();
|
||||
mockSessionManager = createMockSessionManager();
|
||||
todoManager.setSessionManager(mockSessionManager);
|
||||
});
|
||||
|
||||
it('pending -> in_progress -> completed', async () => {
|
||||
const todo = await todoManager.addTodo('Task');
|
||||
|
||||
expect(todo.status).toBe('pending');
|
||||
|
||||
await todoManager.updateTodoStatus(todo.id, 'in_progress');
|
||||
let updated = todoManager.getTodos().find(t => t.id === todo.id);
|
||||
expect(updated?.status).toBe('in_progress');
|
||||
|
||||
await todoManager.updateTodoStatus(todo.id, 'completed');
|
||||
updated = todoManager.getTodos().find(t => t.id === todo.id);
|
||||
expect(updated?.status).toBe('completed');
|
||||
});
|
||||
|
||||
it('直接从 pending 到 completed', async () => {
|
||||
const todo = await todoManager.addTodo('Quick task');
|
||||
|
||||
await todoManager.updateTodoStatus(todo.id, 'completed');
|
||||
const updated = todoManager.getTodos().find(t => t.id === todo.id);
|
||||
expect(updated?.status).toBe('completed');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,274 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { todoManager } from '../../../../src/tools/todo/todo-manager.js';
|
||||
import type { SessionManager } from '../../../../src/session/index.js';
|
||||
import type { Todo } from '../../../../src/session/types.js';
|
||||
|
||||
describe('TodoManager', () => {
|
||||
let mockSessionManager: SessionManager;
|
||||
let mockTodos: Todo[];
|
||||
|
||||
beforeEach(() => {
|
||||
mockTodos = [];
|
||||
mockSessionManager = {
|
||||
getTodos: vi.fn(() => mockTodos),
|
||||
setTodos: vi.fn(async (todos: Todo[]) => {
|
||||
mockTodos = todos;
|
||||
}),
|
||||
} as unknown as SessionManager;
|
||||
|
||||
// 重置 todoManager 状态
|
||||
todoManager.setSessionManager(mockSessionManager);
|
||||
});
|
||||
|
||||
describe('setSessionManager', () => {
|
||||
it('设置会话管理器后 isInitialized 返回 true', () => {
|
||||
const freshManager = {
|
||||
getTodos: vi.fn(() => []),
|
||||
setTodos: vi.fn(),
|
||||
} as unknown as SessionManager;
|
||||
|
||||
todoManager.setSessionManager(freshManager);
|
||||
expect(todoManager.isInitialized()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getTodos', () => {
|
||||
it('返回空数组当没有 todo', () => {
|
||||
const todos = todoManager.getTodos();
|
||||
expect(todos).toEqual([]);
|
||||
});
|
||||
|
||||
it('返回现有的 todo 列表', () => {
|
||||
mockTodos = [
|
||||
{ id: '1', content: 'Task 1', status: 'pending', createdAt: '', updatedAt: '' },
|
||||
{ id: '2', content: 'Task 2', status: 'completed', createdAt: '', updatedAt: '' },
|
||||
];
|
||||
|
||||
const todos = todoManager.getTodos();
|
||||
expect(todos).toHaveLength(2);
|
||||
expect(todos[0].content).toBe('Task 1');
|
||||
expect(todos[1].content).toBe('Task 2');
|
||||
});
|
||||
|
||||
it('正确调用 sessionManager.getTodos', () => {
|
||||
todoManager.getTodos();
|
||||
expect(mockSessionManager.getTodos).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('setTodos', () => {
|
||||
it('更新 todo 列表', async () => {
|
||||
const newTodos: Todo[] = [
|
||||
{ id: '1', content: 'New Task', status: 'pending', createdAt: '', updatedAt: '' },
|
||||
];
|
||||
|
||||
await todoManager.setTodos(newTodos);
|
||||
|
||||
expect(mockSessionManager.setTodos).toHaveBeenCalledWith(newTodos);
|
||||
expect(mockTodos).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('可以设置空列表', async () => {
|
||||
mockTodos = [
|
||||
{ id: '1', content: 'Task', status: 'pending', createdAt: '', updatedAt: '' },
|
||||
];
|
||||
|
||||
await todoManager.setTodos([]);
|
||||
|
||||
expect(mockTodos).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('addTodo', () => {
|
||||
it('添加新的 todo(默认状态 pending)', async () => {
|
||||
const todo = await todoManager.addTodo('新任务');
|
||||
|
||||
expect(todo.content).toBe('新任务');
|
||||
expect(todo.status).toBe('pending');
|
||||
expect(todo.id).toBeDefined();
|
||||
expect(todo.createdAt).toBeDefined();
|
||||
expect(todo.updatedAt).toBeDefined();
|
||||
expect(mockTodos).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('添加指定状态的 todo', async () => {
|
||||
const todo = await todoManager.addTodo('进行中的任务', 'in_progress');
|
||||
|
||||
expect(todo.content).toBe('进行中的任务');
|
||||
expect(todo.status).toBe('in_progress');
|
||||
});
|
||||
|
||||
it('添加已完成状态的 todo', async () => {
|
||||
const todo = await todoManager.addTodo('已完成任务', 'completed');
|
||||
|
||||
expect(todo.status).toBe('completed');
|
||||
});
|
||||
|
||||
it('生成唯一 ID', async () => {
|
||||
const todo1 = await todoManager.addTodo('Task 1');
|
||||
const todo2 = await todoManager.addTodo('Task 2');
|
||||
|
||||
expect(todo1.id).not.toBe(todo2.id);
|
||||
});
|
||||
|
||||
it('追加到现有列表', async () => {
|
||||
mockTodos = [
|
||||
{ id: 'existing', content: 'Existing', status: 'pending', createdAt: '', updatedAt: '' },
|
||||
];
|
||||
|
||||
await todoManager.addTodo('New Task');
|
||||
|
||||
expect(mockTodos).toHaveLength(2);
|
||||
expect(mockTodos[0].content).toBe('Existing');
|
||||
expect(mockTodos[1].content).toBe('New Task');
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateTodoStatus', () => {
|
||||
beforeEach(() => {
|
||||
mockTodos = [
|
||||
{ id: 'todo-1', content: 'Task 1', status: 'pending', createdAt: '2024-01-01', updatedAt: '2024-01-01' },
|
||||
{ id: 'todo-2', content: 'Task 2', status: 'pending', createdAt: '2024-01-01', updatedAt: '2024-01-01' },
|
||||
];
|
||||
});
|
||||
|
||||
it('更新存在的 todo 状态', async () => {
|
||||
const result = await todoManager.updateTodoStatus('todo-1', 'completed');
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(mockTodos[0].status).toBe('completed');
|
||||
});
|
||||
|
||||
it('更新 updatedAt 时间戳', async () => {
|
||||
const originalUpdatedAt = mockTodos[0].updatedAt;
|
||||
|
||||
await todoManager.updateTodoStatus('todo-1', 'in_progress');
|
||||
|
||||
expect(mockTodos[0].updatedAt).not.toBe(originalUpdatedAt);
|
||||
});
|
||||
|
||||
it('不存在的 todo 返回 false', async () => {
|
||||
const result = await todoManager.updateTodoStatus('non-existent', 'completed');
|
||||
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it('可以更新为任意状态', async () => {
|
||||
await todoManager.updateTodoStatus('todo-1', 'in_progress');
|
||||
expect(mockTodos[0].status).toBe('in_progress');
|
||||
|
||||
await todoManager.updateTodoStatus('todo-1', 'completed');
|
||||
expect(mockTodos[0].status).toBe('completed');
|
||||
|
||||
await todoManager.updateTodoStatus('todo-1', 'pending');
|
||||
expect(mockTodos[0].status).toBe('pending');
|
||||
});
|
||||
});
|
||||
|
||||
describe('deleteTodo', () => {
|
||||
beforeEach(() => {
|
||||
mockTodos = [
|
||||
{ id: 'todo-1', content: 'Task 1', status: 'pending', createdAt: '', updatedAt: '' },
|
||||
{ id: 'todo-2', content: 'Task 2', status: 'completed', createdAt: '', updatedAt: '' },
|
||||
{ id: 'todo-3', content: 'Task 3', status: 'in_progress', createdAt: '', updatedAt: '' },
|
||||
];
|
||||
});
|
||||
|
||||
it('删除存在的 todo', async () => {
|
||||
const result = await todoManager.deleteTodo('todo-2');
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(mockTodos).toHaveLength(2);
|
||||
expect(mockTodos.find(t => t.id === 'todo-2')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('删除第一个 todo', async () => {
|
||||
const result = await todoManager.deleteTodo('todo-1');
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(mockTodos[0].id).toBe('todo-2');
|
||||
});
|
||||
|
||||
it('删除最后一个 todo', async () => {
|
||||
const result = await todoManager.deleteTodo('todo-3');
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(mockTodos).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('不存在的 todo 返回 false', async () => {
|
||||
const result = await todoManager.deleteTodo('non-existent');
|
||||
|
||||
expect(result).toBe(false);
|
||||
expect(mockTodos).toHaveLength(3);
|
||||
});
|
||||
|
||||
it('删除后其他 todo 保持不变', async () => {
|
||||
await todoManager.deleteTodo('todo-2');
|
||||
|
||||
expect(mockTodos[0].content).toBe('Task 1');
|
||||
expect(mockTodos[1].content).toBe('Task 3');
|
||||
});
|
||||
});
|
||||
|
||||
describe('clearTodos', () => {
|
||||
it('清空所有 todo', async () => {
|
||||
mockTodos = [
|
||||
{ id: '1', content: 'Task 1', status: 'pending', createdAt: '', updatedAt: '' },
|
||||
{ id: '2', content: 'Task 2', status: 'completed', createdAt: '', updatedAt: '' },
|
||||
];
|
||||
|
||||
await todoManager.clearTodos();
|
||||
|
||||
expect(mockTodos).toHaveLength(0);
|
||||
expect(mockSessionManager.setTodos).toHaveBeenCalledWith([]);
|
||||
});
|
||||
|
||||
it('空列表时调用也不报错', async () => {
|
||||
mockTodos = [];
|
||||
|
||||
await expect(todoManager.clearTodos()).resolves.not.toThrow();
|
||||
expect(mockTodos).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isInitialized', () => {
|
||||
it('初始化后返回 true', () => {
|
||||
expect(todoManager.isInitialized()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('边界情况', () => {
|
||||
it('处理包含特殊字符的 content', async () => {
|
||||
const specialContent = '任务: "测试" & <script>alert(1)</script>';
|
||||
const todo = await todoManager.addTodo(specialContent);
|
||||
|
||||
expect(todo.content).toBe(specialContent);
|
||||
});
|
||||
|
||||
it('处理非常长的 content', async () => {
|
||||
const longContent = 'A'.repeat(10000);
|
||||
const todo = await todoManager.addTodo(longContent);
|
||||
|
||||
expect(todo.content).toBe(longContent);
|
||||
});
|
||||
|
||||
it('处理空字符串 content', async () => {
|
||||
const todo = await todoManager.addTodo('');
|
||||
|
||||
expect(todo.content).toBe('');
|
||||
});
|
||||
|
||||
it('连续操作保持数据一致性', async () => {
|
||||
await todoManager.addTodo('Task 1');
|
||||
await todoManager.addTodo('Task 2');
|
||||
await todoManager.updateTodoStatus(mockTodos[0].id, 'completed');
|
||||
await todoManager.deleteTodo(mockTodos[1].id);
|
||||
await todoManager.addTodo('Task 3');
|
||||
|
||||
expect(mockTodos).toHaveLength(2);
|
||||
expect(mockTodos[0].status).toBe('completed');
|
||||
expect(mockTodos[1].content).toBe('Task 3');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,103 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// 使用可变的引用对象来绕过 hoisting 问题
|
||||
const mockState = {
|
||||
isInitialized: vi.fn().mockReturnValue(true),
|
||||
getTodos: vi.fn().mockReturnValue([]),
|
||||
};
|
||||
|
||||
vi.mock('../../../../src/tools/todo/todo-manager.js', () => ({
|
||||
todoManager: {
|
||||
isInitialized: () => mockState.isInitialized(),
|
||||
getTodos: () => mockState.getTodos(),
|
||||
},
|
||||
}));
|
||||
|
||||
import { todoReadTool } from '../../../../src/tools/todo/todoread.js';
|
||||
|
||||
describe('todoReadTool - Todo 读取工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockState.isInitialized.mockReturnValue(true);
|
||||
mockState.getTodos.mockReturnValue([]);
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(todoReadTool.name).toBe('todoread');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(todoReadTool.metadata.category).toBe('core');
|
||||
expect(todoReadTool.metadata.keywords).toContain('todo');
|
||||
expect(todoReadTool.metadata.keywords).toContain('task');
|
||||
expect(todoReadTool.metadata.keywords).toContain('list');
|
||||
});
|
||||
|
||||
it('无必需参数', () => {
|
||||
expect(Object.keys(todoReadTool.parameters)).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功读取空列表', async () => {
|
||||
const result = await todoReadTool.execute({});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toBe('[]');
|
||||
expect(result.metadata?.totalCount).toBe(0);
|
||||
expect(result.metadata?.pendingCount).toBe(0);
|
||||
});
|
||||
|
||||
it('成功读取待办列表', async () => {
|
||||
const todos = [
|
||||
{ id: '1', content: '任务1', status: 'pending', createdAt: '2024-01-01', updatedAt: '2024-01-01' },
|
||||
{ id: '2', content: '任务2', status: 'in_progress', createdAt: '2024-01-01', updatedAt: '2024-01-01' },
|
||||
{ id: '3', content: '任务3', status: 'completed', createdAt: '2024-01-01', updatedAt: '2024-01-01' },
|
||||
];
|
||||
mockState.getTodos.mockReturnValue(todos);
|
||||
|
||||
const result = await todoReadTool.execute({});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.metadata?.totalCount).toBe(3);
|
||||
expect(result.metadata?.pendingCount).toBe(2); // pending + in_progress
|
||||
});
|
||||
|
||||
it('返回 JSON 格式输出', async () => {
|
||||
const todos = [
|
||||
{ id: '1', content: '任务1', status: 'pending', createdAt: '2024-01-01', updatedAt: '2024-01-01' },
|
||||
];
|
||||
mockState.getTodos.mockReturnValue(todos);
|
||||
|
||||
const result = await todoReadTool.execute({});
|
||||
|
||||
const parsed = JSON.parse(result.output);
|
||||
expect(parsed).toHaveLength(1);
|
||||
expect(parsed[0].content).toBe('任务1');
|
||||
});
|
||||
|
||||
it('未初始化时返回错误', async () => {
|
||||
mockState.isInitialized.mockReturnValue(false);
|
||||
|
||||
const result = await todoReadTool.execute({});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('会话管理器未初始化');
|
||||
});
|
||||
|
||||
it('返回正确的元数据', async () => {
|
||||
const todos = [
|
||||
{ id: '1', content: '任务1', status: 'pending' },
|
||||
{ id: '2', content: '任务2', status: 'completed' },
|
||||
];
|
||||
mockState.getTodos.mockReturnValue(todos);
|
||||
|
||||
const result = await todoReadTool.execute({});
|
||||
|
||||
expect(result.metadata?.todos).toEqual(todos);
|
||||
expect(result.metadata?.pendingCount).toBe(1);
|
||||
expect(result.metadata?.totalCount).toBe(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,171 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// 使用可变的引用对象来绕过 hoisting 问题
|
||||
const mockState = {
|
||||
isInitialized: vi.fn().mockReturnValue(true),
|
||||
getTodos: vi.fn().mockReturnValue([]),
|
||||
setTodos: vi.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
|
||||
vi.mock('../../../../src/tools/todo/todo-manager.js', () => ({
|
||||
todoManager: {
|
||||
isInitialized: () => mockState.isInitialized(),
|
||||
getTodos: () => mockState.getTodos(),
|
||||
setTodos: (todos: any) => mockState.setTodos(todos),
|
||||
},
|
||||
}));
|
||||
|
||||
import { todoWriteTool } from '../../../../src/tools/todo/todowrite.js';
|
||||
|
||||
describe('todoWriteTool - Todo 写入工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockState.isInitialized.mockReturnValue(true);
|
||||
mockState.getTodos.mockReturnValue([]);
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(todoWriteTool.name).toBe('todowrite');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(todoWriteTool.metadata.category).toBe('core');
|
||||
expect(todoWriteTool.metadata.keywords).toContain('todo');
|
||||
expect(todoWriteTool.metadata.keywords).toContain('task');
|
||||
expect(todoWriteTool.metadata.keywords).toContain('write');
|
||||
});
|
||||
|
||||
it('定义了必需的 todos 参数', () => {
|
||||
expect(todoWriteTool.parameters.todos.required).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功创建待办列表', async () => {
|
||||
const result = await todoWriteTool.execute({
|
||||
todos: [
|
||||
{ content: '任务1', status: 'pending' },
|
||||
{ content: '任务2', status: 'in_progress' },
|
||||
],
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('待办事项已更新');
|
||||
expect(mockState.setTodos).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('返回正确的统计信息', async () => {
|
||||
const result = await todoWriteTool.execute({
|
||||
todos: [
|
||||
{ content: '任务1', status: 'pending' },
|
||||
{ content: '任务2', status: 'in_progress' },
|
||||
{ content: '任务3', status: 'completed' },
|
||||
],
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
// pendingCount 包含所有非 completed 状态的任务(pending + in_progress)
|
||||
expect(result.metadata?.pendingCount).toBe(2);
|
||||
expect(result.metadata?.inProgressCount).toBe(1);
|
||||
expect(result.metadata?.completedCount).toBe(1);
|
||||
});
|
||||
|
||||
it('更新现有任务', async () => {
|
||||
mockState.getTodos.mockReturnValue([
|
||||
{ id: 'existing-1', content: '任务1', status: 'pending', createdAt: '2024-01-01', updatedAt: '2024-01-01' },
|
||||
]);
|
||||
|
||||
const result = await todoWriteTool.execute({
|
||||
todos: [
|
||||
{ content: '任务1', status: 'completed' },
|
||||
],
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
const savedTodos = mockState.setTodos.mock.calls[0][0];
|
||||
expect(savedTodos[0].id).toBe('existing-1'); // 保留原有 ID
|
||||
expect(savedTodos[0].status).toBe('completed');
|
||||
});
|
||||
|
||||
it('未初始化时返回错误', async () => {
|
||||
mockState.isInitialized.mockReturnValue(false);
|
||||
|
||||
const result = await todoWriteTool.execute({
|
||||
todos: [{ content: '任务', status: 'pending' }],
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('会话管理器未初始化');
|
||||
});
|
||||
|
||||
it('todos 非数组返回错误', async () => {
|
||||
const result = await todoWriteTool.execute({
|
||||
todos: 'not an array',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('todos 参数必须是数组');
|
||||
});
|
||||
|
||||
it('无效的待办项格式返回错误', async () => {
|
||||
const result = await todoWriteTool.execute({
|
||||
todos: [
|
||||
{ content: '有效任务', status: 'pending' },
|
||||
{ content: '', status: 'pending' }, // 空内容
|
||||
],
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('第 2 个待办事项格式无效');
|
||||
});
|
||||
|
||||
it('无效的状态值返回错误', async () => {
|
||||
const result = await todoWriteTool.execute({
|
||||
todos: [
|
||||
{ content: '任务', status: 'invalid_status' },
|
||||
],
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('格式无效');
|
||||
});
|
||||
|
||||
it('缺少 content 返回错误', async () => {
|
||||
const result = await todoWriteTool.execute({
|
||||
todos: [
|
||||
{ status: 'pending' },
|
||||
],
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('格式无效');
|
||||
});
|
||||
|
||||
it('为新任务生成 ID', async () => {
|
||||
const result = await todoWriteTool.execute({
|
||||
todos: [
|
||||
{ content: '新任务', status: 'pending' },
|
||||
],
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
const savedTodos = mockState.setTodos.mock.calls[0][0];
|
||||
expect(savedTodos[0].id).toBeDefined();
|
||||
expect(savedTodos[0].id.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('设置创建和更新时间', async () => {
|
||||
const result = await todoWriteTool.execute({
|
||||
todos: [
|
||||
{ content: '新任务', status: 'pending' },
|
||||
],
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
const savedTodos = mockState.setTodos.mock.calls[0][0];
|
||||
expect(savedTodos[0].createdAt).toBeDefined();
|
||||
expect(savedTodos[0].updatedAt).toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,143 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { toolSearchTool } from '../../../src/tools/tool-search.js';
|
||||
|
||||
// Mock toolRegistry
|
||||
vi.mock('../../../src/tools/registry.js', () => ({
|
||||
toolRegistry: {
|
||||
search: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
import { toolRegistry } from '../../../src/tools/registry.js';
|
||||
|
||||
describe('toolSearchTool', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('metadata', () => {
|
||||
it('工具名为 tool_search', () => {
|
||||
expect(toolSearchTool.name).toBe('tool_search');
|
||||
});
|
||||
|
||||
it('有描述信息', () => {
|
||||
expect(toolSearchTool.description).toBeDefined();
|
||||
expect(toolSearchTool.description.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('描述中包含功能类别', () => {
|
||||
expect(toolSearchTool.description).toContain('文件操作');
|
||||
expect(toolSearchTool.description).toContain('目录操作');
|
||||
expect(toolSearchTool.description).toContain('Shell');
|
||||
});
|
||||
|
||||
it('有 query 参数', () => {
|
||||
expect(toolSearchTool.parameters.query).toBeDefined();
|
||||
expect(toolSearchTool.parameters.query.type).toBe('string');
|
||||
expect(toolSearchTool.parameters.query.required).toBe(true);
|
||||
});
|
||||
|
||||
it('metadata 设置正确', () => {
|
||||
expect(toolSearchTool.metadata).toBeDefined();
|
||||
expect(toolSearchTool.metadata?.category).toBe('core');
|
||||
expect(toolSearchTool.metadata?.deferLoading).toBe(false);
|
||||
});
|
||||
|
||||
it('metadata 包含搜索关键词', () => {
|
||||
expect(toolSearchTool.metadata?.keywords).toContain('search');
|
||||
expect(toolSearchTool.metadata?.keywords).toContain('搜索');
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute', () => {
|
||||
it('空 query 返回错误', async () => {
|
||||
const result = await toolSearchTool.execute({ query: '' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBe('请提供搜索关键词');
|
||||
});
|
||||
|
||||
it('空白 query 返回错误', async () => {
|
||||
const result = await toolSearchTool.execute({ query: ' ' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBe('请提供搜索关键词');
|
||||
});
|
||||
|
||||
it('undefined query 返回错误', async () => {
|
||||
const result = await toolSearchTool.execute({});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBe('请提供搜索关键词');
|
||||
});
|
||||
|
||||
it('没有匹配结果时返回提示', async () => {
|
||||
vi.mocked(toolRegistry.search).mockReturnValue([]);
|
||||
|
||||
const result = await toolSearchTool.execute({ query: 'nonexistent' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('没有找到');
|
||||
expect(result.output).toContain('nonexistent');
|
||||
});
|
||||
|
||||
it('有匹配结果时返回工具列表', async () => {
|
||||
vi.mocked(toolRegistry.search).mockReturnValue([
|
||||
{ name: 'read_file', description: '读取文件', category: 'filesystem' },
|
||||
{ name: 'write_file', description: '写入文件', category: 'filesystem' },
|
||||
] as any);
|
||||
|
||||
const result = await toolSearchTool.execute({ query: '文件' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('找到 2 个相关工具');
|
||||
expect(result.output).toContain('read_file');
|
||||
expect(result.output).toContain('write_file');
|
||||
expect(result.output).toContain('[filesystem]');
|
||||
});
|
||||
|
||||
it('返回结果包含使用提示', async () => {
|
||||
vi.mocked(toolRegistry.search).mockReturnValue([
|
||||
{ name: 'bash', description: '执行命令', category: 'shell' },
|
||||
] as any);
|
||||
|
||||
const result = await toolSearchTool.execute({ query: '命令' });
|
||||
|
||||
expect(result.output).toContain('可以直接调用');
|
||||
});
|
||||
|
||||
it('调用 registry.search 时传递正确参数', async () => {
|
||||
vi.mocked(toolRegistry.search).mockReturnValue([]);
|
||||
|
||||
await toolSearchTool.execute({ query: '搜索关键词' });
|
||||
|
||||
expect(toolRegistry.search).toHaveBeenCalledWith('搜索关键词', 5);
|
||||
});
|
||||
|
||||
it('搜索结果格式化正确', async () => {
|
||||
vi.mocked(toolRegistry.search).mockReturnValue([
|
||||
{ name: 'grep_content', description: '搜索文件内容', category: 'filesystem' },
|
||||
] as any);
|
||||
|
||||
const result = await toolSearchTool.execute({ query: 'grep' });
|
||||
|
||||
expect(result.output).toContain('- grep_content: 搜索文件内容 [filesystem]');
|
||||
});
|
||||
|
||||
it('多个搜索结果正确排列', async () => {
|
||||
vi.mocked(toolRegistry.search).mockReturnValue([
|
||||
{ name: 'tool1', description: 'desc1', category: 'cat1' },
|
||||
{ name: 'tool2', description: 'desc2', category: 'cat2' },
|
||||
{ name: 'tool3', description: 'desc3', category: 'cat3' },
|
||||
] as any);
|
||||
|
||||
const result = await toolSearchTool.execute({ query: 'test' });
|
||||
|
||||
expect(result.output).toContain('找到 3 个相关工具');
|
||||
// 验证每个工具都在输出中
|
||||
expect(result.output).toContain('tool1');
|
||||
expect(result.output).toContain('tool2');
|
||||
expect(result.output).toContain('tool3');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,264 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// Mock tavily
|
||||
const mockExtract = vi.fn();
|
||||
vi.mock('@tavily/core', () => ({
|
||||
tavily: vi.fn(() => ({
|
||||
extract: mockExtract,
|
||||
})),
|
||||
}));
|
||||
|
||||
// Mock config
|
||||
vi.mock('../../../../src/utils/config.js', () => ({
|
||||
getConfig: vi.fn(() => ({
|
||||
tavilyApiKey: 'test-api-key',
|
||||
})),
|
||||
}));
|
||||
|
||||
// Mock permission manager
|
||||
vi.mock('../../../../src/permission/index.js', () => ({
|
||||
getPermissionManager: vi.fn(() => ({
|
||||
checkWebPermission: vi.fn().mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
}),
|
||||
})),
|
||||
}));
|
||||
|
||||
// Mock loadDescription
|
||||
vi.mock('../../../../src/tools/load_description.js', () => ({
|
||||
loadDescription: vi.fn(() => '从网页URL提取内容'),
|
||||
}));
|
||||
|
||||
import { webExtractTool } from '../../../../src/tools/web/web_extract.js';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
import { getConfig } from '../../../../src/utils/config.js';
|
||||
|
||||
describe('webExtractTool - 网页内容提取工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockExtract.mockResolvedValue({
|
||||
results: [
|
||||
{
|
||||
url: 'https://example.com',
|
||||
rawContent: '# Hello World\n\nThis is example content.',
|
||||
images: ['https://example.com/img1.png'],
|
||||
},
|
||||
],
|
||||
failedResults: [],
|
||||
responseTime: 1.5,
|
||||
});
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(webExtractTool.name).toBe('web_extract');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(webExtractTool.metadata.category).toBe('web');
|
||||
expect(webExtractTool.metadata.keywords).toContain('extract');
|
||||
expect(webExtractTool.metadata.keywords).toContain('url');
|
||||
expect(webExtractTool.metadata.keywords).toContain('scrape');
|
||||
});
|
||||
|
||||
it('定义了必需的 urls 参数', () => {
|
||||
expect(webExtractTool.parameters.urls.required).toBe(true);
|
||||
});
|
||||
|
||||
it('定义了可选参数', () => {
|
||||
expect(webExtractTool.parameters.extract_depth.required).toBe(false);
|
||||
expect(webExtractTool.parameters.format.required).toBe(false);
|
||||
expect(webExtractTool.parameters.include_images.required).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功提取单个 URL 内容', async () => {
|
||||
const result = await webExtractTool.execute({
|
||||
urls: ['https://example.com'],
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('网页内容提取');
|
||||
expect(result.output).toContain('example.com');
|
||||
expect(result.output).toContain('Hello World');
|
||||
});
|
||||
|
||||
it('支持字符串格式的单个 URL', async () => {
|
||||
const result = await webExtractTool.execute({
|
||||
urls: 'https://example.com',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(mockExtract).toHaveBeenCalledWith(
|
||||
['https://example.com'],
|
||||
expect.any(Object)
|
||||
);
|
||||
});
|
||||
|
||||
it('支持多个 URL', async () => {
|
||||
mockExtract.mockResolvedValue({
|
||||
results: [
|
||||
{ url: 'https://example1.com', rawContent: 'Content 1' },
|
||||
{ url: 'https://example2.com', rawContent: 'Content 2' },
|
||||
],
|
||||
failedResults: [],
|
||||
});
|
||||
|
||||
const result = await webExtractTool.execute({
|
||||
urls: ['https://example1.com', 'https://example2.com'],
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('example1.com');
|
||||
expect(result.output).toContain('example2.com');
|
||||
});
|
||||
|
||||
it('限制最多 20 个 URL', async () => {
|
||||
const urls = Array.from({ length: 25 }, (_, i) => `https://example${i}.com`);
|
||||
|
||||
await webExtractTool.execute({ urls });
|
||||
|
||||
expect(mockExtract).toHaveBeenCalledWith(
|
||||
expect.any(Array),
|
||||
expect.any(Object)
|
||||
);
|
||||
const calledUrls = mockExtract.mock.calls[0][0];
|
||||
expect(calledUrls.length).toBe(20);
|
||||
});
|
||||
|
||||
it('使用 advanced 提取深度', async () => {
|
||||
await webExtractTool.execute({
|
||||
urls: ['https://example.com'],
|
||||
extract_depth: 'advanced',
|
||||
});
|
||||
|
||||
expect(mockExtract).toHaveBeenCalledWith(
|
||||
expect.any(Array),
|
||||
expect.objectContaining({ extractDepth: 'advanced' })
|
||||
);
|
||||
});
|
||||
|
||||
it('包含图片列表', async () => {
|
||||
const result = await webExtractTool.execute({
|
||||
urls: ['https://example.com'],
|
||||
include_images: true,
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('图片');
|
||||
expect(result.output).toContain('img1.png');
|
||||
});
|
||||
|
||||
it('显示失败的 URL', async () => {
|
||||
mockExtract.mockResolvedValue({
|
||||
results: [],
|
||||
failedResults: [
|
||||
{ url: 'https://failed.com', error: '404 Not Found' },
|
||||
],
|
||||
});
|
||||
|
||||
const result = await webExtractTool.execute({
|
||||
urls: ['https://failed.com'],
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('提取失败');
|
||||
expect(result.output).toContain('failed.com');
|
||||
expect(result.output).toContain('404');
|
||||
});
|
||||
|
||||
it('显示响应时间', async () => {
|
||||
const result = await webExtractTool.execute({
|
||||
urls: ['https://example.com'],
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('提取耗时');
|
||||
});
|
||||
|
||||
it('截断过长的内容', async () => {
|
||||
mockExtract.mockResolvedValue({
|
||||
results: [
|
||||
{
|
||||
url: 'https://example.com',
|
||||
rawContent: 'a'.repeat(6000), // 超过 5000 字符
|
||||
},
|
||||
],
|
||||
failedResults: [],
|
||||
});
|
||||
|
||||
const result = await webExtractTool.execute({
|
||||
urls: ['https://example.com'],
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('内容已截断');
|
||||
});
|
||||
|
||||
it('无 API Key 返回错误', async () => {
|
||||
vi.mocked(getConfig).mockReturnValue({} as any);
|
||||
const originalEnv = process.env.TAVILY_API_KEY;
|
||||
delete process.env.TAVILY_API_KEY;
|
||||
|
||||
const result = await webExtractTool.execute({
|
||||
urls: ['https://example.com'],
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('未配置 Tavily API Key');
|
||||
|
||||
process.env.TAVILY_API_KEY = originalEnv;
|
||||
});
|
||||
|
||||
it('权限被拒绝时返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkWebPermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '提取不被允许',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await webExtractTool.execute({
|
||||
urls: ['https://example.com'],
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('权限被拒绝');
|
||||
});
|
||||
|
||||
it('需要确认时返回提示', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkWebPermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'ask',
|
||||
needsConfirmation: true,
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await webExtractTool.execute({
|
||||
urls: ['https://example.com'],
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
});
|
||||
|
||||
it('提取失败返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkWebPermission: vi.fn().mockResolvedValue({ allowed: true }),
|
||||
} as any);
|
||||
mockExtract.mockRejectedValue(new Error('API 调用失败'));
|
||||
|
||||
const result = await webExtractTool.execute({
|
||||
urls: ['https://example.com'],
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('提取失败');
|
||||
expect(result.error).toContain('API 调用失败');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,207 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// Mock tavily
|
||||
const mockSearch = vi.fn();
|
||||
vi.mock('@tavily/core', () => ({
|
||||
tavily: vi.fn(() => ({
|
||||
search: mockSearch,
|
||||
})),
|
||||
}));
|
||||
|
||||
// Mock config
|
||||
vi.mock('../../../../src/utils/config.js', () => ({
|
||||
getConfig: vi.fn(() => ({
|
||||
tavilyApiKey: 'test-api-key',
|
||||
})),
|
||||
}));
|
||||
|
||||
// Mock permission manager
|
||||
vi.mock('../../../../src/permission/index.js', () => ({
|
||||
getPermissionManager: vi.fn(() => ({
|
||||
checkWebPermission: vi.fn().mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
}),
|
||||
})),
|
||||
}));
|
||||
|
||||
// Mock loadDescription
|
||||
vi.mock('../../../../src/tools/load_description.js', () => ({
|
||||
loadDescription: vi.fn(() => '网络搜索'),
|
||||
}));
|
||||
|
||||
import { webSearchTool } from '../../../../src/tools/web/web_search.js';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
import { getConfig } from '../../../../src/utils/config.js';
|
||||
|
||||
describe('webSearchTool - 网络搜索工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockSearch.mockResolvedValue({
|
||||
answer: '搜索摘要',
|
||||
results: [
|
||||
{ title: '结果1', url: 'https://example.com/1', content: '内容1' },
|
||||
{ title: '结果2', url: 'https://example.com/2', content: '内容2' },
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(webSearchTool.name).toBe('web_search');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(webSearchTool.metadata.category).toBe('web');
|
||||
expect(webSearchTool.metadata.keywords).toContain('search');
|
||||
expect(webSearchTool.metadata.keywords).toContain('web');
|
||||
});
|
||||
|
||||
it('定义了必需的 query 参数', () => {
|
||||
expect(webSearchTool.parameters.query.required).toBe(true);
|
||||
});
|
||||
|
||||
it('定义了可选参数', () => {
|
||||
expect(webSearchTool.parameters.max_results.required).toBe(false);
|
||||
expect(webSearchTool.parameters.search_depth.required).toBe(false);
|
||||
expect(webSearchTool.parameters.topic.required).toBe(false);
|
||||
expect(webSearchTool.parameters.include_answer.required).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功搜索并返回结果', async () => {
|
||||
const result = await webSearchTool.execute({ query: 'test query' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('搜索结果');
|
||||
expect(result.output).toContain('test query');
|
||||
expect(result.output).toContain('搜索摘要');
|
||||
expect(result.output).toContain('结果1');
|
||||
expect(result.output).toContain('结果2');
|
||||
});
|
||||
|
||||
it('无结果时显示提示', async () => {
|
||||
mockSearch.mockResolvedValue({
|
||||
answer: null,
|
||||
results: [],
|
||||
});
|
||||
|
||||
const result = await webSearchTool.execute({ query: 'no results' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('未找到相关结果');
|
||||
});
|
||||
|
||||
it('限制最大结果数量', async () => {
|
||||
const result = await webSearchTool.execute({
|
||||
query: 'test',
|
||||
max_results: 100, // 超过限制
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(mockSearch).toHaveBeenCalledWith(
|
||||
'test',
|
||||
expect.objectContaining({ maxResults: 20 }) // 最大 20
|
||||
);
|
||||
});
|
||||
|
||||
it('使用指定的搜索深度', async () => {
|
||||
await webSearchTool.execute({
|
||||
query: 'test',
|
||||
search_depth: 'advanced',
|
||||
});
|
||||
|
||||
expect(mockSearch).toHaveBeenCalledWith(
|
||||
'test',
|
||||
expect.objectContaining({ searchDepth: 'advanced' })
|
||||
);
|
||||
});
|
||||
|
||||
it('使用指定的主题', async () => {
|
||||
await webSearchTool.execute({
|
||||
query: 'test',
|
||||
topic: 'news',
|
||||
});
|
||||
|
||||
expect(mockSearch).toHaveBeenCalledWith(
|
||||
'test',
|
||||
expect.objectContaining({ topic: 'news' })
|
||||
);
|
||||
});
|
||||
|
||||
it('无 API Key 返回错误', async () => {
|
||||
vi.mocked(getConfig).mockReturnValue({} as any);
|
||||
const originalEnv = process.env.TAVILY_API_KEY;
|
||||
delete process.env.TAVILY_API_KEY;
|
||||
|
||||
const result = await webSearchTool.execute({ query: 'test' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('未配置 Tavily API Key');
|
||||
|
||||
process.env.TAVILY_API_KEY = originalEnv;
|
||||
});
|
||||
|
||||
it('权限被拒绝时返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkWebPermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '搜索不被允许',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await webSearchTool.execute({ query: 'test' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('权限被拒绝');
|
||||
});
|
||||
|
||||
it('需要确认时返回提示', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkWebPermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'ask',
|
||||
needsConfirmation: true,
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await webSearchTool.execute({ query: 'test' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
});
|
||||
|
||||
it('搜索失败返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkWebPermission: vi.fn().mockResolvedValue({ allowed: true }),
|
||||
} as any);
|
||||
mockSearch.mockRejectedValue(new Error('API 调用失败'));
|
||||
|
||||
const result = await webSearchTool.execute({ query: 'test' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('搜索失败');
|
||||
expect(result.error).toContain('API 调用失败');
|
||||
});
|
||||
|
||||
it('截断过长的内容', async () => {
|
||||
mockSearch.mockResolvedValue({
|
||||
answer: null,
|
||||
results: [
|
||||
{
|
||||
title: '长内容结果',
|
||||
url: 'https://example.com',
|
||||
content: 'a'.repeat(500), // 超过 300 字符
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const result = await webSearchTool.execute({ query: 'test' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('...');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,362 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { z } from 'zod';
|
||||
import { buildZodSchema, type ToolParameter } from '../../../src/types/index.js';
|
||||
|
||||
describe('buildZodSchema', () => {
|
||||
describe('基本类型转换', () => {
|
||||
it('转换 string 类型参数', () => {
|
||||
const parameters: Record<string, ToolParameter> = {
|
||||
name: {
|
||||
type: 'string',
|
||||
description: '用户名',
|
||||
required: true,
|
||||
},
|
||||
};
|
||||
|
||||
const schema = buildZodSchema(parameters);
|
||||
expect(schema.shape.name).toBeDefined();
|
||||
|
||||
// 验证 schema 可以正确解析
|
||||
const result = schema.safeParse({ name: 'test' });
|
||||
expect(result.success).toBe(true);
|
||||
if (result.success) {
|
||||
expect(result.data.name).toBe('test');
|
||||
}
|
||||
});
|
||||
|
||||
it('转换 number 类型参数', () => {
|
||||
const parameters: Record<string, ToolParameter> = {
|
||||
age: {
|
||||
type: 'number',
|
||||
description: '年龄',
|
||||
required: true,
|
||||
},
|
||||
};
|
||||
|
||||
const schema = buildZodSchema(parameters);
|
||||
|
||||
const result = schema.safeParse({ age: 25 });
|
||||
expect(result.success).toBe(true);
|
||||
if (result.success) {
|
||||
expect(result.data.age).toBe(25);
|
||||
}
|
||||
|
||||
// 字符串应该失败
|
||||
const invalidResult = schema.safeParse({ age: '25' });
|
||||
expect(invalidResult.success).toBe(false);
|
||||
});
|
||||
|
||||
it('转换 boolean 类型参数', () => {
|
||||
const parameters: Record<string, ToolParameter> = {
|
||||
active: {
|
||||
type: 'boolean',
|
||||
description: '是否激活',
|
||||
required: true,
|
||||
},
|
||||
};
|
||||
|
||||
const schema = buildZodSchema(parameters);
|
||||
|
||||
expect(schema.safeParse({ active: true }).success).toBe(true);
|
||||
expect(schema.safeParse({ active: false }).success).toBe(true);
|
||||
expect(schema.safeParse({ active: 'true' }).success).toBe(false);
|
||||
});
|
||||
|
||||
it('转换 array 类型参数', () => {
|
||||
const parameters: Record<string, ToolParameter> = {
|
||||
items: {
|
||||
type: 'array',
|
||||
description: '项目列表',
|
||||
required: true,
|
||||
},
|
||||
};
|
||||
|
||||
const schema = buildZodSchema(parameters);
|
||||
|
||||
const result = schema.safeParse({ items: [1, 'two', { three: 3 }] });
|
||||
expect(result.success).toBe(true);
|
||||
if (result.success) {
|
||||
expect(result.data.items).toEqual([1, 'two', { three: 3 }]);
|
||||
}
|
||||
});
|
||||
|
||||
it('转换 object 类型参数', () => {
|
||||
const parameters: Record<string, ToolParameter> = {
|
||||
config: {
|
||||
type: 'object',
|
||||
description: '配置对象',
|
||||
required: true,
|
||||
},
|
||||
};
|
||||
|
||||
const schema = buildZodSchema(parameters);
|
||||
|
||||
const result = schema.safeParse({ config: { key: 'value', nested: { a: 1 } } });
|
||||
expect(result.success).toBe(true);
|
||||
if (result.success) {
|
||||
expect(result.data.config).toEqual({ key: 'value', nested: { a: 1 } });
|
||||
}
|
||||
});
|
||||
|
||||
it('处理未知类型为 unknown', () => {
|
||||
const parameters = {
|
||||
data: {
|
||||
type: 'custom' as any,
|
||||
description: '自定义类型',
|
||||
required: true,
|
||||
},
|
||||
};
|
||||
|
||||
const schema = buildZodSchema(parameters);
|
||||
|
||||
// unknown 类型接受任何值
|
||||
expect(schema.safeParse({ data: 'anything' }).success).toBe(true);
|
||||
expect(schema.safeParse({ data: 123 }).success).toBe(true);
|
||||
expect(schema.safeParse({ data: { obj: true } }).success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('可选参数', () => {
|
||||
it('required=false 时参数可选', () => {
|
||||
const parameters: Record<string, ToolParameter> = {
|
||||
optional_field: {
|
||||
type: 'string',
|
||||
description: '可选字段',
|
||||
required: false,
|
||||
},
|
||||
};
|
||||
|
||||
const schema = buildZodSchema(parameters);
|
||||
|
||||
// 不提供可选字段应该成功
|
||||
expect(schema.safeParse({}).success).toBe(true);
|
||||
|
||||
// 提供可选字段也应该成功
|
||||
expect(schema.safeParse({ optional_field: 'value' }).success).toBe(true);
|
||||
});
|
||||
|
||||
it('required 未设置时默认可选', () => {
|
||||
const parameters: Record<string, ToolParameter> = {
|
||||
field: {
|
||||
type: 'string',
|
||||
description: '字段',
|
||||
// required 未设置
|
||||
},
|
||||
};
|
||||
|
||||
const schema = buildZodSchema(parameters);
|
||||
|
||||
// 应该是可选的
|
||||
expect(schema.safeParse({}).success).toBe(true);
|
||||
});
|
||||
|
||||
it('required=true 时参数必须', () => {
|
||||
const parameters: Record<string, ToolParameter> = {
|
||||
required_field: {
|
||||
type: 'string',
|
||||
description: '必填字段',
|
||||
required: true,
|
||||
},
|
||||
};
|
||||
|
||||
const schema = buildZodSchema(parameters);
|
||||
|
||||
// 不提供必填字段应该失败
|
||||
expect(schema.safeParse({}).success).toBe(false);
|
||||
|
||||
// 提供必填字段应该成功
|
||||
expect(schema.safeParse({ required_field: 'value' }).success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('混合参数', () => {
|
||||
it('处理多个混合类型参数', () => {
|
||||
const parameters: Record<string, ToolParameter> = {
|
||||
path: {
|
||||
type: 'string',
|
||||
description: '文件路径',
|
||||
required: true,
|
||||
},
|
||||
line_number: {
|
||||
type: 'number',
|
||||
description: '行号',
|
||||
required: false,
|
||||
},
|
||||
recursive: {
|
||||
type: 'boolean',
|
||||
description: '递归处理',
|
||||
required: false,
|
||||
},
|
||||
patterns: {
|
||||
type: 'array',
|
||||
description: '模式列表',
|
||||
required: false,
|
||||
},
|
||||
options: {
|
||||
type: 'object',
|
||||
description: '额外选项',
|
||||
required: false,
|
||||
},
|
||||
};
|
||||
|
||||
const schema = buildZodSchema(parameters);
|
||||
|
||||
// 只提供必填参数
|
||||
expect(schema.safeParse({ path: '/test/file.txt' }).success).toBe(true);
|
||||
|
||||
// 提供所有参数
|
||||
const fullResult = schema.safeParse({
|
||||
path: '/test/file.txt',
|
||||
line_number: 42,
|
||||
recursive: true,
|
||||
patterns: ['*.ts', '*.js'],
|
||||
options: { encoding: 'utf-8' },
|
||||
});
|
||||
expect(fullResult.success).toBe(true);
|
||||
});
|
||||
|
||||
it('空参数对象返回空 schema', () => {
|
||||
const parameters: Record<string, ToolParameter> = {};
|
||||
|
||||
const schema = buildZodSchema(parameters);
|
||||
|
||||
expect(schema.safeParse({}).success).toBe(true);
|
||||
expect(Object.keys(schema.shape)).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('描述信息', () => {
|
||||
it('保留参数描述', () => {
|
||||
const parameters: Record<string, ToolParameter> = {
|
||||
message: {
|
||||
type: 'string',
|
||||
description: '要发送的消息内容',
|
||||
required: true,
|
||||
},
|
||||
};
|
||||
|
||||
const schema = buildZodSchema(parameters);
|
||||
|
||||
// 验证 schema 被正确创建
|
||||
expect(schema.shape.message).toBeDefined();
|
||||
|
||||
// 验证 schema 可以正常工作
|
||||
const result = schema.safeParse({ message: 'test' });
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('实际工具参数示例', () => {
|
||||
it('bash 工具参数', () => {
|
||||
const bashParameters: Record<string, ToolParameter> = {
|
||||
command: {
|
||||
type: 'string',
|
||||
description: '要执行的 bash 命令',
|
||||
required: true,
|
||||
},
|
||||
timeout: {
|
||||
type: 'number',
|
||||
description: '超时时间(毫秒)',
|
||||
required: false,
|
||||
},
|
||||
};
|
||||
|
||||
const schema = buildZodSchema(bashParameters);
|
||||
|
||||
expect(schema.safeParse({ command: 'ls -la' }).success).toBe(true);
|
||||
expect(schema.safeParse({ command: 'ls -la', timeout: 5000 }).success).toBe(true);
|
||||
expect(schema.safeParse({}).success).toBe(false);
|
||||
});
|
||||
|
||||
it('read_file 工具参数', () => {
|
||||
const readFileParameters: Record<string, ToolParameter> = {
|
||||
path: {
|
||||
type: 'string',
|
||||
description: '文件路径',
|
||||
required: true,
|
||||
},
|
||||
encoding: {
|
||||
type: 'string',
|
||||
description: '文件编码',
|
||||
required: false,
|
||||
},
|
||||
};
|
||||
|
||||
const schema = buildZodSchema(readFileParameters);
|
||||
|
||||
expect(schema.safeParse({ path: '/etc/hosts' }).success).toBe(true);
|
||||
expect(schema.safeParse({ path: '/etc/hosts', encoding: 'utf-8' }).success).toBe(true);
|
||||
});
|
||||
|
||||
it('write_file 工具参数', () => {
|
||||
const writeFileParameters: Record<string, ToolParameter> = {
|
||||
path: {
|
||||
type: 'string',
|
||||
description: '文件路径',
|
||||
required: true,
|
||||
},
|
||||
content: {
|
||||
type: 'string',
|
||||
description: '文件内容',
|
||||
required: true,
|
||||
},
|
||||
create_dirs: {
|
||||
type: 'boolean',
|
||||
description: '自动创建目录',
|
||||
required: false,
|
||||
},
|
||||
};
|
||||
|
||||
const schema = buildZodSchema(writeFileParameters);
|
||||
|
||||
expect(schema.safeParse({ path: '/tmp/test.txt', content: 'hello' }).success).toBe(true);
|
||||
expect(schema.safeParse({ path: '/tmp/test.txt', content: 'hello', create_dirs: true }).success).toBe(true);
|
||||
expect(schema.safeParse({ path: '/tmp/test.txt' }).success).toBe(false);
|
||||
});
|
||||
|
||||
it('task 工具参数', () => {
|
||||
const taskParameters: Record<string, ToolParameter> = {
|
||||
description: {
|
||||
type: 'string',
|
||||
description: '任务描述',
|
||||
required: true,
|
||||
},
|
||||
prompt: {
|
||||
type: 'string',
|
||||
description: '任务提示',
|
||||
required: true,
|
||||
},
|
||||
subagent_type: {
|
||||
type: 'string',
|
||||
description: 'Agent 类型',
|
||||
required: true,
|
||||
},
|
||||
run_in_background: {
|
||||
type: 'boolean',
|
||||
description: '是否后台运行',
|
||||
required: false,
|
||||
},
|
||||
images: {
|
||||
type: 'array',
|
||||
description: '图片数据',
|
||||
required: false,
|
||||
},
|
||||
};
|
||||
|
||||
const schema = buildZodSchema(taskParameters);
|
||||
|
||||
expect(schema.safeParse({
|
||||
description: 'Test task',
|
||||
prompt: 'Do something',
|
||||
subagent_type: 'explore',
|
||||
}).success).toBe(true);
|
||||
|
||||
expect(schema.safeParse({
|
||||
description: 'Vision task',
|
||||
prompt: 'Analyze image',
|
||||
subagent_type: 'vision',
|
||||
images: [{ data: 'base64...', mimeType: 'image/png' }],
|
||||
}).success).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,282 @@
|
||||
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
|
||||
import { TerminalUI } from '../../../src/ui/terminal.js';
|
||||
|
||||
// Mock readline
|
||||
const mockReadline = {
|
||||
question: vi.fn(),
|
||||
close: vi.fn(),
|
||||
on: vi.fn(),
|
||||
};
|
||||
|
||||
vi.mock('readline', () => ({
|
||||
createInterface: vi.fn(() => mockReadline),
|
||||
}));
|
||||
|
||||
// Mock chalk
|
||||
vi.mock('chalk', () => ({
|
||||
default: {
|
||||
cyan: vi.fn((s: string) => s),
|
||||
white: vi.fn((s: string) => s),
|
||||
gray: vi.fn((s: string) => s),
|
||||
yellow: vi.fn((s: string) => s),
|
||||
green: vi.fn((s: string) => s),
|
||||
red: vi.fn((s: string) => s),
|
||||
blue: vi.fn((s: string) => s),
|
||||
magenta: vi.fn((s: string) => s),
|
||||
bold: { white: vi.fn((s: string) => s) },
|
||||
},
|
||||
}));
|
||||
|
||||
// Mock agent registry
|
||||
vi.mock('../../../src/agent/index.js', () => ({
|
||||
agentRegistry: {
|
||||
listPrimaryAgents: vi.fn(() => [
|
||||
{ name: 'code-reviewer', description: '代码审查', mode: 'primary' },
|
||||
]),
|
||||
get: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
import * as readline from 'readline';
|
||||
import { agentRegistry } from '../../../src/agent/index.js';
|
||||
|
||||
// Mock Agent class
|
||||
const mockAgent = {
|
||||
getContextUsage: vi.fn(() => ({
|
||||
input: 1000,
|
||||
available: 10000,
|
||||
contextLimit: 128000,
|
||||
usagePercent: 10,
|
||||
})),
|
||||
getAgentModeName: vi.fn(() => 'default'),
|
||||
setAgentMode: vi.fn(),
|
||||
getToolCount: vi.fn(() => ({ total: 10 })),
|
||||
clearHistory: vi.fn(),
|
||||
compactHistory: vi.fn().mockResolvedValue({
|
||||
type: 'compact',
|
||||
freedTokens: 500,
|
||||
}),
|
||||
chat: vi.fn(),
|
||||
};
|
||||
|
||||
describe('TerminalUI - 终端界面', () => {
|
||||
let ui: TerminalUI;
|
||||
let consoleLogSpy: ReturnType<typeof vi.spyOn>;
|
||||
let stdoutWriteSpy: ReturnType<typeof vi.spyOn>;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
ui = new TerminalUI(mockAgent as any);
|
||||
|
||||
// 模拟 close 事件监听
|
||||
const closeHandler = vi.mocked(mockReadline.on).mock.calls.find(
|
||||
call => call[0] === 'close'
|
||||
)?.[1];
|
||||
if (closeHandler) {
|
||||
// 保存 close handler 以便测试
|
||||
}
|
||||
|
||||
consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
||||
stdoutWriteSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
consoleLogSpy.mockRestore();
|
||||
stdoutWriteSpy.mockRestore();
|
||||
});
|
||||
|
||||
describe('构造函数', () => {
|
||||
it('创建 readline 接口', () => {
|
||||
expect(readline.createInterface).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('监听 close 事件', () => {
|
||||
expect(mockReadline.on).toHaveBeenCalledWith('close', expect.any(Function));
|
||||
});
|
||||
});
|
||||
|
||||
describe('close - 关闭', () => {
|
||||
it('关闭 readline', () => {
|
||||
ui.close();
|
||||
|
||||
expect(mockReadline.close).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('多次关闭只执行一次', () => {
|
||||
ui.close();
|
||||
ui.close();
|
||||
|
||||
expect(mockReadline.close).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatContextUsage (通过 prompt 间接测试)', () => {
|
||||
it('低使用率显示绿色', () => {
|
||||
mockAgent.getContextUsage.mockReturnValue({
|
||||
input: 1000,
|
||||
available: 100000,
|
||||
contextLimit: 128000,
|
||||
usagePercent: 10,
|
||||
});
|
||||
|
||||
// 通过创建新实例触发格式化
|
||||
new TerminalUI(mockAgent as any);
|
||||
expect(mockAgent.getContextUsage).toBeDefined();
|
||||
});
|
||||
|
||||
it('中等使用率显示黄色', () => {
|
||||
mockAgent.getContextUsage.mockReturnValue({
|
||||
input: 60000,
|
||||
available: 100000,
|
||||
contextLimit: 128000,
|
||||
usagePercent: 60,
|
||||
});
|
||||
|
||||
new TerminalUI(mockAgent as any);
|
||||
expect(mockAgent.getContextUsage).toBeDefined();
|
||||
});
|
||||
|
||||
it('高使用率显示红色', () => {
|
||||
mockAgent.getContextUsage.mockReturnValue({
|
||||
input: 100000,
|
||||
available: 20000,
|
||||
contextLimit: 128000,
|
||||
usagePercent: 90,
|
||||
});
|
||||
|
||||
new TerminalUI(mockAgent as any);
|
||||
expect(mockAgent.getContextUsage).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('命令处理', () => {
|
||||
describe('/help 命令', () => {
|
||||
it('显示帮助信息', async () => {
|
||||
// 模拟 handleCommand 通过 question 回调
|
||||
mockReadline.question.mockImplementation((_, callback: (answer: string) => void) => {
|
||||
callback('/help');
|
||||
});
|
||||
|
||||
// 验证帮助方法可以被调用
|
||||
expect(mockAgent.getContextUsage).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('/clear 命令', () => {
|
||||
it('清空历史', async () => {
|
||||
mockAgent.clearHistory.mockResolvedValue(undefined);
|
||||
|
||||
// 验证方法存在
|
||||
expect(mockAgent.clearHistory).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('/compact 命令', () => {
|
||||
it('压缩历史', async () => {
|
||||
expect(mockAgent.compactHistory).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('/context 命令', () => {
|
||||
it('显示上下文使用', async () => {
|
||||
expect(mockAgent.getContextUsage).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('/agent 命令', () => {
|
||||
it('无参数显示当前模式', () => {
|
||||
expect(mockAgent.getAgentModeName).toBeDefined();
|
||||
expect(agentRegistry.listPrimaryAgents).toBeDefined();
|
||||
});
|
||||
|
||||
it('切换到 default 模式', () => {
|
||||
expect(mockAgent.setAgentMode).toBeDefined();
|
||||
expect(mockAgent.getToolCount).toBeDefined();
|
||||
});
|
||||
|
||||
it('切换到指定 Agent', () => {
|
||||
vi.mocked(agentRegistry.get).mockReturnValue({
|
||||
name: 'code-reviewer',
|
||||
description: '代码审查',
|
||||
mode: 'primary',
|
||||
prompt: '你是代码审查助手',
|
||||
});
|
||||
|
||||
expect(agentRegistry.get).toBeDefined();
|
||||
});
|
||||
|
||||
it('subagent 模式不能作为主交互', () => {
|
||||
vi.mocked(agentRegistry.get).mockReturnValue({
|
||||
name: 'explore',
|
||||
description: '探索',
|
||||
mode: 'subagent',
|
||||
prompt: '你是探索助手',
|
||||
});
|
||||
|
||||
// 验证 mode 检查
|
||||
const agent = agentRegistry.get('explore');
|
||||
expect(agent?.mode).toBe('subagent');
|
||||
});
|
||||
|
||||
it('未找到 Agent 显示错误', () => {
|
||||
vi.mocked(agentRegistry.get).mockReturnValue(undefined);
|
||||
|
||||
expect(agentRegistry.get('nonexistent')).toBeUndefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('chat 交互', () => {
|
||||
it('调用 agent.chat', async () => {
|
||||
mockAgent.chat.mockResolvedValue('response');
|
||||
|
||||
expect(mockAgent.chat).toBeDefined();
|
||||
});
|
||||
|
||||
it('处理流式输出', async () => {
|
||||
mockAgent.chat.mockImplementation((_input: string, callback: (text: string) => void) => {
|
||||
callback('Hello');
|
||||
callback(' World');
|
||||
return Promise.resolve();
|
||||
});
|
||||
|
||||
// 验证回调被调用
|
||||
await mockAgent.chat('test', (text: string) => {
|
||||
expect(['Hello', ' World']).toContain(text);
|
||||
});
|
||||
});
|
||||
|
||||
it('处理工具调用输出', async () => {
|
||||
mockAgent.chat.mockImplementation((_input: string, callback: (text: string) => void) => {
|
||||
callback('\n[调用工具: bash]');
|
||||
callback('[结果: success]');
|
||||
return Promise.resolve();
|
||||
});
|
||||
|
||||
await mockAgent.chat('test', () => {});
|
||||
expect(mockAgent.chat).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('处理错误', async () => {
|
||||
mockAgent.chat.mockRejectedValue(new Error('API Error'));
|
||||
|
||||
await expect(mockAgent.chat('test', () => {})).rejects.toThrow('API Error');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Agent 模式显示', () => {
|
||||
it('default 模式不显示指示器', () => {
|
||||
mockAgent.getAgentModeName.mockReturnValue('default');
|
||||
|
||||
const mode = mockAgent.getAgentModeName();
|
||||
expect(mode).toBe('default');
|
||||
});
|
||||
|
||||
it('其他模式显示 @ 指示器', () => {
|
||||
mockAgent.getAgentModeName.mockReturnValue('code-reviewer');
|
||||
|
||||
const mode = mockAgent.getAgentModeName();
|
||||
expect(mode).toBe('code-reviewer');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,338 @@
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
||||
import * as fs from 'fs';
|
||||
import * as os from 'os';
|
||||
import * as path from 'path';
|
||||
|
||||
// Mock fs module
|
||||
vi.mock('fs', () => ({
|
||||
existsSync: vi.fn(),
|
||||
readFileSync: vi.fn(),
|
||||
writeFileSync: vi.fn(),
|
||||
mkdirSync: vi.fn(),
|
||||
}));
|
||||
|
||||
// Mock process.exit
|
||||
const mockExit = vi.spyOn(process, 'exit').mockImplementation(() => {
|
||||
throw new Error('process.exit called');
|
||||
});
|
||||
|
||||
// Mock console.error
|
||||
const mockConsoleError = vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||
|
||||
describe('Config - 配置管理扩展测试', () => {
|
||||
const CONFIG_DIR = path.join(os.homedir(), '.ai-terminal-assistant');
|
||||
const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
// 清理环境变量
|
||||
delete process.env.AI_PROVIDER;
|
||||
delete process.env.ANTHROPIC_API_KEY;
|
||||
delete process.env.DEEPSEEK_API_KEY;
|
||||
delete process.env.OPENAI_API_KEY;
|
||||
delete process.env.AI_MODEL;
|
||||
delete process.env.AI_MAX_TOKENS;
|
||||
delete process.env.AI_BASE_URL;
|
||||
delete process.env.VISION_PROVIDER;
|
||||
delete process.env.VISION_MODEL;
|
||||
delete process.env.VISION_API_KEY;
|
||||
delete process.env.VISION_BASE_URL;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
mockExit.mockClear();
|
||||
mockConsoleError.mockClear();
|
||||
});
|
||||
|
||||
describe('getConfig - 获取原始配置', () => {
|
||||
it('配置文件不存在返回空对象', async () => {
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
// 需要重新导入以获取最新的 mock
|
||||
vi.resetModules();
|
||||
const { getConfig } = await import('../../../src/utils/config.js');
|
||||
|
||||
const config = getConfig();
|
||||
expect(config).toEqual({});
|
||||
});
|
||||
|
||||
it('配置文件存在返回解析后的内容', async () => {
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify({
|
||||
provider: 'anthropic',
|
||||
apiKey: 'test-key',
|
||||
model: 'claude-sonnet-4-20250514',
|
||||
}));
|
||||
|
||||
vi.resetModules();
|
||||
const { getConfig } = await import('../../../src/utils/config.js');
|
||||
|
||||
const config = getConfig();
|
||||
expect(config.provider).toBe('anthropic');
|
||||
expect(config.apiKey).toBe('test-key');
|
||||
});
|
||||
|
||||
it('配置文件解析错误返回空对象', async () => {
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.readFileSync).mockReturnValue('invalid json');
|
||||
|
||||
vi.resetModules();
|
||||
const { getConfig } = await import('../../../src/utils/config.js');
|
||||
|
||||
const config = getConfig();
|
||||
expect(config).toEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadConfig - 加载配置', () => {
|
||||
it('优先使用环境变量中的 API Key', async () => {
|
||||
process.env.ANTHROPIC_API_KEY = 'env-api-key';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify({
|
||||
provider: 'anthropic',
|
||||
apiKey: 'file-api-key',
|
||||
}));
|
||||
|
||||
vi.resetModules();
|
||||
const { loadConfig } = await import('../../../src/utils/config.js');
|
||||
|
||||
const config = loadConfig();
|
||||
expect(config.apiKey).toBe('env-api-key');
|
||||
});
|
||||
|
||||
it('使用配置文件中的 provider', async () => {
|
||||
process.env.DEEPSEEK_API_KEY = 'deepseek-key';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify({
|
||||
provider: 'deepseek',
|
||||
}));
|
||||
|
||||
vi.resetModules();
|
||||
const { loadConfig } = await import('../../../src/utils/config.js');
|
||||
|
||||
const config = loadConfig();
|
||||
expect(config.provider).toBe('deepseek');
|
||||
expect(config.apiKey).toBe('deepseek-key');
|
||||
});
|
||||
|
||||
it('OpenAI provider 使用 OPENAI_API_KEY', async () => {
|
||||
process.env.OPENAI_API_KEY = 'openai-key';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify({
|
||||
provider: 'openai',
|
||||
}));
|
||||
|
||||
vi.resetModules();
|
||||
const { loadConfig } = await import('../../../src/utils/config.js');
|
||||
|
||||
const config = loadConfig();
|
||||
expect(config.provider).toBe('openai');
|
||||
expect(config.apiKey).toBe('openai-key');
|
||||
});
|
||||
|
||||
it('环境变量 AI_MODEL 覆盖配置文件', async () => {
|
||||
process.env.ANTHROPIC_API_KEY = 'test-key';
|
||||
process.env.AI_MODEL = 'claude-opus-4-20250514';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify({
|
||||
provider: 'anthropic',
|
||||
model: 'claude-sonnet-4-20250514',
|
||||
}));
|
||||
|
||||
vi.resetModules();
|
||||
const { loadConfig } = await import('../../../src/utils/config.js');
|
||||
|
||||
const config = loadConfig();
|
||||
expect(config.model).toBe('claude-opus-4-20250514');
|
||||
});
|
||||
|
||||
it('使用默认模型', async () => {
|
||||
process.env.ANTHROPIC_API_KEY = 'test-key';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
vi.resetModules();
|
||||
const { loadConfig } = await import('../../../src/utils/config.js');
|
||||
|
||||
const config = loadConfig();
|
||||
expect(config.model).toBe('claude-sonnet-4-20250514');
|
||||
});
|
||||
|
||||
it('环境变量 AI_BASE_URL 覆盖配置文件', async () => {
|
||||
process.env.ANTHROPIC_API_KEY = 'test-key';
|
||||
process.env.AI_BASE_URL = 'https://custom-api.example.com';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify({
|
||||
provider: 'anthropic',
|
||||
baseUrl: 'https://config-api.example.com',
|
||||
}));
|
||||
|
||||
vi.resetModules();
|
||||
const { loadConfig } = await import('../../../src/utils/config.js');
|
||||
|
||||
const config = loadConfig();
|
||||
expect(config.baseUrl).toBe('https://custom-api.example.com');
|
||||
});
|
||||
|
||||
it('无 API Key 时退出程序', async () => {
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
vi.resetModules();
|
||||
const { loadConfig } = await import('../../../src/utils/config.js');
|
||||
|
||||
expect(() => loadConfig()).toThrow('process.exit called');
|
||||
expect(mockConsoleError).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadVisionConfig - 加载 Vision 配置', () => {
|
||||
it('返回 null 当没有 API Key', async () => {
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
vi.resetModules();
|
||||
const { loadVisionConfig } = await import('../../../src/utils/config.js');
|
||||
|
||||
const config = loadVisionConfig();
|
||||
expect(config).toBeNull();
|
||||
});
|
||||
|
||||
it('使用环境变量配置', async () => {
|
||||
process.env.VISION_PROVIDER = 'openai';
|
||||
process.env.VISION_MODEL = 'gpt-4o';
|
||||
process.env.VISION_API_KEY = 'vision-key';
|
||||
process.env.VISION_BASE_URL = 'https://vision-api.example.com';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
vi.resetModules();
|
||||
const { loadVisionConfig } = await import('../../../src/utils/config.js');
|
||||
|
||||
const config = loadVisionConfig();
|
||||
expect(config).not.toBeNull();
|
||||
expect(config!.provider).toBe('openai');
|
||||
expect(config!.model).toBe('gpt-4o');
|
||||
expect(config!.apiKey).toBe('vision-key');
|
||||
expect(config!.baseUrl).toBe('https://vision-api.example.com');
|
||||
});
|
||||
|
||||
it('默认使用 anthropic provider', async () => {
|
||||
process.env.ANTHROPIC_API_KEY = 'anthropic-key';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
vi.resetModules();
|
||||
const { loadVisionConfig } = await import('../../../src/utils/config.js');
|
||||
|
||||
const config = loadVisionConfig();
|
||||
expect(config).not.toBeNull();
|
||||
expect(config!.provider).toBe('anthropic');
|
||||
});
|
||||
|
||||
it('Vision 专用 Key 优先于 provider Key', async () => {
|
||||
process.env.ANTHROPIC_API_KEY = 'anthropic-key';
|
||||
process.env.VISION_API_KEY = 'vision-specific-key';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
vi.resetModules();
|
||||
const { loadVisionConfig } = await import('../../../src/utils/config.js');
|
||||
|
||||
const config = loadVisionConfig();
|
||||
expect(config!.apiKey).toBe('vision-specific-key');
|
||||
});
|
||||
|
||||
it('从配置文件加载 Vision 设置', async () => {
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify({
|
||||
visionProvider: 'openai',
|
||||
visionModel: 'gpt-4o',
|
||||
visionApiKey: 'config-vision-key',
|
||||
visionBaseUrl: 'https://config-vision.example.com',
|
||||
}));
|
||||
|
||||
vi.resetModules();
|
||||
const { loadVisionConfig } = await import('../../../src/utils/config.js');
|
||||
|
||||
const config = loadVisionConfig();
|
||||
expect(config).not.toBeNull();
|
||||
expect(config!.provider).toBe('openai');
|
||||
expect(config!.model).toBe('gpt-4o');
|
||||
expect(config!.apiKey).toBe('config-vision-key');
|
||||
});
|
||||
|
||||
it('DeepSeek provider 回退到 deepseekApiKey', async () => {
|
||||
process.env.DEEPSEEK_API_KEY = 'deepseek-key';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify({
|
||||
visionProvider: 'deepseek',
|
||||
}));
|
||||
|
||||
vi.resetModules();
|
||||
const { loadVisionConfig } = await import('../../../src/utils/config.js');
|
||||
|
||||
const config = loadVisionConfig();
|
||||
expect(config).not.toBeNull();
|
||||
expect(config!.apiKey).toBe('deepseek-key');
|
||||
});
|
||||
});
|
||||
|
||||
describe('saveConfig - 保存配置', () => {
|
||||
it('创建配置目录(如果不存在)', async () => {
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
vi.resetModules();
|
||||
const { saveConfig } = await import('../../../src/utils/config.js');
|
||||
|
||||
saveConfig({ provider: 'anthropic' });
|
||||
|
||||
expect(fs.mkdirSync).toHaveBeenCalledWith(CONFIG_DIR, { recursive: true });
|
||||
});
|
||||
|
||||
it('合并现有配置', async () => {
|
||||
vi.mocked(fs.existsSync).mockImplementation((p) => p === CONFIG_FILE);
|
||||
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify({
|
||||
provider: 'anthropic',
|
||||
apiKey: 'existing-key',
|
||||
}));
|
||||
|
||||
vi.resetModules();
|
||||
const { saveConfig } = await import('../../../src/utils/config.js');
|
||||
|
||||
saveConfig({ model: 'claude-opus-4-20250514' });
|
||||
|
||||
expect(fs.writeFileSync).toHaveBeenCalledWith(
|
||||
CONFIG_FILE,
|
||||
expect.stringContaining('"model": "claude-opus-4-20250514"')
|
||||
);
|
||||
expect(fs.writeFileSync).toHaveBeenCalledWith(
|
||||
CONFIG_FILE,
|
||||
expect.stringContaining('"provider": "anthropic"')
|
||||
);
|
||||
});
|
||||
|
||||
it('覆盖相同字段', async () => {
|
||||
vi.mocked(fs.existsSync).mockImplementation((p) => p === CONFIG_FILE);
|
||||
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify({
|
||||
provider: 'anthropic',
|
||||
model: 'old-model',
|
||||
}));
|
||||
|
||||
vi.resetModules();
|
||||
const { saveConfig } = await import('../../../src/utils/config.js');
|
||||
|
||||
saveConfig({ model: 'new-model' });
|
||||
|
||||
const writeCall = vi.mocked(fs.writeFileSync).mock.calls[0];
|
||||
const savedConfig = JSON.parse(writeCall[1] as string);
|
||||
expect(savedConfig.model).toBe('new-model');
|
||||
});
|
||||
|
||||
it('解析错误时使用空对象合并', async () => {
|
||||
vi.mocked(fs.existsSync).mockImplementation((p) => p === CONFIG_FILE);
|
||||
vi.mocked(fs.readFileSync).mockReturnValue('invalid json');
|
||||
|
||||
vi.resetModules();
|
||||
const { saveConfig } = await import('../../../src/utils/config.js');
|
||||
|
||||
saveConfig({ provider: 'deepseek' });
|
||||
|
||||
expect(fs.writeFileSync).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,357 @@
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
||||
|
||||
// Mock fs
|
||||
vi.mock('fs', () => ({
|
||||
existsSync: vi.fn(),
|
||||
readFileSync: vi.fn(),
|
||||
writeFileSync: vi.fn(),
|
||||
mkdirSync: vi.fn(),
|
||||
}));
|
||||
|
||||
import * as fs from 'fs';
|
||||
import { getConfig, loadConfig, saveConfig, loadVisionConfig } from '../../../src/utils/config.js';
|
||||
|
||||
describe('Config - 配置管理', () => {
|
||||
const originalEnv = { ...process.env };
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
// 清理环境变量
|
||||
delete process.env.AI_PROVIDER;
|
||||
delete process.env.ANTHROPIC_API_KEY;
|
||||
delete process.env.DEEPSEEK_API_KEY;
|
||||
delete process.env.OPENAI_API_KEY;
|
||||
delete process.env.AI_MODEL;
|
||||
delete process.env.AI_MAX_TOKENS;
|
||||
delete process.env.AI_BASE_URL;
|
||||
delete process.env.VISION_PROVIDER;
|
||||
delete process.env.VISION_MODEL;
|
||||
delete process.env.VISION_API_KEY;
|
||||
delete process.env.VISION_BASE_URL;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
// 恢复环境变量
|
||||
process.env = { ...originalEnv };
|
||||
});
|
||||
|
||||
describe('getConfig - 获取原始配置', () => {
|
||||
it('配置文件存在时返回内容', () => {
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify({
|
||||
provider: 'anthropic',
|
||||
apiKey: 'test-key',
|
||||
model: 'claude-3-opus',
|
||||
}));
|
||||
|
||||
const config = getConfig();
|
||||
|
||||
expect(config.provider).toBe('anthropic');
|
||||
expect(config.apiKey).toBe('test-key');
|
||||
expect(config.model).toBe('claude-3-opus');
|
||||
});
|
||||
|
||||
it('配置文件不存在时返回空对象', () => {
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
const config = getConfig();
|
||||
|
||||
expect(config).toEqual({});
|
||||
});
|
||||
|
||||
it('配置文件解析错误时返回空对象', () => {
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.readFileSync).mockReturnValue('invalid json');
|
||||
|
||||
const config = getConfig();
|
||||
|
||||
expect(config).toEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadConfig - 加载完整配置', () => {
|
||||
it('从环境变量获取 Anthropic 配置', () => {
|
||||
process.env.ANTHROPIC_API_KEY = 'env-anthropic-key';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
const config = loadConfig();
|
||||
|
||||
expect(config.provider).toBe('anthropic');
|
||||
expect(config.apiKey).toBe('env-anthropic-key');
|
||||
expect(config.model).toBe('claude-sonnet-4-20250514');
|
||||
});
|
||||
|
||||
it('从环境变量获取 DeepSeek 配置', () => {
|
||||
process.env.AI_PROVIDER = 'deepseek';
|
||||
process.env.DEEPSEEK_API_KEY = 'env-deepseek-key';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
const config = loadConfig();
|
||||
|
||||
expect(config.provider).toBe('deepseek');
|
||||
expect(config.apiKey).toBe('env-deepseek-key');
|
||||
expect(config.model).toBe('deepseek-chat');
|
||||
});
|
||||
|
||||
it('配置文件优先级高于默认值', () => {
|
||||
process.env.ANTHROPIC_API_KEY = 'env-key';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify({
|
||||
model: 'custom-model',
|
||||
maxTokens: 8192,
|
||||
}));
|
||||
|
||||
const config = loadConfig();
|
||||
|
||||
expect(config.model).toBe('custom-model');
|
||||
expect(config.maxTokens).toBe(8192);
|
||||
});
|
||||
|
||||
it('配置文件中的 provider 优先', () => {
|
||||
process.env.ANTHROPIC_API_KEY = 'anthropic-key';
|
||||
process.env.DEEPSEEK_API_KEY = 'deepseek-key';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify({
|
||||
provider: 'deepseek',
|
||||
deepseekApiKey: 'stored-deepseek-key',
|
||||
}));
|
||||
|
||||
const config = loadConfig();
|
||||
|
||||
expect(config.provider).toBe('deepseek');
|
||||
// 使用环境变量中的 API Key(优先级更高)
|
||||
expect(config.apiKey).toBe('deepseek-key');
|
||||
});
|
||||
|
||||
it('包含系统提示词', () => {
|
||||
process.env.ANTHROPIC_API_KEY = 'test-key';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
const config = loadConfig();
|
||||
|
||||
expect(config.systemPrompt).toBeDefined();
|
||||
expect(config.systemPrompt).toContain('终端');
|
||||
});
|
||||
|
||||
it('默认 maxTokens 为 4096', () => {
|
||||
process.env.ANTHROPIC_API_KEY = 'test-key';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
const config = loadConfig();
|
||||
|
||||
expect(config.maxTokens).toBe(4096);
|
||||
});
|
||||
|
||||
it('环境变量设置的 maxTokens', () => {
|
||||
process.env.ANTHROPIC_API_KEY = 'test-key';
|
||||
process.env.AI_MAX_TOKENS = '16384';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
const config = loadConfig();
|
||||
|
||||
expect(config.maxTokens).toBe(16384);
|
||||
});
|
||||
});
|
||||
|
||||
describe('saveConfig - 保存配置', () => {
|
||||
it('创建目录并保存配置', () => {
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
saveConfig({ provider: 'anthropic', apiKey: 'new-key' });
|
||||
|
||||
expect(fs.mkdirSync).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
{ recursive: true }
|
||||
);
|
||||
expect(fs.writeFileSync).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
expect.stringContaining('anthropic')
|
||||
);
|
||||
});
|
||||
|
||||
it('合并现有配置', () => {
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify({
|
||||
provider: 'anthropic',
|
||||
apiKey: 'old-key',
|
||||
model: 'old-model',
|
||||
}));
|
||||
|
||||
saveConfig({ apiKey: 'new-key' });
|
||||
|
||||
const writeCall = vi.mocked(fs.writeFileSync).mock.calls[0];
|
||||
const savedConfig = JSON.parse(writeCall[1] as string);
|
||||
|
||||
expect(savedConfig.provider).toBe('anthropic'); // 保留
|
||||
expect(savedConfig.apiKey).toBe('new-key'); // 更新
|
||||
expect(savedConfig.model).toBe('old-model'); // 保留
|
||||
});
|
||||
|
||||
it('目录已存在时不重新创建', () => {
|
||||
vi.mocked(fs.existsSync)
|
||||
.mockReturnValueOnce(true) // 目录存在
|
||||
.mockReturnValueOnce(true); // 配置文件存在
|
||||
vi.mocked(fs.readFileSync).mockReturnValue('{}');
|
||||
|
||||
saveConfig({ apiKey: 'test' });
|
||||
|
||||
expect(fs.mkdirSync).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadConfig - baseUrl 支持', () => {
|
||||
it('从环境变量获取 baseUrl', () => {
|
||||
process.env.ANTHROPIC_API_KEY = 'test-key';
|
||||
process.env.AI_BASE_URL = 'https://custom.api.com/v1';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
const config = loadConfig();
|
||||
|
||||
expect(config.baseUrl).toBe('https://custom.api.com/v1');
|
||||
});
|
||||
|
||||
it('从配置文件获取 baseUrl', () => {
|
||||
process.env.ANTHROPIC_API_KEY = 'test-key';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify({
|
||||
baseUrl: 'https://stored.api.com/v1',
|
||||
}));
|
||||
|
||||
const config = loadConfig();
|
||||
|
||||
expect(config.baseUrl).toBe('https://stored.api.com/v1');
|
||||
});
|
||||
|
||||
it('环境变量 baseUrl 优先于配置文件', () => {
|
||||
process.env.ANTHROPIC_API_KEY = 'test-key';
|
||||
process.env.AI_BASE_URL = 'https://env.api.com/v1';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify({
|
||||
baseUrl: 'https://stored.api.com/v1',
|
||||
}));
|
||||
|
||||
const config = loadConfig();
|
||||
|
||||
expect(config.baseUrl).toBe('https://env.api.com/v1');
|
||||
});
|
||||
|
||||
it('OpenAI provider 支持 baseUrl', () => {
|
||||
process.env.AI_PROVIDER = 'openai';
|
||||
process.env.OPENAI_API_KEY = 'test-openai-key';
|
||||
process.env.AI_BASE_URL = 'https://dashscope.aliyuncs.com/compatible-mode/v1';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
const config = loadConfig();
|
||||
|
||||
expect(config.provider).toBe('openai');
|
||||
expect(config.baseUrl).toBe('https://dashscope.aliyuncs.com/compatible-mode/v1');
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadVisionConfig - Vision 配置', () => {
|
||||
it('返回 null 当没有配置 Vision API Key', () => {
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
const visionConfig = loadVisionConfig();
|
||||
|
||||
expect(visionConfig).toBeNull();
|
||||
});
|
||||
|
||||
it('从环境变量获取 Vision 配置', () => {
|
||||
process.env.VISION_PROVIDER = 'openai';
|
||||
process.env.VISION_API_KEY = 'vision-api-key';
|
||||
process.env.VISION_MODEL = 'gpt-4-vision';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
const visionConfig = loadVisionConfig();
|
||||
|
||||
expect(visionConfig).not.toBeNull();
|
||||
expect(visionConfig?.provider).toBe('openai');
|
||||
expect(visionConfig?.apiKey).toBe('vision-api-key');
|
||||
expect(visionConfig?.model).toBe('gpt-4-vision');
|
||||
});
|
||||
|
||||
it('从配置文件获取 Vision 配置', () => {
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify({
|
||||
visionProvider: 'anthropic',
|
||||
visionApiKey: 'stored-vision-key',
|
||||
visionModel: 'claude-3-opus',
|
||||
}));
|
||||
|
||||
const visionConfig = loadVisionConfig();
|
||||
|
||||
expect(visionConfig).not.toBeNull();
|
||||
expect(visionConfig?.provider).toBe('anthropic');
|
||||
expect(visionConfig?.apiKey).toBe('stored-vision-key');
|
||||
expect(visionConfig?.model).toBe('claude-3-opus');
|
||||
});
|
||||
|
||||
it('回退到对应 provider 的 API Key', () => {
|
||||
process.env.ANTHROPIC_API_KEY = 'anthropic-main-key';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
const visionConfig = loadVisionConfig();
|
||||
|
||||
expect(visionConfig).not.toBeNull();
|
||||
expect(visionConfig?.provider).toBe('anthropic'); // 默认
|
||||
expect(visionConfig?.apiKey).toBe('anthropic-main-key'); // 回退
|
||||
});
|
||||
|
||||
it('Vision baseUrl 配置', () => {
|
||||
process.env.VISION_PROVIDER = 'openai';
|
||||
process.env.VISION_API_KEY = 'vision-key';
|
||||
process.env.VISION_BASE_URL = 'https://vision.api.com/v1';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
const visionConfig = loadVisionConfig();
|
||||
|
||||
expect(visionConfig?.baseUrl).toBe('https://vision.api.com/v1');
|
||||
});
|
||||
|
||||
it('从配置文件回退到 deepseek API key', () => {
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify({
|
||||
visionProvider: 'deepseek',
|
||||
deepseekApiKey: 'deepseek-stored-key',
|
||||
}));
|
||||
|
||||
const visionConfig = loadVisionConfig();
|
||||
|
||||
expect(visionConfig?.provider).toBe('deepseek');
|
||||
expect(visionConfig?.apiKey).toBe('deepseek-stored-key');
|
||||
});
|
||||
|
||||
it('从配置文件回退到 openai API key', () => {
|
||||
vi.mocked(fs.existsSync).mockReturnValue(true);
|
||||
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify({
|
||||
visionProvider: 'openai',
|
||||
openaiApiKey: 'openai-stored-key',
|
||||
}));
|
||||
|
||||
const visionConfig = loadVisionConfig();
|
||||
|
||||
expect(visionConfig?.provider).toBe('openai');
|
||||
expect(visionConfig?.apiKey).toBe('openai-stored-key');
|
||||
});
|
||||
|
||||
it('使用默认 Vision 模型', () => {
|
||||
process.env.ANTHROPIC_API_KEY = 'test-key';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
const visionConfig = loadVisionConfig();
|
||||
|
||||
expect(visionConfig?.model).toBe('claude-sonnet-4-20250514');
|
||||
});
|
||||
|
||||
it('Vision 专用 key 优先于 provider key', () => {
|
||||
process.env.ANTHROPIC_API_KEY = 'anthropic-main-key';
|
||||
process.env.VISION_API_KEY = 'vision-specific-key';
|
||||
vi.mocked(fs.existsSync).mockReturnValue(false);
|
||||
|
||||
const visionConfig = loadVisionConfig();
|
||||
|
||||
expect(visionConfig?.apiKey).toBe('vision-specific-key');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,363 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import {
|
||||
computeDiff,
|
||||
formatDiff,
|
||||
countChanges,
|
||||
formatEditDiff,
|
||||
confirmFileChange,
|
||||
} from '../../../src/utils/diff.js';
|
||||
|
||||
// Mock readline
|
||||
vi.mock('readline', () => ({
|
||||
createInterface: vi.fn(() => ({
|
||||
question: vi.fn(),
|
||||
close: vi.fn(),
|
||||
})),
|
||||
}));
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
readFile: vi.fn(),
|
||||
}));
|
||||
|
||||
// Mock chalk (保留原始功能以便测试输出格式)
|
||||
vi.mock('chalk', () => ({
|
||||
default: {
|
||||
yellow: (s: string) => `[yellow]${s}[/yellow]`,
|
||||
cyan: (s: string) => `[cyan]${s}[/cyan]`,
|
||||
white: (s: string) => `[white]${s}[/white]`,
|
||||
gray: (s: string) => `[gray]${s}[/gray]`,
|
||||
red: (s: string) => `[red]${s}[/red]`,
|
||||
green: (s: string) => `[green]${s}[/green]`,
|
||||
},
|
||||
}));
|
||||
|
||||
import * as readline from 'readline';
|
||||
import * as fs from 'fs/promises';
|
||||
|
||||
describe('Diff - 差异比较扩展测试', () => {
|
||||
describe('computeDiff - 计算 diff', () => {
|
||||
it('新文件所有行都是添加', () => {
|
||||
const diff = computeDiff(null, 'line1\nline2\nline3');
|
||||
|
||||
expect(diff.isNew).toBe(true);
|
||||
expect(diff.oldContent).toBeNull();
|
||||
expect(diff.hunks.length).toBe(1);
|
||||
expect(diff.hunks[0].lines.every(l => l.type === 'add')).toBe(true);
|
||||
expect(diff.hunks[0].newCount).toBe(3);
|
||||
});
|
||||
|
||||
it('相同内容返回空 hunks', () => {
|
||||
const content = 'line1\nline2\nline3';
|
||||
const diff = computeDiff(content, content);
|
||||
|
||||
expect(diff.isNew).toBe(false);
|
||||
// 相同内容可能没有实际变化的 hunks
|
||||
const hasChanges = diff.hunks.some(h =>
|
||||
h.lines.some(l => l.type === 'add' || l.type === 'remove')
|
||||
);
|
||||
expect(hasChanges).toBe(false);
|
||||
});
|
||||
|
||||
it('检测添加的行', () => {
|
||||
const oldContent = 'line1\nline2';
|
||||
const newContent = 'line1\nline2\nline3';
|
||||
const diff = computeDiff(oldContent, newContent);
|
||||
|
||||
expect(diff.isNew).toBe(false);
|
||||
const addedLines = diff.hunks.flatMap(h => h.lines.filter(l => l.type === 'add'));
|
||||
expect(addedLines.some(l => l.content === 'line3')).toBe(true);
|
||||
});
|
||||
|
||||
it('检测删除的行', () => {
|
||||
const oldContent = 'line1\nline2\nline3';
|
||||
const newContent = 'line1\nline2';
|
||||
const diff = computeDiff(oldContent, newContent);
|
||||
|
||||
const removedLines = diff.hunks.flatMap(h => h.lines.filter(l => l.type === 'remove'));
|
||||
expect(removedLines.some(l => l.content === 'line3')).toBe(true);
|
||||
});
|
||||
|
||||
it('检测修改的行(删除+添加)', () => {
|
||||
const oldContent = 'line1\nold line\nline3';
|
||||
const newContent = 'line1\nnew line\nline3';
|
||||
const diff = computeDiff(oldContent, newContent);
|
||||
|
||||
const removedLines = diff.hunks.flatMap(h => h.lines.filter(l => l.type === 'remove'));
|
||||
const addedLines = diff.hunks.flatMap(h => h.lines.filter(l => l.type === 'add'));
|
||||
|
||||
expect(removedLines.some(l => l.content === 'old line')).toBe(true);
|
||||
expect(addedLines.some(l => l.content === 'new line')).toBe(true);
|
||||
});
|
||||
|
||||
it('处理空文件', () => {
|
||||
const diff = computeDiff('', 'new content');
|
||||
|
||||
expect(diff.isNew).toBe(false);
|
||||
// 空到有内容是添加
|
||||
const addedLines = diff.hunks.flatMap(h => h.lines.filter(l => l.type === 'add'));
|
||||
expect(addedLines.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('处理多个不连续的变更', () => {
|
||||
const oldContent = 'line1\nkeep1\nold2\nkeep2\nold3\nline6';
|
||||
const newContent = 'line1\nkeep1\nnew2\nkeep2\nnew3\nline6';
|
||||
const diff = computeDiff(oldContent, newContent);
|
||||
|
||||
// 应该有变更
|
||||
const changes = diff.hunks.flatMap(h =>
|
||||
h.lines.filter(l => l.type === 'add' || l.type === 'remove')
|
||||
);
|
||||
expect(changes.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('处理完全不同的内容', () => {
|
||||
const oldContent = 'completely\ndifferent\ncontent';
|
||||
const newContent = 'totally\nnew\nstuff';
|
||||
const diff = computeDiff(oldContent, newContent);
|
||||
|
||||
expect(diff.hunks.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatDiff - 格式化 diff', () => {
|
||||
it('新文件显示 +++ 新文件标记', () => {
|
||||
const diff = computeDiff(null, 'new content');
|
||||
const formatted = formatDiff(diff, '/test/file.ts');
|
||||
|
||||
expect(formatted).toContain('新文件');
|
||||
expect(formatted).toContain('/test/file.ts');
|
||||
});
|
||||
|
||||
it('修改文件显示 --- 和 +++ 标记', () => {
|
||||
const diff = computeDiff('old', 'new');
|
||||
const formatted = formatDiff(diff, '/test/file.ts');
|
||||
|
||||
expect(formatted).toContain('原文件');
|
||||
expect(formatted).toContain('修改后');
|
||||
});
|
||||
|
||||
it('显示 hunk 头部 @@ 信息', () => {
|
||||
const diff = computeDiff('old', 'new');
|
||||
const formatted = formatDiff(diff, '/test/file.ts');
|
||||
|
||||
expect(formatted).toContain('@@');
|
||||
});
|
||||
|
||||
it('添加行使用 + 前缀', () => {
|
||||
const diff = computeDiff('', 'added line');
|
||||
const formatted = formatDiff(diff, '/test/file.ts');
|
||||
|
||||
expect(formatted).toContain('+ added line');
|
||||
});
|
||||
|
||||
it('删除行使用 - 前缀', () => {
|
||||
const diff = computeDiff('removed line', '');
|
||||
const formatted = formatDiff(diff, '/test/file.ts');
|
||||
|
||||
expect(formatted).toContain('- removed line');
|
||||
});
|
||||
});
|
||||
|
||||
describe('countChanges - 统计变更', () => {
|
||||
it('统计添加行数', () => {
|
||||
const diff = computeDiff(null, 'line1\nline2\nline3');
|
||||
const changes = countChanges(diff);
|
||||
|
||||
expect(changes.additions).toBe(3);
|
||||
expect(changes.deletions).toBe(0);
|
||||
});
|
||||
|
||||
it('统计删除行数', () => {
|
||||
const diff = computeDiff('line1\nline2\nline3', '');
|
||||
const changes = countChanges(diff);
|
||||
|
||||
expect(changes.deletions).toBe(3);
|
||||
});
|
||||
|
||||
it('统计混合变更', () => {
|
||||
const diff = computeDiff('old1\nold2', 'new1\nold2\nnew2');
|
||||
const changes = countChanges(diff);
|
||||
|
||||
// 具体数字取决于 diff 算法实现
|
||||
expect(changes.additions).toBeGreaterThanOrEqual(0);
|
||||
expect(changes.deletions).toBeGreaterThanOrEqual(0);
|
||||
});
|
||||
|
||||
it('无变更返回零', () => {
|
||||
const diff = computeDiff('same', 'same');
|
||||
const changes = countChanges(diff);
|
||||
|
||||
expect(changes.additions + changes.deletions).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatEditDiff - 编辑 diff 格式化', () => {
|
||||
it('显示删除和添加内容', () => {
|
||||
const formatted = formatEditDiff('old text', 'new text');
|
||||
|
||||
expect(formatted).toContain('old text');
|
||||
expect(formatted).toContain('new text');
|
||||
expect(formatted).toContain('-');
|
||||
expect(formatted).toContain('+');
|
||||
});
|
||||
|
||||
it('处理多行内容', () => {
|
||||
const formatted = formatEditDiff('old1\nold2', 'new1\nnew2');
|
||||
|
||||
expect(formatted).toContain('old1');
|
||||
expect(formatted).toContain('old2');
|
||||
expect(formatted).toContain('new1');
|
||||
expect(formatted).toContain('new2');
|
||||
});
|
||||
|
||||
it('包含变更内容标题', () => {
|
||||
const formatted = formatEditDiff('old', 'new');
|
||||
|
||||
expect(formatted).toContain('变更内容');
|
||||
});
|
||||
});
|
||||
|
||||
describe('confirmFileChange - 文件变更确认', () => {
|
||||
let consoleLogSpy: ReturnType<typeof vi.spyOn>;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
||||
});
|
||||
|
||||
it('内容相同直接返回确认', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('same content');
|
||||
|
||||
const result = await confirmFileChange('/test/file.ts', 'same content', 'write');
|
||||
|
||||
expect(result.confirmed).toBe(true);
|
||||
expect(result.remember).toBe(false);
|
||||
});
|
||||
|
||||
it('新文件(读取失败)显示 diff', async () => {
|
||||
vi.mocked(fs.readFile).mockRejectedValue(new Error('ENOENT'));
|
||||
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('y')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
const result = await confirmFileChange('/test/file.ts', 'new content', 'write');
|
||||
|
||||
expect(result.confirmed).toBe(true);
|
||||
});
|
||||
|
||||
it('用户输入 y 确认写入', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('old content');
|
||||
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('y')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
const result = await confirmFileChange('/test/file.ts', 'new content', 'write');
|
||||
|
||||
expect(result.confirmed).toBe(true);
|
||||
expect(result.remember).toBe(false);
|
||||
});
|
||||
|
||||
it('用户输入 Y 确认并记住', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('old content');
|
||||
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('Y')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
const result = await confirmFileChange('/test/file.ts', 'new content', 'write');
|
||||
|
||||
expect(result.confirmed).toBe(true);
|
||||
expect(result.remember).toBe(true);
|
||||
});
|
||||
|
||||
it('用户输入 n 取消操作', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('old content');
|
||||
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('n')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
const result = await confirmFileChange('/test/file.ts', 'new content', 'write');
|
||||
|
||||
expect(result.confirmed).toBe(false);
|
||||
expect(result.remember).toBe(false);
|
||||
});
|
||||
|
||||
it('用户输入 N 取消并记住', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('old content');
|
||||
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('N')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
const result = await confirmFileChange('/test/file.ts', 'new content', 'write');
|
||||
|
||||
expect(result.confirmed).toBe(false);
|
||||
expect(result.remember).toBe(true);
|
||||
});
|
||||
|
||||
it('无效输入默认取消', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('old content');
|
||||
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('invalid')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
const result = await confirmFileChange('/test/file.ts', 'new content', 'write');
|
||||
|
||||
expect(result.confirmed).toBe(false);
|
||||
expect(result.remember).toBe(false);
|
||||
});
|
||||
|
||||
it('显示变更预览信息', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('old content');
|
||||
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('y')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
await confirmFileChange('/test/file.ts', 'new content', 'write');
|
||||
|
||||
const output = consoleLogSpy.mock.calls.flat().join('\n');
|
||||
expect(output).toContain('文件变更预览');
|
||||
expect(output).toContain('写入文件');
|
||||
expect(output).toContain('/test/file.ts');
|
||||
});
|
||||
|
||||
it('显示编辑操作类型', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('old content');
|
||||
|
||||
const mockRl = {
|
||||
question: vi.fn((_, callback) => callback('y')),
|
||||
close: vi.fn(),
|
||||
};
|
||||
vi.mocked(readline.createInterface).mockReturnValue(mockRl as any);
|
||||
|
||||
await confirmFileChange('/test/file.ts', 'new content', 'edit');
|
||||
|
||||
const output = consoleLogSpy.mock.calls.flat().join('\n');
|
||||
expect(output).toContain('编辑文件');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
consoleLogSpy.mockRestore();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,353 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { computeDiff, countChanges, formatEditDiff, formatDiff } from '../../../src/utils/diff.js';
|
||||
|
||||
describe('computeDiff - 计算文件差异', () => {
|
||||
describe('新文件', () => {
|
||||
it('新文件所有行标记为新增', () => {
|
||||
const diff = computeDiff(null, 'line1\nline2\nline3');
|
||||
|
||||
expect(diff.isNew).toBe(true);
|
||||
expect(diff.oldContent).toBeNull();
|
||||
expect(diff.hunks).toHaveLength(1);
|
||||
|
||||
const hunk = diff.hunks[0];
|
||||
expect(hunk.oldStart).toBe(0);
|
||||
expect(hunk.oldCount).toBe(0);
|
||||
expect(hunk.newStart).toBe(1);
|
||||
expect(hunk.newCount).toBe(3);
|
||||
|
||||
expect(hunk.lines).toHaveLength(3);
|
||||
expect(hunk.lines.every((l) => l.type === 'add')).toBe(true);
|
||||
});
|
||||
|
||||
it('空新文件', () => {
|
||||
const diff = computeDiff(null, '');
|
||||
|
||||
expect(diff.isNew).toBe(true);
|
||||
expect(diff.hunks).toHaveLength(1);
|
||||
expect(diff.hunks[0].lines).toHaveLength(1); // 空行也是一行
|
||||
});
|
||||
});
|
||||
|
||||
describe('修改文件', () => {
|
||||
it('相同内容无变化', () => {
|
||||
const content = 'line1\nline2\nline3';
|
||||
const diff = computeDiff(content, content);
|
||||
|
||||
expect(diff.isNew).toBe(false);
|
||||
expect(diff.hunks).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('单行修改', () => {
|
||||
const oldContent = 'line1\nline2\nline3';
|
||||
const newContent = 'line1\nmodified\nline3';
|
||||
const diff = computeDiff(oldContent, newContent);
|
||||
|
||||
expect(diff.isNew).toBe(false);
|
||||
expect(diff.hunks.length).toBeGreaterThan(0);
|
||||
|
||||
// 应该有删除和新增
|
||||
const allLines = diff.hunks.flatMap((h) => h.lines);
|
||||
expect(allLines.some((l) => l.type === 'remove' && l.content === 'line2')).toBe(true);
|
||||
expect(allLines.some((l) => l.type === 'add' && l.content === 'modified')).toBe(true);
|
||||
});
|
||||
|
||||
it('添加行', () => {
|
||||
const oldContent = 'line1\nline3';
|
||||
const newContent = 'line1\nline2\nline3';
|
||||
const diff = computeDiff(oldContent, newContent);
|
||||
|
||||
expect(diff.isNew).toBe(false);
|
||||
const allLines = diff.hunks.flatMap((h) => h.lines);
|
||||
expect(allLines.some((l) => l.type === 'add' && l.content === 'line2')).toBe(true);
|
||||
});
|
||||
|
||||
it('删除行', () => {
|
||||
const oldContent = 'line1\nline2\nline3';
|
||||
const newContent = 'line1\nline3';
|
||||
const diff = computeDiff(oldContent, newContent);
|
||||
|
||||
expect(diff.isNew).toBe(false);
|
||||
const allLines = diff.hunks.flatMap((h) => h.lines);
|
||||
expect(allLines.some((l) => l.type === 'remove' && l.content === 'line2')).toBe(true);
|
||||
});
|
||||
|
||||
it('全部替换', () => {
|
||||
const oldContent = 'old1\nold2\nold3';
|
||||
const newContent = 'new1\nnew2';
|
||||
const diff = computeDiff(oldContent, newContent);
|
||||
|
||||
expect(diff.isNew).toBe(false);
|
||||
expect(diff.hunks.length).toBeGreaterThan(0);
|
||||
|
||||
const changes = countChanges(diff);
|
||||
expect(changes.deletions).toBeGreaterThan(0);
|
||||
expect(changes.additions).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('特殊情况', () => {
|
||||
it('空文件变为非空', () => {
|
||||
const diff = computeDiff('', 'new content');
|
||||
|
||||
expect(diff.isNew).toBe(false);
|
||||
expect(diff.hunks.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('非空文件变为空', () => {
|
||||
const diff = computeDiff('old content', '');
|
||||
|
||||
expect(diff.isNew).toBe(false);
|
||||
expect(diff.hunks.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('包含空行的内容', () => {
|
||||
const oldContent = 'line1\n\nline3';
|
||||
const newContent = 'line1\nline2\n\nline3';
|
||||
const diff = computeDiff(oldContent, newContent);
|
||||
|
||||
expect(diff.isNew).toBe(false);
|
||||
// 应该正确处理空行
|
||||
});
|
||||
|
||||
it('单行文件', () => {
|
||||
const diff = computeDiff('old', 'new');
|
||||
|
||||
expect(diff.isNew).toBe(false);
|
||||
expect(diff.hunks.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('行号正确性', () => {
|
||||
it('新增行有正确的行号', () => {
|
||||
const diff = computeDiff(null, 'line1\nline2\nline3');
|
||||
|
||||
const lines = diff.hunks[0].lines;
|
||||
expect(lines[0].lineNumber).toBe(1);
|
||||
expect(lines[1].lineNumber).toBe(2);
|
||||
expect(lines[2].lineNumber).toBe(3);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('countChanges - 统计变更数量', () => {
|
||||
it('新文件计算新增行', () => {
|
||||
const diff = computeDiff(null, 'line1\nline2\nline3');
|
||||
const changes = countChanges(diff);
|
||||
|
||||
expect(changes.additions).toBe(3);
|
||||
expect(changes.deletions).toBe(0);
|
||||
});
|
||||
|
||||
it('修改文件计算增删', () => {
|
||||
const diff = computeDiff('old1\nold2', 'new1\nold2\nnew2');
|
||||
const changes = countChanges(diff);
|
||||
|
||||
expect(changes.additions).toBeGreaterThan(0);
|
||||
expect(changes.deletions).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('空 diff 返回零', () => {
|
||||
const diff = computeDiff('same', 'same');
|
||||
const changes = countChanges(diff);
|
||||
|
||||
expect(changes.additions).toBe(0);
|
||||
expect(changes.deletions).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatEditDiff - 格式化编辑差异', () => {
|
||||
it('显示删除和新增内容', () => {
|
||||
const result = formatEditDiff('old text', 'new text');
|
||||
|
||||
expect(result).toContain('变更内容');
|
||||
expect(result).toContain('old text');
|
||||
expect(result).toContain('new text');
|
||||
});
|
||||
|
||||
it('多行内容正确显示', () => {
|
||||
const result = formatEditDiff('line1\nline2', 'new1\nnew2\nnew3');
|
||||
|
||||
expect(result).toContain('line1');
|
||||
expect(result).toContain('line2');
|
||||
expect(result).toContain('new1');
|
||||
expect(result).toContain('new2');
|
||||
expect(result).toContain('new3');
|
||||
});
|
||||
|
||||
it('空内容处理', () => {
|
||||
const result = formatEditDiff('', 'new');
|
||||
|
||||
expect(result).toContain('new');
|
||||
});
|
||||
});
|
||||
|
||||
describe('DiffResult 结构', () => {
|
||||
it('包含所有必要字段', () => {
|
||||
const diff = computeDiff('old', 'new');
|
||||
|
||||
expect(diff).toHaveProperty('oldContent');
|
||||
expect(diff).toHaveProperty('newContent');
|
||||
expect(diff).toHaveProperty('isNew');
|
||||
expect(diff).toHaveProperty('hunks');
|
||||
});
|
||||
|
||||
it('hunk 包含所有必要字段', () => {
|
||||
const diff = computeDiff('old', 'new');
|
||||
|
||||
if (diff.hunks.length > 0) {
|
||||
const hunk = diff.hunks[0];
|
||||
expect(hunk).toHaveProperty('oldStart');
|
||||
expect(hunk).toHaveProperty('oldCount');
|
||||
expect(hunk).toHaveProperty('newStart');
|
||||
expect(hunk).toHaveProperty('newCount');
|
||||
expect(hunk).toHaveProperty('lines');
|
||||
}
|
||||
});
|
||||
|
||||
it('line 包含所有必要字段', () => {
|
||||
const diff = computeDiff(null, 'content');
|
||||
|
||||
const line = diff.hunks[0].lines[0];
|
||||
expect(line).toHaveProperty('type');
|
||||
expect(line).toHaveProperty('lineNumber');
|
||||
expect(line).toHaveProperty('content');
|
||||
});
|
||||
});
|
||||
|
||||
describe('LCS 算法测试', () => {
|
||||
it('相同前缀保留', () => {
|
||||
const oldContent = 'prefix\ncommon\nold';
|
||||
const newContent = 'prefix\ncommon\nnew';
|
||||
const diff = computeDiff(oldContent, newContent);
|
||||
|
||||
// common 行应该保持为上下文
|
||||
const contextLines = diff.hunks.flatMap((h) =>
|
||||
h.lines.filter((l) => l.type === 'context')
|
||||
);
|
||||
// prefix 和 common 可能作为上下文保留
|
||||
expect(diff.hunks.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('相同后缀保留', () => {
|
||||
const oldContent = 'old\ncommon\nsuffix';
|
||||
const newContent = 'new\ncommon\nsuffix';
|
||||
const diff = computeDiff(oldContent, newContent);
|
||||
|
||||
expect(diff.hunks.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('完全不同的内容', () => {
|
||||
const oldContent = 'a\nb\nc';
|
||||
const newContent = 'x\ny\nz';
|
||||
const diff = computeDiff(oldContent, newContent);
|
||||
|
||||
const changes = countChanges(diff);
|
||||
expect(changes.additions).toBe(3);
|
||||
expect(changes.deletions).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatDiff - 格式化 diff 输出', () => {
|
||||
it('新文件显示新增标记', () => {
|
||||
const diff = computeDiff(null, 'line1\nline2');
|
||||
const result = formatDiff(diff, '/test/file.ts');
|
||||
|
||||
expect(result).toContain('新文件');
|
||||
expect(result).toContain('/test/file.ts');
|
||||
});
|
||||
|
||||
it('修改文件显示原文件和修改后标记', () => {
|
||||
const diff = computeDiff('old', 'new');
|
||||
const result = formatDiff(diff, '/test/file.ts');
|
||||
|
||||
expect(result).toContain('原文件');
|
||||
expect(result).toContain('修改后');
|
||||
expect(result).toContain('/test/file.ts');
|
||||
});
|
||||
|
||||
it('hunk 头部显示正确的行号范围', () => {
|
||||
const diff = computeDiff('line1\nline2', 'line1\nnew\nline2');
|
||||
const result = formatDiff(diff, '/test/file.ts');
|
||||
|
||||
expect(result).toContain('@@');
|
||||
});
|
||||
|
||||
it('新增行显示 + 前缀', () => {
|
||||
const diff = computeDiff(null, 'added line');
|
||||
const result = formatDiff(diff, '/test/file.ts');
|
||||
|
||||
expect(result).toContain('+');
|
||||
expect(result).toContain('added line');
|
||||
});
|
||||
|
||||
it('删除行显示 - 前缀', () => {
|
||||
const diff = computeDiff('removed line', 'new line');
|
||||
const result = formatDiff(diff, '/test/file.ts');
|
||||
|
||||
expect(result).toContain('-');
|
||||
expect(result).toContain('removed line');
|
||||
});
|
||||
|
||||
it('空 diff 返回基本格式', () => {
|
||||
const diff = computeDiff('same', 'same');
|
||||
const result = formatDiff(diff, '/test/file.ts');
|
||||
|
||||
// 没有 hunks 时只有头部
|
||||
expect(result).toContain('/test/file.ts');
|
||||
});
|
||||
});
|
||||
|
||||
describe('实际代码场景', () => {
|
||||
it('函数修改', () => {
|
||||
const oldContent = `function hello() {
|
||||
console.log("Hello");
|
||||
}`;
|
||||
|
||||
const newContent = `function hello() {
|
||||
console.log("Hello World");
|
||||
return true;
|
||||
}`;
|
||||
|
||||
const diff = computeDiff(oldContent, newContent);
|
||||
|
||||
expect(diff.isNew).toBe(false);
|
||||
expect(diff.hunks.length).toBeGreaterThan(0);
|
||||
|
||||
const changes = countChanges(diff);
|
||||
expect(changes.additions).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('导入语句添加', () => {
|
||||
const oldContent = `import { a } from 'module';
|
||||
|
||||
export function test() {}`;
|
||||
|
||||
const newContent = `import { a } from 'module';
|
||||
import { b } from 'another';
|
||||
|
||||
export function test() {}`;
|
||||
|
||||
const diff = computeDiff(oldContent, newContent);
|
||||
|
||||
const changes = countChanges(diff);
|
||||
expect(changes.additions).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('配置文件修改', () => {
|
||||
const oldContent = `{
|
||||
"name": "test",
|
||||
"version": "1.0.0"
|
||||
}`;
|
||||
|
||||
const newContent = `{
|
||||
"name": "test",
|
||||
"version": "1.1.0",
|
||||
"description": "Added description"
|
||||
}`;
|
||||
|
||||
const diff = computeDiff(oldContent, newContent);
|
||||
|
||||
expect(diff.hunks.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,361 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import {
|
||||
isImagePath,
|
||||
extractImageReferences,
|
||||
formatFileSize,
|
||||
loadImage,
|
||||
loadImages,
|
||||
IMAGE_EXTENSIONS,
|
||||
} from '../../../src/utils/image.js';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
readFile: vi.fn(),
|
||||
stat: vi.fn(),
|
||||
}));
|
||||
|
||||
import * as fs from 'fs/promises';
|
||||
|
||||
describe('Image Utils - 图片处理工具', () => {
|
||||
describe('IMAGE_EXTENSIONS', () => {
|
||||
it('包含常见图片扩展名', () => {
|
||||
expect(IMAGE_EXTENSIONS).toContain('.png');
|
||||
expect(IMAGE_EXTENSIONS).toContain('.jpg');
|
||||
expect(IMAGE_EXTENSIONS).toContain('.jpeg');
|
||||
expect(IMAGE_EXTENSIONS).toContain('.gif');
|
||||
expect(IMAGE_EXTENSIONS).toContain('.webp');
|
||||
});
|
||||
});
|
||||
|
||||
describe('isImagePath - 判断是否为图片路径', () => {
|
||||
it('识别 PNG 文件', () => {
|
||||
expect(isImagePath('screenshot.png')).toBe(true);
|
||||
expect(isImagePath('path/to/image.PNG')).toBe(true);
|
||||
});
|
||||
|
||||
it('识别 JPG/JPEG 文件', () => {
|
||||
expect(isImagePath('photo.jpg')).toBe(true);
|
||||
expect(isImagePath('photo.jpeg')).toBe(true);
|
||||
expect(isImagePath('photo.JPG')).toBe(true);
|
||||
});
|
||||
|
||||
it('识别 GIF 文件', () => {
|
||||
expect(isImagePath('animation.gif')).toBe(true);
|
||||
});
|
||||
|
||||
it('识别 WebP 文件', () => {
|
||||
expect(isImagePath('modern.webp')).toBe(true);
|
||||
});
|
||||
|
||||
it('不识别非图片文件', () => {
|
||||
expect(isImagePath('document.txt')).toBe(false);
|
||||
expect(isImagePath('script.ts')).toBe(false);
|
||||
expect(isImagePath('data.json')).toBe(false);
|
||||
expect(isImagePath('readme.md')).toBe(false);
|
||||
});
|
||||
|
||||
it('处理没有扩展名的文件', () => {
|
||||
expect(isImagePath('noextension')).toBe(false);
|
||||
expect(isImagePath('Makefile')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('extractImageReferences - 提取图片引用', () => {
|
||||
it('提取单个 @ 引用', () => {
|
||||
const result = extractImageReferences('请分析这张图片 @screenshot.png');
|
||||
expect(result.imagePaths).toEqual(['screenshot.png']);
|
||||
expect(result.textContent).toBe('请分析这张图片');
|
||||
});
|
||||
|
||||
it('提取多个 @ 引用', () => {
|
||||
const result = extractImageReferences('对比 @before.png 和 @after.jpg');
|
||||
expect(result.imagePaths).toEqual(['before.png', 'after.jpg']);
|
||||
expect(result.textContent).toBe('对比 和');
|
||||
});
|
||||
|
||||
it('提取带路径的图片引用', () => {
|
||||
const result = extractImageReferences('分析 @./images/test.png');
|
||||
expect(result.imagePaths).toEqual(['./images/test.png']);
|
||||
});
|
||||
|
||||
it('提取绝对路径图片引用', () => {
|
||||
const result = extractImageReferences('查看 @/tmp/screenshot.png');
|
||||
expect(result.imagePaths).toEqual(['/tmp/screenshot.png']);
|
||||
});
|
||||
|
||||
it('提取带空格的绝对路径(自动匹配到扩展名)', () => {
|
||||
const result = extractImageReferences('这张图片内容是什么?@/Users/xd/Adobe Express - file.png');
|
||||
expect(result.imagePaths).toEqual(['/Users/xd/Adobe Express - file.png']);
|
||||
expect(result.textContent).toBe('这张图片内容是什么?');
|
||||
});
|
||||
|
||||
it('提取带引号的路径(支持空格)', () => {
|
||||
const result = extractImageReferences('分析 @"./my images/test photo.png"');
|
||||
expect(result.imagePaths).toEqual(['./my images/test photo.png']);
|
||||
expect(result.textContent).toBe('分析');
|
||||
});
|
||||
|
||||
it('提取带单引号的路径', () => {
|
||||
const result = extractImageReferences("查看 @'./path with spaces/image.jpg'");
|
||||
expect(result.imagePaths).toEqual(['./path with spaces/image.jpg']);
|
||||
expect(result.textContent).toBe('查看');
|
||||
});
|
||||
|
||||
it('忽略非图片的 @ 引用', () => {
|
||||
const result = extractImageReferences('请查看 @readme.md 文件');
|
||||
expect(result.imagePaths).toEqual([]);
|
||||
expect(result.textContent).toBe('请查看 @readme.md 文件');
|
||||
});
|
||||
|
||||
it('忽略邮箱地址', () => {
|
||||
const result = extractImageReferences('联系 user@example.com 了解详情');
|
||||
expect(result.imagePaths).toEqual([]);
|
||||
expect(result.textContent).toBe('联系 user@example.com 了解详情');
|
||||
});
|
||||
|
||||
it('混合图片和非图片引用', () => {
|
||||
const result = extractImageReferences(
|
||||
'查看 @screenshot.png 和 @config.json'
|
||||
);
|
||||
expect(result.imagePaths).toEqual(['screenshot.png']);
|
||||
expect(result.textContent).toBe('查看 和 @config.json');
|
||||
});
|
||||
|
||||
it('没有引用时返回原文本', () => {
|
||||
const result = extractImageReferences('这是一段普通文本');
|
||||
expect(result.imagePaths).toEqual([]);
|
||||
expect(result.textContent).toBe('这是一段普通文本');
|
||||
});
|
||||
|
||||
it('只有图片引用时文本内容为空', () => {
|
||||
const result = extractImageReferences('@image.png');
|
||||
expect(result.imagePaths).toEqual(['image.png']);
|
||||
expect(result.textContent).toBe('');
|
||||
});
|
||||
|
||||
it('处理多个空格', () => {
|
||||
const result = extractImageReferences(' @test.png 描述 ');
|
||||
expect(result.imagePaths).toEqual(['test.png']);
|
||||
expect(result.textContent.trim()).toBe('描述');
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatFileSize - 格式化文件大小', () => {
|
||||
it('格式化字节', () => {
|
||||
expect(formatFileSize(0)).toBe('0B');
|
||||
expect(formatFileSize(100)).toBe('100B');
|
||||
expect(formatFileSize(1023)).toBe('1023B');
|
||||
});
|
||||
|
||||
it('格式化 KB', () => {
|
||||
expect(formatFileSize(1024)).toBe('1.0KB');
|
||||
expect(formatFileSize(1536)).toBe('1.5KB');
|
||||
expect(formatFileSize(10240)).toBe('10.0KB');
|
||||
});
|
||||
|
||||
it('格式化 MB', () => {
|
||||
expect(formatFileSize(1024 * 1024)).toBe('1.0MB');
|
||||
expect(formatFileSize(1024 * 1024 * 2.5)).toBe('2.5MB');
|
||||
});
|
||||
|
||||
it('大文件以 MB 为单位', () => {
|
||||
expect(formatFileSize(1024 * 1024 * 1024)).toBe('1024.0MB');
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadImage - 加载图片文件', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('成功加载 PNG 图片', async () => {
|
||||
const mockBuffer = Buffer.from('fake image data');
|
||||
vi.mocked(fs.readFile).mockResolvedValue(mockBuffer);
|
||||
vi.mocked(fs.stat).mockResolvedValue({ size: mockBuffer.length } as any);
|
||||
|
||||
const result = await loadImage('/test/image.png');
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.image).toBeDefined();
|
||||
expect(result.image?.filename).toBe('image.png');
|
||||
expect(result.image?.extension).toBe('.png');
|
||||
expect(result.image?.mimeType).toBe('image/png');
|
||||
expect(result.image?.base64).toBe(mockBuffer.toString('base64'));
|
||||
});
|
||||
|
||||
it('成功加载 JPG 图片', async () => {
|
||||
const mockBuffer = Buffer.from('fake jpg data');
|
||||
vi.mocked(fs.readFile).mockResolvedValue(mockBuffer);
|
||||
vi.mocked(fs.stat).mockResolvedValue({ size: mockBuffer.length } as any);
|
||||
|
||||
const result = await loadImage('/test/photo.jpg');
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.image?.mimeType).toBe('image/jpeg');
|
||||
});
|
||||
|
||||
it('成功加载 JPEG 图片', async () => {
|
||||
const mockBuffer = Buffer.from('fake jpeg data');
|
||||
vi.mocked(fs.readFile).mockResolvedValue(mockBuffer);
|
||||
vi.mocked(fs.stat).mockResolvedValue({ size: mockBuffer.length } as any);
|
||||
|
||||
const result = await loadImage('/test/photo.jpeg');
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.image?.mimeType).toBe('image/jpeg');
|
||||
});
|
||||
|
||||
it('成功加载 GIF 图片', async () => {
|
||||
const mockBuffer = Buffer.from('fake gif data');
|
||||
vi.mocked(fs.readFile).mockResolvedValue(mockBuffer);
|
||||
vi.mocked(fs.stat).mockResolvedValue({ size: mockBuffer.length } as any);
|
||||
|
||||
const result = await loadImage('/test/animation.gif');
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.image?.mimeType).toBe('image/gif');
|
||||
});
|
||||
|
||||
it('成功加载 WebP 图片', async () => {
|
||||
const mockBuffer = Buffer.from('fake webp data');
|
||||
vi.mocked(fs.readFile).mockResolvedValue(mockBuffer);
|
||||
vi.mocked(fs.stat).mockResolvedValue({ size: mockBuffer.length } as any);
|
||||
|
||||
const result = await loadImage('/test/modern.webp');
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.image?.mimeType).toBe('image/webp');
|
||||
});
|
||||
|
||||
it('返回正确的 dataUrl', async () => {
|
||||
const mockBuffer = Buffer.from('test data');
|
||||
vi.mocked(fs.readFile).mockResolvedValue(mockBuffer);
|
||||
vi.mocked(fs.stat).mockResolvedValue({ size: mockBuffer.length } as any);
|
||||
|
||||
const result = await loadImage('/test/image.png');
|
||||
|
||||
expect(result.image?.dataUrl).toBe(
|
||||
`data:image/png;base64,${mockBuffer.toString('base64')}`
|
||||
);
|
||||
});
|
||||
|
||||
it('不支持的格式返回错误', async () => {
|
||||
const result = await loadImage('/test/document.txt');
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('不支持的图片格式');
|
||||
expect(result.error).toContain('.txt');
|
||||
});
|
||||
|
||||
it('文件不存在返回错误', async () => {
|
||||
const error = new Error('File not found');
|
||||
(error as any).code = 'ENOENT';
|
||||
vi.mocked(fs.readFile).mockRejectedValue(error);
|
||||
|
||||
const result = await loadImage('/nonexistent/image.png');
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('图片文件不存在');
|
||||
});
|
||||
|
||||
it('读取文件错误返回错误信息', async () => {
|
||||
vi.mocked(fs.readFile).mockRejectedValue(new Error('Permission denied'));
|
||||
|
||||
const result = await loadImage('/test/image.png');
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('加载图片失败');
|
||||
expect(result.error).toContain('Permission denied');
|
||||
});
|
||||
|
||||
it('处理相对路径', async () => {
|
||||
const mockBuffer = Buffer.from('test');
|
||||
vi.mocked(fs.readFile).mockResolvedValue(mockBuffer);
|
||||
vi.mocked(fs.stat).mockResolvedValue({ size: mockBuffer.length } as any);
|
||||
|
||||
const result = await loadImage('./relative/image.png', '/workdir');
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(fs.readFile).toHaveBeenCalledWith(
|
||||
expect.stringContaining('relative/image.png')
|
||||
);
|
||||
});
|
||||
|
||||
it('处理大写扩展名', async () => {
|
||||
const mockBuffer = Buffer.from('test');
|
||||
vi.mocked(fs.readFile).mockResolvedValue(mockBuffer);
|
||||
vi.mocked(fs.stat).mockResolvedValue({ size: mockBuffer.length } as any);
|
||||
|
||||
const result = await loadImage('/test/image.PNG');
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.image?.mimeType).toBe('image/png');
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadImages - 批量加载图片', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('成功加载多张图片', async () => {
|
||||
const mockBuffer = Buffer.from('test');
|
||||
vi.mocked(fs.readFile).mockResolvedValue(mockBuffer);
|
||||
vi.mocked(fs.stat).mockResolvedValue({ size: mockBuffer.length } as any);
|
||||
|
||||
const result = await loadImages(['/test/a.png', '/test/b.jpg']);
|
||||
|
||||
expect(result.images).toHaveLength(2);
|
||||
expect(result.errors).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('部分加载失败返回错误列表', async () => {
|
||||
const mockBuffer = Buffer.from('test');
|
||||
vi.mocked(fs.readFile).mockImplementation((path) => {
|
||||
if (String(path).includes('good')) {
|
||||
return Promise.resolve(mockBuffer);
|
||||
}
|
||||
const error = new Error('Not found');
|
||||
(error as any).code = 'ENOENT';
|
||||
return Promise.reject(error);
|
||||
});
|
||||
vi.mocked(fs.stat).mockResolvedValue({ size: mockBuffer.length } as any);
|
||||
|
||||
const result = await loadImages(['/test/good.png', '/test/bad.png']);
|
||||
|
||||
expect(result.images).toHaveLength(1);
|
||||
expect(result.errors).toHaveLength(1);
|
||||
expect(result.errors[0].path).toBe('/test/bad.png');
|
||||
});
|
||||
|
||||
it('空列表返回空结果', async () => {
|
||||
const result = await loadImages([]);
|
||||
|
||||
expect(result.images).toHaveLength(0);
|
||||
expect(result.errors).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('全部加载失败时 images 为空', async () => {
|
||||
const error = new Error('Not found');
|
||||
(error as any).code = 'ENOENT';
|
||||
vi.mocked(fs.readFile).mockRejectedValue(error);
|
||||
|
||||
const result = await loadImages(['/test/a.png', '/test/b.png']);
|
||||
|
||||
expect(result.images).toHaveLength(0);
|
||||
expect(result.errors).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('使用自定义工作目录', async () => {
|
||||
const mockBuffer = Buffer.from('test');
|
||||
vi.mocked(fs.readFile).mockResolvedValue(mockBuffer);
|
||||
vi.mocked(fs.stat).mockResolvedValue({ size: mockBuffer.length } as any);
|
||||
|
||||
await loadImages(['./image.png'], '/custom/workdir');
|
||||
|
||||
expect(fs.readFile).toHaveBeenCalledWith(
|
||||
expect.stringContaining('custom/workdir')
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user