🧪 Cover session expiry, network failures and user switches

The db tests get an echo server that honours the pull cursor, records
the filter of every pull, answers with a status of the test's choice
and keeps stale answers like the real one. New cases: a second user
opens without the collections, cursors, realtime subscriptions and
sync status of the first; wipeLocal really removes the cursors and the
database so that the next open pulls everything again; a push that
fails after a good pull and a pull that fails before the push both
keep the change records and the cursor, and the next round pushes it
all; a stale answer applies the server record and a newer local change
wins afterwards.

The stress test now compares every local record, tombstones included,
with the mock server after each round, so a divergence between the two
fails the test. closeDb() resets the last sync time for the same
reason: it belonged to the session that just ended.
This commit is contained in:
2026-12-01 19:16:35 +01:00
parent a29cedbef1
commit 03c7b17ccb
3 changed files with 161 additions and 10 deletions
+12 -1
View File
@@ -1,7 +1,7 @@
import { tick } from 'svelte';
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
import { addAdjustment, balance, deleteAdjustment } from './app.svelte';
import { all, closeDb, get, openDb, pendingCount, remove, save, status, syncNow, update } from './db.svelte';
import { all, closeDb, get, openDb, pendingCount, remove, save, status, syncNow, toRecord, update } from './db.svelte';
import { watch, type Watcher } from './testing.svelte';
const mocks = vi.hoisted(() => {
@@ -107,6 +107,15 @@ const tables: Table[] = [
const sum = (values: Map<string, number>) => [...values.values()].reduce((total, v) => total + v, 0);
const rowsOf = (table: Table) => all(table.name) as unknown as ({ id: string } & Record<Field, number>)[];
const byId = (a: { id: string }, b: { id: string }) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
/** After a round every local record, tombstones included, equals its server record; a round may not leave the two apart. */
const expectMirrorsServer = () => {
for (const table of tables) {
const server = [...(mocks.server.get(table.name)?.values() ?? [])].map((r) => toRecord(table.name, r)).sort(byId);
expect(all(table.name, true).sort(byId)).toEqual(server);
}
};
describe('reactive reads after writes', () => {
let anchor = '';
@@ -180,6 +189,7 @@ describe('reactive reads after writes', () => {
if (round % FLIGHT_EVERY !== 0) continue;
await syncNow();
expect(pendingCount()).toBe(0);
expectMirrorsServer();
const push = mocks.holdNextPush();
const adjustment = await addAdjustment({ delta_ms: 7, effective_at: round });
tables[2].live.set(adjustment, 7);
@@ -196,6 +206,7 @@ describe('reactive reads after writes', () => {
await syncNow();
await expectModel(round, true);
expect(pendingCount()).toBe(0);
expectMirrorsServer();
expect(pending.current).toBe(0);
const counts = watchers[2]?.count.seen.slice(seen) ?? [];
expect(counts.every((count) => count === tables[2].live.size)).toBe(true);