5835799b69
- 新增 vitest 配置和测试基础设施 - 添加 adapter.test.ts: 测试 Core 模块初始化和 Agent 管理 (18 tests) - 添加 token.test.ts: 测试 Token 生成、验证和中间件 (25 tests) - 添加 handler.test.ts: 测试权限处理器 (18 tests) - 添加 ws.test.ts: 测试 WebSocket 连接和消息处理 (19 tests) - 添加 sse.test.ts: 测试 SSE 事件发送 (14 tests) - 添加 sessions.test.ts: 测试会话路由 (16 tests) - 添加 config.test.ts: 测试配置路由 (10 tests) - 添加 context.test.ts: 测试上下文压缩路由 (9 tests) - 添加 providers.test.ts: 测试 Provider 管理路由 (18 tests) - 添加 manager.test.ts: 测试 SessionManager (48 tests) 总计 195 个测试,覆盖率从 0% 提升至 29.59%
142 lines
3.8 KiB
TypeScript
142 lines
3.8 KiB
TypeScript
/**
|
|
* SSE (Server-Sent Events) Handler 测试
|
|
*
|
|
* 测试 SSE 事件格式、订阅者管理、各类事件发送等功能
|
|
*/
|
|
|
|
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
|
|
// Mock dependencies before imports
|
|
const mockExists = vi.fn();
|
|
|
|
vi.mock('../../src/session/manager.js', () => ({
|
|
getSessionManager: vi.fn(() => ({
|
|
exists: mockExists,
|
|
})),
|
|
}));
|
|
|
|
// Import after mocking - note: some functions are internal and not testable directly
|
|
import {
|
|
emitEvent,
|
|
broadcastEvent,
|
|
emitStatusEvent,
|
|
emitLogEvent,
|
|
emitProgressEvent,
|
|
emitFileChangeEvent,
|
|
getSSEStats,
|
|
} from '../../src/sse.js';
|
|
|
|
describe('SSE Handler', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mockExists.mockReturnValue(true);
|
|
});
|
|
|
|
describe('emitEvent - 发送事件', () => {
|
|
it('无订阅者时不抛出错误', () => {
|
|
expect(() => {
|
|
emitEvent('non-existent-session', {
|
|
event: 'test',
|
|
data: { timestamp: Date.now(), payload: null },
|
|
});
|
|
}).not.toThrow();
|
|
});
|
|
});
|
|
|
|
describe('broadcastEvent - 广播事件', () => {
|
|
it('无订阅者时不抛出错误', () => {
|
|
expect(() => {
|
|
broadcastEvent({
|
|
event: 'broadcast-test',
|
|
data: { timestamp: Date.now(), payload: { message: 'hello' } },
|
|
});
|
|
}).not.toThrow();
|
|
});
|
|
});
|
|
|
|
describe('emitStatusEvent - 发送状态事件', () => {
|
|
it('调用时不抛出错误', () => {
|
|
expect(() => {
|
|
emitStatusEvent('session-1', 'processing', { step: 1, total: 5 });
|
|
}).not.toThrow();
|
|
});
|
|
|
|
it('只传状态不传详情时不抛出错误', () => {
|
|
expect(() => {
|
|
emitStatusEvent('session-1', 'idle');
|
|
}).not.toThrow();
|
|
});
|
|
});
|
|
|
|
describe('emitLogEvent - 发送日志事件', () => {
|
|
it('发送 info 级别日志', () => {
|
|
expect(() => {
|
|
emitLogEvent('session-1', 'info', 'Processing started');
|
|
}).not.toThrow();
|
|
});
|
|
|
|
it('发送 warn 级别日志', () => {
|
|
expect(() => {
|
|
emitLogEvent('session-1', 'warn', 'Rate limit approaching');
|
|
}).not.toThrow();
|
|
});
|
|
|
|
it('发送 error 级别日志', () => {
|
|
expect(() => {
|
|
emitLogEvent('session-1', 'error', 'Connection failed');
|
|
}).not.toThrow();
|
|
});
|
|
});
|
|
|
|
describe('emitProgressEvent - 发送进度事件', () => {
|
|
it('发送进度(带消息)', () => {
|
|
expect(() => {
|
|
emitProgressEvent('session-1', 50, 'Half way done');
|
|
}).not.toThrow();
|
|
});
|
|
|
|
it('发送进度(不带消息)', () => {
|
|
expect(() => {
|
|
emitProgressEvent('session-1', 100);
|
|
}).not.toThrow();
|
|
});
|
|
|
|
it('发送 0% 进度', () => {
|
|
expect(() => {
|
|
emitProgressEvent('session-1', 0, 'Starting...');
|
|
}).not.toThrow();
|
|
});
|
|
});
|
|
|
|
describe('emitFileChangeEvent - 发送文件变更事件', () => {
|
|
it('发送文件创建事件', () => {
|
|
expect(() => {
|
|
emitFileChangeEvent('session-1', 'created', '/path/to/new/file.ts');
|
|
}).not.toThrow();
|
|
});
|
|
|
|
it('发送文件修改事件', () => {
|
|
expect(() => {
|
|
emitFileChangeEvent('session-1', 'modified', '/path/to/existing/file.ts');
|
|
}).not.toThrow();
|
|
});
|
|
|
|
it('发送文件删除事件', () => {
|
|
expect(() => {
|
|
emitFileChangeEvent('session-1', 'deleted', '/path/to/removed/file.ts');
|
|
}).not.toThrow();
|
|
});
|
|
});
|
|
|
|
describe('getSSEStats - 获取统计信息', () => {
|
|
it('无订阅者时返回 0', () => {
|
|
const stats = getSSEStats();
|
|
// 由于没有实际连接,应该至少有这些属性
|
|
expect(stats).toHaveProperty('sessions');
|
|
expect(stats).toHaveProperty('subscribers');
|
|
expect(typeof stats.sessions).toBe('number');
|
|
expect(typeof stats.subscribers).toBe('number');
|
|
});
|
|
});
|
|
});
|