feat(framework): net Transport 接口 + 测试用 FakeTransport
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { FakeTransport } from '../helpers/fake-transport.ts';
|
||||
|
||||
test('FakeTransport:connect 后异步触发 onOpen', async () => {
|
||||
const t = new FakeTransport();
|
||||
let opened = false;
|
||||
t.onOpen(() => { opened = true; });
|
||||
t.connect('ws://x');
|
||||
await t.flush();
|
||||
assert.equal(opened, true);
|
||||
});
|
||||
|
||||
test('FakeTransport:客户端 send 进入 sent 队列;serverPush 触发 onMessage', async () => {
|
||||
const t = new FakeTransport();
|
||||
const got: string[] = [];
|
||||
t.onMessage((f) => got.push(f));
|
||||
t.connect('ws://x');
|
||||
await t.flush();
|
||||
t.send('hello');
|
||||
assert.deepEqual(t.sent, ['hello']);
|
||||
t.serverPush('world');
|
||||
await t.flush();
|
||||
assert.deepEqual(got, ['world']);
|
||||
});
|
||||
|
||||
test('FakeTransport:close 触发 onClose', async () => {
|
||||
const t = new FakeTransport();
|
||||
let closed = false;
|
||||
t.onClose(() => { closed = true; });
|
||||
t.connect('ws://x');
|
||||
await t.flush();
|
||||
t.close();
|
||||
await t.flush();
|
||||
assert.equal(closed, true);
|
||||
});
|
||||
Reference in New Issue
Block a user