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,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',
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user