docs(zulip): 完善Zulip业务模块功能文档

范围: src/business/zulip/README.md
- 补充对外提供的接口章节(14个公共方法)
- 添加使用的项目内部依赖说明(7个依赖)
- 完善核心特性描述(5个特性)
- 添加潜在风险评估(4个风险及缓解措施)
- 优化文档结构和内容完整性
This commit is contained in:
moyin
2026-01-15 10:53:04 +08:00
parent 30a4a2813d
commit ed04b8c92d
32 changed files with 622 additions and 8886 deletions

View File

@@ -12,16 +12,23 @@
* - 大量消息批量处理性能
* - 内存使用和资源清理
*
* 更新记录:
* - 2026-01-14: 重构后更新 - 使用新的四层架构模块
* - ChatService 替代 ZulipService
* - ChatSessionService 替代 SessionManagerService
* - ChatFilterService 替代 MessageFilterService
*
* @author moyin
* @version 1.0.0
* @version 2.0.0
* @since 2026-01-10
* @lastModified 2026-01-14
*/
import { Test, TestingModule } from '@nestjs/testing';
import { ZulipService } from '../../../src/business/zulip/zulip.service';
import { ChatService } from '../../../src/business/chat/chat.service';
import { ChatSessionService } from '../../../src/business/chat/services/chat_session.service';
import { ChatFilterService } from '../../../src/business/chat/services/chat_filter.service';
import { ZulipClientPoolService } from '../../../src/core/zulip_core/services/zulip_client_pool.service';
import { SessionManagerService } from '../../../src/business/zulip/services/session_manager.service';
import { MessageFilterService } from '../../../src/business/zulip/services/message_filter.service';
// 模拟WebSocket网关
class MockWebSocketGateway {
@@ -45,8 +52,8 @@ class MockWebSocketGateway {
}
describe('Zulip聊天性能测试', () => {
let zulipService: ZulipService;
let sessionManager: SessionManagerService;
let chatService: ChatService;
let sessionManager: ChatSessionService;
let mockWebSocketGateway: MockWebSocketGateway;
let mockZulipClientPool: any;
@@ -88,17 +95,17 @@ describe('Zulip聊天性能测试', () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
ZulipService,
ChatService,
{
provide: 'ZULIP_CLIENT_POOL_SERVICE',
useValue: mockZulipClientPool,
},
{
provide: SessionManagerService,
provide: ChatSessionService,
useValue: mockSessionManager,
},
{
provide: MessageFilterService,
provide: ChatFilterService,
useValue: mockMessageFilter,
},
{
@@ -123,12 +130,12 @@ describe('Zulip聊天性能测试', () => {
],
}).compile();
zulipService = module.get<ZulipService>(ZulipService);
sessionManager = module.get<SessionManagerService>(SessionManagerService);
chatService = module.get<ChatService>(ChatService);
sessionManager = module.get<ChatSessionService>(ChatSessionService);
// 设置WebSocket网关
mockWebSocketGateway = new MockWebSocketGateway();
zulipService.setWebSocketGateway(mockWebSocketGateway as any);
chatService.setWebSocketGateway(mockWebSocketGateway as any);
});
beforeEach(() => {
@@ -140,7 +147,7 @@ describe('Zulip聊天性能测试', () => {
it('应该在50ms内完成游戏内广播', async () => {
const startTime = Date.now();
const result = await zulipService.sendChatMessage({
const result = await chatService.sendChatMessage({
socketId: 'test-socket',
content: 'Performance test message',
scope: 'local',
@@ -165,7 +172,7 @@ describe('Zulip聊天性能测试', () => {
const startTime = Date.now();
const result = await zulipService.sendChatMessage({
const result = await chatService.sendChatMessage({
socketId: 'test-socket',
content: 'Async test message',
scope: 'local',
@@ -186,7 +193,7 @@ describe('Zulip聊天性能测试', () => {
const startTime = Date.now();
const promises = Array.from({ length: messageCount }, (_, i) =>
zulipService.sendChatMessage({
chatService.sendChatMessage({
socketId: `socket-${i}`,
content: `Concurrent message ${i}`,
scope: 'local',
@@ -210,7 +217,7 @@ describe('Zulip聊天性能测试', () => {
}, 10000);
it('应该正确广播给地图内的所有玩家', async () => {
await zulipService.sendChatMessage({
await chatService.sendChatMessage({
socketId: 'sender-socket',
content: 'Broadcast test message',
scope: 'local',
@@ -234,7 +241,7 @@ describe('Zulip聊天性能测试', () => {
// 创建批量消息
const batchPromises = Array.from({ length: batchSize }, (_, i) =>
zulipService.sendChatMessage({
chatService.sendChatMessage({
socketId: 'batch-socket',
content: `Batch message ${i}`,
scope: 'local',
@@ -267,7 +274,7 @@ describe('Zulip聊天性能测试', () => {
// 模拟会话创建
for (const sessionId of sessionIds) {
await zulipService.handlePlayerLogin({
await chatService.handlePlayerLogin({
socketId: sessionId,
token: 'valid-jwt-token',
});
@@ -275,7 +282,7 @@ describe('Zulip聊天性能测试', () => {
// 清理所有会话
for (const sessionId of sessionIds) {
await zulipService.handlePlayerLogout(sessionId);
await chatService.handlePlayerLogout(sessionId);
}
// 验证资源清理
@@ -294,7 +301,7 @@ describe('Zulip聊天性能测试', () => {
// 处理大量消息
const promises = largeDataSet.map((item, i) =>
zulipService.sendChatMessage({
chatService.sendChatMessage({
socketId: `memory-test-${i}`,
content: `Memory test ${item.id}: ${item.data.substring(0, 50)}...`,
scope: 'local',
@@ -322,7 +329,7 @@ describe('Zulip聊天性能测试', () => {
it('应该快速处理无效会话', async () => {
const startTime = Date.now();
const result = await zulipService.sendChatMessage({
const result = await chatService.sendChatMessage({
socketId: 'invalid-socket',
content: 'This should fail quickly',
scope: 'local',
@@ -341,7 +348,7 @@ describe('Zulip聊天性能测试', () => {
// 模拟Zulip服务异常
mockZulipClientPool.sendMessage.mockRejectedValue(new Error('Zulip service unavailable'));
const result = await zulipService.sendChatMessage({
const result = await chatService.sendChatMessage({
socketId: 'test-socket',
content: 'Message during Zulip outage',
scope: 'local',
@@ -355,4 +362,4 @@ describe('Zulip聊天性能测试', () => {
expect(broadcastMessages).toHaveLength(1);
});
});
});
});