test: cover gateway sequence reset on reconnect

This commit is contained in:
Shakker
2026-07-29 19:13:10 +01:00
committed by Shakker
parent ab3f52c367
commit dbcd479007
@@ -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();