// The Posts library's view, keys and undo (lane posts-library): pure rules. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import test from "node:test"; import { captureSentence, filterWords, goneSentence, libraryHref, listingParamsOf, parseLibraryView } from "./filters.mjs"; import { isTyping, LIBRARY_KEYS, libraryAction, stateOfAction } from "./keys.mjs"; import { pressSetOf, UNDO_SAYS, undoSetOf } from "./undo.mjs"; test("a view is read from the URL and written back; an ill-shaped value is dropped", () => { const view = parseLibraryView({ lib: "trending-lib", topic: "kettles", state: "unjudged", gone: "1", media: "1", author: "@teapot_news", q: " lid ", new: "1", offset: "50" }); assert.deepEqual(view, { lib: "trending-lib", topic: "kettles", state: "unjudged", gone: true, media: true, author: "teapot_news", q: "lid", fresh: true, offset: 50 }); assert.equal(libraryHref(view), "/tools/library?lib=trending-lib&topic=kettles&state=unjudged&gone=1&media=1&author=teapot_news&q=lid&new=1&offset=50"); assert.deepEqual(parseLibraryView({ lib: "../x", topic: "A B", state: "deleted", gone: "yes", author: "a/b", offset: "-3", q: ["two", "three"] }), { lib: null, topic: null, state: null, gone: false, media: false, author: null, q: "two", fresh: false, offset: 0 }); assert.equal(libraryHref({}), "/tools/library"); assert.equal(libraryHref({ lib: "l", state: "keep" }), "/tools/library?lib=l&state=keep"); }); test("\"new\" is first shown since the topic's Mark seen; a topic never marked has no since", () => { const view = parseLibraryView({ lib: "l", topic: "kettles", new: "1" }); assert.equal(listingParamsOf(view, { seenAt: "2026-11-17T10:00:00.000Z" }).since, "2026-11-17T10:00:00.000Z"); assert.equal(listingParamsOf(view).since, null); assert.equal(listingParamsOf(parseLibraryView({ lib: "l", topic: "kettles" }), { seenAt: "2026-11-17T10:00:00.000Z" }).since, null); }); test("the sentences: the filters in words, gone from the page (never deleted), a capture's state verbatim", () => { assert.equal(filterWords(parseLibraryView({ state: "unjudged", media: "1", author: "x_y", q: "lid", gone: "1", new: "1" })), "unjudged, new since Mark seen, gone from the page, with media, by @x_y, matching “lid”"); assert.equal(filterWords(parseLibraryView({})), ""); assert.equal(goneSentence({ at: "2026-11-17T11:00:00.000Z", read: "r", trendId: "1" }), "Gone from the page at 2026-11-17T11:00:00.000Z"); assert.equal(goneSentence(undefined), ""); assert.doesNotMatch(goneSentence({ at: "t" }), /delet/i); assert.equal(captureSentence({ state: "deleted", at: "2026-11-17T12:00:00.000Z" }), "capture: deleted at 2026-11-17T12:00:00.000Z"); assert.equal(captureSentence({ state: "captured", at: "T" }, () => "noon"), "capture: captured at noon"); }); test("the keys: one action per key, none while typing or with a modifier held", () => { assert.equal(new Set(LIBRARY_KEYS.map((k) => k.key)).size, LIBRARY_KEYS.length); const of = (key, extra = {}) => libraryAction({ key, ...extra }); assert.deepEqual(["j", "k", "a", "d", "l", "c", "t", "n", "x", "u", "/", "g", "m", "?"].map((k) => of(k)), ["next", "prev", "keep", "drop", "later", "clear", "scope", "note", "select", "undo", "search", "gone", "media", "help"]); assert.deepEqual(["A", "D", "L"].map((k) => of(k)), ["bulk-keep", "bulk-drop", "bulk-later"]); assert.equal(of("a", { typing: true }), null); assert.equal(of("a", { ctrlKey: true }), null); assert.equal(of("d", { metaKey: true }), null); assert.equal(of("q"), null); assert.equal(isTyping({ tagName: "TEXTAREA" }), true); assert.equal(isTyping({ tagName: "DIV", isContentEditable: true }), true); assert.equal(isTyping({ tagName: "BUTTON" }), false); assert.deepEqual(["keep", "drop", "later", "clear", "bulk-drop", "next"].map(stateOfAction), ["keep", "drop", "later", "undecided", "drop", null]); }); test("undo puts back each id's OWN entry, or removes the judgement it had none of; a thread press restores the thread's", () => { const rows = [ { id: "1", thread: "1", detail: { ownJudgement: { state: "later", note: "check", by: "agent", at: "t" }, threadJudgement: { state: "keep", by: "operator", at: "t" } } }, { id: "2", thread: "1", detail: { threadJudgement: { state: "keep", by: "operator", at: "t" } } }, { id: "3", thread: "3", detail: {} }, ]; assert.deepEqual(undoSetOf(rows, [{ id: "1" }, { id: "2" }, { id: "3" }, { id: "9" }]), [ { id: "1", state: "later", note: "check" }, // 2 had only its thread's judgement: its own goes back to none. { id: "2", state: "undecided" }, { id: "3", state: "undecided" }, { id: "9", state: "undecided" }, ]); assert.deepEqual(undoSetOf(rows, [{ thread: "1" }, { thread: "3" }]), [{ thread: "1", state: "keep" }, { thread: "3", state: "undecided" }]); assert.deepEqual(pressSetOf([{ id: "1" }, { thread: "3" }], "drop", "spam"), [{ id: "1", state: "drop", note: "spam" }, { thread: "3", state: "drop", note: "spam" }]); assert.deepEqual(pressSetOf([{ id: "1" }], "undecided", "ignored"), [{ id: "1", state: "undecided" }]); assert.match(UNDO_SAYS, /does not cancel a capture already queued/); // S7: an undone entry becomes the operator's, and the bar says so. assert.match(UNDO_SAYS, /written as the operator's whoever wrote them before/); }); test("gone from one tab is its own sentence, never \"deleted\"", async () => { const { goneTabSentence } = await import("./filters.mjs"); assert.equal(goneTabSentence({ tab: "Latest", at: "2026-11-17T12:00:00.000Z" }, (iso) => iso.slice(11, 16)), "Gone from the “Latest” tab at 12:00; another tab still shows it."); });