feat(framework): net 信封编解码 + 握手/心跳/错误包识别
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { encodeOutbound, decodeFrame } from '../../YouleNexus/assets/framework/net/envelope-codec.ts';
|
||||
|
||||
test('encodeOutbound 产出单层信封字符串', () => {
|
||||
const s = encodeOutbound('agent', 'player_login', { openid: 'o' });
|
||||
assert.deepEqual(JSON.parse(s), { app: 'youle', route: 'agent', rpc: 'player_login', data: { openid: 'o' } });
|
||||
});
|
||||
|
||||
test('decodeFrame:握手包 @toconcon 识别为 handshake', () => {
|
||||
const frame = JSON.stringify({ data: '@toconconXYZ...' });
|
||||
assert.deepEqual(decodeFrame(frame), { kind: 'handshake' });
|
||||
});
|
||||
|
||||
test('decodeFrame:心跳包 @serverheartbeat 识别为 heartbeat', () => {
|
||||
const frame = JSON.stringify({ data: JSON.stringify({ com: '@serverheartbeat' }) });
|
||||
assert.deepEqual(decodeFrame(frame), { kind: 'heartbeat' });
|
||||
});
|
||||
|
||||
test('decodeFrame:特殊错误包 webserve-服务器未工作', () => {
|
||||
const frame = JSON.stringify({ data: 'webserve-服务器未工作' });
|
||||
assert.deepEqual(decodeFrame(frame), { kind: 'serverDown' });
|
||||
});
|
||||
|
||||
test('decodeFrame:正常业务包解出内层 {route,rpc,data}', () => {
|
||||
const inner = { route: 'agent', rpc: 'player_login', data: { state: 0, playerid: 9 } };
|
||||
const frame = JSON.stringify({ data: JSON.stringify(inner) });
|
||||
assert.deepEqual(decodeFrame(frame), { kind: 'message', message: inner });
|
||||
});
|
||||
|
||||
test('decodeFrame:内层为对象(非字符串)也能解', () => {
|
||||
const inner = { route: 'room', rpc: 'other_join_room', data: { seat: 1 } };
|
||||
const frame = JSON.stringify({ data: inner });
|
||||
assert.deepEqual(decodeFrame(frame), { kind: 'message', message: inner });
|
||||
});
|
||||
Reference in New Issue
Block a user