feat(framework): net NetClient 可选 debug 收发包日志
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -123,3 +123,41 @@ test('TcpID 去重:旧连接的残留包被丢弃', async () => {
|
||||
await t1.flush();
|
||||
assert.equal(messages.length, 0);
|
||||
});
|
||||
|
||||
test('debug=true 记录 send 与 recv(注入 logger)', async () => {
|
||||
const t = new FakeTransport();
|
||||
const { clock } = fakeClock();
|
||||
const logs: unknown[][] = [];
|
||||
const client = new NetClient({
|
||||
servers: 'ws://a',
|
||||
transportFactory: () => t,
|
||||
clock,
|
||||
debug: true,
|
||||
logger: (...a: unknown[]) => logs.push(a),
|
||||
});
|
||||
client.setIdentity(IDENTITY);
|
||||
client.start();
|
||||
await t.flush();
|
||||
assert.ok(logs.some((l) => l[0] === '[net] →')); // 发出 player_login 被记录
|
||||
t.serverPush(JSON.stringify({ data: JSON.stringify({ route: 'agent', rpc: 'player_login', data: { state: 0, playerid: 9 } }) }));
|
||||
await t.flush();
|
||||
assert.ok(logs.some((l) => l[0] === '[net] ←')); // 收包被记录
|
||||
});
|
||||
|
||||
test('debug 默认关闭:logger 不被调用', async () => {
|
||||
const t = new FakeTransport();
|
||||
const { clock } = fakeClock();
|
||||
const logs: unknown[][] = [];
|
||||
const client = new NetClient({
|
||||
servers: 'ws://a',
|
||||
transportFactory: () => t,
|
||||
clock,
|
||||
logger: (...a: unknown[]) => logs.push(a),
|
||||
});
|
||||
client.setIdentity(IDENTITY);
|
||||
client.start();
|
||||
await t.flush();
|
||||
t.serverPush(JSON.stringify({ data: JSON.stringify({ route: 'agent', rpc: 'player_login', data: { state: 0, playerid: 9 } }) }));
|
||||
await t.flush();
|
||||
assert.equal(logs.length, 0);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user