import { test } from 'node:test'; import assert from 'node:assert/strict'; import { Rpc, type RpcName } from '../../YouleNexus/assets/framework/protocol/routes.ts'; test('Rpc 表: 每个值等于 key(自映射契约,避免错拼)', () => { // 每个 Rpc.X 必须 === 'X'(自映射契约;改任一即不一致) for (const [k, v] of Object.entries(Rpc)) { assert.equal(v, k, `Rpc.${k} 应等于 "${k}",实际 "${v}"`); } }); test('Rpc 表: 至少覆盖核心业务场景', () => { const required: RpcName[] = [ 'player_login', 'kick_server', 'connect_roomserver', 'connect_agentserver', 'create_room', 'self_join_room', 'other_join_room', 'self_exit_room', 'other_exit_room', 'player_prepare', 'change_seat', 'self_makewar', 'other_makewar', 'self_apply_free_room', 'free_room', 'send_text', 'receive_chat', 'update_bean', 'update_roomcard', 'joinRoom', ]; for (const rpc of required) { assert.ok(typeof Rpc[rpc] === 'string', `Rpc.${rpc} 必须是字符串`); } }); test('RpcName 联合类型: 字面量类型正确推导', () => { // 编译期类型测试:这里的赋值如果类型不对,tsc 会报错 const sample: RpcName = 'player_login'; assert.equal(sample, 'player_login'); }); test('Rpc 表: 房间 handler 已覆盖的关键 rpc 都在白名单', () => { // 验证 room-handlers.ts 用的 14 个 rpc 都在 Rpc 表里 const roomRpcs: RpcName[] = [ 'self_join_room', 'other_join_room', 'self_exit_room', 'other_exit_room', 'self_apply_free_room', 'other_apply_free_room', 'self_agree_free_room', 'other_agree_free_room', 'self_refuse_free_room', 'other_refuse_free_room', 'other_offline', 'other_online', 'change_seat', 'free_room', ]; for (const rpc of roomRpcs) { assert.ok(Rpc[rpc] === rpc, `Rpc.${rpc} 必须等于自身字符串`); } });