import assert from "node:assert/strict"; import test from "node:test"; import { addable, breadcrumb, effectText, folderContents, folderTree, roomMarks, tileKind, tileLines } from "./library-core.js"; const cases = [ { id: "a", title: "Vermietung", parent_case_id: null, is_owner: true, shared_with: [{ id: "r1", name: "Werkstatt", access: "edit" }], child_cases: 2, matter_count: 1 }, { id: "b", title: "Nebenkosten", parent_case_id: "a", is_owner: true, shared_with: [], child_cases: 0, matter_count: 0 }, { id: "c", title: "Auto", parent_case_id: null, is_owner: false, shared_with: [] }, { id: "d", title: "Kaution", parent_case_id: "a", is_owner: true, shared_with: [] }, ]; test("a tile kind follows the hierarchy", () => { assert.equal(tileKind(cases[0], "case"), "case"); assert.equal(tileKind(cases[1], "case"), "subcase"); assert.equal(tileKind({ parent_matter_id: "x" }, "matter"), "submatter"); assert.equal(tileKind({}, "room"), "room"); }); test("tile lines show only counts and say when a folder is empty", () => { assert.deepEqual(tileLines(cases[0], "case"), ["2 Unterakten", "1 Vorgang"]); assert.deepEqual(tileLines(cases[1], "case"), ["Leer"]); assert.deepEqual(tileLines({ child_matters: 1, contexts: [{ kind: "case" }, { kind: "case" }] }, "matter"), ["1 Teilvorgang", "in 2 Akten"]); assert.deepEqual(tileLines({ contexts: [] }, "matter"), ["Eigenständig"]); assert.deepEqual(tileLines({ member_count: 1, case_count: 0, matter_count: 0 }, "room"), ["1 Mitglied", "Noch nichts darin"]); assert.deepEqual(tileLines({ member_count: 3, case_count: 2, matter_count: 1 }, "room"), ["3 Mitglieder", "2 Akten, 1 Vorgang"]); }); test("room marks mark read-only shares", () => { assert.deepEqual(roomMarks(cases[0]), [{ id: "r1", name: "Werkstatt", readOnly: false }]); assert.deepEqual(roomMarks({}), []); assert.equal(roomMarks({ shared_with: [{ id: "x", name: "Anwalt", access: "read" }] })[0].readOnly, true); }); test("the effect of a share is told in words", () => { const effect = [ { display_name: "Ben", rights: { read: true, edit: true } }, { display_name: "Cem", rights: { read: true, edit: false } }, ]; assert.equal(effectText("Werkstatt", effect, "Vermietung"), "„Vermietung“ liegt jetzt in „Werkstatt“. Ben bearbeitet, Cem liest."); assert.equal(effectText("Werkstatt", [], "Vermietung"), "„Vermietung“ liegt jetzt in „Werkstatt“. Außer dir hat noch niemand Zugriff."); assert.match(effectText("Werkstatt", effect), /^Die Auswahl liegt jetzt/); }); test("the folder tree nests children and a folder shows its children", () => { const { roots, children } = folderTree(cases, "parent_case_id"); assert.deepEqual(roots.map((c) => c.id), ["c", "a"]); assert.deepEqual(children.get("a").map((c) => c.id), ["d", "b"]); assert.deepEqual(folderContents(cases, "parent_case_id", "a").map((c) => c.title), ["Kaution", "Nebenkosten"]); assert.deepEqual(folderContents(cases, "parent_case_id", "").map((c) => c.title), ["Auto", "Vermietung"]); assert.deepEqual(folderContents(cases, "parent_case_id", "zzz"), []); }); test("an orphaned child becomes a root and a breadcrumb stops at the top", () => { const items = [{ id: "x", title: "Kind", parent_case_id: "weg" }]; assert.deepEqual(folderTree(items, "parent_case_id").roots.map((c) => c.id), ["x"]); assert.deepEqual(breadcrumb(cases, "parent_case_id", "b").map((e) => e.title), ["Vermietung", "Nebenkosten"]); assert.deepEqual(breadcrumb(cases, "parent_case_id", "nope"), []); }); test("only own entries that are not yet in the room can be added", () => { assert.deepEqual(addable(cases, "r1").map((c) => c.id), ["b", "d"]); assert.deepEqual(addable(cases, "r2").map((c) => c.id), ["a", "b", "d"]); }); import { caseHref, caseKey, chapterTree, focusKeys, matterHref, matterKey, outlineSignature, rowState } from "./library-core.js"; const allCases = [ { id: "a", parent_case_id: null }, { id: "b", parent_case_id: "a" }, ]; const allMatters = [ { id: "m1", parent_matter_id: null }, { id: "m2", parent_matter_id: "m1" }, ]; test("the focus opens the path to a case, its parents and its section", () => { assert.deepEqual([...focusKeys({ kind: "case", id: "b" }, allCases, allMatters)].sort(), ["c:a", "c:b", "sec:cases"]); assert.equal(focusKeys(null, allCases, allMatters).size, 0); }); test("a matter opens under its case context or under the matters section", () => { const inCase = focusKeys({ kind: "matter", id: "m2", contextId: "b" }, allCases, allMatters); assert.deepEqual([...inCase].sort(), ["c:a", "c:b", "m:m1@b", "m:m2@b", "sec:cases"]); const alone = focusKeys({ kind: "matter", id: "m2", contextId: "" }, allCases, allMatters); assert.deepEqual([...alone].sort(), ["m:m1", "m:m2", "sec:matters"]); assert.equal(matterKey("x", "c"), "m:x@c"); assert.equal(matterKey("x"), "m:x"); assert.equal(caseKey("x"), "c:x"); }); test("chapters become a tree that knows its first and last sibling", () => { const chapters = [ { id: "1", parent_chapter_id: null }, { id: "2", parent_chapter_id: "1" }, { id: "3", parent_chapter_id: "1" }, { id: "4", parent_chapter_id: null }, ]; const tree = chapterTree(chapters); assert.deepEqual(tree.map((n) => [n.chapter.id, n.first, n.last]), [["1", true, false], ["4", false, true]]); assert.deepEqual(tree[0].children.map((n) => [n.chapter.id, n.first, n.last]), [["2", true, false], ["3", false, true]]); }); test("links keep the case context of a matter", () => { assert.equal(caseHref("a"), "/cases/a"); assert.equal(caseHref("a", { kapitel: "k" }), "/cases/a?kapitel=k"); assert.equal(matterHref("m", "a"), "/matters/m?context_kind=case&context_id=a"); assert.equal(matterHref("m", "a", { kapitel: "k" }), "/matters/m?context_kind=case&context_id=a&kapitel=k"); assert.equal(matterHref("m"), "/matters/m"); }); test("a row is current only for its own context", () => { const focus = { kind: "matter", id: "m", contextId: "a", chapterId: "" }; assert.equal(rowState(focus, "matter", "m", "a").whole, true); assert.equal(rowState(focus, "matter", "m", "b").here, false); assert.equal(rowState(focus, "matter", "m", "").here, false); assert.equal(rowState({ kind: "case", id: "a", overview: true }, "case", "a").overview, true); assert.equal(rowState({ kind: "case", id: "a", chapterId: "k" }, "case", "a").whole, false); assert.equal(rowState(null, "case", "a").here, false); }); test("the outline signature changes with chapters, subcases and matters", () => { const base = { id: "a", title: "T", chapters: [{ id: "1", title: "K", position: 1 }], subcases: [{ id: "s" }], matters: [{ id: "m", children: [{ id: "n", children: [] }] }] }; const same = outlineSignature(base); assert.equal(outlineSignature({ ...base }), same); assert.notEqual(outlineSignature({ ...base, chapters: [...base.chapters, { id: "2", title: "L", position: 2 }] }), same); assert.notEqual(outlineSignature({ ...base, subcases: [] }), same); assert.notEqual(outlineSignature({ ...base, matters: [] }), same); assert.equal(outlineSignature(null), ""); }); import { TREE_DEFAULT, TREE_MAX, TREE_MIN, clampTreeWidth, readTreeWidth, writeTreeWidth } from "./library-core.js"; function memory(initial = {}) { const data = { ...initial }; return { getItem: (key) => (key in data ? data[key] : null), setItem: (key, value) => { data[key] = value; }, data }; } test("the tree width stays within its limits and falls back to the default", () => { assert.equal(clampTreeWidth(10), TREE_MIN); assert.equal(clampTreeWidth(9999), TREE_MAX); assert.equal(clampTreeWidth(300.4), 300); assert.equal(clampTreeWidth("abc"), TREE_DEFAULT); assert.equal(clampTreeWidth(undefined), TREE_DEFAULT); }); test("the tree width is remembered and a broken store does not matter", () => { const store = memory(); assert.equal(readTreeWidth(store), TREE_DEFAULT); assert.equal(writeTreeWidth(380, store), 380); assert.equal(readTreeWidth(store), 380); assert.equal(readTreeWidth(memory({ "tsumugu-tree-width": "99999" })), TREE_MAX); const broken = { getItem() { throw new Error("blocked"); }, setItem() { throw new Error("blocked"); } }; assert.equal(readTreeWidth(broken), TREE_DEFAULT); assert.equal(writeTreeWidth(300, broken), 300); assert.equal(readTreeWidth(null), TREE_DEFAULT); });