import { test } from "node:test"; import assert from "node:assert/strict"; import { groupMembers, pageOrOverview, rotateSeconds, ROTATE_DEFAULT, ROTATE_MAX, ROTATE_MIN, stepPage, wallPages } from "../../web/src/utils/wallPages.js"; const integ = (type: string, name = type, status = "ok") => ({ id: name, type, name, status }); test("the overview is always the first page, and the only one when there is nothing by name", () => { assert.deepEqual(wallPages({ problems: null, integrations: null }).map((p) => p.id), ["overview"]); assert.deepEqual(wallPages({ problems: [], integrations: [] }).map((p) => p.id), ["overview"]); }); test("the attention page exists only while something needs attention", () => { assert.deepEqual(wallPages({ problems: [{}], integrations: [] }).map((p) => p.id), ["overview", "alerts"]); assert.ok(!wallPages({ problems: [], integrations: [integ("proxmox")] }).some((p) => p.id === "alerts")); }); test("a page for each group that has integrations, in the order of the menu, skipping empty ones", () => { const pages = wallPages({ problems: [], integrations: [integ("grafana"), integ("proxmox"), integ("jellyfin"), integ("proxmox", "pve2")] }); assert.deepEqual(pages.map((p) => p.id), ["overview", "group:infrastructure", "group:apps", "group:monitoring"]); assert.deepEqual(pages.map((p) => p.label), ["Overview", "Infrastructure", "Apps", "Monitoring"]); assert.equal(pages[1].groupId, "infrastructure"); }); test("a kind in no group gets an Other page last", () => { const pages = wallPages({ problems: [], integrations: [integ("proxmox"), integ("mystery")] }); assert.deepEqual(pages.map((p) => p.id), ["overview", "group:infrastructure", "group:other"]); }); test("stepping goes on and wraps round, both ways", () => { const pages = wallPages({ problems: [{}], integrations: [integ("proxmox")] }); // overview, alerts, infrastructure assert.equal(stepPage(pages, "overview", 1).id, "alerts"); assert.equal(stepPage(pages, "group:infrastructure", 1).id, "overview"); assert.equal(stepPage(pages, "overview", -1).id, "group:infrastructure"); assert.equal(stepPage(pages, "alerts", -1).id, "overview"); }); test("a page that has gone sends the board to the overview", () => { const pages = wallPages({ problems: [], integrations: [integ("proxmox")] }); assert.equal(stepPage(pages, "alerts", 1).id, "overview"); assert.equal(pageOrOverview(pages, "alerts").id, "overview"); assert.equal(pageOrOverview(pages, "group:infrastructure").id, "group:infrastructure"); }); test("a board with one page stays on it", () => { const pages = wallPages({ problems: null, integrations: null }); assert.equal(stepPage(pages, "overview", 1).id, "overview"); assert.equal(stepPage(pages, "overview", -1).id, "overview"); }); test("rotation is off unless asked for", () => { assert.equal(rotateSeconds(null, null), null); assert.equal(rotateSeconds("", ""), null); }); test("the address says how long each page stays, and beats what the browser remembered", () => { assert.equal(rotateSeconds("45", "15"), 45); assert.equal(rotateSeconds("on", null), ROTATE_DEFAULT); assert.equal(rotateSeconds("true", null), ROTATE_DEFAULT); assert.equal(rotateSeconds("off", "30"), null); assert.equal(rotateSeconds("0", "30"), null); }); test("without an address, the browser's last choice is used", () => { assert.equal(rotateSeconds(null, "60"), 60); assert.equal(rotateSeconds(null, "off"), null); }); test("a number out of range is brought into it, and anything else means off", () => { assert.equal(rotateSeconds("2", null), ROTATE_MIN); assert.equal(rotateSeconds("99999", null), ROTATE_MAX); assert.equal(rotateSeconds("abc", "30"), null); assert.equal(rotateSeconds("-5", null), null); assert.equal(rotateSeconds("1e3", null), null); }); test("a group's page lists the failing ones first, then by name, and only its own", () => { const list = [integ("proxmox", "pve-b", "ok"), integ("synology", "nas", "failing"), integ("jellyfin", "media"), integ("proxmox", "pve-a", "ok"), integ("tailscale", "ts", "unknown")]; assert.deepEqual(groupMembers(list, "infrastructure").map((i) => i.name), ["nas", "pve-a", "pve-b", "ts"]); assert.deepEqual(groupMembers(list, "apps").map((i) => i.name), ["media"]); assert.deepEqual(groupMembers(list, "backups"), []); }); test("grouping does not change the list it is given", () => { const list = [integ("proxmox", "b"), integ("proxmox", "a")]; groupMembers(list, "infrastructure"); assert.deepEqual(list.map((i) => i.name), ["b", "a"]); });