Skip to content

Commit dca2180

Browse files
committed
test(js/client): cover runForever lifecycle on Publication and Connection
5 node:test cases mirror tests/test_client_lifecycle.py: runForever blocks until stop(), returns immediately when called after stop(), and resolves multiple concurrent waiters. Pre-fix, every case fails at the first runForever() call with 'is not a function'.
1 parent 996540e commit dca2180

2 files changed

Lines changed: 102 additions & 1 deletion

File tree

js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
],
2727
"scripts": {
2828
"build": "tsc -p tsconfig.json",
29-
"test": "tsc -p tsconfig.test.json && node --test dist-test/test/manifest.test.js dist-test/test/protocol.test.js dist-test/test/client.test.js dist-test/test/client_url.test.js"
29+
"test": "tsc -p tsconfig.test.json && node --test dist-test/test/manifest.test.js dist-test/test/protocol.test.js dist-test/test/client.test.js dist-test/test/client_url.test.js dist-test/test/client_lifecycle.test.js"
3030
},
3131
"dependencies": {
3232
"ws": "^8.18.0"

js/test/client_lifecycle.test.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* runForever() / stop() lifecycle parity with Python (commit 2e5d6bb).
3+
*
4+
* Mirrors tests/test_client_lifecycle.py: runForever() must block until
5+
* stop() resolves it, on both ZhubPublication and ZhubConnection. Before
6+
* the port, users following the Python quickstart got
7+
* `pub.runForever is not a function`.
8+
*/
9+
import { describe, it } from 'node:test';
10+
import assert from 'node:assert/strict';
11+
import { ZhubPublication, ZhubConnection } from '../src/client.js';
12+
import type { Manifest } from '../src/manifest.js';
13+
14+
function fakeManifest(name: string): Manifest {
15+
return {
16+
schema_version: '0.1',
17+
name,
18+
description: '',
19+
operator: '',
20+
capabilities: [],
21+
auth: { type: 'bearer' },
22+
rate_limit: '60/min',
23+
public: false,
24+
contact: '',
25+
extensions: {},
26+
};
27+
}
28+
29+
// Build a Publication/Connection without starting any reconnect loop.
30+
function makePub(): ZhubPublication {
31+
return new ZhubPublication(
32+
{
33+
name: 'p',
34+
description: '',
35+
hubUrl: 'http://127.0.0.1:1',
36+
chatHandler: () => '',
37+
},
38+
fakeManifest('p'),
39+
);
40+
}
41+
function makeConn(): ZhubConnection {
42+
return new ZhubConnection({
43+
aiName: 'a',
44+
apiKey: 'zk_test',
45+
hubUrl: 'http://127.0.0.1:1',
46+
});
47+
}
48+
49+
describe('ZhubPublication.runForever()', () => {
50+
it('blocks until stop() is called', async () => {
51+
const pub = makePub();
52+
let resolved = false;
53+
const p = pub.runForever().then(() => { resolved = true; });
54+
55+
await new Promise((r) => setTimeout(r, 50));
56+
assert.equal(resolved, false, 'runForever resolved before stop()');
57+
58+
await pub.stop();
59+
await p;
60+
assert.equal(resolved, true);
61+
});
62+
63+
it('returns immediately when called after stop()', async () => {
64+
const pub = makePub();
65+
await pub.stop();
66+
const start = Date.now();
67+
await pub.runForever();
68+
assert(Date.now() - start < 100);
69+
});
70+
71+
it('resolves multiple concurrent waiters', async () => {
72+
const pub = makePub();
73+
const a = pub.runForever();
74+
const b = pub.runForever();
75+
await pub.stop();
76+
await Promise.all([a, b]);
77+
});
78+
});
79+
80+
describe('ZhubConnection.runForever()', () => {
81+
it('blocks until stop() is called', async () => {
82+
const conn = makeConn();
83+
let resolved = false;
84+
const p = conn.runForever().then(() => { resolved = true; });
85+
86+
await new Promise((r) => setTimeout(r, 50));
87+
assert.equal(resolved, false, 'runForever resolved before stop()');
88+
89+
await conn.stop();
90+
await p;
91+
assert.equal(resolved, true);
92+
});
93+
94+
it('returns immediately when called after stop()', async () => {
95+
const conn = makeConn();
96+
await conn.stop();
97+
const start = Date.now();
98+
await conn.runForever();
99+
assert(Date.now() - start < 100);
100+
});
101+
});

0 commit comments

Comments
 (0)