@hyper-fetch/graphql 9.9.0 → 10.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +152 -84
- package/__tests__/e2e/e2e.conformance.spec.ts +206 -0
- package/__tests__/e2e/e2e.graphql-ws-server.ts +187 -0
- package/__tests__/e2e/e2e.graphql-ws.spec.ts +306 -0
- package/__tests__/e2e/e2e.react-subscriptions.spec.tsx +208 -0
- package/__tests__/features/adapter/adapter.browser.spec.ts +23 -5
- package/__tests__/features/adapter/adapter.document.spec.ts +17 -13
- package/__tests__/features/adapter/adapter.headers.spec.ts +65 -0
- package/__tests__/features/adapter/adapter.keys.spec.ts +235 -0
- package/__tests__/features/adapter/adapter.persisted-queries.spec.ts +207 -0
- package/__tests__/features/adapter/adapter.progress.spec.ts +39 -0
- package/__tests__/features/adapter/adapter.responses.spec.ts +162 -1
- package/__tests__/features/adapter/adapter.server.spec.ts +6 -6
- package/__tests__/features/adapter/adapter.types.spec.ts +121 -2
- package/__tests__/features/adapter/adapter.utils.spec.ts +4 -2
- package/__tests__/features/adapter/adapter.variables.spec.ts +3 -3
- package/__tests__/features/socket/socket.connection.spec.ts +892 -0
- package/__tests__/features/socket/socket.fuzz.spec.ts +329 -0
- package/__tests__/features/socket/socket.subscriptions.spec.ts +515 -0
- package/__tests__/features/socket/socket.types.spec.ts +160 -0
- package/__tests__/utils/controlled-websocket.ts +238 -0
- package/dist/adapter/adapter.constants.d.ts +5 -0
- package/dist/adapter/adapter.constants.d.ts.map +1 -1
- package/dist/adapter/adapter.types.d.ts +55 -2
- package/dist/adapter/adapter.types.d.ts.map +1 -1
- package/dist/adapter/adapter.utils.d.ts +51 -4
- package/dist/adapter/adapter.utils.d.ts.map +1 -1
- package/dist/adapter/http-adapter.fetch.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +958 -128
- package/dist/index.mjs.map +1 -1
- package/dist/socket/index.d.ts +5 -0
- package/dist/socket/index.d.ts.map +1 -0
- package/dist/socket/socket.constants.d.ts +44 -0
- package/dist/socket/socket.constants.d.ts.map +1 -0
- package/dist/socket/socket.subscriptions.d.ts +74 -0
- package/dist/socket/socket.subscriptions.d.ts.map +1 -0
- package/dist/socket/socket.types.d.ts +102 -0
- package/dist/socket/socket.types.d.ts.map +1 -0
- package/dist/socket/socket.utils.d.ts +80 -0
- package/dist/socket/socket.utils.d.ts.map +1 -0
- package/dist/socket/socket.websocket.d.ts +30 -0
- package/dist/socket/socket.websocket.d.ts.map +1 -0
- package/package.json +14 -9
- package/src/adapter/adapter.constants.ts +6 -0
- package/src/adapter/adapter.types.ts +69 -3
- package/src/adapter/adapter.utils.ts +196 -25
- package/src/adapter/http-adapter.fetch.ts +93 -43
- package/src/index.ts +1 -0
- package/src/socket/index.ts +4 -0
- package/src/socket/socket.constants.ts +59 -0
- package/src/socket/socket.subscriptions.ts +160 -0
- package/src/socket/socket.types.ts +118 -0
- package/src/socket/socket.utils.ts +150 -0
- package/src/socket/socket.websocket.ts +555 -0
- package/vite.config.ts +4 -0
|
@@ -0,0 +1,892 @@
|
|
|
1
|
+
import { AppManager } from "@hyper-fetch/core";
|
|
2
|
+
import { Socket } from "@hyper-fetch/sockets";
|
|
3
|
+
|
|
4
|
+
import type { GraphqlWsAdapterOptionsType } from "../../../src";
|
|
5
|
+
import { GRAPHQL_TRANSPORT_WS_PROTOCOL, GraphqlWsAdapter, shouldReconnectGraphqlWs } from "../../../src";
|
|
6
|
+
import {
|
|
7
|
+
ControlledWebSocket,
|
|
8
|
+
acknowledgeWebSocket,
|
|
9
|
+
flushPromises,
|
|
10
|
+
installControlledWebSocket,
|
|
11
|
+
openWebSocket,
|
|
12
|
+
uninstallControlledWebSocket,
|
|
13
|
+
waitForWebSocket,
|
|
14
|
+
} from "../../utils/controlled-websocket";
|
|
15
|
+
|
|
16
|
+
const url = "ws://localhost:4000/graphql";
|
|
17
|
+
|
|
18
|
+
describe("Graphql Ws Adapter [ Connection ]", () => {
|
|
19
|
+
let appManager = new AppManager();
|
|
20
|
+
// The lower end of the reconnection jitter - every delay is exactly `reconnectTime`
|
|
21
|
+
let random = vi.spyOn(Math, "random");
|
|
22
|
+
|
|
23
|
+
const createSocket = (
|
|
24
|
+
adapterOptions?: GraphqlWsAdapterOptionsType,
|
|
25
|
+
{
|
|
26
|
+
reconnectTime = 100,
|
|
27
|
+
connectionTimeout = 300,
|
|
28
|
+
shouldReconnect,
|
|
29
|
+
}: {
|
|
30
|
+
reconnectTime?: number;
|
|
31
|
+
connectionTimeout?: number;
|
|
32
|
+
shouldReconnect?: ConstructorParameters<typeof Socket>[0]["shouldReconnect"];
|
|
33
|
+
} = {},
|
|
34
|
+
) => {
|
|
35
|
+
const socket = new Socket({
|
|
36
|
+
url,
|
|
37
|
+
adapter: GraphqlWsAdapter(),
|
|
38
|
+
adapterOptions: adapterOptions ?? { autoConnect: false },
|
|
39
|
+
reconnectTime,
|
|
40
|
+
connectionTimeout,
|
|
41
|
+
shouldReconnect,
|
|
42
|
+
appManager: () => appManager,
|
|
43
|
+
});
|
|
44
|
+
const errors: Error[] = [];
|
|
45
|
+
socket.onError(({ error }) => {
|
|
46
|
+
errors.push(error as Error);
|
|
47
|
+
});
|
|
48
|
+
const onTick = socket.createListener<{ tick: number }>()({ topic: "subscription OnTick { tick }" });
|
|
49
|
+
return { socket, errors, onTick };
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
let setup = createSocket();
|
|
53
|
+
|
|
54
|
+
beforeEach(() => {
|
|
55
|
+
random = vi.spyOn(Math, "random").mockReturnValue(0);
|
|
56
|
+
installControlledWebSocket();
|
|
57
|
+
appManager = new AppManager();
|
|
58
|
+
setup = createSocket();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
afterEach(async () => {
|
|
62
|
+
await setup.socket.disconnect();
|
|
63
|
+
vi.useRealTimers();
|
|
64
|
+
uninstallControlledWebSocket();
|
|
65
|
+
random.mockRestore();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe("when connecting", () => {
|
|
69
|
+
it("should connect on creation by default", async () => {
|
|
70
|
+
setup = createSocket({});
|
|
71
|
+
|
|
72
|
+
const ws = await waitForWebSocket(1);
|
|
73
|
+
|
|
74
|
+
expect(ws.url).toBe(url);
|
|
75
|
+
expect(setup.socket.adapter.connecting).toBeTrue();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("should ask for the graphql-transport-ws subprotocol", async () => {
|
|
79
|
+
setup.socket.connect();
|
|
80
|
+
|
|
81
|
+
const ws = await waitForWebSocket(1);
|
|
82
|
+
|
|
83
|
+
expect(ws.protocols).toBe(GRAPHQL_TRANSPORT_WS_PROTOCOL);
|
|
84
|
+
expect(GRAPHQL_TRANSPORT_WS_PROTOCOL).toBe("graphql-transport-ws");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("should add the query params of the socket to the url", async () => {
|
|
88
|
+
setup = createSocket();
|
|
89
|
+
setup.socket.setQueryParams({ client: "web" });
|
|
90
|
+
setup.socket.connect();
|
|
91
|
+
|
|
92
|
+
const ws = await waitForWebSocket(1);
|
|
93
|
+
|
|
94
|
+
expect(ws.url).toBe(`${url}?client=web`);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("should use the WebSocket class from the options", async () => {
|
|
98
|
+
class CustomWebSocket extends ControlledWebSocket {}
|
|
99
|
+
setup = createSocket({ autoConnect: false, WebSocket: CustomWebSocket as unknown as typeof WebSocket });
|
|
100
|
+
setup.socket.connect();
|
|
101
|
+
|
|
102
|
+
const ws = await waitForWebSocket(1);
|
|
103
|
+
|
|
104
|
+
expect(ws).toBeInstanceOf(CustomWebSocket);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("should send connection_init without a payload when there are no connection params", async () => {
|
|
108
|
+
setup.socket.connect();
|
|
109
|
+
|
|
110
|
+
const ws = await openWebSocket();
|
|
111
|
+
|
|
112
|
+
expect(ws.messages).toStrictEqual([{ type: "connection_init" }]);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("should stay connecting until the server acknowledges the connection", async () => {
|
|
116
|
+
const connected = vi.fn();
|
|
117
|
+
setup.socket.onConnected(connected);
|
|
118
|
+
setup.socket.connect();
|
|
119
|
+
const ws = await openWebSocket();
|
|
120
|
+
|
|
121
|
+
expect(setup.socket.adapter.connected).toBeFalse();
|
|
122
|
+
expect(setup.socket.adapter.connecting).toBeTrue();
|
|
123
|
+
expect(connected).not.toHaveBeenCalled();
|
|
124
|
+
|
|
125
|
+
ws.serverAck({ server: "yoga" });
|
|
126
|
+
await flushPromises();
|
|
127
|
+
|
|
128
|
+
expect(setup.socket.adapter.connected).toBeTrue();
|
|
129
|
+
expect(connected).toHaveBeenCalledTimes(1);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("should resolve connect() once the connection is acknowledged", async () => {
|
|
133
|
+
const resolved = vi.fn();
|
|
134
|
+
setup.socket.connect().then(resolved);
|
|
135
|
+
const ws = await openWebSocket();
|
|
136
|
+
await flushPromises();
|
|
137
|
+
|
|
138
|
+
expect(resolved).not.toHaveBeenCalled();
|
|
139
|
+
ws.serverAck();
|
|
140
|
+
await flushPromises();
|
|
141
|
+
|
|
142
|
+
expect(resolved).toHaveBeenCalledTimes(1);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("should ignore a repeated acknowledgement", async () => {
|
|
146
|
+
const connected = vi.fn();
|
|
147
|
+
setup.socket.onConnected(connected);
|
|
148
|
+
setup.socket.connect();
|
|
149
|
+
const ws = await acknowledgeWebSocket();
|
|
150
|
+
setup.onTick.listen(vi.fn());
|
|
151
|
+
|
|
152
|
+
ws.serverAck();
|
|
153
|
+
await flushPromises();
|
|
154
|
+
|
|
155
|
+
expect(connected).toHaveBeenCalledTimes(1);
|
|
156
|
+
expect(ws.subscriptions).toHaveLength(1);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("should time out when the server does not acknowledge in time", async () => {
|
|
160
|
+
vi.useFakeTimers();
|
|
161
|
+
setup.socket.connect();
|
|
162
|
+
const ws = await openWebSocket();
|
|
163
|
+
|
|
164
|
+
await vi.advanceTimersByTimeAsync(300);
|
|
165
|
+
|
|
166
|
+
expect(setup.socket.adapter.connected).toBeFalse();
|
|
167
|
+
expect(ws.closeCalls).toStrictEqual([{ code: 1000, reason: "Normal Closure" }]);
|
|
168
|
+
expect(setup.errors.map(({ message }) => message)).toStrictEqual([
|
|
169
|
+
"Connection timeout - no connection within 300ms",
|
|
170
|
+
]);
|
|
171
|
+
|
|
172
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
173
|
+
expect(ControlledWebSocket.instances).toHaveLength(2);
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("should connect without adapter options", async () => {
|
|
178
|
+
setup = { ...setup, socket: new Socket({ url, adapter: GraphqlWsAdapter(), appManager: () => appManager }) };
|
|
179
|
+
|
|
180
|
+
const ws = await acknowledgeWebSocket(1);
|
|
181
|
+
|
|
182
|
+
expect(ws.messages).toStrictEqual([{ type: "connection_init" }]);
|
|
183
|
+
expect(setup.socket.adapter.connected).toBeTrue();
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("should not send anything when a connected callback closes the connection", async () => {
|
|
187
|
+
const listener = setup.onTick;
|
|
188
|
+
listener.listen(vi.fn());
|
|
189
|
+
setup.socket.onConnected(() => {
|
|
190
|
+
setup.socket.disconnect();
|
|
191
|
+
});
|
|
192
|
+
setup.socket.connect();
|
|
193
|
+
|
|
194
|
+
const ws = await acknowledgeWebSocket();
|
|
195
|
+
|
|
196
|
+
expect(ws.subscriptions).toHaveLength(0);
|
|
197
|
+
expect(setup.socket.adapter.connected).toBeFalse();
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("should ignore a transport that opens after its attempt was closed", async () => {
|
|
201
|
+
setup.socket.connect();
|
|
202
|
+
const ws = await waitForWebSocket(1);
|
|
203
|
+
// Some runtimes throw when closing a transport that never connected - it may still open later
|
|
204
|
+
vi.spyOn(ws, "close").mockImplementation(() => {
|
|
205
|
+
throw new Error("WebSocket was closed before the connection was established");
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
await setup.socket.disconnect();
|
|
209
|
+
ws.serverOpen();
|
|
210
|
+
await flushPromises();
|
|
211
|
+
|
|
212
|
+
expect(ws.sent).toStrictEqual([]);
|
|
213
|
+
expect(setup.socket.adapter.connecting).toBeFalse();
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("should replace the connection on reconnect and send the subscriptions again", async () => {
|
|
217
|
+
setup.onTick.listen(vi.fn());
|
|
218
|
+
setup.socket.connect();
|
|
219
|
+
const ws = await acknowledgeWebSocket();
|
|
220
|
+
|
|
221
|
+
setup.socket.reconnect();
|
|
222
|
+
const next = await acknowledgeWebSocket(2);
|
|
223
|
+
|
|
224
|
+
expect(ws.closeCalls).toStrictEqual([{ code: 1000, reason: "Normal Closure" }]);
|
|
225
|
+
expect(next.subscriptions.map(({ id }) => id)).toStrictEqual(["2"]);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
describe("when sending connection params", () => {
|
|
229
|
+
it("should send the connection params with connection_init", async () => {
|
|
230
|
+
setup = createSocket({ autoConnect: false, connectionParams: { token: "abc" } });
|
|
231
|
+
setup.socket.connect();
|
|
232
|
+
|
|
233
|
+
const ws = await openWebSocket();
|
|
234
|
+
|
|
235
|
+
expect(ws.messages).toStrictEqual([{ type: "connection_init", payload: { token: "abc" } }]);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("should run a connection params function on every connection attempt", async () => {
|
|
239
|
+
vi.useFakeTimers();
|
|
240
|
+
let token = 0;
|
|
241
|
+
const connectionParams = vi.fn(() => {
|
|
242
|
+
token += 1;
|
|
243
|
+
return { token };
|
|
244
|
+
});
|
|
245
|
+
setup = createSocket({ autoConnect: false, connectionParams });
|
|
246
|
+
setup.socket.connect();
|
|
247
|
+
const ws = await acknowledgeWebSocket();
|
|
248
|
+
|
|
249
|
+
ws.serverClose({ code: 1006 });
|
|
250
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
251
|
+
const next = await openWebSocket(2);
|
|
252
|
+
|
|
253
|
+
expect(connectionParams).toHaveBeenCalledTimes(2);
|
|
254
|
+
expect(ws.messages[0]).toStrictEqual({ type: "connection_init", payload: { token: 1 } });
|
|
255
|
+
expect(next.messages[0]).toStrictEqual({ type: "connection_init", payload: { token: 2 } });
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it("should wait for async connection params", async () => {
|
|
259
|
+
let resolveParams: (params: { token: string }) => void = () => null;
|
|
260
|
+
setup = createSocket({
|
|
261
|
+
autoConnect: false,
|
|
262
|
+
connectionParams: () =>
|
|
263
|
+
new Promise((resolve) => {
|
|
264
|
+
resolveParams = resolve;
|
|
265
|
+
}),
|
|
266
|
+
});
|
|
267
|
+
setup.socket.connect();
|
|
268
|
+
const ws = await openWebSocket();
|
|
269
|
+
|
|
270
|
+
expect(ws.messages).toStrictEqual([]);
|
|
271
|
+
resolveParams({ token: "late" });
|
|
272
|
+
await flushPromises();
|
|
273
|
+
|
|
274
|
+
expect(ws.messages).toStrictEqual([{ type: "connection_init", payload: { token: "late" } }]);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("should send no payload when the connection params function returns nothing", async () => {
|
|
278
|
+
// eslint-disable-next-line unicorn/no-useless-undefined -- a function that returns no params is the case
|
|
279
|
+
setup = createSocket({ autoConnect: false, connectionParams: () => undefined });
|
|
280
|
+
setup.socket.connect();
|
|
281
|
+
|
|
282
|
+
const ws = await openWebSocket();
|
|
283
|
+
|
|
284
|
+
expect(ws.messages).toStrictEqual([{ type: "connection_init" }]);
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it("should close with 4005 and not retry when the connection params fail", async () => {
|
|
288
|
+
vi.useFakeTimers();
|
|
289
|
+
const failure = new Error("No token");
|
|
290
|
+
setup = createSocket({
|
|
291
|
+
autoConnect: false,
|
|
292
|
+
connectionParams: () => Promise.reject(failure),
|
|
293
|
+
});
|
|
294
|
+
setup.socket.connect();
|
|
295
|
+
const ws = await openWebSocket();
|
|
296
|
+
await flushPromises();
|
|
297
|
+
|
|
298
|
+
expect(ws.closeCalls).toStrictEqual([{ code: 4005, reason: "No token" }]);
|
|
299
|
+
expect(setup.errors).toHaveLength(1);
|
|
300
|
+
expect(setup.errors[0].message).toBe("Could not get the connection params");
|
|
301
|
+
expect(setup.errors[0].cause).toBe(failure);
|
|
302
|
+
|
|
303
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
304
|
+
expect(ControlledWebSocket.instances).toHaveLength(1);
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it("should report connection params rejected with a value that is not an error", async () => {
|
|
308
|
+
// eslint-disable-next-line prefer-promise-reject-errors -- a rejection that is not an Error is the case
|
|
309
|
+
setup = createSocket({ autoConnect: false, connectionParams: () => Promise.reject("No session") });
|
|
310
|
+
setup.socket.connect();
|
|
311
|
+
const ws = await openWebSocket();
|
|
312
|
+
await flushPromises();
|
|
313
|
+
|
|
314
|
+
expect(ws.closeCalls).toStrictEqual([{ code: 4005, reason: "No session" }]);
|
|
315
|
+
expect((setup.errors[0].cause as Error).message).toBe("No session");
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it("should drop the connection params of an attempt that is gone", async () => {
|
|
319
|
+
let resolveParams: (params: { token: string }) => void = () => null;
|
|
320
|
+
setup = createSocket({
|
|
321
|
+
autoConnect: false,
|
|
322
|
+
connectionParams: () =>
|
|
323
|
+
new Promise((resolve) => {
|
|
324
|
+
resolveParams = resolve;
|
|
325
|
+
}),
|
|
326
|
+
});
|
|
327
|
+
setup.socket.connect();
|
|
328
|
+
const ws = await openWebSocket();
|
|
329
|
+
|
|
330
|
+
await setup.socket.disconnect();
|
|
331
|
+
resolveParams({ token: "late" });
|
|
332
|
+
await flushPromises();
|
|
333
|
+
|
|
334
|
+
expect(ws.sent).toStrictEqual([]);
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
it("should not report failing connection params of an attempt that is gone", async () => {
|
|
338
|
+
let rejectParams: (error: Error) => void = () => null;
|
|
339
|
+
setup = createSocket({
|
|
340
|
+
autoConnect: false,
|
|
341
|
+
connectionParams: () =>
|
|
342
|
+
new Promise((_, reject) => {
|
|
343
|
+
rejectParams = reject;
|
|
344
|
+
}),
|
|
345
|
+
});
|
|
346
|
+
setup.socket.connect();
|
|
347
|
+
await openWebSocket();
|
|
348
|
+
|
|
349
|
+
await setup.socket.disconnect();
|
|
350
|
+
rejectParams(new Error("Too late"));
|
|
351
|
+
await flushPromises();
|
|
352
|
+
|
|
353
|
+
expect(setup.errors).toStrictEqual([]);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
it("should use the connection params set by an onConnect interceptor", async () => {
|
|
357
|
+
setup.socket.onConnect(({ connection }) => ({
|
|
358
|
+
...connection,
|
|
359
|
+
adapterOptions: { ...connection.adapterOptions, connectionParams: { token: "fresh" } },
|
|
360
|
+
}));
|
|
361
|
+
setup.socket.connect();
|
|
362
|
+
|
|
363
|
+
const ws = await openWebSocket();
|
|
364
|
+
|
|
365
|
+
expect(ws.url).toBe(url);
|
|
366
|
+
expect(ws.messages).toStrictEqual([{ type: "connection_init", payload: { token: "fresh" } }]);
|
|
367
|
+
});
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
describe("when the server breaks the protocol", () => {
|
|
371
|
+
it.each([
|
|
372
|
+
["a frame that is not JSON", "not json"],
|
|
373
|
+
["a message without a type", JSON.stringify({ id: "1" })],
|
|
374
|
+
["an unknown message type", JSON.stringify({ type: "data", id: "1" })],
|
|
375
|
+
["a subscribe message", JSON.stringify({ type: "subscribe", id: "1", payload: { query: "{ a }" } })],
|
|
376
|
+
["next without an id", JSON.stringify({ type: "next", payload: {} })],
|
|
377
|
+
["next without a payload", JSON.stringify({ type: "next", id: "1" })],
|
|
378
|
+
["error without GraphQL errors", JSON.stringify({ type: "error", id: "1", payload: [] })],
|
|
379
|
+
["complete with an empty id", JSON.stringify({ type: "complete", id: "" })],
|
|
380
|
+
["a ping with a string payload", JSON.stringify({ type: "ping", payload: "hi" })],
|
|
381
|
+
["a binary frame", new ArrayBuffer(1)],
|
|
382
|
+
])("should close with 4004 on %s and not retry", async (_, frame) => {
|
|
383
|
+
vi.useFakeTimers();
|
|
384
|
+
setup.socket.connect();
|
|
385
|
+
const ws = await acknowledgeWebSocket();
|
|
386
|
+
|
|
387
|
+
ws.serverSend(frame);
|
|
388
|
+
|
|
389
|
+
expect(ws.closeCalls[0].code).toBe(4004);
|
|
390
|
+
// The client closed it, so only the reason is reported - not the close that follows
|
|
391
|
+
expect(setup.errors.map(({ message }) => message)).toStrictEqual([
|
|
392
|
+
"Received a malformed GraphQL over WebSocket message",
|
|
393
|
+
]);
|
|
394
|
+
expect(setup.socket.adapter.connected).toBeFalse();
|
|
395
|
+
|
|
396
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
397
|
+
expect(ControlledWebSocket.instances).toHaveLength(1);
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
it("should close with 4004 when the first message is not the acknowledgement", async () => {
|
|
401
|
+
setup.socket.connect();
|
|
402
|
+
const ws = await openWebSocket();
|
|
403
|
+
|
|
404
|
+
ws.serverNext("1", { data: {} });
|
|
405
|
+
|
|
406
|
+
expect(ws.closeCalls).toStrictEqual([{ code: 4004, reason: "First message cannot be of type next" }]);
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
it("should cut a close reason that is too long", async () => {
|
|
410
|
+
setup.socket.connect();
|
|
411
|
+
const ws = await acknowledgeWebSocket();
|
|
412
|
+
|
|
413
|
+
ws.serverMessage({ type: "x".repeat(200) });
|
|
414
|
+
|
|
415
|
+
expect(ws.closeCalls).toStrictEqual([{ code: 4004, reason: "Bad response" }]);
|
|
416
|
+
});
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
describe("when the connection closes", () => {
|
|
420
|
+
it.each([4500, 4005, 4400, 4004, 4401, 4406, 4409, 4429, 1002, 1003, 1007, 1008, 1011])(
|
|
421
|
+
"should not reconnect after %i",
|
|
422
|
+
async (code) => {
|
|
423
|
+
vi.useFakeTimers();
|
|
424
|
+
setup.socket.connect();
|
|
425
|
+
const ws = await acknowledgeWebSocket();
|
|
426
|
+
|
|
427
|
+
ws.serverClose({ code, reason: "Closed" });
|
|
428
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
429
|
+
|
|
430
|
+
expect(ControlledWebSocket.instances).toHaveLength(1);
|
|
431
|
+
expect(setup.socket.adapter.connected).toBeFalse();
|
|
432
|
+
},
|
|
433
|
+
);
|
|
434
|
+
|
|
435
|
+
it.each([4403, 4408, 4504, 4499, 1001, 1005, 1006, 1012, 1013, 1014])("should reconnect after %i", async (code) => {
|
|
436
|
+
vi.useFakeTimers();
|
|
437
|
+
setup.socket.connect();
|
|
438
|
+
const ws = await acknowledgeWebSocket();
|
|
439
|
+
|
|
440
|
+
ws.serverClose({ code });
|
|
441
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
442
|
+
|
|
443
|
+
expect(ControlledWebSocket.instances).toHaveLength(2);
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
it("should stay closed after a normal closure", async () => {
|
|
447
|
+
vi.useFakeTimers();
|
|
448
|
+
setup.socket.connect();
|
|
449
|
+
const ws = await acknowledgeWebSocket();
|
|
450
|
+
|
|
451
|
+
ws.serverClose({ code: 1000, wasClean: true });
|
|
452
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
453
|
+
|
|
454
|
+
expect(ControlledWebSocket.instances).toHaveLength(1);
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
it("should report the code and reason of a protocol close", async () => {
|
|
458
|
+
setup.socket.connect();
|
|
459
|
+
const ws = await acknowledgeWebSocket();
|
|
460
|
+
|
|
461
|
+
ws.serverClose({ code: 4401, reason: "Unauthorized" });
|
|
462
|
+
|
|
463
|
+
expect(setup.errors.map(({ message }) => message)).toStrictEqual([
|
|
464
|
+
"The server closed the connection with 4401: Unauthorized",
|
|
465
|
+
]);
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
it("should report a protocol close without a reason", async () => {
|
|
469
|
+
setup.socket.connect();
|
|
470
|
+
const ws = await acknowledgeWebSocket();
|
|
471
|
+
|
|
472
|
+
ws.serverClose({ code: 4400 });
|
|
473
|
+
|
|
474
|
+
expect(setup.errors[0].message).toBe("The server closed the connection with 4400: no reason given");
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
it("should report the meaning of a standard close", async () => {
|
|
478
|
+
setup.socket.connect();
|
|
479
|
+
const ws = await acknowledgeWebSocket();
|
|
480
|
+
|
|
481
|
+
ws.serverClose({ code: 1006 });
|
|
482
|
+
|
|
483
|
+
expect(setup.errors[0].message).toContain("closed abnormally");
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
it("should let the socket's shouldReconnect decide", async () => {
|
|
487
|
+
vi.useFakeTimers();
|
|
488
|
+
const shouldReconnect = vi.fn(() => true);
|
|
489
|
+
setup = createSocket({ autoConnect: false }, { shouldReconnect });
|
|
490
|
+
setup.socket.connect();
|
|
491
|
+
const ws = await acknowledgeWebSocket();
|
|
492
|
+
|
|
493
|
+
ws.serverClose({ code: 4401, reason: "Unauthorized" });
|
|
494
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
495
|
+
|
|
496
|
+
expect(shouldReconnect).toHaveBeenCalledWith({ code: 4401, reason: "Unauthorized" });
|
|
497
|
+
expect(ControlledWebSocket.instances).toHaveLength(2);
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
it("should treat a failed handshake reported only with an error as an abnormal closure", async () => {
|
|
501
|
+
vi.useFakeTimers();
|
|
502
|
+
setup.socket.connect();
|
|
503
|
+
const ws = await waitForWebSocket(1);
|
|
504
|
+
|
|
505
|
+
ws.serverRefuseWithoutClose();
|
|
506
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
507
|
+
|
|
508
|
+
expect(ControlledWebSocket.instances).toHaveLength(2);
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
it("should ignore an error of an open connection - its close follows", async () => {
|
|
512
|
+
setup.socket.connect();
|
|
513
|
+
const ws = await acknowledgeWebSocket();
|
|
514
|
+
|
|
515
|
+
ws.serverRefuseWithoutClose();
|
|
516
|
+
|
|
517
|
+
expect(setup.socket.adapter.connected).toBeTrue();
|
|
518
|
+
expect(setup.errors).toStrictEqual([]);
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
it("should close the transport with 1000 on disconnect", async () => {
|
|
522
|
+
setup.socket.connect();
|
|
523
|
+
const ws = await acknowledgeWebSocket();
|
|
524
|
+
|
|
525
|
+
await setup.socket.disconnect();
|
|
526
|
+
|
|
527
|
+
expect(ws.closeCalls).toStrictEqual([{ code: 1000, reason: "Normal Closure" }]);
|
|
528
|
+
expect(setup.errors).toStrictEqual([]);
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
it("should finish disconnecting when closing the transport throws", async () => {
|
|
532
|
+
setup.socket.connect();
|
|
533
|
+
const ws = await acknowledgeWebSocket();
|
|
534
|
+
vi.spyOn(ws, "close").mockImplementation(() => {
|
|
535
|
+
throw new Error("Close failed");
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
await setup.socket.disconnect();
|
|
539
|
+
|
|
540
|
+
expect(setup.socket.adapter.connected).toBeFalse();
|
|
541
|
+
expect(setup.socket.adapter.forceClosed).toBeTrue();
|
|
542
|
+
});
|
|
543
|
+
});
|
|
544
|
+
|
|
545
|
+
describe("when the server pings", () => {
|
|
546
|
+
it("should answer with pong", async () => {
|
|
547
|
+
setup.socket.connect();
|
|
548
|
+
const ws = await acknowledgeWebSocket();
|
|
549
|
+
|
|
550
|
+
ws.serverMessage({ type: "ping" });
|
|
551
|
+
|
|
552
|
+
expect(ws.messages.at(-1)).toStrictEqual({ type: "pong" });
|
|
553
|
+
});
|
|
554
|
+
|
|
555
|
+
it("should send the payload of the ping back", async () => {
|
|
556
|
+
setup.socket.connect();
|
|
557
|
+
const ws = await acknowledgeWebSocket();
|
|
558
|
+
|
|
559
|
+
ws.serverMessage({ type: "ping", payload: { at: 1 } });
|
|
560
|
+
|
|
561
|
+
expect(ws.messages.at(-1)).toStrictEqual({ type: "pong", payload: { at: 1 } });
|
|
562
|
+
});
|
|
563
|
+
|
|
564
|
+
it("should answer a ping before the acknowledgement", async () => {
|
|
565
|
+
setup.socket.connect();
|
|
566
|
+
const ws = await openWebSocket();
|
|
567
|
+
|
|
568
|
+
ws.serverMessage({ type: "ping" });
|
|
569
|
+
|
|
570
|
+
expect(ws.messages).toStrictEqual([{ type: "connection_init" }, { type: "pong" }]);
|
|
571
|
+
expect(setup.socket.adapter.connecting).toBeTrue();
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
it("should ignore a pong it did not ask for", async () => {
|
|
575
|
+
setup.socket.connect();
|
|
576
|
+
const ws = await acknowledgeWebSocket();
|
|
577
|
+
|
|
578
|
+
ws.serverMessage({ type: "pong" });
|
|
579
|
+
|
|
580
|
+
expect(ws.messages).toStrictEqual([{ type: "connection_init" }]);
|
|
581
|
+
expect(setup.errors).toStrictEqual([]);
|
|
582
|
+
});
|
|
583
|
+
});
|
|
584
|
+
|
|
585
|
+
describe("when keeping the connection alive", () => {
|
|
586
|
+
const keepAlive = { enabled: true, intervalMs: 1000, pongTimeoutMs: 500 };
|
|
587
|
+
|
|
588
|
+
it("should not ping by default", async () => {
|
|
589
|
+
vi.useFakeTimers();
|
|
590
|
+
setup.socket.connect();
|
|
591
|
+
const ws = await acknowledgeWebSocket();
|
|
592
|
+
|
|
593
|
+
await vi.advanceTimersByTimeAsync(60_000);
|
|
594
|
+
|
|
595
|
+
expect(ws.messages).toStrictEqual([{ type: "connection_init" }]);
|
|
596
|
+
});
|
|
597
|
+
|
|
598
|
+
it("should ping a quiet connection", async () => {
|
|
599
|
+
vi.useFakeTimers();
|
|
600
|
+
setup = createSocket({ autoConnect: false, keepAlive });
|
|
601
|
+
setup.socket.connect();
|
|
602
|
+
const ws = await acknowledgeWebSocket();
|
|
603
|
+
|
|
604
|
+
await vi.advanceTimersByTimeAsync(999);
|
|
605
|
+
expect(ws.messages.at(-1)).toStrictEqual({ type: "connection_init" });
|
|
606
|
+
await vi.advanceTimersByTimeAsync(1);
|
|
607
|
+
|
|
608
|
+
expect(ws.messages.at(-1)).toStrictEqual({ type: "ping" });
|
|
609
|
+
});
|
|
610
|
+
|
|
611
|
+
it("should use the default timings", async () => {
|
|
612
|
+
vi.useFakeTimers();
|
|
613
|
+
setup = createSocket({ autoConnect: false, keepAlive: { enabled: true } }, { connectionTimeout: 60_000 });
|
|
614
|
+
setup.socket.connect();
|
|
615
|
+
const ws = await acknowledgeWebSocket();
|
|
616
|
+
|
|
617
|
+
await vi.advanceTimersByTimeAsync(10_000);
|
|
618
|
+
expect(ws.messages.at(-1)).toStrictEqual({ type: "ping" });
|
|
619
|
+
await vi.advanceTimersByTimeAsync(4999);
|
|
620
|
+
expect(setup.socket.adapter.connected).toBeTrue();
|
|
621
|
+
await vi.advanceTimersByTimeAsync(1);
|
|
622
|
+
|
|
623
|
+
expect(setup.socket.adapter.connected).toBeFalse();
|
|
624
|
+
});
|
|
625
|
+
|
|
626
|
+
it("should postpone the ping while messages arrive", async () => {
|
|
627
|
+
vi.useFakeTimers();
|
|
628
|
+
setup = createSocket({ autoConnect: false, keepAlive });
|
|
629
|
+
setup.socket.connect();
|
|
630
|
+
const ws = await acknowledgeWebSocket();
|
|
631
|
+
|
|
632
|
+
await vi.advanceTimersByTimeAsync(800);
|
|
633
|
+
ws.serverMessage({ type: "pong" });
|
|
634
|
+
await vi.advanceTimersByTimeAsync(800);
|
|
635
|
+
|
|
636
|
+
expect(ws.messages).toStrictEqual([{ type: "connection_init" }]);
|
|
637
|
+
});
|
|
638
|
+
|
|
639
|
+
it("should reconnect when the pong does not arrive in time", async () => {
|
|
640
|
+
vi.useFakeTimers();
|
|
641
|
+
setup = createSocket({ autoConnect: false, keepAlive });
|
|
642
|
+
setup.socket.connect();
|
|
643
|
+
const ws = await acknowledgeWebSocket();
|
|
644
|
+
|
|
645
|
+
await vi.advanceTimersByTimeAsync(1500);
|
|
646
|
+
|
|
647
|
+
expect(setup.socket.adapter.connected).toBeFalse();
|
|
648
|
+
expect(ws.closeCalls).toHaveLength(1);
|
|
649
|
+
expect(setup.errors.map(({ message }) => message)).toStrictEqual([
|
|
650
|
+
"Keep-alive timeout - the server did not answer the ping in time",
|
|
651
|
+
]);
|
|
652
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
653
|
+
expect(ControlledWebSocket.instances).toHaveLength(2);
|
|
654
|
+
});
|
|
655
|
+
|
|
656
|
+
it("should keep the connection when the pong arrives", async () => {
|
|
657
|
+
vi.useFakeTimers();
|
|
658
|
+
setup = createSocket({ autoConnect: false, keepAlive });
|
|
659
|
+
setup.socket.connect();
|
|
660
|
+
const ws = await acknowledgeWebSocket();
|
|
661
|
+
|
|
662
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
663
|
+
ws.serverMessage({ type: "pong" });
|
|
664
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
665
|
+
|
|
666
|
+
expect(setup.socket.adapter.connected).toBeTrue();
|
|
667
|
+
expect(ws.messages.filter(({ type }) => type === "ping")).toHaveLength(2);
|
|
668
|
+
});
|
|
669
|
+
|
|
670
|
+
it("should stop pinging after disconnect", async () => {
|
|
671
|
+
vi.useFakeTimers();
|
|
672
|
+
setup = createSocket({ autoConnect: false, keepAlive });
|
|
673
|
+
setup.socket.connect();
|
|
674
|
+
const ws = await acknowledgeWebSocket();
|
|
675
|
+
|
|
676
|
+
await setup.socket.disconnect();
|
|
677
|
+
await vi.advanceTimersByTimeAsync(5000);
|
|
678
|
+
|
|
679
|
+
expect(ws.messages).toStrictEqual([{ type: "connection_init" }]);
|
|
680
|
+
expect(vi.getTimerCount()).toBe(0);
|
|
681
|
+
});
|
|
682
|
+
});
|
|
683
|
+
|
|
684
|
+
describe("when the connection is lazy", () => {
|
|
685
|
+
const lazy = { lazy: true } satisfies GraphqlWsAdapterOptionsType;
|
|
686
|
+
|
|
687
|
+
it("should not connect without subscriptions, even with autoConnect", async () => {
|
|
688
|
+
setup = createSocket({ ...lazy, autoConnect: true });
|
|
689
|
+
await flushPromises();
|
|
690
|
+
|
|
691
|
+
expect(ControlledWebSocket.instances).toHaveLength(0);
|
|
692
|
+
});
|
|
693
|
+
|
|
694
|
+
it("should connect when the first subscription starts", async () => {
|
|
695
|
+
setup = createSocket(lazy);
|
|
696
|
+
|
|
697
|
+
setup.onTick.listen(vi.fn());
|
|
698
|
+
const ws = await acknowledgeWebSocket(1);
|
|
699
|
+
|
|
700
|
+
expect(ws.subscriptions).toHaveLength(1);
|
|
701
|
+
});
|
|
702
|
+
|
|
703
|
+
it("should open one connection for many subscriptions", async () => {
|
|
704
|
+
setup = createSocket(lazy);
|
|
705
|
+
|
|
706
|
+
setup.onTick.listen(vi.fn());
|
|
707
|
+
setup.socket.createListener()({ topic: "subscription { other }" }).listen(vi.fn());
|
|
708
|
+
const ws = await acknowledgeWebSocket(1);
|
|
709
|
+
|
|
710
|
+
expect(ControlledWebSocket.instances).toHaveLength(1);
|
|
711
|
+
expect(ws.subscriptions).toHaveLength(2);
|
|
712
|
+
});
|
|
713
|
+
|
|
714
|
+
it("should close the connection when the last subscription stops", async () => {
|
|
715
|
+
setup = createSocket(lazy);
|
|
716
|
+
const stop = setup.onTick.listen(vi.fn());
|
|
717
|
+
const ws = await acknowledgeWebSocket(1);
|
|
718
|
+
|
|
719
|
+
stop();
|
|
720
|
+
await flushPromises();
|
|
721
|
+
|
|
722
|
+
expect(ws.messages.at(-1)).toStrictEqual({ id: "1", type: "complete" });
|
|
723
|
+
expect(ws.closeCalls).toStrictEqual([{ code: 1000, reason: "Normal Closure" }]);
|
|
724
|
+
expect(setup.socket.adapter.connected).toBeFalse();
|
|
725
|
+
});
|
|
726
|
+
|
|
727
|
+
it("should close the connection when the server ends the last subscription", async () => {
|
|
728
|
+
setup = createSocket(lazy);
|
|
729
|
+
setup.onTick.listen(vi.fn());
|
|
730
|
+
const ws = await acknowledgeWebSocket(1);
|
|
731
|
+
|
|
732
|
+
ws.serverComplete("1");
|
|
733
|
+
await flushPromises();
|
|
734
|
+
|
|
735
|
+
expect(ws.closeCalls).toHaveLength(1);
|
|
736
|
+
});
|
|
737
|
+
|
|
738
|
+
it("should keep the connection for lazyCloseTimeout and reuse it", async () => {
|
|
739
|
+
vi.useFakeTimers();
|
|
740
|
+
setup = createSocket({ ...lazy, lazyCloseTimeout: 1000 });
|
|
741
|
+
const stop = setup.onTick.listen(vi.fn());
|
|
742
|
+
const ws = await acknowledgeWebSocket(1);
|
|
743
|
+
|
|
744
|
+
stop();
|
|
745
|
+
await vi.advanceTimersByTimeAsync(999);
|
|
746
|
+
setup.onTick.listen(vi.fn());
|
|
747
|
+
await vi.advanceTimersByTimeAsync(5000);
|
|
748
|
+
|
|
749
|
+
expect(ControlledWebSocket.instances).toHaveLength(1);
|
|
750
|
+
expect(ws.closeCalls).toHaveLength(0);
|
|
751
|
+
expect(ws.subscriptions.map(({ id }) => id)).toStrictEqual(["1", "2"]);
|
|
752
|
+
});
|
|
753
|
+
|
|
754
|
+
it("should close after lazyCloseTimeout", async () => {
|
|
755
|
+
vi.useFakeTimers();
|
|
756
|
+
setup = createSocket({ ...lazy, lazyCloseTimeout: 1000 });
|
|
757
|
+
const stop = setup.onTick.listen(vi.fn());
|
|
758
|
+
const ws = await acknowledgeWebSocket(1);
|
|
759
|
+
|
|
760
|
+
stop();
|
|
761
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
762
|
+
|
|
763
|
+
expect(ws.closeCalls).toHaveLength(1);
|
|
764
|
+
});
|
|
765
|
+
|
|
766
|
+
it("should connect again for a subscription after the lazy close", async () => {
|
|
767
|
+
setup = createSocket(lazy);
|
|
768
|
+
const stop = setup.onTick.listen(vi.fn());
|
|
769
|
+
await acknowledgeWebSocket(1);
|
|
770
|
+
stop();
|
|
771
|
+
await flushPromises();
|
|
772
|
+
|
|
773
|
+
setup.onTick.listen(vi.fn());
|
|
774
|
+
const next = await acknowledgeWebSocket(2);
|
|
775
|
+
|
|
776
|
+
expect(next.subscriptions).toHaveLength(1);
|
|
777
|
+
});
|
|
778
|
+
|
|
779
|
+
it("should close a connection that is not acknowledged yet when its subscriptions stop", async () => {
|
|
780
|
+
setup = createSocket(lazy);
|
|
781
|
+
const stop = setup.onTick.listen(vi.fn());
|
|
782
|
+
const ws = await openWebSocket(1);
|
|
783
|
+
|
|
784
|
+
stop();
|
|
785
|
+
await flushPromises();
|
|
786
|
+
|
|
787
|
+
expect(ws.closeCalls).toHaveLength(1);
|
|
788
|
+
expect(ws.subscriptions).toHaveLength(0);
|
|
789
|
+
});
|
|
790
|
+
|
|
791
|
+
it("should not reopen a connection closed by hand", async () => {
|
|
792
|
+
setup = createSocket(lazy);
|
|
793
|
+
setup.onTick.listen(vi.fn());
|
|
794
|
+
await acknowledgeWebSocket(1);
|
|
795
|
+
|
|
796
|
+
await setup.socket.disconnect();
|
|
797
|
+
setup.socket.createListener()({ topic: "subscription { other }" }).listen(vi.fn());
|
|
798
|
+
await flushPromises();
|
|
799
|
+
|
|
800
|
+
expect(ControlledWebSocket.instances).toHaveLength(1);
|
|
801
|
+
});
|
|
802
|
+
|
|
803
|
+
it("should leave recovery to the reconnection while it retries", async () => {
|
|
804
|
+
vi.useFakeTimers();
|
|
805
|
+
setup = createSocket(lazy);
|
|
806
|
+
setup.onTick.listen(vi.fn());
|
|
807
|
+
const ws = await acknowledgeWebSocket(1);
|
|
808
|
+
|
|
809
|
+
ws.serverClose({ code: 1006 });
|
|
810
|
+
setup.socket.createListener()({ topic: "subscription { other }" }).listen(vi.fn());
|
|
811
|
+
await flushPromises();
|
|
812
|
+
expect(ControlledWebSocket.instances).toHaveLength(1);
|
|
813
|
+
|
|
814
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
815
|
+
const next = await acknowledgeWebSocket(2);
|
|
816
|
+
expect(next.subscriptions).toHaveLength(2);
|
|
817
|
+
});
|
|
818
|
+
|
|
819
|
+
it("should stop reconnecting when the last subscription stops while it retries", async () => {
|
|
820
|
+
vi.useFakeTimers();
|
|
821
|
+
setup = createSocket(lazy);
|
|
822
|
+
const stop = setup.onTick.listen(vi.fn());
|
|
823
|
+
const ws = await acknowledgeWebSocket(1);
|
|
824
|
+
|
|
825
|
+
ws.serverClose({ code: 1006 });
|
|
826
|
+
stop();
|
|
827
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
828
|
+
|
|
829
|
+
expect(ControlledWebSocket.instances).toHaveLength(1);
|
|
830
|
+
});
|
|
831
|
+
|
|
832
|
+
it("should open a lazy connection on reconnect without subscriptions", async () => {
|
|
833
|
+
setup = createSocket(lazy);
|
|
834
|
+
|
|
835
|
+
setup.socket.reconnect();
|
|
836
|
+
const ws = await acknowledgeWebSocket(1);
|
|
837
|
+
|
|
838
|
+
expect(setup.socket.adapter.connected).toBeTrue();
|
|
839
|
+
expect(ws.subscriptions).toHaveLength(0);
|
|
840
|
+
});
|
|
841
|
+
|
|
842
|
+
it("should cancel a pending lazy close on disconnect and destroy", async () => {
|
|
843
|
+
vi.useFakeTimers();
|
|
844
|
+
setup = createSocket({ ...lazy, lazyCloseTimeout: 1000 });
|
|
845
|
+
const stop = setup.onTick.listen(vi.fn());
|
|
846
|
+
await acknowledgeWebSocket(1);
|
|
847
|
+
|
|
848
|
+
stop();
|
|
849
|
+
setup.socket.destroy();
|
|
850
|
+
|
|
851
|
+
expect(vi.getTimerCount()).toBe(0);
|
|
852
|
+
});
|
|
853
|
+
});
|
|
854
|
+
|
|
855
|
+
describe("when the environment can't connect", () => {
|
|
856
|
+
it("should report a missing WebSocket", async () => {
|
|
857
|
+
globalThis.WebSocket = undefined as unknown as typeof WebSocket;
|
|
858
|
+
|
|
859
|
+
await setup.socket.connect();
|
|
860
|
+
|
|
861
|
+
expect(setup.errors.map(({ message }) => message)).toStrictEqual([
|
|
862
|
+
"WebSocket is not available in this environment",
|
|
863
|
+
]);
|
|
864
|
+
expect(setup.socket.adapter.connecting).toBeFalse();
|
|
865
|
+
});
|
|
866
|
+
|
|
867
|
+
it("should report a WebSocket constructor that throws", async () => {
|
|
868
|
+
ControlledWebSocket.constructorError = new SyntaxError("Invalid url");
|
|
869
|
+
|
|
870
|
+
await setup.socket.connect();
|
|
871
|
+
|
|
872
|
+
expect(setup.errors.map(({ message }) => message)).toStrictEqual(["Invalid url"]);
|
|
873
|
+
expect(setup.socket.adapter.connecting).toBeFalse();
|
|
874
|
+
});
|
|
875
|
+
});
|
|
876
|
+
|
|
877
|
+
describe("when deciding about reconnecting", () => {
|
|
878
|
+
it.each([
|
|
879
|
+
[{}, true],
|
|
880
|
+
[{ error: new Error("Timeout") }, true],
|
|
881
|
+
[{ code: 1000 }, false],
|
|
882
|
+
[{ code: 1006 }, true],
|
|
883
|
+
[{ code: 1002 }, false],
|
|
884
|
+
[{ code: 4401 }, false],
|
|
885
|
+
[{ code: 4403 }, true],
|
|
886
|
+
[{ code: 4499 }, true],
|
|
887
|
+
[{ code: 3000 }, true],
|
|
888
|
+
])("should decide %j as %s", (loss, expected) => {
|
|
889
|
+
expect(shouldReconnectGraphqlWs(loss)).toBe(expected);
|
|
890
|
+
});
|
|
891
|
+
});
|
|
892
|
+
});
|