/** * Grouping the alerts on the Alerts page (by source, by kind of problem, by type of alert): the pure function the page uses, run here against the web's own source file — the page has no test * runner of its own — and the words for a type of alert. * * Run with: npm test --workspace server */ import { test, describe } from "node:test"; import assert from "node:assert/strict"; import { pathToFileURL } from "node:url"; import { join } from "node:path"; const web = (file: string) => pathToFileURL(join(import.meta.dirname, "../../web/src/utils", file)).href; const { groupAlerts, isGroup, GROUP_BY_LABELS } = (await import(web("alertGroups.ts"))) as typeof import("../../web/src/utils/alertGroups"); const { kindWords } = (await import(web("alertKinds.ts"))) as typeof import("../../web/src/utils/alertKinds"); type A = Parameters[0][number]; const alert = (id: string, source: string, severity: A["severity"], extra: Partial = {}): A => ({ id, source, severity, silenced: false, category: "services", kind: "disk", ...extra }); const label = (c: string) => ({ services: "Services", disk: "Disk", backup: "Backups" })[c] ?? c; const none = new Set(); const shape = (list: ReturnType) => list.map((x) => (isGroup(x) ? `# ${x.label} (${x.count}: ${x.critical}c ${x.warning}w ${x.info}i ${x.silenced}s)` : (x as A).id)); describe("the words for a type of alert", () => { test("a type that has words says them; one that has not is its own name", () => { assert.equal(kindWords("hetzner-cert"), "a Hetzner certificate about to end"); assert.equal(kindWords("sonarr-queue"), "stuck downloads in Sonarr"); assert.equal(kindWords("lidarr-queue"), "stuck downloads in Lidarr"); assert.equal(kindWords("something-new"), "something new alerts"); }); }); describe("grouping alerts", () => { const list = [ alert("1", "Hetzner", "warning", { category: "services", kind: "hetzner-cert" }), alert("2", "Sonarr", "critical", { category: "disk", kind: "sonarr-disk" }), alert("3", "Hetzner", "warning", { category: "backup", kind: "hetzner-backup" }), alert("4", "Sonarr", "warning", { category: "services", kind: "sonarr-queue" }), alert("5", "Hetzner", "info", { category: "services", kind: "hetzner-cert" }), alert("6", "Fragnet", "warning", { category: "backup", kind: "fragnet-backup", silenced: true }), ]; test("not grouped is the list as it was", () => { assert.deepEqual(groupAlerts(list, "none", none, label), list); assert.deepEqual(Object.keys(GROUP_BY_LABELS), ["none", "source", "category", "kind"]); }); test("by source: the worst group first, then the biggest; each group a header with its counts and then its alerts in the order given; a group of only held-back alerts last", () => { assert.deepEqual(shape(groupAlerts(list, "source", none, label)), ["# Sonarr (2: 1c 1w 0i 0s)", "2", "4", "# Hetzner (3: 0c 2w 1i 0s)", "1", "3", "5", "# Fragnet (1: 0c 0w 0i 1s)", "6"]); }); test("by kind of problem it is the category's name; by type of alert the words for the type", () => { assert.deepEqual(shape(groupAlerts(list, "category", none, label)).filter((x) => x.startsWith("#")), ["# Disk (1: 1c 0w 0i 0s)", "# Services (3: 0c 2w 1i 0s)", "# Backups (2: 0c 1w 0i 1s)"]); assert.deepEqual( shape(groupAlerts(list, "kind", none, label)).filter((x) => x.startsWith("#")), ["# a disk on Sonarr (1: 1c 0w 0i 0s)".replace("a disk on Sonarr", kindWords("sonarr-disk")), `# ${kindWords("hetzner-cert")} (2: 0c 1w 1i 0s)`, `# ${kindWords("hetzner-backup")} (1: 0c 1w 0i 0s)`, `# ${kindWords("sonarr-queue")} (1: 0c 1w 0i 0s)`, `# ${kindWords("fragnet-backup")} (1: 0c 0w 0i 1s)`], ); }); test("a folded group keeps its header, with the count, and shows none of its alerts", () => { const folded = groupAlerts(list, "source", new Set(["source:Hetzner"]), label); assert.deepEqual(shape(folded), ["# Sonarr (2: 1c 1w 0i 0s)", "2", "4", "# Hetzner (3: 0c 2w 1i 0s)", "# Fragnet (1: 0c 0w 0i 1s)", "6"]); assert.equal((folded.find((x) => isGroup(x) && x.label === "Hetzner") as { folded: boolean }).folded, true); }); test("what can be picked from a group is what is not held back; ids are by grouping, so folding one grouping does not fold another", () => { const g = groupAlerts(list, "source", none, label).filter(isGroup); assert.deepEqual(g.map((x) => [x.id, x.pickable]), [["source:Sonarr", ["2", "4"]], ["source:Hetzner", ["1", "3", "5"]], ["source:Fragnet", []]]); assert.equal(groupAlerts(list, "category", new Set(["source:Hetzner"]), label).filter((x) => !isGroup(x)).length, 6, "nothing folded"); }); test("no alerts is no groups", () => { assert.deepEqual(groupAlerts([], "source", none, label), []); }); });