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 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/**

 * @fileoverview Reconnect-robustness tests for `Connection`.

 *

 * The case under test is the one that used to wedge the client in dev: a

 * connect attempt that fails WITHOUT a clean `onClose` (only `onError` fires, as

 * a flaky transport or a mid-handshake network blip can produce). Before the

 * fix the reconnect loop was driven solely by `onClose`, so such a failure broke

 * the chain and the client sat silently disconnected until a full relaunch.

 *

 * The fix makes the loop self-sustaining (a failed attempt reschedules itself

 * from its own catch) and adds a watchdog backstop. The first test pins the

 * primary fix: after an error-only failure the `ws` factory is invoked AGAIN, so

 * the client retries instead of wedging. The remaining tests cover the

 * idempotency guard and that a host close stops the loop.

 */
import { describe, test, expect } from "bun:test";

import { Connection, type ConnectionDeps } from "./connection";
import type { WebSocketLike } from "../../transports";
import { noopLogger } from "../../logger";
import { systemTimers, type CloudClientTimers } from "../../timers";
import {
  PROTOCOL_MAJOR,
  type ConnectionInit,
  type ConnectionAck,
} from "@mentra/cloud-protocol";

/**

 * A scriptable `WebSocketLike` that records its callbacks so the test can drive

 * them by hand. It does nothing on its own: the test (acting as the fake peer)

 * decides whether this socket errors, opens, acks, or closes, and when.

 */
class FakeSocket implements WebSocketLike {
  sent: string[] = [];
  sentBinary: Uint8Array[] = [];
  openCb: (() => void) | null = null;
  messageCb: ((data: string) => void) | null = null;
  closeCb: ((info: { code: number; reason: string }) => void) | null = null;
  errorCb: ((err: unknown) => void) | null = null;
  closed = false;

  send(data: string): void {
    this.sent.push(data);
  }
  sendBinary(data: Uint8Array): void {
    this.sentBinary.push(data);
  }
  close(): void {
    this.closed = true;
  }
  onOpen(cb: () => void): void {
    this.openCb = cb;
  }
  onMessage(cb: (data: string) => void): void {
    this.messageCb = cb;
  }
  onClose(cb: (info: { code: number; reason: string }) => void): void {
    this.closeCb = cb;
  }
  onError(cb: (err: unknown) => void): void {
    this.errorCb = cb;
  }

  /** Drive a successful handshake: open, receive init, then send back an ack. */
  driveSuccessfulHandshake(ack: ConnectionAck): void {
    this.openCb?.();
    this.messageCb?.(
      JSON.stringify({
        v: PROTOCOL_MAJOR,
        type: "connection.ack",
        timestamp: Date.now(),
        payload: ack,
      }),
    );
  }
}

const SAMPLE_ACK: ConnectionAck = {
  sessionId: "s1",
  negotiatedVersion: "2.0.0",
};

const SAMPLE_INIT: ConnectionInit = { protocolVersion: "2.0.0" };

// A tiny, fast, deterministic backoff so the test never waits long. No jitter so
// delays are exact and the test is not flaky.
const FAST_RECONNECT = { baseMs: 5, maxMs: 20, jitter: false };

/** Build a Connection plus the factory state the test inspects. */
function makeConnection(opts: {

  // Sockets are returned in order, one per (re)connect attempt.

  sockets: FakeSocket[];

  // Optional hook to fail a given (re)connect attempt before its socket opens,

  // simulating a transient failure that never produces an onClose.

  getToken?: () => Promise<string>;

  timers?: CloudClientTimers;

}): { conn: Connection; createdCount: () => number } {
  let created = 0;
  const ws = (_url: string): WebSocketLike => {
    const socket = opts.sockets[created] ?? new FakeSocket();
    created += 1;
    return socket;
  };

  const deps: ConnectionDeps = {
    ws,
    url: "wss://example.test/ws",
    getToken: opts.getToken ?? (async () => "test-token"),
    initPayload: () => SAMPLE_INIT,
    reconnect: FAST_RECONNECT,
    timers: opts.timers ?? systemTimers,
    logger: noopLogger,
  };

  return { conn: new Connection(deps), createdCount: () => created };
}

/** Resolve after `ms` of real time (kept small so tests stay fast). */
function wait(ms: number): Promise<void> {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

describe("Connection reconnect robustness", () => {
  test("uses the injected scheduler for handshake and liveness timers", async () => {
    const socket = new FakeSocket();
    const timeouts: Array<{ callback: () => void; delayMs: number }> = [];
    const intervals: Array<{ callback: () => void; intervalMs: number }> = [];
    const clearedTimeouts: unknown[] = [];
    const clearedIntervals: unknown[] = [];
    const timers: CloudClientTimers = {
      setTimeout(callback, delayMs) {
        const handle = { kind: "timeout", index: timeouts.length };
        timeouts.push({ callback, delayMs });
        return handle;
      },
      clearTimeout(handle) {
        clearedTimeouts.push(handle);
      },
      setInterval(callback, intervalMs) {
        const handle = { kind: "interval", index: intervals.length };
        intervals.push({ callback, intervalMs });
        return handle;
      },
      clearInterval(handle) {
        clearedIntervals.push(handle);
      },
    };
    const { conn } = makeConnection({ sockets: [socket], timers });

    const opening = conn.open();
    await Promise.resolve();
    expect(timeouts.map(({ delayMs }) => delayMs)).toEqual([15_000]);
    expect(intervals.map(({ intervalMs }) => intervalMs)).toEqual([20_000]);

    socket.driveSuccessfulHandshake(SAMPLE_ACK);
    await opening;
    expect(intervals.map(({ intervalMs }) => intervalMs)).toEqual([20_000, 15_000]);
    expect(clearedTimeouts).toHaveLength(1);

    conn.close();
    expect(clearedIntervals).toHaveLength(2);
  });

  test("a failed initial open still enters the reconnect loop", async () => {
    const retry = new FakeSocket();
    let tokenCalls = 0;
    const { conn, createdCount } = makeConnection({
      sockets: [retry],
      getToken: async () => {
        tokenCalls += 1;
        if (tokenCalls === 1) {
          throw new Error("cloud temporarily unavailable during app boot");
        }
        return "test-token";
      },
    });

    await conn.open().catch(() => undefined);
    expect(createdCount()).toBe(0);

    await wait(60);
    expect(createdCount()).toBe(1);

    retry.driveSuccessfulHandshake(SAMPLE_ACK);
    await wait(5);
    expect(conn.ack).toEqual(SAMPLE_ACK);

    conn.close();
  });

  test("a reconnect attempt that fails without onClose still schedules the next try", async () => {
    // This is the exact wedge from dev. We get a live session, drop it (entering
    // the reconnect loop), and then make the scheduled reconnect attempt FAIL in
    // a way that produces no clean onClose -- here the attempt's `getToken`
    // rejects, so `connectOnce()` rejects before any socket exists and no close
    // event can ever fire. Pre-fix, the loop relied entirely on onClose to queue
    // the next try, so this killed it: the client sat silently disconnected.
    // Post-fix, the scheduled attempt's catch reschedules itself, so the loop
    // survives and the next attempt is made.
    const first = new FakeSocket(); // initial session
    const third = new FakeSocket(); // the eventual successful retry

    // Attempt 1 (initial open): token ok.
    // Attempt 2 (first scheduled reconnect): token REJECTS -> fails, no onClose.
    // Attempt 3+ (next scheduled reconnect): token ok again.
    let tokenCalls = 0;
    const { conn, createdCount } = makeConnection({
      sockets: [first, /* attempt 2 socket is never opened */ third],
      getToken: async () => {
        tokenCalls += 1;
        if (tokenCalls === 2) {
          throw new Error("transient token/network blip mid-reconnect");
        }
        return "test-token";
      },
    });

    // Establish a live session.
    const opened = conn.open();
    await wait(5);
    first.driveSuccessfulHandshake(SAMPLE_ACK);
    await opened;
    expect(createdCount()).toBe(1);

    // Drop the socket cleanly: this schedules reconnect attempt 2.
    first.closeCb?.({ code: 1006, reason: "abnormal" });

    // Attempt 2 fires, its getToken rejects (no socket, no onClose). Without the
    // self-rescheduling catch the loop would stop here. Give the loop time to
    // make attempt 2 (which fails) AND reschedule + run attempt 3.
    await wait(120);

    // The ws factory was invoked again AFTER the failed-without-close attempt:
    // the loop did NOT wedge. (Attempt 2 never reaches ws() because getToken
    // throws first, so a third created socket proves attempt 3 ran.)
    expect(createdCount()).toBeGreaterThanOrEqual(2);

    // And that later attempt completes a real handshake -- we recovered fully,
    // not just spun. The successful retry is the last socket created.
    const recovered = third;
    recovered.driveSuccessfulHandshake(SAMPLE_ACK);
    await wait(5);
    expect(conn.ack).toEqual(SAMPLE_ACK);

    conn.close();
  });

  test("a socket that fires only onError (never onClose) does not wedge the loop", async () => {
    // The transport-level variant: a `WebSocketLike` that emits onError and
    // never onClose. The handshake eventually times out and rejects the attempt,
    // and the self-rescheduling catch then queues the next try. We assert the
    // loop keeps producing attempts rather than dying on the error-only socket.
    //
    // Note: the handshake timeout is a fixed internal constant, so this test
    // only checks that the FIRST reconnect (driven by the clean drop below) is
    // scheduled and runs; the error-only socket is exercised to prove onError
    // alone never tears anything down prematurely.
    const first = new FakeSocket();
    const second = new FakeSocket();
    const { conn, createdCount } = makeConnection({
      sockets: [first, second],
    });

    const opened = conn.open();
    await wait(5);
    first.driveSuccessfulHandshake(SAMPLE_ACK);
    await opened;
    expect(createdCount()).toBe(1);

    // Drop -> schedule reconnect. The reconnect socket fires only onError.
    first.closeCb?.({ code: 1006, reason: "abnormal" });
    await wait(40);
    expect(createdCount()).toBe(2);

    // onError alone must not tear down or wedge: the socket is still the active
    // one, and a later ack can still complete the handshake.
    second.errorCb?.(new Error("transient transport error"));
    second.driveSuccessfulHandshake(SAMPLE_ACK);
    await wait(5);
    expect(conn.ack).toEqual(SAMPLE_ACK);

    conn.close();
  });

  test("a normal onClose still drives exactly one reconnect (no double-fire)", async () => {
    const first = new FakeSocket();
    const second = new FakeSocket();
    const { conn, createdCount } = makeConnection({
      sockets: [first, second],
    });

    const opened = conn.open();
    // Wait for getToken() to resolve and the socket to be created before driving
    // its handshake callbacks.
    await wait(5);
    first.driveSuccessfulHandshake(SAMPLE_ACK);
    await opened;
    expect(createdCount()).toBe(1);

    // The socket drops cleanly. handleClose schedules a reconnect; the
    // idempotency guard must keep that to a single queued attempt even though
    // the catch path also exists.
    first.closeCb?.({ code: 1006, reason: "abnormal" });

    await wait(60);

    // One reconnect, not a storm: the second attempt was created.
    expect(createdCount()).toBe(2);

    conn.close();
  });

  test("a stale close from a previous socket cannot close the recovered socket", async () => {
    const first = new FakeSocket();
    const second = new FakeSocket();
    const { conn, createdCount } = makeConnection({
      sockets: [first, second],
    });

    const opened = conn.open();
    await wait(5);
    first.driveSuccessfulHandshake(SAMPLE_ACK);
    await opened;

    first.closeCb?.({ code: 1006, reason: "network lost" });
    await wait(40);
    expect(createdCount()).toBe(2);

    const recoveredAck = { ...SAMPLE_ACK, sessionId: "s2" };
    second.driveSuccessfulHandshake(recoveredAck);
    await wait(5);
    expect(conn.ack).toEqual(recoveredAck);

    // Some transports can deliver a late/duplicate close for an old socket
    // after a newer socket is already current. That stale event must not mark
    // the recovered connection closed or schedule a third socket.
    first.closeCb?.({ code: 1006, reason: "late duplicate close" });
    await wait(60);

    expect(conn.ack).toEqual(recoveredAck);
    expect(createdCount()).toBe(2);

    conn.close();
  });

  test("superseded by a newer session does not reconnect", async () => {
    const first = new FakeSocket();
    const second = new FakeSocket();
    const { conn, createdCount } = makeConnection({
      sockets: [first, second],
    });

    const opened = conn.open();
    await wait(5);
    first.driveSuccessfulHandshake(SAMPLE_ACK);
    await opened;
    expect(createdCount()).toBe(1);

    first.closeCb?.({ code: 1012, reason: "superseded by newer session" });
    await wait(80);

    expect(createdCount()).toBe(1);

    conn.close();
  });

  test("an unauthorized WebSocket upgrade refreshes auth before reconnecting", async () => {
    const first = new FakeSocket();
    const second = new FakeSocket();
    let created = 0;
    let refreshed = false;
    let refreshCalls = 0;

    const ws = (_url: string): WebSocketLike => {
      const socket = [first, second][created] ?? new FakeSocket();
      created += 1;
      return socket;
    };

    const conn = new Connection({
      ws,
      url: "wss://example.test/ws",
      getToken: async () => (refreshed ? "fresh-token" : "stale-token"),
      initPayload: () => SAMPLE_INIT,
      reconnect: FAST_RECONNECT,
      onAuthRejected: async () => {
        refreshCalls += 1;
        await wait(10);
        refreshed = true;
      },
      logger: noopLogger,
    });

    const readInitToken = (sock: FakeSocket): string => {
      const frame = sock.sent
        .map((s) => JSON.parse(s) as { type: string; payload: ConnectionInit & { token?: string } })
        .find((m) => m.type === "connection.init");
      if (!frame?.payload.token) throw new Error("no connection.init token");
      return frame.payload.token;
    };

    const opened = conn.open();
    await wait(5);
    first.driveSuccessfulHandshake(SAMPLE_ACK);
    await opened;
    expect(readInitToken(first)).toBe("stale-token");

    first.closeCb?.({
      code: 1006,
      reason: "Expected HTTP 101 response but was '401 Unauthorized'",
    });

    await wait(40);
    expect(refreshCalls).toBe(1);
    expect(created).toBe(2);

    second.driveSuccessfulHandshake({ ...SAMPLE_ACK, sessionId: "s2" });
    await wait(5);
    expect(readInitToken(second)).toBe("fresh-token");

    conn.close();
  });

  test("each (re)connect's connection.init carries the live initPayload (initialSubscriptions)", async () => {
    // Regression: a reconnect's new cloud session is brand-new and starts with an
    // empty subscription set. If `connection.init` does not carry the live
    // subscriptions, the reconnected session has audio but no transcription
    // provider until a follow-up REST resend lands — and that resend's
    // control-stream nudge can be missed by the new owner pod's just-created
    // consumer group. So the init payload MUST carry the current set on EVERY
    // (re)open. Here `initPayload` reads a mutable holder, exactly as the real
    // client reads `subscriptions.currentSet()`; we assert both the initial and
    // the reconnect handshake frames carry it.
    const first = new FakeSocket();
    const second = new FakeSocket();

    // Stand-in for the live subscription set the real `initPayload` reads.
    const liveSubs = [
      { kind: "transcription", language: { mode: "specific", code: "en-US" } },
    ] as unknown as NonNullable<
      NonNullable<ConnectionInit["audio"]>["initialSubscriptions"]
    >;

    let created = 0;
    const ws = (_url: string): WebSocketLike => {
      const socket = [first, second][created] ?? new FakeSocket();
      created += 1;
      return socket;
    };
    const conn = new Connection({
      ws,
      url: "wss://example.test/ws",
      getToken: async () => "test-token",
      // Factory re-read on every open, mirroring client.ts.
      initPayload: () => ({
        protocolVersion: "2.0.0",
        audio: { codec: "lc3", sampleRate: 16000, frameSizeBytes: 60, initialSubscriptions: liveSubs },
      }),
      reconnect: FAST_RECONNECT,
      logger: noopLogger,
    });

    const readInit = (sock: FakeSocket): ConnectionInit => {
      const frame = sock.sent
        .map((s) => JSON.parse(s) as { type: string; payload: ConnectionInit })
        .find((m) => m.type === "connection.init");
      if (!frame) throw new Error("no connection.init frame was sent");
      return frame.payload;
    };

    // Initial connect.
    const opened = conn.open();
    await wait(5);
    first.driveSuccessfulHandshake(SAMPLE_ACK);
    await opened;
    expect(readInit(first).audio?.initialSubscriptions).toEqual(liveSubs);

    // Reconnect: the new handshake must also carry the live set.
    first.closeCb?.({ code: 1006, reason: "abnormal" });
    await wait(40);
    expect(created).toBe(2);
    second.driveSuccessfulHandshake({ ...SAMPLE_ACK, sessionId: "s2" });
    await wait(5);
    expect(readInit(second).audio?.initialSubscriptions).toEqual(liveSubs);

    conn.close();
  });

  test("close() stops the reconnect loop", async () => {
    const first = new FakeSocket();
    const { conn, createdCount } = makeConnection({ sockets: [first] });

    const opened = conn.open().catch(() => {});
    await wait(5);
    expect(createdCount()).toBe(1);

    // Host closes before the failed attempt can reschedule.
    conn.close();
    first.errorCb?.(new Error("transport blip"));
    await opened;

    await wait(60);

    // No new socket: the host close wins and the loop stays down.
    expect(createdCount()).toBe(1);
  });
});