diff --git a/web/src/lib/db.svelte.ts b/web/src/lib/db.svelte.ts index f642bb2..5f0916c 100644 --- a/web/src/lib/db.svelte.ts +++ b/web/src/lib/db.svelte.ts @@ -32,7 +32,8 @@ type AnyCollection = Collection; interface PushResult { id: string; collection: string; - status: 'ok' | 'stale' | 'forbidden' | 'readonly'; + status: 'ok' | 'stale' | 'forbidden' | 'readonly' | 'invalid'; + error?: unknown; record?: Record | null; } @@ -151,6 +152,8 @@ const pendingCursor = new Map(); const staleBuffer = new Map(); /** Change records the last push of a collection sent; the pull after it keeps newer local changes. */ const pushedChanges = new Map>(); +/** Set when the server rejected a change of the current round; its message stays until a round succeeds. */ +let rejected = false; /** Collections whose local state the sync is about to touch or is touching, by name and nesting depth. */ const applying = new Map(); @@ -197,7 +200,7 @@ function onSynced(name: string) { pendingCursor.delete(name); } status.lastSync = Date.now(); - status.error = null; + if (!rejected) status.error = null; } class WotraSync extends SyncManager, AnyDoc, string> { @@ -324,6 +327,11 @@ async function doOpenDb(uid: string): Promise { } }); for (const r of res?.results ?? []) { + if (r.status === 'invalid') { + if (!rejected) status.error = `${name}: ${String(r.error ?? 'rejected')}`.slice(0, 200); + rejected = true; + continue; + } if (r.status === 'ok' || !r.record || !isCollection(r.collection)) continue; const list = staleBuffer.get(r.collection) ?? []; list.push(toRecord(r.collection, r.record)); @@ -377,6 +385,7 @@ export function syncNow(): Promise { if (syncing) return syncing; const m = sm; if (!m || !status.online) return Promise.resolve(); + rejected = false; const run: Promise = runBeforeSync() .then(() => m.syncAll()) .catch((err: Error) => { diff --git a/web/src/lib/db.test.ts b/web/src/lib/db.test.ts index 82f9c39..c68bd70 100644 --- a/web/src/lib/db.test.ts +++ b/web/src/lib/db.test.ts @@ -8,11 +8,15 @@ const mocks = vi.hoisted(() => { const server = new Map>>(); const pushes: { collection: string; id: string; data: Record; client_updated_at: number; deleted: boolean }[] = []; const batches: number[] = []; + const reject = { collection: '', error: '' }; + const slow = { name: '' }; let tick = 0; return { server, pushes, batches, + reject, + slow, stamp: () => new Date(Date.UTC(2026, 0, 1) + tick++).toISOString().replace('T', ' ') }; }); @@ -22,7 +26,10 @@ vi.mock('$lib/pb', () => ({ autoCancellation: () => undefined, filter: (tpl: string, params: Record) => tpl.replace(/\{:(\w+)\}/g, (_m, key: string) => JSON.stringify(String(params[key]))), collection: (name: string) => ({ - getFullList: async () => [...(mocks.server.get(name)?.values() ?? [])], + getFullList: async () => { + if (mocks.slow.name === name) await new Promise((resolve) => setTimeout(resolve, 5)); + return [...(mocks.server.get(name)?.values() ?? [])]; + }, subscribe: async () => async () => undefined }), send: async (path: string, options: { method?: string; body?: unknown }) => { @@ -30,6 +37,8 @@ vi.mock('$lib/pb', () => ({ const body = options.body as { changes: (typeof mocks.pushes)[number][] }; mocks.batches.push(body.changes.length); const results = body.changes.map((c) => { + mocks.pushes.push(c); + if (mocks.reject.collection === c.collection) return { id: c.id, collection: c.collection, status: 'invalid', error: mocks.reject.error }; const record = { id: c.id, ...c.data, client_updated_at: c.client_updated_at, rev: 1, deleted: c.deleted, updated: mocks.stamp() }; let store = mocks.server.get(c.collection); if (!store) { @@ -37,7 +46,6 @@ vi.mock('$lib/pb', () => ({ mocks.server.set(c.collection, store); } store.set(c.id, record); - mocks.pushes.push(c); return { id: c.id, collection: c.collection, status: 'ok', record }; }); return { results }; @@ -218,4 +226,37 @@ describe('sync boundary', () => { expect(Math.max(...mocks.batches)).toBeLessThanOrEqual(200); expect(mocks.batches.reduce((sum, size) => sum + size, 0)).toBe(250); }, 20000); + + it('a rejected change is dropped and reported, and a later success of the round keeps the message', async () => { + status.online = true; + mocks.reject.collection = 'settings_history'; + mocks.reject.error = 'hours_per_week must be at most 168'; + mocks.slow.name = 'balance_adjustments'; + try { + const id = await save('settings_history', null, settingsRow); + await save('balance_adjustments', null, { delta_ms: 1000, note: '', effective_at: 1 }); + await syncNow(); + expect(status.error).toBe('settings_history: hours_per_week must be at most 168'); + mocks.reject.collection = ''; + await syncNow(); + expect(status.error).toBeNull(); + expect(mocks.pushes.filter((c) => c.id === id)).toHaveLength(1); + } finally { + mocks.reject.collection = ''; + mocks.slow.name = ''; + } + }); + + it('a long rejection is cut to 200 characters', async () => { + status.online = true; + mocks.reject.collection = 'balance_adjustments'; + mocks.reject.error = 'x'.repeat(300); + try { + await save('balance_adjustments', null, { delta_ms: 1000, note: '', effective_at: 1 }); + await syncNow(); + expect(status.error).toBe(`balance_adjustments: ${'x'.repeat(200 - 'balance_adjustments: '.length)}`); + } finally { + mocks.reject.collection = ''; + } + }); });