Spaces:
Sleeping
Sleeping
File size: 10,265 Bytes
79ed62f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | /**
* tripSyncManager unit tests.
*
* Covers: trip filtering (shouldCache/isStale), bundle fetch β Dexie upsert,
* stale trip eviction, offline guard, file blob caching.
*/
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 { tripSyncManager } from '../../../src/sync/tripSyncManager';
import { setAuthed } from '../../../src/sync/authGate';
import { offlineDb, clearAll, upsertTrip } from '../../../src/db/offlineDb';
import {
buildTrip,
buildDay,
buildPlace,
buildPackingItem,
buildTodoItem,
buildBudgetItem,
buildReservation,
buildTripFile,
} from '../../helpers/factories';
// Helper to get today Β± N days as YYYY-MM-DD
function dateOffset(days: number): string {
const d = new Date();
d.setDate(d.getDate() + days);
return d.toISOString().slice(0, 10);
}
function makeBundle(tripId: number) {
const trip = buildTrip({ id: tripId, end_date: dateOffset(3) });
return {
trip,
days: [buildDay({ trip_id: tripId, assignments: [], notes_items: [] })],
places: [buildPlace({ trip_id: tripId })],
packingItems: [buildPackingItem({ trip_id: tripId })],
todoItems: [buildTodoItem({ trip_id: tripId })],
budgetItems: [buildBudgetItem({ trip_id: tripId })],
reservations: [buildReservation({ trip_id: tripId })],
files: [buildTripFile({ trip_id: tripId, url: `/api/trips/${tripId}/files/99/download`, mime_type: 'application/pdf' })],
};
}
beforeEach(async () => {
await clearAll();
tripSyncManager._resetSyncing();
setAuthed(true);
Object.defineProperty(navigator, 'onLine', { value: true, writable: true, configurable: true });
// Stub fetch for blob caching (used by cacheFilesForTrip)
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
ok: true,
blob: async () => new Blob(['data'], { type: 'application/pdf' }),
}));
});
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
setAuthed(false);
});
describe('tripSyncManager.syncAll β auth gate (B4)', () => {
it('no-ops when logged out (gate closed)', async () => {
setAuthed(false);
let called = false;
server.use(
http.get('/api/trips', () => { called = true; return HttpResponse.json({ trips: [] }); }),
);
await tripSyncManager.syncAll();
expect(called).toBe(false);
});
});
// ββ offline guard βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('tripSyncManager.syncAll β offline guard', () => {
it('does nothing when offline', async () => {
Object.defineProperty(navigator, 'onLine', { value: false });
let listed = false;
server.use(
http.get('/api/trips', () => { listed = true; return HttpResponse.json({ trips: [] }); }),
);
await tripSyncManager.syncAll();
expect(listed).toBe(false);
});
});
// ββ trip filtering βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('tripSyncManager.syncAll β trip filtering', () => {
it('caches ongoing trips (end_date >= today)', async () => {
const tripId = 100;
const bundle = makeBundle(tripId);
server.use(
http.get('/api/trips', () =>
HttpResponse.json({ trips: [buildTrip({ id: tripId, end_date: dateOffset(2) })] }),
),
http.get(`/api/trips/${tripId}/bundle`, () => HttpResponse.json(bundle)),
);
await tripSyncManager.syncAll();
const cached = await offlineDb.trips.get(tripId);
expect(cached).toBeDefined();
expect(cached!.id).toBe(tripId);
});
it('caches trips with no end_date', async () => {
const tripId = 101;
const bundle = makeBundle(tripId);
const trip = buildTrip({ id: tripId, end_date: null as unknown as string });
server.use(
http.get('/api/trips', () => HttpResponse.json({ trips: [trip] })),
http.get(`/api/trips/${tripId}/bundle`, () => HttpResponse.json({ ...bundle, trip })),
);
await tripSyncManager.syncAll();
expect(await offlineDb.trips.get(tripId)).toBeDefined();
});
it('does not cache past trips (end_date < today)', async () => {
const tripId = 102;
server.use(
http.get('/api/trips', () =>
HttpResponse.json({ trips: [buildTrip({ id: tripId, end_date: dateOffset(-1) })] }),
),
);
// Bundle should NOT be called for past trips
let bundleCalled = false;
server.use(
http.get(`/api/trips/${tripId}/bundle`, () => {
bundleCalled = true;
return HttpResponse.json({});
}),
);
await tripSyncManager.syncAll();
expect(bundleCalled).toBe(false);
expect(await offlineDb.trips.get(tripId)).toBeUndefined();
});
});
// ββ stale eviction βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('tripSyncManager.syncAll β stale eviction', () => {
it('evicts trips that ended more than 7 days ago', async () => {
const staleId = 200;
// Seed Dexie as if previously cached
await upsertTrip(buildTrip({ id: staleId, end_date: dateOffset(-8) }));
server.use(
http.get('/api/trips', () =>
HttpResponse.json({ trips: [buildTrip({ id: staleId, end_date: dateOffset(-8) })] }),
),
);
await tripSyncManager.syncAll();
expect(await offlineDb.trips.get(staleId)).toBeUndefined();
});
it('does NOT evict trips that ended exactly 6 days ago', async () => {
const recentId = 201;
const bundle = makeBundle(recentId);
const trip = buildTrip({ id: recentId, end_date: dateOffset(-6) });
server.use(
http.get('/api/trips', () => HttpResponse.json({ trips: [trip] })),
http.get(`/api/trips/${recentId}/bundle`, () => HttpResponse.json({ ...bundle, trip })),
);
await tripSyncManager.syncAll();
// end_date = -6 days: still within 7d window, but < today so not cached
// i.e., shouldCache is false (end_date < today) so won't be fetched
// but also isStale is false (end_date = -6 >= cutoff -7), so won't be evicted
// β trip should simply not appear in Dexie (not cached, not evicted pre-seeded data)
expect(await offlineDb.trips.get(recentId)).toBeUndefined();
});
});
// ββ bundle upsert ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('tripSyncManager.syncAll β bundle upsert', () => {
it('writes all bundle entities to Dexie', async () => {
const tripId = 300;
const bundle = makeBundle(tripId);
server.use(
http.get('/api/trips', () =>
HttpResponse.json({ trips: [buildTrip({ id: tripId, end_date: dateOffset(5) })] }),
),
http.get(`/api/trips/${tripId}/bundle`, () => HttpResponse.json(bundle)),
);
await tripSyncManager.syncAll();
expect(await offlineDb.trips.get(tripId)).toBeDefined();
expect(await offlineDb.days.where('trip_id').equals(tripId).count()).toBe(1);
expect(await offlineDb.places.where('trip_id').equals(tripId).count()).toBe(1);
expect(await offlineDb.packingItems.where('trip_id').equals(tripId).count()).toBe(1);
expect(await offlineDb.todoItems.where('trip_id').equals(tripId).count()).toBe(1);
expect(await offlineDb.budgetItems.where('trip_id').equals(tripId).count()).toBe(1);
expect(await offlineDb.reservations.where('trip_id').equals(tripId).count()).toBe(1);
expect(await offlineDb.tripFiles.where('trip_id').equals(tripId).count()).toBe(1);
});
it('writes syncMeta with lastSyncedAt', async () => {
const tripId = 301;
const bundle = makeBundle(tripId);
server.use(
http.get('/api/trips', () =>
HttpResponse.json({ trips: [buildTrip({ id: tripId, end_date: dateOffset(5) })] }),
),
http.get(`/api/trips/${tripId}/bundle`, () => HttpResponse.json(bundle)),
);
const before = Date.now();
await tripSyncManager.syncAll();
const after = Date.now();
const meta = await offlineDb.syncMeta.get(tripId);
expect(meta).toBeDefined();
expect(meta!.lastSyncedAt).toBeGreaterThanOrEqual(before);
expect(meta!.lastSyncedAt).toBeLessThanOrEqual(after);
});
});
// ββ file blob caching ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('tripSyncManager β file blob caching', () => {
it('caches non-photo files after bundle sync', async () => {
const tripId = 400;
const bundle = makeBundle(tripId);
server.use(
http.get('/api/trips', () =>
HttpResponse.json({ trips: [buildTrip({ id: tripId, end_date: dateOffset(5) })] }),
),
http.get(`/api/trips/${tripId}/bundle`, () => HttpResponse.json(bundle)),
);
await tripSyncManager.syncAll();
// Give fire-and-forget a tick
await new Promise(r => setTimeout(r, 50));
const cached = await offlineDb.blobCache.toArray();
expect(cached.length).toBeGreaterThan(0);
expect(cached[0].url).toContain('/download');
});
it('does not cache photo files (image/* MIME)', async () => {
const tripId = 401;
const photoFile = buildTripFile({
trip_id: tripId,
mime_type: 'image/jpeg',
url: `/api/trips/${tripId}/files/77/download`,
});
const bundle = {
...makeBundle(tripId),
files: [photoFile],
};
server.use(
http.get('/api/trips', () =>
HttpResponse.json({ trips: [buildTrip({ id: tripId, end_date: dateOffset(5) })] }),
),
http.get(`/api/trips/${tripId}/bundle`, () => HttpResponse.json(bundle)),
);
await tripSyncManager.syncAll();
await new Promise(r => setTimeout(r, 50));
const cached = await offlineDb.blobCache.toArray();
expect(cached.length).toBe(0);
});
});
|