Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
3import {
4 exhaustWsReconnectIfStillWaiting,
5 getWsConnectionStatus,
6 getWsReconnectDelayMsForRetry,
7 getWsConnectionUiState,
8 recordWsConnectionAttempt,
9 recordWsConnectionClosed,
10 recordWsConnectionErrored,
11 recordWsConnectionOpened,
12 resetWsConnectionStateForTests,
13 setBrowserOnlineStatus,
14 WS_RECONNECT_MAX_ATTEMPTS,
15} from "./wsConnectionState";
16
17describe("wsConnectionState", () => {
18 beforeEach(() => {
19 vi.useFakeTimers();
20 vi.setSystemTime(new Date("2026-04-03T20:30:00.000Z"));
21 resetWsConnectionStateForTests();
22 });
23
24 afterEach(() => {
25 vi.useRealTimers();
26 });
27
28 it("treats a disconnected browser as offline once the websocket drops", () => {
29 recordWsConnectionAttempt("ws://localhost:3020/ws");
30 recordWsConnectionOpened();
31 recordWsConnectionClosed({ code: 1006, reason: "offline" });
32 setBrowserOnlineStatus(false);
33
34 expect(getWsConnectionUiState(getWsConnectionStatus())).toBe("offline");
35 });
36
37 it("stays in the initial connecting state until the first disconnect", () => {
38 recordWsConnectionAttempt("ws://localhost:3020/ws");
39
40 expect(getWsConnectionStatus()).toMatchObject({
41 attemptCount: 1,
42 hasConnected: false,
43 phase: "connecting",
44 });
45 expect(getWsConnectionUiState(getWsConnectionStatus())).toBe("connecting");
46 });
47
48 it("schedules the next retry after a failed websocket attempt", () => {
49 recordWsConnectionAttempt("ws://localhost:3020/ws");
50 recordWsConnectionErrored("Unable to connect to the T3 server WebSocket.");
51
52 const firstRetryDelayMs = getWsReconnectDelayMsForRetry(0);
53 if (firstRetryDelayMs === null) {
54 throw new Error("Expected an initial retry delay.");
55 }
56
57 expect(getWsConnectionStatus()).toMatchObject({
58 nextRetryAt: new Date(Date.now() + firstRetryDelayMs).toISOString(),
59 reconnectAttemptCount: 1,
60 reconnectPhase: "waiting",
61 });
62 });
63
64 it("marks the reconnect cycle as exhausted after the final attempt fails", () => {
65 for (let attempt = 0; attempt < WS_RECONNECT_MAX_ATTEMPTS; attempt += 1) {
66 recordWsConnectionAttempt("ws://localhost:3020/ws");
67 recordWsConnectionErrored("Unable to connect to the T3 server WebSocket.");
68 }
69
70 expect(getWsConnectionStatus()).toMatchObject({
71 nextRetryAt: null,
72 reconnectAttemptCount: WS_RECONNECT_MAX_ATTEMPTS,
73 reconnectPhase: "exhausted",
74 });
75 });
76
77 it("can exhaust a stalled final retry window when no new attempt starts", () => {
78 recordWsConnectionAttempt("ws://localhost:3020/ws");
79 recordWsConnectionOpened();
80
81 for (let attempt = 0; attempt < WS_RECONNECT_MAX_ATTEMPTS - 1; attempt += 1) {
82 recordWsConnectionAttempt("ws://localhost:3020/ws");
83 recordWsConnectionErrored("Unable to connect to the T3 server WebSocket.");
84 }
85
86 const finalRetryDelayMs = getWsReconnectDelayMsForRetry(WS_RECONNECT_MAX_ATTEMPTS - 2);
87 if (finalRetryDelayMs === null) {
88 throw new Error("Expected a final retry delay.");
89 }
90
91 const statusBeforeExhaust = getWsConnectionStatus();
92 expect(statusBeforeExhaust).toMatchObject({
93 nextRetryAt: new Date(Date.now() + finalRetryDelayMs).toISOString(),
94 reconnectAttemptCount: 7,
95 reconnectPhase: "waiting",
96 });
97
98 const nextRetryAt = statusBeforeExhaust.nextRetryAt;
99 if (!nextRetryAt) {
100 throw new Error("Expected a scheduled retry.");
101 }
102
103 vi.setSystemTime(new Date(Date.now() + finalRetryDelayMs + 1_000));
104 exhaustWsReconnectIfStillWaiting(nextRetryAt);
105
106 expect(getWsConnectionStatus()).toMatchObject({
107 nextRetryAt: null,
108 reconnectAttemptCount: WS_RECONNECT_MAX_ATTEMPTS,
109 reconnectPhase: "exhausted",
110 });
111 });
112});