|
| 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