范围: src/business/zulip/README.md - 补充对外提供的接口章节(14个公共方法) - 添加使用的项目内部依赖说明(7个依赖) - 完善核心特性描述(5个特性) - 添加潜在风险评估(4个风险及缓解措施) - 优化文档结构和内容完整性
196 lines
5.8 KiB
TypeScript
196 lines
5.8 KiB
TypeScript
/**
|
||
* WebSocket测试控制器测试
|
||
*
|
||
* 功能描述:
|
||
* - 测试WebSocket测试工具功能
|
||
* - 验证测试页面生成功能
|
||
* - 测试HTML内容和结构
|
||
* - 验证响应处理
|
||
*
|
||
* 测试范围:
|
||
* - 测试页面生成测试
|
||
* - HTML内容验证测试
|
||
* - 响应处理测试
|
||
* - 错误处理测试
|
||
*
|
||
* 最近修改:
|
||
* - 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 { WebSocketTestController } from './websocket_test.controller';
|
||
import { Response } from 'express';
|
||
|
||
describe('WebSocketTestController', () => {
|
||
let controller: WebSocketTestController;
|
||
let mockResponse: jest.Mocked<Response>;
|
||
|
||
beforeEach(async () => {
|
||
const module: TestingModule = await Test.createTestingModule({
|
||
controllers: [WebSocketTestController],
|
||
}).compile();
|
||
|
||
controller = module.get<WebSocketTestController>(WebSocketTestController);
|
||
|
||
// Mock Express Response object
|
||
mockResponse = {
|
||
send: jest.fn(),
|
||
status: jest.fn().mockReturnThis(),
|
||
json: jest.fn(),
|
||
setHeader: jest.fn(),
|
||
} as any;
|
||
});
|
||
|
||
it('should be defined', () => {
|
||
expect(controller).toBeDefined();
|
||
});
|
||
|
||
describe('getTestPage', () => {
|
||
it('should return WebSocket test page HTML', () => {
|
||
// Act
|
||
controller.getTestPage(mockResponse);
|
||
|
||
// Assert
|
||
expect(mockResponse.send).toHaveBeenCalledWith(expect.stringContaining('<!DOCTYPE html>'));
|
||
expect(mockResponse.send).toHaveBeenCalledWith(expect.stringContaining('WebSocket 测试工具'));
|
||
});
|
||
|
||
it('should include WebSocket connection script', () => {
|
||
// Act
|
||
controller.getTestPage(mockResponse);
|
||
|
||
// Assert
|
||
const htmlContent = mockResponse.send.mock.calls[0][0];
|
||
expect(htmlContent).toContain('WebSocket');
|
||
expect(htmlContent).toContain('connect');
|
||
});
|
||
|
||
it('should include test controls', () => {
|
||
// Act
|
||
controller.getTestPage(mockResponse);
|
||
|
||
// Assert
|
||
const htmlContent = mockResponse.send.mock.calls[0][0];
|
||
expect(htmlContent).toContain('button');
|
||
expect(htmlContent).toContain('input');
|
||
});
|
||
|
||
it('should include connection status display', () => {
|
||
// Act
|
||
controller.getTestPage(mockResponse);
|
||
|
||
// Assert
|
||
const htmlContent = mockResponse.send.mock.calls[0][0];
|
||
expect(htmlContent).toContain('status');
|
||
expect(htmlContent).toContain('connected');
|
||
});
|
||
|
||
it('should include message history display', () => {
|
||
// Act
|
||
controller.getTestPage(mockResponse);
|
||
|
||
// Assert
|
||
const htmlContent = mockResponse.send.mock.calls[0][0];
|
||
expect(htmlContent).toContain('message');
|
||
expect(htmlContent).toContain('log');
|
||
});
|
||
|
||
it('should include notification system features', () => {
|
||
// Act
|
||
controller.getTestPage(mockResponse);
|
||
|
||
// Assert
|
||
const htmlContent = mockResponse.send.mock.calls[0][0];
|
||
expect(htmlContent).toContain('通知');
|
||
expect(htmlContent).toContain('notice'); // 使用实际存在的英文单词
|
||
});
|
||
|
||
it('should include API monitoring features', () => {
|
||
// Act
|
||
controller.getTestPage(mockResponse);
|
||
|
||
// Assert
|
||
const htmlContent = mockResponse.send.mock.calls[0][0];
|
||
expect(htmlContent).toContain('API');
|
||
expect(htmlContent).toContain('监控');
|
||
});
|
||
|
||
it('should generate valid HTML structure', () => {
|
||
// Act
|
||
controller.getTestPage(mockResponse);
|
||
|
||
// Assert
|
||
const htmlContent = mockResponse.send.mock.calls[0][0];
|
||
expect(htmlContent).toContain('<html');
|
||
expect(htmlContent).toContain('<head>');
|
||
expect(htmlContent).toContain('<body>');
|
||
expect(htmlContent).toContain('</html>');
|
||
});
|
||
|
||
it('should include required meta tags', () => {
|
||
// Act
|
||
controller.getTestPage(mockResponse);
|
||
|
||
// Assert
|
||
const htmlContent = mockResponse.send.mock.calls[0][0];
|
||
expect(htmlContent).toContain('<meta charset="UTF-8">');
|
||
expect(htmlContent).toContain('viewport');
|
||
});
|
||
|
||
it('should include WebSocket JavaScript code', () => {
|
||
// Act
|
||
controller.getTestPage(mockResponse);
|
||
|
||
// Assert
|
||
const htmlContent = mockResponse.send.mock.calls[0][0];
|
||
expect(htmlContent).toContain('<script>');
|
||
expect(htmlContent).toContain('WebSocket');
|
||
expect(htmlContent).toContain('</script>');
|
||
});
|
||
|
||
it('should include CSS styling', () => {
|
||
// Act
|
||
controller.getTestPage(mockResponse);
|
||
|
||
// Assert
|
||
const htmlContent = mockResponse.send.mock.calls[0][0];
|
||
expect(htmlContent).toContain('<style>');
|
||
expect(htmlContent).toContain('</style>');
|
||
});
|
||
|
||
it('should include JWT token functionality', () => {
|
||
// Act
|
||
controller.getTestPage(mockResponse);
|
||
|
||
// Assert
|
||
const htmlContent = mockResponse.send.mock.calls[0][0];
|
||
expect(htmlContent).toContain('JWT');
|
||
expect(htmlContent).toContain('token');
|
||
});
|
||
|
||
it('should include login and registration features', () => {
|
||
// Act
|
||
controller.getTestPage(mockResponse);
|
||
|
||
// Assert
|
||
const htmlContent = mockResponse.send.mock.calls[0][0];
|
||
expect(htmlContent).toContain('登录');
|
||
expect(htmlContent).toContain('注册');
|
||
});
|
||
|
||
it('should handle response object correctly', () => {
|
||
// Act
|
||
controller.getTestPage(mockResponse);
|
||
|
||
// Assert
|
||
expect(mockResponse.send).toHaveBeenCalledTimes(1);
|
||
expect(mockResponse.send).toHaveBeenCalledWith(expect.any(String));
|
||
});
|
||
});
|
||
}); |