mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 02:14:05 +00:00
0b92dcc86b
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.
109 lines
4.3 KiB
TypeScript
109 lines
4.3 KiB
TypeScript
// CW-03: a city walk's visits online. The server answers, and the answer is kept on this device.
|
|
import 'fake-indexeddb/auto';
|
|
import { http, HttpResponse } from 'msw';
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { server } from '../../tests/helpers/msw/server';
|
|
import { clearAll, offlineDb } from '../db/offlineDb';
|
|
import { tourVisitRepo } from './tourVisitRepo';
|
|
|
|
function setOnline(value: boolean): void {
|
|
Object.defineProperty(navigator, 'onLine', { value, writable: true, configurable: true });
|
|
}
|
|
|
|
const cachedKeys = async () =>
|
|
(await offlineDb.tourStationVisits.toArray()).map((row) => `${row.trip_id}:${row.assignment_id}:${row.station_key}`);
|
|
|
|
beforeEach(async () => {
|
|
await clearAll();
|
|
setOnline(true);
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
setOnline(true);
|
|
});
|
|
|
|
describe('tourVisitRepo online', () => {
|
|
it("FE-REPO-VISIT-001: list reads the trip's visits and keeps the whole trip on this device", async () => {
|
|
await offlineDb.tourStationVisits.put({ assignment_id: 9, station_key: 'gone-01', status: 'visited', trip_id: 4 });
|
|
const visits = [
|
|
{ assignment_id: 9, station_key: 'stop-01', status: 'visited' as const },
|
|
{ assignment_id: 10, station_key: 'stop-01', status: 'skipped' as const },
|
|
];
|
|
server.use(http.get('/api/trips/4/tour-visits', () => HttpResponse.json({ visits })));
|
|
|
|
await expect(tourVisitRepo.list(4)).resolves.toEqual({ visits });
|
|
|
|
await vi.waitFor(async () => expect((await cachedKeys()).sort()).toEqual(['4:10:stop-01', '4:9:stop-01']));
|
|
});
|
|
|
|
it("FE-REPO-VISIT-002: a mark goes straight to the server, and the day's answer replaces only that day here", async () => {
|
|
await offlineDb.tourStationVisits.bulkPut([
|
|
{ assignment_id: 9, station_key: 'stop-02', status: 'visited', trip_id: 4 },
|
|
{ assignment_id: 10, station_key: 'stop-01', status: 'skipped', trip_id: 4 },
|
|
]);
|
|
const bodies: unknown[] = [];
|
|
const answer = { visits: [{ assignment_id: 9, station_key: 'stop-01', status: 'visited' as const }] };
|
|
server.use(
|
|
http.put('/api/trips/4/tour-visits/9/stop-01', async ({ request }) => {
|
|
bodies.push(await request.json());
|
|
return HttpResponse.json(answer);
|
|
})
|
|
);
|
|
|
|
await expect(tourVisitRepo.update(4, 9, 'stop-01', { status: 'visited' })).resolves.toEqual(answer);
|
|
|
|
expect(bodies).toEqual([{ status: 'visited' }]);
|
|
expect(await offlineDb.mutationQueue.count()).toBe(0);
|
|
await vi.waitFor(async () => expect((await cachedKeys()).sort()).toEqual(['4:10:stop-01', '4:9:stop-01']));
|
|
});
|
|
|
|
it('FE-REPO-VISIT-003: a refused mark throws and keeps what this device had', async () => {
|
|
await offlineDb.tourStationVisits.put({ assignment_id: 9, station_key: 'stop-01', status: 'visited', trip_id: 4 });
|
|
server.use(
|
|
http.put('/api/trips/4/tour-visits/9/stop-01', () =>
|
|
HttpResponse.json({ error: 'Station not found' }, { status: 404 })
|
|
)
|
|
);
|
|
|
|
await expect(tourVisitRepo.update(4, 9, 'stop-01', { status: 'planned' })).rejects.toMatchObject({
|
|
response: { status: 404 },
|
|
});
|
|
expect(await cachedKeys()).toEqual(['4:9:stop-01']);
|
|
});
|
|
|
|
it('FE-REPO-VISIT-004: a lost connection reads the copy on this device', async () => {
|
|
await offlineDb.tourStationVisits.put({ assignment_id: 9, station_key: 'stop-01', status: 'skipped', trip_id: 4 });
|
|
server.use(http.get('/api/trips/4/tour-visits', () => HttpResponse.error()));
|
|
|
|
await expect(tourVisitRepo.list(4)).resolves.toEqual({
|
|
visits: [{ assignment_id: 9, station_key: 'stop-01', status: 'skipped' }],
|
|
});
|
|
});
|
|
|
|
it('FE-REPO-VISIT-005: an answer from the server still shows a mark waiting in the queue', async () => {
|
|
await offlineDb.mutationQueue.put({
|
|
id: 'mark-1',
|
|
tripId: 4,
|
|
method: 'PUT',
|
|
url: '/trips/4/tour-visits/9/stop-02',
|
|
body: { status: 'visited' },
|
|
createdAt: 1,
|
|
status: 'syncing',
|
|
attempts: 0,
|
|
lastError: null,
|
|
resource: 'tourStationVisits',
|
|
entityId: 9,
|
|
});
|
|
server.use(
|
|
http.get('/api/trips/4/tour-visits', () =>
|
|
HttpResponse.json({ visits: [{ assignment_id: 9, station_key: 'stop-01', status: 'visited' }] })
|
|
)
|
|
);
|
|
|
|
const { visits } = await tourVisitRepo.list('4');
|
|
|
|
expect(visits.map((visit) => visit.station_key).sort()).toEqual(['stop-01', 'stop-02']);
|
|
});
|
|
});
|