Files
youle_cocos/cocoscreator_projects/framework-tests/protocol/active-game.test.ts
T
joywayerandClaude Opus 5 045d9ec8c9 feat(framework): 收包路由分界(ActiveGame + Router)
C 规范 §4.1 的红线落地:
- platform/agent/room 三个 route → 平台 handlers(未注册显式抛错)
- 其他 route → activeGame.onReceive(rpc, data)(未激活或不匹配显式抛错)

ActiveGame: 当前子游戏槽(set/clear/get),进房时 setGame、离房 clear。
Router: 纯分发,不持状态。错误一律抛(第二准则:不静默吞包)。

128/128 tests pass, typecheck exit 0。

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-31 19:55:05 +08:00

40 lines
1.2 KiB
TypeScript

import { test } from 'node:test';
import assert from 'node:assert/strict';
import { ActiveGame } from '../../YouleNexus/assets/framework/protocol/active-game.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 { this.events.push('onEnter'); }
onExit(): void { this.events.push('onExit'); }
onReceive(rpc: string, _d: unknown): void { this.events.push(`onReceive:${rpc}`); }
onReconnect(_d: unknown): void { this.events.push('onReconnect'); }
}
test('初始: getGame 返回 null', () => {
assert.equal(new ActiveGame().get(), null);
});
test('setGame: 设置后 get 返回同一实例', () => {
const slot = new ActiveGame();
const g = new MockGame();
slot.set(g);
assert.equal(slot.get(), g);
});
test('clear: 清空后 get 返回 null', () => {
const slot = new ActiveGame();
slot.set(new MockGame());
slot.clear();
assert.equal(slot.get(), null);
});
test('set 覆盖前一个 game', () => {
const slot = new ActiveGame();
const g1 = new MockGame();
const g2 = new MockGame();
slot.set(g1);
slot.set(g2);
assert.equal(slot.get(), g2);
});