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