Files
TREK/client/tests/unit/repo/placeRepo.test.ts
T
jubnl b194e8317d feat(pwa): implement real offline mode with IndexedDB sync
Add genuine offline read/write capability for trips:

- Dexie IndexedDB schema (trips, places, packing, todo, budget,
  reservations, files, mutationQueue, syncMeta, blobCache)
- Repo layer for all domains: offline reads from Dexie, writes
  optimistically to Dexie and enqueue mutations for later replay
- Mutation queue with UUID idempotency keys (X-Idempotency-Key),
  FIFO flush, temp-ID reconciliation on 2xx, fail-and-continue on 4xx
- Trip sync manager: caches all trips with end_date >= today or null,
  auto-evicts 7d after end_date, fetches bundle endpoint in one request
- Map tile prefetcher: bbox from place coords, zooms 10-16, 50MB cap,
  warms SW cache via fetch
- Sync triggers: network online → flush + syncAll; WS reconnect →
  flush only (rate-limiter safe); visibilitychange/30s → flush only
- WS remoteEventHandler writes through to Dexie on every event
- Server idempotency middleware + idempotency_keys table (migration 100,
  24h TTL nightly cleanup)
- GET /api/trips/:id/bundle endpoint for efficient single-request sync
- OfflineBanner component: amber (offline) / blue (syncing) / hidden
- OfflineTab in Settings: cached trip list, re-sync and clear actions
- usePendingMutations hook for per-item pending indicators

Closes #505 #541
2026-04-14 23:04:25 +02:00

121 lines
3.9 KiB
TypeScript

/**
* placeRepo unit tests.
*
* Online path: calls REST via MSW, writes result to Dexie.
* Offline path: returns Dexie cache, skips REST.
*/
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import 'fake-indexeddb/auto';
import { server } from '../../helpers/msw/server';
import { http, HttpResponse } from 'msw';
import { placeRepo } from '../../../src/repo/placeRepo';
import { offlineDb, clearAll } from '../../../src/db/offlineDb';
import { buildPlace } from '../../helpers/factories';
beforeEach(async () => {
await clearAll();
Object.defineProperty(navigator, 'onLine', { value: true, writable: true, configurable: true });
});
afterEach(() => {
vi.restoreAllMocks();
});
describe('placeRepo.list', () => {
it('online — fetches from REST and caches in Dexie', async () => {
const place = buildPlace({ trip_id: 1 });
server.use(
http.get('/api/trips/1/places', () => HttpResponse.json({ places: [place] })),
);
const result = await placeRepo.list(1);
expect(result.places).toHaveLength(1);
expect(result.places[0].id).toBe(place.id);
// Give fire-and-forget a tick to flush
await new Promise(r => setTimeout(r, 0));
const cached = await offlineDb.places.where('trip_id').equals(1).toArray();
expect(cached).toHaveLength(1);
expect(cached[0].id).toBe(place.id);
});
it('offline — returns Dexie cache without REST call', async () => {
Object.defineProperty(navigator, 'onLine', { value: false });
const place = buildPlace({ trip_id: 1 });
await offlineDb.places.put(place);
let restCalled = false;
server.use(
http.get('/api/trips/1/places', () => {
restCalled = true;
return HttpResponse.json({ places: [] });
}),
);
const result = await placeRepo.list(1);
expect(result.places).toHaveLength(1);
expect(result.places[0].id).toBe(place.id);
expect(restCalled).toBe(false);
});
it('offline — returns empty array when nothing cached', async () => {
Object.defineProperty(navigator, 'onLine', { value: false });
const result = await placeRepo.list(99);
expect(result.places).toHaveLength(0);
});
});
describe('placeRepo.create', () => {
it('calls REST and caches created place in Dexie', async () => {
const place = buildPlace({ trip_id: 1, name: 'Eiffel Tower' });
server.use(
http.post('/api/trips/1/places', () => HttpResponse.json({ place })),
);
const result = await placeRepo.create(1, { name: 'Eiffel Tower' });
expect(result.place.name).toBe('Eiffel Tower');
await new Promise(r => setTimeout(r, 0));
const cached = await offlineDb.places.get(place.id);
expect(cached).toBeDefined();
expect(cached!.name).toBe('Eiffel Tower');
});
});
describe('placeRepo.update', () => {
it('calls REST and updates Dexie cache', async () => {
const original = buildPlace({ trip_id: 1, name: 'Old Name' });
await offlineDb.places.put(original);
const updated = { ...original, name: 'New Name' };
server.use(
http.put(`/api/trips/1/places/${original.id}`, () => HttpResponse.json({ place: updated })),
);
const result = await placeRepo.update(1, original.id, { name: 'New Name' });
expect(result.place.name).toBe('New Name');
await new Promise(r => setTimeout(r, 0));
const cached = await offlineDb.places.get(original.id);
expect(cached!.name).toBe('New Name');
});
});
describe('placeRepo.delete', () => {
it('calls REST and removes from Dexie', async () => {
const place = buildPlace({ trip_id: 1 });
await offlineDb.places.put(place);
server.use(
http.delete(`/api/trips/1/places/${place.id}`, () => HttpResponse.json({ success: true })),
);
await placeRepo.delete(1, place.id);
await new Promise(r => setTimeout(r, 0));
const cached = await offlineDb.places.get(place.id);
expect(cached).toBeUndefined();
});
});