// The 대화 screen's paging (app/ui/talk.js; admin.spec.md, "Screens"). Run: node --test app/tests/ui/talk.test.mjs import test from "node:test"; import assert from "node:assert/strict"; import { PAGE, PAGE_MAX, compare, empty, withLive, withOlder } from "../../ui/talk.js"; const rec = (n) => ({ key: `h${n}`, at: n, type: "said", text: `말 ${n}` }); const range = (a, b) => Array.from({ length: b - a }, (_, i) => rec(a + i)); const keys = (s) => s.older.concat(s.shown).map((i) => i.key); /** The screen's state after the first page and as many "이전 기록 더 보기" as the store allows. */ function allLoaded(total) { const store = range(0, total); let s = withLive(empty(), store.slice(-PAGE), total > PAGE); while (s.hasOlder) { const oldest = (s.older[0] || s.shown[0]).at; const before = store.filter((i) => i.at < oldest); s = withOlder(s, before.slice(-PAGE), before.length > PAGE); } return { s, store }; } test("every older page joins once, past the live window", () => { const { s } = allLoaded(2501); assert.equal(s.shown.length, PAGE_MAX); assert.deepEqual(keys(s), range(0, 2501).map((i) => i.key)); }); test("the same older page arriving twice is not added twice", () => { let s = withLive(empty(), range(300, 500), true); s = withOlder(s, range(100, 300), true); s = withOlder(s, range(100, 300), true); assert.deepEqual(keys(s), range(100, 500).map((i) => i.key)); }); test("a new item on a full window keeps everything loaded, in order and once", () => { const { s, store } = allLoaded(2501); const now = store.concat(rec(2501)); // The live answer: from the window's start, at most PAGE_MAX, newest first cut. const since = now.filter((i) => compare(i, s.shown[0]) >= 0); const next = withLive(s, since.slice(-PAGE_MAX), since.length > PAGE_MAX); assert.deepEqual(keys(next), range(0, 2502).map((i) => i.key)); assert.equal(next.shown.length, PAGE_MAX); }); test("items are ordered by time, then records before traces, then id", () => { const items = [{ key: "t2", at: 1 }, { key: "h10", at: 1 }, { key: "h9", at: 1 }, { key: "h1", at: 0 }]; assert.deepEqual(items.sort(compare).map((i) => i.key), ["h1", "h9", "h10", "t2"]); });