端到端打通「收包 → 路由分界 → RoomStore 更新」」」」」」」」」」」 RoomRPCBus.start() 订阅 NetClient.bus 'message': - route==='room' 的包 → 查 roomHandlers[msg.rpc](ctx, msg.data) - 其他 route 不处理(归 Router 单独管平台/agent) 未注册 room rpc 显式抛错(第二准则:不静默吞包) start/stop 幂等,可重复调用 业务侧在 PlatformSession 构造后: const roomBus = new RoomRPCBus(net.bus, session); roomBus.start(); // 进房前 roomBus.stop(); // 退房后 162/162 tests pass, typecheck exit 0。 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
71 lines
2.6 KiB
TypeScript
71 lines
2.6 KiB
TypeScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { EventBus } from '../../YouleNexus/assets/framework/core/events.ts';
|
|
import { PlatformSession } from '../../YouleNexus/assets/framework/platform/session.ts';
|
|
import { RoomRPCBus } from '../../YouleNexus/assets/framework/platform/room-rpc-bus.ts';
|
|
import type { NetClientEvents } from '../../YouleNexus/assets/framework/net/net-client.ts';
|
|
|
|
test('RoomRPCBus: route=room 的包分发到对应 handler', () => {
|
|
const bus = new EventBus<NetClientEvents>();
|
|
const session = new PlatformSession(bus);
|
|
const roomBus = new RoomRPCBus(bus, session);
|
|
roomBus.start();
|
|
|
|
// 触发 room apply_free_room
|
|
bus.emit('message', { route: 'room', rpc: 'other_apply_free_room', data: { applyer: 2 } } as any);
|
|
assert.equal(session.room.state.value.state, 1, 'apply_free_room 后 state=1');
|
|
|
|
roomBus.stop();
|
|
});
|
|
|
|
test('RoomRPCBus: route=agent/platform 不被分发(归 Router 处理)', () => {
|
|
const bus = new EventBus<NetClientEvents>();
|
|
const session = new PlatformSession(bus);
|
|
const roomBus = new RoomRPCBus(bus, session);
|
|
roomBus.start();
|
|
|
|
// 故意发一个 route=room + rpc 不存在的包,不应崩
|
|
bus.emit('message', { route: 'room', rpc: 'unknown_rpc', data: {} } as any);
|
|
// state 不应变化
|
|
assert.equal(session.room.state.value.state, 0);
|
|
|
|
roomBus.stop();
|
|
});
|
|
|
|
test('RoomRPCBus: stop 后不再分发', () => {
|
|
const bus = new EventBus<NetClientEvents>();
|
|
const session = new PlatformSession(bus);
|
|
const roomBus = new RoomRPCBus(bus, session);
|
|
roomBus.start();
|
|
roomBus.stop();
|
|
|
|
bus.emit('message', { route: 'room', rpc: 'other_apply_free_room', data: {} } as any);
|
|
// stop 后 handler 不该被调用,state 应保持 0
|
|
assert.equal(session.room.state.value.state, 0);
|
|
});
|
|
|
|
test('RoomRPCBus: room rpc 走 handler,其他 route 忽略', () => {
|
|
const bus = new EventBus<NetClientEvents>();
|
|
const session = new PlatformSession(bus);
|
|
const roomBus = new RoomRPCBus(bus, session);
|
|
roomBus.start();
|
|
|
|
// 模拟 login 后玩家入房,players 数组已存在
|
|
session.room.applyRecovery({
|
|
roomcode: 'R1', seat: 1,
|
|
players: [null, { playerid: 100 } as any, null, null],
|
|
} as any);
|
|
|
|
// 第三方加入
|
|
bus.emit('message', {
|
|
route: 'room', rpc: 'other_join_room',
|
|
data: { seat: 3, playerid: 300, nickname: '丙' },
|
|
} as any);
|
|
assert.equal(session.room.state.value.players[2]?.playerid, 300);
|
|
|
|
// 第三方退出
|
|
bus.emit('message', { route: 'room', rpc: 'other_exit_room', data: { seat: 3 } } as any);
|
|
assert.equal(session.room.state.value.players[2], null);
|
|
|
|
roomBus.stop();
|
|
}); |