fix(timeline): resync after sse reconnect
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { SSE_EVENT_TYPES } from "@capakraken/shared";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { getTimelineSseResyncKeys } from "./timelineSsePolicy.js";
|
||||
|
||||
const invalidateQueries = vi.fn();
|
||||
const effectCleanups: Array<() => void> = [];
|
||||
@@ -83,11 +84,23 @@ describe("useTimelineSSE", () => {
|
||||
|
||||
expect(MockEventSource.instances).toHaveLength(1);
|
||||
|
||||
MockEventSource.instances[0]?.emitOpen();
|
||||
|
||||
MockEventSource.instances[0]?.emitMessage(JSON.stringify({ type: SSE_EVENT_TYPES.PING }));
|
||||
|
||||
expect(invalidateQueries).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not resync on the initial successful SSE connection", () => {
|
||||
useTimelineSSE();
|
||||
|
||||
expect(MockEventSource.instances).toHaveLength(1);
|
||||
|
||||
MockEventSource.instances[0]?.emitOpen();
|
||||
|
||||
expect(invalidateQueries).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("resets reconnect backoff after ping and reconnects only once per pending timer", () => {
|
||||
useTimelineSSE();
|
||||
|
||||
@@ -117,6 +130,31 @@ describe("useTimelineSSE", () => {
|
||||
expect(invalidateQueries).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("resyncs timeline queries exactly once after a successful reconnect", () => {
|
||||
useTimelineSSE();
|
||||
|
||||
const firstConnection = MockEventSource.instances[0];
|
||||
expect(firstConnection).toBeDefined();
|
||||
|
||||
firstConnection?.emitOpen();
|
||||
firstConnection?.emitError();
|
||||
|
||||
vi.advanceTimersByTime(2_000);
|
||||
|
||||
expect(MockEventSource.instances).toHaveLength(2);
|
||||
|
||||
const secondConnection = MockEventSource.instances[1];
|
||||
expect(secondConnection).toBeDefined();
|
||||
|
||||
secondConnection?.emitOpen();
|
||||
secondConnection?.emitOpen();
|
||||
|
||||
expect(invalidateQueries).toHaveBeenCalledTimes(getTimelineSseResyncKeys().length);
|
||||
expect(invalidateQueries.mock.calls).toEqual(
|
||||
getTimelineSseResyncKeys().map((queryKey) => [{ queryKey }]),
|
||||
);
|
||||
});
|
||||
|
||||
it("clears a pending reconnect when the hook is disposed", () => {
|
||||
useTimelineSSE();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user