Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

wsConnectionState.test.ts

107 lines3.5 KB

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