From dbcd479007223e5aa4d9dc0efb264d07e8f739d9 Mon Sep 17 00:00:00 2001 From: Shakker Date: Wed, 29 Jul 2026 19:13:10 +0100 Subject: [PATCH] test: cover gateway sequence reset on reconnect --- .../src/client.watchdog.test.ts | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/packages/gateway-client/src/client.watchdog.test.ts b/packages/gateway-client/src/client.watchdog.test.ts index 04645953a592..812b0a9c93d8 100644 --- a/packages/gateway-client/src/client.watchdog.test.ts +++ b/packages/gateway-client/src/client.watchdog.test.ts @@ -482,6 +482,42 @@ describe("GatewayClient", () => { client.stop(); }); + test("establishes a fresh event sequence baseline after an automatic reconnect", async () => { + vi.useFakeTimers(); + const onEvent = vi.fn(); + const onGap = vi.fn(); + const { client, connections } = createSyntheticGatewayProtocol({ onEvent, onGap }); + client.start(); + const firstConnection = connections[0]; + if (!firstConnection) { + throw new Error("synthetic protocol connection missing"); + } + firstConnection.handlers.message( + JSON.stringify({ type: "event", event: "board.changed", payload: {}, seq: 1 }), + ); + + firstConnection.close(1012, "service restart"); + await vi.advanceTimersByTimeAsync(10); + const replacementConnection = connections[1]; + if (!replacementConnection) { + throw new Error("synthetic replacement connection missing"); + } + replacementConnection.handlers.message( + JSON.stringify({ type: "event", event: "board.changed", payload: {}, seq: 3 }), + ); + + expect(onGap).not.toHaveBeenCalled(); + expect(onEvent).toHaveBeenCalledTimes(2); + + replacementConnection.handlers.message( + JSON.stringify({ type: "event", event: "board.changed", payload: {}, seq: 5 }), + ); + + expect(onGap).toHaveBeenCalledExactlyOnceWith({ expected: 4, received: 5 }); + expect(onEvent).toHaveBeenCalledTimes(3); + client.stop(); + }); + test("restarts immediately after resetting a pending reconnect", async () => { vi.useFakeTimers(); const { client, connections } = createSyntheticGatewayProtocol();