first commit
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 1m4s
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 1m4s
This commit is contained in:
commit
6fc0a01c56
51 changed files with 11319 additions and 0 deletions
136
tests/client-sync.test.js
Normal file
136
tests/client-sync.test.js
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
import "fake-indexeddb/auto";
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { openDB } from "idb";
|
||||
import { compareRecords, emptyNutrients } from "../shared/domain.js";
|
||||
|
||||
// Exercise the real browser store against IndexedDB with controllable network timing.
|
||||
globalThis.window = new EventTarget();
|
||||
globalThis.document = Object.assign(new EventTarget(), { hidden: false });
|
||||
Object.defineProperty(globalThis, "navigator", {
|
||||
value: { onLine: false },
|
||||
configurable: true,
|
||||
});
|
||||
globalThis.BroadcastChannel = class {
|
||||
addEventListener() {}
|
||||
postMessage() {}
|
||||
};
|
||||
const originalInterval = globalThis.setInterval;
|
||||
globalThis.setInterval = () => 0;
|
||||
const client = await import("../src/store.ts");
|
||||
globalThis.setInterval = originalInterval;
|
||||
|
||||
const remote = new Map();
|
||||
let pause = null;
|
||||
let arrived = null;
|
||||
let fail = false;
|
||||
let unauthorized = false;
|
||||
globalThis.fetch = async (url, options) => {
|
||||
if (fail) throw new Error("Connection unavailable");
|
||||
if (unauthorized && url.endsWith("/sync"))
|
||||
return Response.json(
|
||||
{ error: "Sign in to sync your journal" },
|
||||
{ status: 401 },
|
||||
);
|
||||
if (url.endsWith("/login") || url.endsWith("/logout"))
|
||||
return Response.json({ ok: true });
|
||||
const { records } = JSON.parse(options.body);
|
||||
const gate = pause;
|
||||
pause = null;
|
||||
if (gate) {
|
||||
arrived();
|
||||
await gate;
|
||||
}
|
||||
for (const record of records) {
|
||||
const existing = remote.get(record.id);
|
||||
if (!existing || compareRecords(record, existing) > 0)
|
||||
remote.set(record.id, record);
|
||||
}
|
||||
return Response.json({
|
||||
records: [...remote.values()],
|
||||
serverTime: new Date().toISOString(),
|
||||
});
|
||||
};
|
||||
const food = (name) => ({
|
||||
name,
|
||||
serving: "1 serving",
|
||||
nutrients: { ...emptyNutrients(), calories: 100 },
|
||||
});
|
||||
const read = async () =>
|
||||
await (await openDB("calio-journal", 1)).get("state", "journal");
|
||||
|
||||
test("offline journal and outbox are atomic, in-flight edits remain queued, reconnect merges without duplication", async () => {
|
||||
await client.initStore();
|
||||
await client.login("test", "test-only-password"); // offline mock sign-in seeds the local unlocked flag.
|
||||
await client.saveRecord("food", food("First"), "food-one");
|
||||
let state = await read();
|
||||
assert.equal(state.records.length, 1);
|
||||
assert.equal(state.outbox.length, 1);
|
||||
assert.deepEqual(state.records[0], state.outbox[0]);
|
||||
let release;
|
||||
let signalArrival;
|
||||
pause = new Promise((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
const sent = new Promise((resolve) => {
|
||||
signalArrival = resolve;
|
||||
});
|
||||
arrived = signalArrival;
|
||||
navigator.onLine = true;
|
||||
const syncing = client.sync();
|
||||
await sent;
|
||||
// This write happens after the request captured the old version.
|
||||
await client.saveRecord("food", food("Edited while syncing"), "food-one");
|
||||
state = await read();
|
||||
assert.equal(state.outbox[0].data.name, "Edited while syncing");
|
||||
release();
|
||||
await syncing;
|
||||
state = await read();
|
||||
assert.equal(state.outbox.length, 0);
|
||||
assert.equal(state.records.length, 1);
|
||||
assert.equal(remote.get("food-one").data.name, "Edited while syncing");
|
||||
await client.sync();
|
||||
assert.equal(remote.size, 1);
|
||||
});
|
||||
test("network errors and expired sessions preserve all pending changes", async () => {
|
||||
navigator.onLine = false;
|
||||
await client.saveRecord("food", food("Retained"), "food-two");
|
||||
navigator.onLine = true;
|
||||
fail = true;
|
||||
await client.sync();
|
||||
assert.equal((await read()).outbox.length, 1);
|
||||
await assert.rejects(client.logout(), /pending changes/);
|
||||
assert.equal((await read()).unlocked, true);
|
||||
fail = false;
|
||||
unauthorized = true;
|
||||
await client.sync();
|
||||
assert.equal((await read()).outbox.length, 1);
|
||||
unauthorized = false;
|
||||
await client.sync();
|
||||
assert.equal((await read()).outbox.length, 0);
|
||||
});
|
||||
test("a newer remote deletion beats a stale local write and explicit logout clears device data", async () => {
|
||||
const state = await read();
|
||||
const old = state.records.find((r) => r.id === "food-two");
|
||||
remote.set(old.id, {
|
||||
...old,
|
||||
updatedAt: new Date(Date.parse(old.updatedAt) + 1000).toISOString(),
|
||||
deleted: true,
|
||||
});
|
||||
navigator.onLine = false;
|
||||
await client.saveRecord("food", food("Stale offline edit"), old.id);
|
||||
navigator.onLine = true;
|
||||
await client.sync();
|
||||
assert.equal(
|
||||
(await read()).records.find((r) => r.id === old.id).deleted,
|
||||
true,
|
||||
);
|
||||
assert.equal((await read()).outbox.length, 0);
|
||||
await client.logout();
|
||||
assert.deepEqual(await read(), {
|
||||
records: [],
|
||||
outbox: [],
|
||||
unlocked: false,
|
||||
lastSync: null,
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue