mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 09:54:14 +00:00
fix(client): make the new rollbacks roll back only what they wrote
The rollbacks this branch added were snapshot-shaped: capture the whole collection before the write, restore it on any failure. That is wrong in two ways at once. It undoes work the server already accepted when a later step is the one that fails, and it throws away whatever a collaborator's socket event applied while the requests were in flight. The day-plan reorder is the clearest case: five awaits, and a failing day-note write reverted transport positions the server had already stored. It now records only the fields it is about to overwrite, per booking, and drops each one as its request lands - so a note failure reaches the catch with nothing to undo, and a booking that arrived mid-write survives. The notification toggle had the same shape underneath: the rollback was made surgical but the request still shipped the whole matrix built from the pre-toggle render, so two overlapping toggles overwrote each other anyway. It sends the one cell that changed. The test pinned the disagreement as correct; it asserts the payload now. Photo reorder awaits allSettled rather than all, so the UI is not snapped back while the remaining writes are still landing - a partial run really does leave the server holding part of the new order, and only a total refusal justifies restoring the old one. Both the desktop editor and the mobile sheet, which was still swallowing every rejection. Also: the geolocation start guard is a run generation now, because a start issued after a stop re-sets a plain flag while the first run is still parked in the iOS permission prompt; a category rename reports once for the batch and rethrows, so the settings dialog stops closing as if it had worked; and a 404'd import job stops telling the user their file was invalid when it had simply expired. The clipboard fallback for plain-HTTP installs was pasted into three files by this branch. It is one helper now, and the two copy buttons in the members modal that still called navigator.clipboard unguarded use it too. Same for the fourth local-date helper written next to the three that already import the existing one.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// FE-HOOK-GEO-001 to FE-HOOK-GEO-023
|
||||
// FE-HOOK-GEO-001 to FE-HOOK-GEO-025
|
||||
import { StrictMode } from 'react';
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { GeoOnceError, getCurrentPositionOnce, useGeolocation } from './useGeolocation';
|
||||
@@ -272,6 +272,39 @@ describe('useGeolocation', () => {
|
||||
expect(() => act(() => result.current.setMode('off'))).not.toThrow();
|
||||
});
|
||||
|
||||
it('FE-HOOK-GEO-024: stopping while the iOS prompt is open leaves no watch behind', async () => {
|
||||
let grant!: () => void;
|
||||
const requestPermission = vi.fn(() => new Promise<string>(resolve => { grant = () => resolve('granted'); }));
|
||||
vi.stubGlobal('DeviceOrientationEvent', Object.assign(function () {}, { requestPermission }));
|
||||
|
||||
const { result } = renderHook(() => useGeolocation());
|
||||
act(() => result.current.setMode('show'));
|
||||
act(() => result.current.setMode('off'));
|
||||
|
||||
await act(async () => { grant(); await new Promise(r => setTimeout(r, 0)); });
|
||||
|
||||
expect(watchPosition).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-HOOK-GEO-025: stop and start during the iOS prompt still ends with one watch', async () => {
|
||||
const grants: ((v: string) => void)[] = [];
|
||||
const requestPermission = vi.fn(() => new Promise<string>(resolve => { grants.push(resolve); }));
|
||||
vi.stubGlobal('DeviceOrientationEvent', Object.assign(function () {}, { requestPermission }));
|
||||
|
||||
const { result } = renderHook(() => useGeolocation());
|
||||
act(() => result.current.setMode('show'));
|
||||
act(() => result.current.setMode('off'));
|
||||
act(() => result.current.setMode('show'));
|
||||
|
||||
await act(async () => { grants.forEach(g => g('granted')); await new Promise(r => setTimeout(r, 0)); });
|
||||
|
||||
// Both runs got past the prompt; only the newer one may subscribe, or the older
|
||||
// watch keeps running with its id overwritten.
|
||||
expect(requestPermission).toHaveBeenCalledTimes(2);
|
||||
expect(watchPosition).toHaveBeenCalledTimes(1);
|
||||
expect(result.current.mode).toBe('show');
|
||||
});
|
||||
|
||||
it('FE-HOOK-GEO-019: stops the watch when the component unmounts', async () => {
|
||||
const { result, unmount } = renderHook(() => useGeolocation());
|
||||
await act(() => result.current.cycleMode());
|
||||
|
||||
Reference in New Issue
Block a user