Files

119 lines
6.3 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { keepsWithAccount, combineStreaks, decodeSyncSetting, newerRecord, newestSaved, sumModels, syncStance, unionHistory } from "./sync.ts";
import { EMPTY_MODEL } from "../practice/model.ts";
import { EMPTY_STREAK } from "./streak.ts";
const model = (lines: Record<string, [number, number]>) => ({
...EMPTY_MODEL,
lines: Object.fromEntries(Object.entries(lines).map(([k, [asked, missed]]) => [k, { asked, missed }])),
});
test("two devices' counts add, and syncing again does not add them twice", () => {
const phone = model({ "shopping:0": [2, 1] });
const laptop = model({ "shopping:0": [1, 0], "tram:3": [3, 0] });
const view = sumModels([phone, laptop]);
assert.deepEqual(view.lines, { "shopping:0": { asked: 3, missed: 1 }, "tram:3": { asked: 3, missed: 0 } });
// The phone's OWN record is unchanged by viewing — the next view is the same.
assert.deepEqual(sumModels([phone, laptop]), view);
});
test("no other devices is just this one", () => {
const phone = model({ "shopping:0": [2, 1] });
assert.deepEqual(sumModels([phone]), phone);
assert.deepEqual(sumModels([]), EMPTY_MODEL);
});
test("seen questions are the union", () => {
assert.deepEqual(unionHistory([["a", "b"], ["b", "c"]]), ["a", "b", "c"]);
});
test("the streak is the newest record, with the best of all", () => {
const phone = { ...EMPTY_STREAK, current: 4, best: 4, lastDay: "2026-09-24" };
const laptop = { ...EMPTY_STREAK, current: 2, best: 9, lastDay: "2026-09-25" };
const s = combineStreaks([phone, laptop]);
assert.equal(s.current, 2);
assert.equal(s.lastDay, "2026-09-25");
assert.equal(s.best, 9);
assert.deepEqual(combineStreaks([]), EMPTY_STREAK);
});
test("the saved list: another device's wins only if it changed later", () => {
const theirs = [{ value: "B", updatedAt: "2026-09-25T10:00:00Z" }];
assert.equal(newestSaved("2026-09-25T09:00:00Z", theirs), "B");
assert.equal(newestSaved("2026-09-25T11:00:00Z", theirs), null, "mine is newer: keep mine, removals included");
assert.equal(newestSaved(null, theirs), "B", "a fresh device takes the list");
assert.equal(newestSaved("2026-09-25T09:00:00Z", []), null);
});
test("the finished weeks of two devices: one entry per week, the fuller count, never the sum", () => {
const phone = { ...EMPTY_STREAK, lastDay: "2026-11-24", week: "2026-W48", weekDays: 1, past: [{ week: "2026-W47", days: 2, goal: 3 }] };
// The laptop was last used in W47, so that week is still its current one.
const laptop = { ...EMPTY_STREAK, lastDay: "2026-11-20", week: "2026-W47", weekDays: 3, past: [{ week: "2026-W46", days: 1, goal: 3 }] };
const s = combineStreaks([phone, laptop]);
assert.equal(s.week, "2026-W48");
assert.deepEqual(s.past, [
{ week: "2026-W47", days: 3, goal: 3 },
{ week: "2026-W46", days: 1, goal: 3 },
]);
});
const DEVICE = "0123456789abcdef0123";
test("progress follows the account by default, and only for somebody who was told", () => {
// Never chose, never saw the line (signed in before the default changed): asked, not switched.
assert.equal(syncStance(null), "ask");
// Saw the line on the sign-in screen: on once signed in.
const informed = decodeSyncSetting(JSON.stringify({ on: false, device: DEVICE, savedChangedAt: null, informed: true }))!;
assert.equal(syncStance(informed), "pending");
// On by default: the page still owes the notice and the one tap back.
const byDefault = decodeSyncSetting(JSON.stringify({ on: true, device: DEVICE, savedChangedAt: null, by: "default" }))!;
assert.equal(syncStance(byDefault), "told");
// Their own choice, either way, is final until they change it.
assert.equal(syncStance({ on: true, device: DEVICE, savedChangedAt: null }), "on");
assert.equal(syncStance({ on: false, device: DEVICE, savedChangedAt: null }), "off");
});
test("a setting written before the default changed still means what it meant", () => {
assert.deepEqual(decodeSyncSetting(JSON.stringify({ on: false, device: DEVICE, savedChangedAt: null })), { on: false, device: DEVICE, savedChangedAt: null });
assert.deepEqual(decodeSyncSetting(JSON.stringify({ on: true, device: DEVICE, savedChangedAt: "2026-11-01T10:00:00Z" })), {
on: true,
device: DEVICE,
savedChangedAt: "2026-11-01T10:00:00Z",
});
// The two marks cannot contradict the switch: "informed" only while off, "by default" only while on.
assert.equal(decodeSyncSetting(JSON.stringify({ on: true, device: DEVICE, informed: true }))!.informed, undefined);
assert.equal(decodeSyncSetting(JSON.stringify({ on: false, device: DEVICE, by: "default" }))!.by, undefined);
assert.equal(decodeSyncSetting(JSON.stringify({ on: true, device: "short" })), null);
});
test("the profile: the newer record wins, whichever device it is on", () => {
const mine = { reason: "work", at: "2026-11-25T10:00:00.000Z" };
const older = { reason: "care", at: "2026-11-24T10:00:00.000Z" };
const newer = { reason: "family", at: "2026-11-26T10:00:00.000Z" };
assert.equal(newerRecord(mine, [older]), null, "mine is newer: keep mine");
assert.equal(newerRecord(mine, [older, newer]), newer);
assert.equal(newerRecord(null, [older]), older, "a fresh device takes what there is");
assert.equal(newerRecord(mine, []), null);
});
test("signed in, progress is saved with the account unless the learner chose «device only»", () => {
const on = { on: true, device: DEVICE, savedChangedAt: null };
const off = { on: false, device: DEVICE, savedChangedAt: null };
const informed = { ...off, informed: true };
const byDefault = { ...on, by: "default" as const };
// No stored choice (ask), the sign-in marker, the default and an explicit yes: saved.
for (const setting of [null, informed, byDefault, on]) assert.equal(keepsWithAccount(setting, true), true);
// An explicit {on:false} is never overridden.
assert.equal(keepsWithAccount(off, true), false);
// Signed out: nothing is sent, whatever the record says.
for (const setting of [null, informed, byDefault, on, off]) assert.equal(keepsWithAccount(setting, false), false);
});
test("an explicit «device only» survives a round trip through the decoder", () => {
const raw = JSON.stringify({ on: false, device: DEVICE, savedChangedAt: "2026-11-01T10:00:00Z" });
const decoded = decodeSyncSetting(raw);
assert.equal(syncStance(decoded), "off");
assert.equal(keepsWithAccount(decoded, true), false);
});