Files
TREK/client/tests/unit/sync/syncTriggers.test.ts
T
Maurice 8c93dc8d9c feat(tours): show a city walk's progress on the phone's day row, and check its stops off offline (CW-03)
The day row on the phone now shows how many stops of a city walk were
visited on that day, "2 of 5 visited", like the desktop row does. Both rows
read it from one place, and a tap on the phone names it in full without
opening the row.

A walk's stops can be checked off, skipped and taken back without a
connection. The mark shows at once, waits in the offline queue and goes out
when the connection is back, in the order the marks were made and behind any
older mark of the same day that still waits. The day plan, the walk's detail
and a reload read the visits kept on this device with the waiting marks on
top, so a mark made offline stays on screen until the server has it. A mark
the server answers with "not found", because the stop, the day or the walk
was removed meanwhile, counts as done and holds back no later mark of that
day. Any other refusal, the Tours addon turned off among them, keeps the mark
as a failed change to try again from the offline settings. The visits are
read again when the connection comes and goes, and once more after the
waiting marks went out, so a mark the server dropped or refused leaves the
day plan. A trip's visits that answer after another trip was opened no longer
show there.

The offline sync keeps each trip's walk visits and reads the stops of a walk
this device has never opened, so it can be checked off offline too. A walk
whose stops this device could not read lists no stand-in stops and no count,
and says to open it again while online. A walk put on a day while offline
says its stops can be checked off once it has synced, instead of offering
buttons that cannot work yet. A mark that fails says to open the walk again
and try once more, in every language.

Marks carry no version, so the last one to reach the server wins, as with
the other changes sent after being offline. The offline guide says so.
2026-10-20 02:49:47 +00:00

144 lines
5.1 KiB
TypeScript

/**
* syncTriggers — reconnect/online wiring (H1).
*
* Verifies the previously-dead refetch path is wired: on WS reconnect and on the
* `online` event the active trip's store is re-hydrated (after the queue flush).
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
const flush = vi.fn(() => Promise.resolve());
const syncAll = vi.fn(() => Promise.resolve());
const hydrate = vi.fn(() => Promise.resolve());
const loadSettings = vi.fn(() => Promise.resolve());
const reloadVisits = vi.fn();
let refetchCb: ((tripId: string) => void) | null = null;
let preReconnect: (() => Promise<void>) | null = null;
let settingsLoaded = false;
let authenticated = true;
vi.mock('../../../src/sync/mutationQueue', () => ({
mutationQueue: { flush: () => flush() },
}));
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 }) },
}));
vi.mock('../../../src/store/tourVisitStore', () => ({
reloadTourVisits: (tripId: string) => reloadVisits(tripId),
}));
vi.mock('../../../src/store/settingsStore', () => ({
useSettingsStore: { getState: () => ({ isLoaded: settingsLoaded, loadSettings }) },
}));
vi.mock('../../../src/store/authStore', () => ({
useAuthStore: { getState: () => ({ isAuthenticated: authenticated }) },
}));
import { registerSyncTriggers, unregisterSyncTriggers } from '../../../src/sync/syncTriggers';
const flushMicrotasks = async () => {
for (let i = 0; i < 5; i++) await Promise.resolve();
};
beforeEach(() => {
flush.mockClear(); syncAll.mockClear(); hydrate.mockClear(); loadSettings.mockClear(); reloadVisits.mockClear();
refetchCb = null; preReconnect = null; reconnects = 0;
settingsLoaded = false; authenticated = true;
Object.defineProperty(navigator, 'onLine', { value: true, writable: true, configurable: true });
});
afterEach(() => {
unregisterSyncTriggers();
});
describe('syncTriggers', () => {
it('registers a refetch callback that hydrates the active trip', () => {
registerSyncTriggers();
expect(refetchCb).toBeTypeOf('function');
refetchCb!('7');
expect(hydrate).toHaveBeenCalledWith('7');
});
it('the refetch after a reconnect also reads the walk visits of the trip again (CW-03)', () => {
registerSyncTriggers();
refetchCb?.('7');
expect(reloadVisits).toHaveBeenCalledWith('7');
});
it('also registers the pre-reconnect flush hook', () => {
registerSyncTriggers();
expect(preReconnect).toBeTypeOf('function');
});
it('clears both reconnect hooks on unregister', () => {
registerSyncTriggers();
unregisterSyncTriggers();
expect(refetchCb).toBeNull();
expect(preReconnect).toBeNull();
});
it('online event flushes, then re-seeds Dexie and re-hydrates active trips', async () => {
registerSyncTriggers();
window.dispatchEvent(new Event('online'));
await flushMicrotasks();
expect(flush).toHaveBeenCalled();
expect(syncAll).toHaveBeenCalled();
expect(hydrate).toHaveBeenCalledWith('7');
// The order is the point: re-seeding Dexie before the queue drains would
// overwrite edits that have not been sent yet.
expect(flush.mock.invocationCallOrder[0]).toBeLessThan(syncAll.mock.invocationCallOrder[0]);
expect(flush.mock.invocationCallOrder[0]).toBeLessThan(hydrate.mock.invocationCallOrder[0]);
// CW-03: the walk visits are read again only once the queued marks went out.
expect(reloadVisits).toHaveBeenCalledWith('7');
expect(flush.mock.invocationCallOrder[0]).toBeLessThan(reloadVisits.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'));
await flushMicrotasks();
expect(loadSettings).toHaveBeenCalled();
});
it('does not retry the settings load once it has loaded', async () => {
settingsLoaded = true;
registerSyncTriggers();
window.dispatchEvent(new Event('online'));
await flushMicrotasks();
expect(loadSettings).not.toHaveBeenCalled();
});
it('does not retry the settings load while unauthenticated', async () => {
authenticated = false;
registerSyncTriggers();
window.dispatchEvent(new Event('online'));
await flushMicrotasks();
expect(loadSettings).not.toHaveBeenCalled();
});
});