fix(realtime): open no socket after a logout, and reconnect as soon as the network is back

A connect still waiting for its ws token when the user logged out went on
to open a socket authenticated as that user, which the next login on the
same browser then reused. And a socket in its reconnect backoff waited up
to 30 s after the network came back or the tab became visible. Both
events now reconnect right away.
This commit is contained in:
Maurice
2026-10-07 16:25:00 +02:00
parent f02253a44c
commit 3f9b751ee3
5 changed files with 71 additions and 4 deletions
+15 -1
View File
@@ -22,10 +22,12 @@ vi.mock('../../../src/sync/mutationQueue', () => ({
vi.mock('../../../src/sync/tripSyncManager', () => ({
tripSyncManager: { syncAll: () => syncAll() },
}));
let reconnects = 0;
vi.mock('../../../src/api/websocket', () => ({
setPreReconnectHook: (fn: (() => Promise<void>) | null) => { preReconnect = fn; },
setRefetchCallback: (fn: ((tripId: string) => void) | null) => { refetchCb = fn; },
getActiveTrips: () => ['7'],
reconnectNow: () => { reconnects++; },
}));
vi.mock('../../../src/store/tripStore', () => ({
useTripStore: { getState: () => ({ hydrateActiveTrip: hydrate }) },
@@ -45,7 +47,7 @@ const flushMicrotasks = async () => {
beforeEach(() => {
flush.mockClear(); syncAll.mockClear(); hydrate.mockClear(); loadSettings.mockClear();
refetchCb = null; preReconnect = null;
refetchCb = null; preReconnect = null; reconnects = 0;
settingsLoaded = false; authenticated = true;
Object.defineProperty(navigator, 'onLine', { value: true, writable: true, configurable: true });
});
@@ -88,6 +90,18 @@ describe('syncTriggers', () => {
expect(flush.mock.invocationCallOrder[0]).toBeLessThan(hydrate.mock.invocationCallOrder[0]);
});
it('online event and a tab coming back reconnect the socket right away instead of waiting out its backoff', async () => {
registerSyncTriggers();
window.dispatchEvent(new Event('online'));
await flushMicrotasks();
expect(reconnects).toBe(1);
Object.defineProperty(document, 'hidden', { value: false, configurable: true });
document.dispatchEvent(new Event('visibilitychange'));
await flushMicrotasks();
expect(reconnects).toBe(2);
});
it('online event retries the settings load when it has not yet succeeded (#1618)', async () => {
registerSyncTriggers();
window.dispatchEvent(new Event('online'));