* Open a WS subscriber and wait for server_hello + client_hello ack. * Returns a handle exposing the received frame queue. * * Mirrors the queueing pattern from `ws-broadcast.e2e.test.ts` — message * listener is attached BEFORE the `open` event resolves, so frames that land * in the same tick a
( r: RunningServer, sid: string, )
| 210 | * in the same tick as the upgrade aren't lost. |
| 211 | */ |
| 212 | async function openSubscriber( |
| 213 | r: RunningServer, |
| 214 | sid: string, |
| 215 | ): Promise<{ |
| 216 | ws: WebSocket; |
| 217 | received: Record<string, unknown>[]; |
| 218 | }> { |
| 219 | const wsUrl = r.address.replace('http://', 'ws://') + '/api/v1/ws'; |
| 220 | const received: Record<string, unknown>[] = []; |
| 221 | const ws = await new Promise<WebSocket>((resolve, reject) => { |
| 222 | const sock = new WebSocket(wsUrl, ['kimi-code.bearer.test-token']); |
| 223 | sock.on('message', (data) => { |
| 224 | try { |
| 225 | received.push(JSON.parse(wsDataToString(data)) as Record<string, unknown>); |
| 226 | } catch { |
| 227 | // ignore |
| 228 | } |
| 229 | }); |
| 230 | sock.once('open', () => resolve(sock)); |
| 231 | sock.once('error', reject); |
| 232 | }); |
| 233 | // Wait for server_hello. |
| 234 | await waitFor(received, (f) => f['type'] === 'server_hello'); |
| 235 | ws.send( |
| 236 | JSON.stringify({ |
| 237 | type: 'client_hello', |
| 238 | id: 'h1', |
| 239 | payload: { client_id: 'test', subscriptions: [sid] }, |
| 240 | }), |
| 241 | ); |
| 242 | await waitFor(received, (f) => f['type'] === 'ack' && f['id'] === 'h1'); |
| 243 | return { ws, received }; |
| 244 | } |
| 245 | |
| 246 | async function waitFor( |
| 247 | received: Record<string, unknown>[], |
no test coverage detected