// The inbox's grouping and ranking: pure, over hand-made rows. import assert from "node:assert/strict"; import test from "node:test"; import { GROUP_IDS, actionOf, annotate, categoryOf, groupOf, rankDecisions, summarizeGroups } from "./groups.mjs"; const row = (kind, severity, extra = {}) => ({ kind, severity, project: extra.project ?? "proj", target: extra.target ?? "t", why: extra.why ?? `${kind} why`, href: extra.href ?? "/browse/proj", at: extra.at ?? 0, ...extra, }); test("each kind lands in its group, in the brief's order", () => { const cases = [ ["research-proposal", "open", "your-call", "spend"], ["iteration-failed", "open", "your-call", "spend"], ["iteration-ready", "open", "review", "review"], ["claim-unadjudicated", "blocking", "blocking", "fix"], ["unreadable-notes", "blocking", "blocking", "fix"], ["open-note", "open", "notes", "note"], ["link-ambiguous", "open", "open", "choose"], ["unjudged-variant", "open", "open", "choose"], ["looks-done", "open", "ready-to-finish", "finish"], ["loudness", "info", "quiet", "info"], ["unbuilt", "info", "quiet", "info"], ]; for (const [kind, severity, group, category] of cases) { assert.equal(groupOf({ kind, severity }), group, kind); assert.equal(categoryOf({ kind, severity }), category, kind); } assert.deepEqual(GROUP_IDS, ["your-call", "review", "blocking", "notes", "open", "ready-to-finish", "quiet"]); }); test("ranking: group order, proposals before failed runs, newest first, hidden last", () => { const items = annotate( [ row("loudness", "info", { at: 9 }), row("looks-done", "open", { at: 8 }), row("open-note", "open", { at: 7 }), row("claim-unadjudicated", "blocking", { at: 1, project: "old" }), row("claim-unadjudicated", "blocking", { at: 5, project: "new" }), row("iteration-ready", "open", { at: 6 }), row("iteration-failed", "open", { at: 10 }), row("research-proposal", "open", { at: 2 }), row("clip-no-cues", "blocking", { at: 99, project: "parked-one" }), ], { pieceOf: (d) => `p:${d.project}`, lifecycleOf: (p) => (p === "p:parked-one" ? "parked" : "active") }, ); const ranked = rankDecisions(items).map((d) => `${d.kind}:${d.project}`); assert.deepEqual(ranked, [ "research-proposal:proj", "iteration-failed:proj", "iteration-ready:proj", "claim-unadjudicated:new", "claim-unadjudicated:old", "open-note:proj", "looks-done:proj", "loudness:proj", "clip-no-cues:parked-one", ]); }); test("annotate adds the fields and never drops, merges or reorders a row", () => { const input = [row("open-note", "open", { href: "/sites/priv/x?note=n1" }), row("clip-no-cues", "blocking", { project: "gone" })]; const out = annotate(input, { pieceOf: (d) => (d.project === "gone" ? null : "a:priv/x"), lifecycleOf: () => "done" }); assert.equal(out.length, 2); assert.equal(out[0].piece, "a:priv/x"); assert.equal(out[0].lifecycle, "done"); assert.equal(out[0].hidden, true); // A note's action keeps its href, `note=` last. assert.deepEqual(out[0].action, { label: "Answer note", href: "/sites/priv/x?note=n1" }); assert.match(out[0].href, /note=n1$/); // No piece: active, not hidden, no piece key. assert.equal("piece" in out[1], false); assert.equal(out[1].lifecycle, "active"); assert.equal(out[1].hidden, false); // A piece already on the row wins. assert.equal(annotate([row("x", "open", { piece: "p:keep" })], { pieceOf: () => "p:other", lifecycleOf: () => "active" })[0].piece, "p:keep"); }); test("an action names its result; an iteration's names the iteration", () => { assert.equal(actionOf(row("iteration-ready", "open", { target: "i3" })).label, "Make i3 current"); assert.equal(actionOf(row("looks-done", "open")).label, "Mark done"); assert.equal(actionOf(row("never-heard-of-it", "blocking")).label, "Fix it"); assert.equal(actionOf(row("never-heard-of-it", "info")).label, "Open"); }); test("the summary: one card per piece and kind, counts that add up, hidden counted", () => { const claims = Array.from({ length: 483 }, (_, i) => row("claim-unadjudicated", "blocking", { project: "quartering-diet", target: `c${i}`, at: i })); const items = annotate( [...claims, row("clip-no-cues", "blocking", { project: "other" }), row("open-note", "open", { project: "parked" }), row("loudness", "info")], { pieceOf: (d) => `p:${d.project}`, lifecycleOf: (p) => (p === "p:parked" ? "parked" : "active") }, ); const groups = summarizeGroups(items); const blocking = groups.find((g) => g.id === "blocking"); assert.equal(blocking.decisions, 484); assert.equal(blocking.cards.length, 2); assert.equal(blocking.cards[0].piece, "p:quartering-diet"); assert.equal(blocking.cards[0].count, 483); assert.equal(blocking.cards[0].at, 482); const notes = groups.find((g) => g.id === "notes"); assert.equal(notes.decisions, 0); assert.equal(notes.hidden, 1); assert.equal(groups.find((g) => g.id === "quiet").collapsed, true); assert.equal(groups.reduce((n, g) => n + g.decisions + g.hidden, 0), items.length); // Parking the video hides all 483 and leaves one card. const parked = summarizeGroups(annotate(items, { pieceOf: (d) => `p:${d.project}`, lifecycleOf: (p) => (p === "p:quartering-diet" ? "done" : "active") })); assert.equal(parked.find((g) => g.id === "blocking").decisions, 1); assert.equal(parked.find((g) => g.id === "blocking").hidden, 483); }); test("a muted kind on a piece is hidden by `muted`; a parked piece's reason wins", () => { const items = annotate( [row("clip-mid-sentence", "open", { project: "a" }), row("stale-build", "open", { project: "a" }), row("clip-mid-sentence", "open", { project: "b" })], { pieceOf: (d) => `p:${d.project}`, lifecycleOf: (p) => (p === "p:b" ? "parked" : "active"), isMuted: (p, k) => k === "clip-mid-sentence", }, ); assert.deepEqual( items.map((d) => [d.piece, d.kind, d.hidden, d.hiddenBy ?? null, d.muted]), [ ["p:a", "clip-mid-sentence", true, "muted", true], ["p:a", "stale-build", false, null, false], ["p:b", "clip-mid-sentence", true, "parked", true], ], ); // Counted as hidden, never dropped, never a card. const g = summarizeGroups(items); assert.equal(g.reduce((n, x) => n + x.decisions + x.hidden, 0), 3); assert.equal(g.flatMap((x) => x.cards).length, 1); // A row with no piece cannot be muted. assert.equal(annotate([row("unbuilt", "open")], { pieceOf: () => null, lifecycleOf: () => "active", isMuted: () => true })[0].hidden, false); });