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
This commit is contained in:
jubnl
2026-04-14 23:04:13 +02:00
parent 8c7567faf3
commit b194e8317d
64 changed files with 3837 additions and 638 deletions
+72
View File
@@ -892,3 +892,75 @@ describe('Copy trip with data', () => {
expect(newNotes[0].text).toBe('Pack early!');
});
});
// ─────────────────────────────────────────────────────────────────────────────
// Bundle endpoint — GET /api/trips/:id/bundle
// ─────────────────────────────────────────────────────────────────────────────
describe('Trip bundle', () => {
it('BUNDLE-001 — returns all sub-collections for owned trip', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id, { start_date: '2026-07-01', end_date: '2026-07-03' });
const res = await request(app)
.get(`/api/trips/${trip.id}/bundle`)
.set('Cookie', authCookie(user.id));
expect(res.status).toBe(200);
expect(res.body.trip).toBeDefined();
expect(res.body.trip.id).toBe(trip.id);
expect(Array.isArray(res.body.days)).toBe(true);
expect(res.body.days).toHaveLength(3);
expect(Array.isArray(res.body.places)).toBe(true);
expect(Array.isArray(res.body.packingItems)).toBe(true);
expect(Array.isArray(res.body.todoItems)).toBe(true);
expect(Array.isArray(res.body.budgetItems)).toBe(true);
expect(Array.isArray(res.body.reservations)).toBe(true);
expect(Array.isArray(res.body.files)).toBe(true);
});
it('BUNDLE-002 — returns 404 for trip that does not exist', async () => {
const { user } = createUser(testDb);
const res = await request(app)
.get('/api/trips/999999/bundle')
.set('Cookie', authCookie(user.id));
expect(res.status).toBe(404);
});
it('BUNDLE-003 — returns 404 when user has no access to trip', async () => {
const { user: owner } = createUser(testDb);
const { user: other } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const res = await request(app)
.get(`/api/trips/${trip.id}/bundle`)
.set('Cookie', authCookie(other.id));
expect(res.status).toBe(404);
});
it('BUNDLE-004 — members can fetch bundle', async () => {
const { user: owner } = createUser(testDb);
const { user: member } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
testDb.prepare('INSERT INTO trip_members (trip_id, user_id) VALUES (?, ?)').run(trip.id, member.id);
const res = await request(app)
.get(`/api/trips/${trip.id}/bundle`)
.set('Cookie', authCookie(member.id));
expect(res.status).toBe(200);
expect(res.body.trip.id).toBe(trip.id);
});
it('BUNDLE-005 — returns 401 when unauthenticated', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const res = await request(app).get(`/api/trips/${trip.id}/bundle`);
expect(res.status).toBe(401);
});
});