Rpc 表覆盖: - 登录连接 5 个(player_login/kick_server/connect_*server/agentserver_game) - 房间生命周期 13 个(create_room/self/other_join_room/.../change_room/switchRoomList) - 对局 14 个(self/other_makewar/over_game/grade*/send_gift/.../beanroom_surrender/...) - 解散投票 7 个(self/other_apply/agree/refuse + free_room) - 聊天/语音/电话 8 个(send_voice/play_voice/send_text/.../hangup_phone/...) - 资产/经济 9 个(update_roomcard/bean/charm + giveCoin/change_star/topup_card/...) - 任务 4 个 + 社交邀请分享 4 个 + 房间列表高级 3 个 - 银行 1 个 + 手机绑定验证码 4 个 + 位置反馈 2 个 - 签名/短码房间 5 个 + VIP/黑名单/白名单 6 个 - H5 专用 1 个(joinRoom) 字面量联合类型 RpcName:编译期校验入参,拼错即报错 自映射契约:每个 Rpc.X === 'X',避免错拼 171/171 tests pass, typecheck exit 0。 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
48 lines
1.8 KiB
TypeScript
48 lines
1.8 KiB
TypeScript
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} 必须等于自身字符串`);
|
|
}
|
|
}); |