fix(client): make the offline core survive a logout and stop clobbering itself

logout() unregistered every sync trigger, and the only place that registers
them is App's mount effect - which never runs again, because logout navigates
through the router rather than reloading. Log out, log back in, go offline,
edit something: it queues and nothing ever flushes it. No online listener, no
visibilitychange, no 30s heartbeat, no WS reconnect hook. Register on auth
success instead; registerSyncTriggers is already idempotent.

The background sync loops only checked the auth gate on entry. Logout swaps
the Dexie handle to the shared anonymous database, so a trip loop or a blob
download still in flight afterwards wrote the logged-out account's itinerary
and documents into it - the database that survives logout and greets the next
person on a shared device. Check the gate per iteration.

And the remote place:updated applier replaced the whole embedded place, while
the local reducer right next to it preserves the assignment's own times. Same
event, two answers, and the remote one wipes a scheduled 09:00-10:30 off the
itinerary when someone else renames the place. One merge helper now, used by
both, so they cannot drift again. The test that should have caught it seeded
null times and asserted only the name.

Also: moving a day note deleted before it created, and restored the note only
locally when the create failed; t() interpolation was corrupted by a '$' in a
parameter value; a 429 upload was treated as permanently failed; and the
geolocation watch was started inside a state updater.
This commit is contained in:
Maurice
2026-08-23 10:07:24 +02:00
committed by Maurice
parent eadb3703bc
commit 47a4276e84
15 changed files with 187 additions and 24 deletions
@@ -0,0 +1,10 @@
import { describe, it, expect } from 'vitest';
// Guards the lifecycle option in tests/setup.ts: a cross-origin call nobody mocked must
// fail here rather than actually leaving the runner (see msw/handlers/external.ts for the
// FX widget that once did, and answered after its test environment was gone).
describe('msw unhandled requests', () => {
it('FE-MSW-EGRESS-001: an unmocked cross-origin request is refused', async () => {
await expect(fetch('https://unmocked.example/rates')).rejects.toThrow();
});
});
@@ -51,6 +51,29 @@ describe('remoteEventHandler > places', () => {
expect(assignments['10'][0].place?.name).toBe('Cascaded Update');
});
it('FE-WSEVT-PLACE-004b: place:updated keeps the assignment its own times', () => {
// The day view renders COALESCE(assignment_time, place_time), so the copy
// embedded in an assignment carries the per-stop schedule. A broadcast about
// the pool row must not overwrite it (the local reducer merges the same way).
const place = buildPlace({ id: 1, name: 'Original', place_time: '08:00', end_time: '09:00' });
const assignment = buildAssignment({
id: 100,
day_id: 10,
place: { ...place, place_time: '14:00', end_time: '15:30' },
});
useTripStore.setState({ places: [place], assignments: { '10': [assignment] } });
useTripStore.getState().handleRemoteEvent({
type: 'place:updated',
place: buildPlace({ id: 1, name: 'Renamed', place_time: '08:00', end_time: '09:00' }),
});
const embedded = useTripStore.getState().assignments['10'][0].place!;
expect(embedded.name).toBe('Renamed');
expect(embedded.place_time).toBe('14:00');
expect(embedded.end_time).toBe('15:30');
});
it('FE-WSEVT-PLACE-005: place:deleted removes place from places array', () => {
seedData();
useTripStore.getState().handleRemoteEvent({ type: 'place:deleted', placeId: 1 });
@@ -82,6 +82,10 @@ describe('syncTriggers', () => {
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]);
});
it('online event retries the settings load when it has not yet succeeded (#1618)', async () => {