import { test } from 'node:test'; import assert from 'node:assert/strict'; import { Router } from '../../YouleNexus/assets/framework/protocol/router.ts'; import { ActiveGame } from '../../YouleNexus/assets/framework/protocol/active-game.ts'; import type { InboundMessage } from '../../YouleNexus/assets/framework/core/types/envelope.ts'; import type { IGameModule, GameContext } from '../../YouleNexus/assets/framework/sdk/index.ts'; class MockGame implements IGameModule { readonly route = 'mock'; events: string[] = []; onEnter(_ctx: GameContext): void {} onExit(): void {} onReceive(rpc: string, _d: unknown): void { this.events.push(`onReceive:${rpc}`); } onReconnect(_d: unknown): void {} } const msg = (route: string, rpc: string, data: unknown = {}): InboundMessage => ({ route, rpc, data } as InboundMessage); test('platform route → 平台 handler(已注册)', () => { const slot = new ActiveGame(); const calls: Array<[string, unknown]> = []; const router = new Router({ activeGame: slot, platformHandlers: { player_login: (d) => calls.push(['player_login', d]) }, }); router.dispatch(msg('agent', 'player_login', { state: 0 })); assert.equal(calls.length, 1); assert.equal(calls[0][0], 'player_login'); }); test('platform route → 平台 handler 未注册 → 显式抛错(第二准则)', () => { const slot = new ActiveGame(); const router = new Router({ activeGame: slot, platformHandlers: {} }); assert.throws(() => router.dispatch(msg('agent', 'unknown_rpc')), /unknown_rpc/); }); test('非 platform route 但 activeGame 已注册 → onReceive', () => { const slot = new ActiveGame(); const game = new MockGame(); slot.set(game); const router = new Router({ activeGame: slot, platformHandlers: {} }); router.dispatch(msg('mock', 'deal_card', { n: 1 })); assert.deepEqual(game.events, ['onReceive:deal_card']); }); test('非 platform route 但 activeGame 未注册 → 显式抛错', () => { const slot = new ActiveGame(); const router = new Router({ activeGame: slot, platformHandlers: {} }); assert.throws(() => router.dispatch(msg('mock', 'deal_card')), /mock.*deal_card/); }); test('route 与 activeGame.route 不匹配 → 显式抛错', () => { const slot = new ActiveGame(); slot.set(new MockGame()); // route = 'mock' const router = new Router({ activeGame: slot, platformHandlers: {} }); assert.throws(() => router.dispatch(msg('other-game', 'rpc')), /other-game/); }); test('platform/agent/room 三 route 全部走平台 handler(对称)', () => { const slot = new ActiveGame(); const calls: string[] = []; const router = new Router({ activeGame: slot, platformHandlers: { r1: () => calls.push('r1'), r2: () => calls.push('r2'), r3: () => calls.push('r3'), }, }); router.dispatch(msg('platform', 'r1')); router.dispatch(msg('agent', 'r2')); router.dispatch(msg('room', 'r3')); assert.deepEqual(calls, ['r1', 'r2', 'r3']); });