test(server): 补充 checkpoints/mcp/sse/adapter 测试

- 新增 checkpoints.test.ts: 29 个测试覆盖 Checkpoint 管理 API
- 新增 mcp.test.ts: 15 个测试覆盖 MCP 服务器管理 API
- 扩展 sse.test.ts: 添加 handleSSE 路由测试
- 扩展 adapter.test.ts: 添加 cancelProcessing, getContextUsage,
  compressContext, processMessage 测试

覆盖率提升: 60% -> 80.82%
测试数量: 288 -> 344
This commit is contained in:
2025-12-15 00:32:04 +08:00
parent 67e129fce2
commit 7bc4f006a0
4 changed files with 1165 additions and 4 deletions
+37 -4
View File
@@ -5,17 +5,19 @@
*/
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { Hono } from 'hono';
// Mock dependencies before imports
const mockExists = vi.fn();
const mockSessionManager = {
exists: mockExists,
};
vi.mock('../../src/session/manager.js', () => ({
getSessionManager: vi.fn(() => ({
exists: mockExists,
})),
getSessionManager: vi.fn(() => mockSessionManager),
}));
// Import after mocking - note: some functions are internal and not testable directly
// Import after mocking
import {
emitEvent,
broadcastEvent,
@@ -24,6 +26,7 @@ import {
emitProgressEvent,
emitFileChangeEvent,
getSSEStats,
handleSSE,
} from '../../src/sse.js';
describe('SSE Handler', () => {
@@ -138,4 +141,34 @@ describe('SSE Handler', () => {
expect(typeof stats.subscribers).toBe('number');
});
});
describe('handleSSE - SSE 路由处理', () => {
it('会话不存在时返回 404', async () => {
mockExists.mockReturnValue(false);
const app = new Hono();
app.get('/api/sessions/:id/events', handleSSE);
const res = await app.request('/api/sessions/non-existent/events');
const json = await res.json();
expect(res.status).toBe(404);
expect(json.success).toBe(false);
expect(json.error).toBe('Session not found');
});
it('会话存在时返回 SSE 流响应', async () => {
mockExists.mockReturnValue(true);
const app = new Hono();
app.get('/api/sessions/:id/events', handleSSE);
const res = await app.request('/api/sessions/valid-session/events');
// SSE 流应该返回 200 状态码
expect(res.status).toBe(200);
// Content-Type 应该是 text/event-stream
expect(res.headers.get('content-type')).toContain('text/event-stream');
});
});
});