范围: src/business/zulip/README.md - 补充对外提供的接口章节(14个公共方法) - 添加使用的项目内部依赖说明(7个依赖) - 完善核心特性描述(5个特性) - 添加潜在风险评估(4个风险及缓解措施) - 优化文档结构和内容完整性
250 lines
8.4 KiB
TypeScript
250 lines
8.4 KiB
TypeScript
/**
|
||
* WebSocket文档控制器测试
|
||
*
|
||
* 功能描述:
|
||
* - 测试WebSocket API文档功能
|
||
* - 验证文档内容和结构
|
||
* - 测试消息格式示例
|
||
* - 验证API响应格式
|
||
*
|
||
* 测试范围:
|
||
* - WebSocket文档API测试
|
||
* - 消息示例API测试
|
||
* - 文档结构验证
|
||
* - 响应格式测试
|
||
*
|
||
* 最近修改:
|
||
* - 2026-01-12: Bug修复 - 修复测试用例中的方法名,只测试实际存在的方法 (修改者: moyin)
|
||
* - 2026-01-12: 代码规范优化 - 创建测试文件,确保WebSocket文档控制器功能的测试覆盖 (修改者: moyin)
|
||
*
|
||
* @author moyin
|
||
* @version 1.0.1
|
||
* @since 2026-01-12
|
||
* @lastModified 2026-01-12
|
||
*/
|
||
|
||
import { Test, TestingModule } from '@nestjs/testing';
|
||
import { WebSocketDocsController } from './websocket_docs.controller';
|
||
|
||
describe('WebSocketDocsController', () => {
|
||
let controller: WebSocketDocsController;
|
||
|
||
beforeEach(async () => {
|
||
const module: TestingModule = await Test.createTestingModule({
|
||
controllers: [WebSocketDocsController],
|
||
}).compile();
|
||
|
||
controller = module.get<WebSocketDocsController>(WebSocketDocsController);
|
||
});
|
||
|
||
it('should be defined', () => {
|
||
expect(controller).toBeDefined();
|
||
});
|
||
|
||
describe('getWebSocketDocs', () => {
|
||
it('should return WebSocket API documentation', () => {
|
||
// Act
|
||
const result = controller.getWebSocketDocs();
|
||
|
||
// Assert
|
||
expect(result).toBeDefined();
|
||
expect(result).toHaveProperty('connection');
|
||
expect(result).toHaveProperty('authentication');
|
||
expect(result).toHaveProperty('events');
|
||
expect(result).toHaveProperty('troubleshooting');
|
||
});
|
||
|
||
it('should include connection configuration', () => {
|
||
// Act
|
||
const result = controller.getWebSocketDocs();
|
||
|
||
// Assert
|
||
expect(result.connection).toBeDefined();
|
||
expect(result.connection).toHaveProperty('url');
|
||
expect(result.connection).toHaveProperty('namespace');
|
||
expect(result.connection).toHaveProperty('transports');
|
||
expect(result.connection.url).toContain('wss://');
|
||
});
|
||
|
||
it('should include authentication information', () => {
|
||
// Act
|
||
const result = controller.getWebSocketDocs();
|
||
|
||
// Assert
|
||
expect(result.authentication).toBeDefined();
|
||
expect(result.authentication).toHaveProperty('required');
|
||
expect(result.authentication).toHaveProperty('method');
|
||
expect(result.authentication.required).toBe(true);
|
||
});
|
||
|
||
it('should include client to server events', () => {
|
||
// Act
|
||
const result = controller.getWebSocketDocs();
|
||
|
||
// Assert
|
||
expect(result.events).toBeDefined();
|
||
expect(result.events).toHaveProperty('clientToServer');
|
||
expect(result.events.clientToServer).toHaveProperty('login');
|
||
expect(result.events.clientToServer).toHaveProperty('chat');
|
||
expect(result.events.clientToServer).toHaveProperty('position_update');
|
||
});
|
||
|
||
it('should include server to client events', () => {
|
||
// Act
|
||
const result = controller.getWebSocketDocs();
|
||
|
||
// Assert
|
||
expect(result.events).toBeDefined();
|
||
expect(result.events).toHaveProperty('serverToClient');
|
||
expect(result.events.serverToClient).toHaveProperty('login_success');
|
||
expect(result.events.serverToClient).toHaveProperty('login_error');
|
||
expect(result.events.serverToClient).toHaveProperty('chat_render');
|
||
});
|
||
|
||
it('should include troubleshooting information', () => {
|
||
// Act
|
||
const result = controller.getWebSocketDocs();
|
||
|
||
// Assert
|
||
expect(result).toHaveProperty('troubleshooting');
|
||
expect(result.troubleshooting).toBeDefined();
|
||
});
|
||
|
||
it('should include proper connection options', () => {
|
||
// Act
|
||
const result = controller.getWebSocketDocs();
|
||
|
||
// Assert
|
||
expect(result.connection.options).toBeDefined();
|
||
expect(result.connection.options).toHaveProperty('timeout');
|
||
expect(result.connection.options).toHaveProperty('forceNew');
|
||
expect(result.connection.options).toHaveProperty('reconnection');
|
||
});
|
||
|
||
it('should include message format descriptions', () => {
|
||
// Act
|
||
const result = controller.getWebSocketDocs();
|
||
|
||
// Assert
|
||
expect(result.events.clientToServer.login).toHaveProperty('description');
|
||
expect(result.events.clientToServer.chat).toHaveProperty('description');
|
||
expect(result.events.clientToServer.position_update).toHaveProperty('description');
|
||
});
|
||
|
||
it('should include response format descriptions', () => {
|
||
// Act
|
||
const result = controller.getWebSocketDocs();
|
||
|
||
// Assert
|
||
expect(result.events.serverToClient.login_success).toHaveProperty('description');
|
||
expect(result.events.serverToClient.login_error).toHaveProperty('description');
|
||
// Note: chat_render might not exist in actual implementation, so we'll check what's available
|
||
expect(result.events.serverToClient).toBeDefined();
|
||
});
|
||
});
|
||
|
||
describe('getMessageExamples', () => {
|
||
it('should return message format examples', () => {
|
||
// Act
|
||
const result = controller.getMessageExamples();
|
||
|
||
// Assert
|
||
expect(result).toBeDefined();
|
||
expect(result).toHaveProperty('login');
|
||
expect(result).toHaveProperty('chat');
|
||
expect(result).toHaveProperty('position');
|
||
});
|
||
|
||
it('should include login message examples', () => {
|
||
// Act
|
||
const result = controller.getMessageExamples();
|
||
|
||
// Assert
|
||
expect(result.login).toBeDefined();
|
||
expect(result.login).toHaveProperty('request');
|
||
expect(result.login).toHaveProperty('successResponse');
|
||
expect(result.login).toHaveProperty('errorResponse');
|
||
});
|
||
|
||
it('should include chat message examples', () => {
|
||
// Act
|
||
const result = controller.getMessageExamples();
|
||
|
||
// Assert
|
||
expect(result.chat).toBeDefined();
|
||
expect(result.chat).toHaveProperty('request');
|
||
expect(result.chat).toHaveProperty('successResponse');
|
||
expect(result.chat).toHaveProperty('errorResponse');
|
||
});
|
||
|
||
it('should include position message examples', () => {
|
||
// Act
|
||
const result = controller.getMessageExamples();
|
||
|
||
// Assert
|
||
expect(result.position).toBeDefined();
|
||
expect(result.position).toHaveProperty('request');
|
||
// Position messages might not have responses, so we'll just check the request
|
||
});
|
||
|
||
it('should include valid JWT token example', () => {
|
||
// Act
|
||
const result = controller.getMessageExamples();
|
||
|
||
// Assert
|
||
expect(result.login.request.token).toBeDefined();
|
||
expect(result.login.request.token).toContain('eyJ');
|
||
expect(typeof result.login.request.token).toBe('string');
|
||
});
|
||
|
||
it('should include proper message types', () => {
|
||
// Act
|
||
const result = controller.getMessageExamples();
|
||
|
||
// Assert
|
||
expect(result.login.request.type).toBe('login');
|
||
expect(result.chat.request.t).toBe('chat');
|
||
expect(result.position.request.t).toBe('position');
|
||
});
|
||
|
||
it('should include error response examples', () => {
|
||
// Act
|
||
const result = controller.getMessageExamples();
|
||
|
||
// Assert
|
||
expect(result.login.errorResponse).toBeDefined();
|
||
expect(result.login.errorResponse).toHaveProperty('t');
|
||
expect(result.login.errorResponse).toHaveProperty('message');
|
||
expect(result.login.errorResponse.t).toBe('login_error');
|
||
});
|
||
|
||
it('should include success response examples', () => {
|
||
// Act
|
||
const result = controller.getMessageExamples();
|
||
|
||
// Assert
|
||
expect(result.login.successResponse).toBeDefined();
|
||
expect(result.login.successResponse).toHaveProperty('t');
|
||
expect(result.login.successResponse).toHaveProperty('sessionId');
|
||
expect(result.login.successResponse).toHaveProperty('userId');
|
||
expect(result.login.successResponse.t).toBe('login_success');
|
||
});
|
||
});
|
||
|
||
describe('Controller Structure', () => {
|
||
it('should be a valid NestJS controller', () => {
|
||
expect(controller).toBeDefined();
|
||
expect(controller.constructor).toBeDefined();
|
||
expect(controller.constructor.name).toBe('WebSocketDocsController');
|
||
});
|
||
|
||
it('should have proper API documentation methods', () => {
|
||
expect(typeof controller.getWebSocketDocs).toBe('function');
|
||
expect(typeof controller.getMessageExamples).toBe('function');
|
||
});
|
||
|
||
it('should be properly instantiated by NestJS', () => {
|
||
expect(controller).toBeInstanceOf(WebSocketDocsController);
|
||
});
|
||
});
|
||
}); |