// Work's facets: pure, over hand-made rows. import assert from "node:assert/strict"; import test from "node:test"; import { keepRow, lifeFilter, sortRows, stageOf, stepOf } from "./facets.mjs"; const row = (over = {}) => ({ id: "a:articles/poker", title: "Zero dollars to play", kinds: ["article", "vid"], bucket: "video", site: "articles", lifecycle: "active", templates: ["report"], states: ["built"], status: "published", audience: "public", haystack: "poker deposit", counts: { blocking: 0, open: 0, notes: 0 }, newestMs: 0, ...over, }); test("a stage is an article's own, and a project's deliver or video", () => { assert.equal(stageOf({ kind: "article", stage: "research", looksDone: [] }), "research"); assert.equal(stageOf({ kind: "article", stage: "deliver", looksDone: ["x"] }), "deliver"); assert.equal(stageOf({ kind: "project", stage: "built", looksDone: [] }), "video"); assert.equal(stageOf({ kind: "project", stage: "built", looksDone: ["final video and share-1"] }), "deliver"); assert.equal(stageOf({ kind: "project", stage: "shipped", looksDone: [], projectState: "shipped" }), "deliver"); assert.deepEqual(["research", "article", "video", "deliver"].map(stepOf), [1, 2, 3, 4]); // With the graph's facts, the one stage rule (lib/pieces/tabs.mjs pieceSteps) decides. assert.equal(stageOf({ kind: "article", stage: "research", looksDone: [], reached: { research: true, article: false, video: true, deliver: false } }), "video"); assert.equal(stageOf({ kind: "project", stage: "built", looksDone: [], reached: { research: false, article: false, video: true, deliver: true } }), "deliver"); }); test("lifecycle defaults to active; all shows every piece", () => { assert.equal(lifeFilter(undefined), "active"); assert.equal(lifeFilter("junk"), "active"); assert.equal(lifeFilter("parked"), "parked"); assert.equal(lifeFilter("all"), null); assert.equal(keepRow(row({ lifecycle: "done" }), {}), false); assert.equal(keepRow(row({ lifecycle: "done" }), { life: "done" }), true); assert.equal(keepRow(row({ lifecycle: "done" }), { life: "all" }), true); }); test("the old /browse keys: a project kind finds the article whose video it is; state, template, open", () => { assert.equal(keepRow(row(), { kind: "vid" }), true); assert.equal(keepRow(row(), { kind: "jingle" }), false); assert.equal(keepRow(row(), { state: "built", template: "report" }), true); assert.equal(keepRow(row(), { state: "draft" }), false); assert.equal(keepRow(row(), { open: "1" }), false); assert.equal(keepRow(row({ counts: { blocking: 0, open: 2, notes: 0 } }), { open: "1" }), true); assert.equal(keepRow(row({ counts: { blocking: 0, open: 2, notes: 0 } }), { open: "blocking" }), false); }); test("the old /articles and /sites keys: status, audience, notes, site", () => { assert.equal(keepRow(row(), { kind: "article", status: "draft" }), false); assert.equal(keepRow(row(), { kind: "article", status: "published", audience: "public" }), true); assert.equal(keepRow(row(), { notes: "open" }), false); assert.equal(keepRow(row({ counts: { blocking: 0, open: 0, notes: 3 } }), { notes: "open" }), true); assert.equal(keepRow(row(), { site: "jeralyzer" }), false); }); test("search reads the title, the id and the haystack; a facet can ignore itself", () => { assert.equal(keepRow(row(), { q: "DEPOSIT" }), true); assert.equal(keepRow(row(), { q: "a:articles/poker" }), true); assert.equal(keepRow(row(), { q: "ferret" }), false); assert.equal(keepRow(row(), { stage: "deliver" }), false); assert.equal(keepRow(row(), { stage: "deliver" }, { ignore: ["stage"] }), true); }); test("sort: newest first, or by title", () => { const rows = [row({ id: "a", title: "B", newestMs: 1 }), row({ id: "b", title: "A", newestMs: 2 })]; assert.deepEqual(sortRows(rows).map((r) => r.id), ["b", "a"]); assert.deepEqual(sortRows(rows, "name").map((r) => r.id), ["b", "a"]); assert.deepEqual(sortRows([row({ id: "x", title: "A" }), row({ id: "y", title: "B", newestMs: 9 })], "name").map((r) => r.id), ["x", "y"]); }); test("recurring (R5): `1` keeps the series and the living reports, nothing else", () => { assert.equal(keepRow(row(), { recurring: "1" }), false); assert.equal(keepRow(row({ recurring: true }), { recurring: "1" }), true); assert.equal(keepRow(row({ id: "s:cohost", kinds: ["series"], recurring: true }), { recurring: "1", kind: "series" }), true); assert.equal(keepRow(row(), {}), true); });