Files
youle_cocos/cocoscreator_projects/framework-tests/net/transport.test.ts
T
2026-06-28 14:22:47 +08:00

37 lines
1021 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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);
});