Open History to see the last commit
1import { describe, expect, it } from "vitest";
2
3import type { WsConnectionStatus } from "../rpc/wsConnectionState";
4import { shouldAutoReconnect, shouldRestartStalledReconnect } from "./WebSocketConnectionSurface";
5
6function makeStatus(overrides: Partial<WsConnectionStatus> = {}): WsConnectionStatus {
7 return {
8 attemptCount: 0,
9 closeCode: null,
10 closeReason: null,
11 connectionLabel: null,
12 connectedAt: null,
13 disconnectedAt: null,
14 hasConnected: false,
15 lastError: null,
16 lastErrorAt: null,
17 nextRetryAt: null,
18 online: true,
19 phase: "idle",
20 reconnectAttemptCount: 0,
21 reconnectMaxAttempts: 8,
22 reconnectPhase: "idle",
23 socketUrl: null,
24 ...overrides,
25 };
26}
27
28describe("WebSocketConnectionSurface.logic", () => {
29 it("forces reconnect on online when the app was offline", () => {
30 expect(
31 shouldAutoReconnect(
32 makeStatus({
33 disconnectedAt: "2026-04-03T20:00:00.000Z",
34 online: false,
35 phase: "disconnected",
36 }),
37 "online",
38 ),
39 ).toBe(true);
40 });
41
42 it("forces reconnect on focus only for previously connected disconnected states", () => {
43 expect(
44 shouldAutoReconnect(
45 makeStatus({
46 hasConnected: true,
47 online: true,
48 phase: "disconnected",
49 reconnectAttemptCount: 3,
50 reconnectPhase: "waiting",
51 }),
52 "focus",
53 ),
54 ).toBe(true);
55
56 expect(
57 shouldAutoReconnect(
58 makeStatus({
59 hasConnected: false,
60 online: true,
61 phase: "disconnected",
62 reconnectAttemptCount: 1,
63 reconnectPhase: "waiting",
64 }),
65 "focus",
66 ),
67 ).toBe(false);
68 });
69
70 it("forces reconnect on focus for exhausted reconnect loops", () => {
71 expect(
72 shouldAutoReconnect(
73 makeStatus({
74 hasConnected: true,
75 online: true,
76 phase: "disconnected",
77 reconnectAttemptCount: 8,
78 reconnectPhase: "exhausted",
79 }),
80 "focus",
81 ),
82 ).toBe(true);
83 });
84
85 it("restarts a stalled reconnect window after the scheduled retry time passes", () => {
86 expect(
87 shouldRestartStalledReconnect(
88 makeStatus({
89 hasConnected: true,
90 nextRetryAt: "2026-04-03T20:00:01.000Z",
91 online: true,
92 phase: "disconnected",
93 reconnectAttemptCount: 3,
94 reconnectPhase: "waiting",
95 }),
96 "2026-04-03T20:00:01.000Z",
97 ),
98 ).toBe(true);
99
100 expect(
101 shouldRestartStalledReconnect(
102 makeStatus({
103 hasConnected: true,
104 nextRetryAt: "2026-04-03T20:00:01.000Z",
105 online: true,
106 phase: "disconnected",
107 reconnectAttemptCount: 3,
108 reconnectPhase: "attempting",
109 }),
110 "2026-04-03T20:00:01.000Z",
111 ),
112 ).toBe(false);
113 });
114});