/** * Leaving out of the menu the pages of integrations nobody has added: which pages belong to which kinds, what is left out for what has been added, the store the menu, * the command palette and the shortcuts share, and the Display setting that switches it on (off until someone does). * * Run with: npm test --workspace server */ import { test, describe, before, after } from "node:test"; import assert from "node:assert/strict"; import { mkdtempSync, readFileSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import type { AddressInfo } from "node:net"; import type { Server } from "node:http"; const dir = mkdtempSync(join(tmpdir(), "hm-menu-")); process.env.DATABASE_PATH = join(dir, "test.sqlite"); process.env.SESSION_DIR = join(dir, "sessions"); process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32); const express = (await import("express")).default; const { db } = await import("../src/db/client.js"); const { users } = await import("../src/db/schema.js"); const { runMigrations } = await import("../src/db/migrate.js"); const { settingsRouter } = await import("../src/routes/settings.js"); const { PAGE_KINDS, pagePath, unusedPages, setHiddenPages, getHiddenPages, isPageHidden, subscribeHiddenPages } = await import("../../web/src/utils/navVisibility.js"); const { TYPE_PAGES } = await import("../../web/src/utils/integrationTypes.js"); await runMigrations(); const navSource = readFileSync(join(import.meta.dirname, "..", "..", "web", "src", "layout", "nav.tsx"), "utf8"); const navPaths = new Set([...navSource.matchAll(/to: "([^"]+)"/g)].map((m) => pagePath(m[1]))); describe("which pages belong to an integration", () => { test("a path loses its query", () => { assert.equal(pagePath("/backups?view=pbs"), "/backups"); assert.equal(pagePath("/media"), "/media"); assert.equal(pagePath("/x#top"), "/x"); }); test("every kind of integration has its page, and every such page is one in the menu", () => { for (const page of Object.values(TYPE_PAGES)) assert.ok(PAGE_KINDS[pagePath(page)], `${page} has no kinds`); for (const page of Object.keys(PAGE_KINDS)) assert.ok(navPaths.has(page), `${page} is not a page in the menu`); }); test("pages shared by several kinds list them all; the backups page also reads Proxmox", () => { assert.deepEqual([...PAGE_KINDS["/media"]].sort(), ["lidarr", "prowlarr", "radarr", "readarr", "sonarr"]); assert.deepEqual([...PAGE_KINDS["/downloads"]].sort(), ["qbittorrent", "sabnzbd"]); assert.deepEqual([...PAGE_KINDS["/backups"]].sort(), ["pbs", "proxmox"]); }); test("pages with uses of their own are never among them", () => { for (const page of ["/", "/alerts", "/servers", "/dns", "/secrets", "/duplicati", "/ports", "/endpoints", "/settings", "/integrations", "/inventory", "/maintenance"]) { assert.equal(PAGE_KINDS[page], undefined, page); } }); }); describe("what is left out for what has been added", () => { test("nothing added: every integration page is left out, and only those", () => { const left = unusedPages([]); assert.deepEqual([...left].sort(), Object.keys(PAGE_KINDS).sort()); assert.ok(!left.has("/servers")); }); test("a kind added brings its page back, and nothing else", () => { const left = unusedPages(["jellyfin"]); assert.ok(!left.has("/jellyfin")); assert.ok(left.has("/gitea") && left.has("/proxmox")); }); test("any one of the kinds behind a page is enough", () => { assert.ok(!unusedPages(["lidarr"]).has("/media")); assert.ok(!unusedPages(["sabnzbd"]).has("/downloads")); assert.ok(unusedPages(["sabnzbd"]).has("/media")); }); test("a Proxmox keeps the backups page, a Backup Server does too, neither leaves it out", () => { assert.ok(!unusedPages(["proxmox"]).has("/backups")); assert.ok(!unusedPages(["pbs"]).has("/backups")); assert.ok(unusedPages(["gitea"]).has("/backups")); }); test("a kind the app does not know is no reason to keep anything", () => { assert.deepEqual([...unusedPages(["martian"])].sort(), Object.keys(PAGE_KINDS).sort()); }); }); describe("the store the menu shares", () => { test("it starts leaving nothing out, and a link with a query is judged by its path", () => { assert.equal(getHiddenPages().size, 0); assert.equal(isPageHidden("/gitea"), false); setHiddenPages(["/backups"]); assert.equal(isPageHidden("/backups?view=pbs"), true); assert.equal(isPageHidden("/servers"), false); setHiddenPages([]); }); test("listeners hear of a change, not of the same set again, and can stop listening", () => { let heard = 0; const stop = subscribeHiddenPages(() => heard++); setHiddenPages(["/gitea", "/n8n"]); assert.equal(heard, 1); setHiddenPages(["/n8n", "/gitea"]); assert.equal(heard, 1, "the same pages in another order are no change"); setHiddenPages(["/gitea"]); assert.equal(heard, 2); stop(); setHiddenPages([]); assert.equal(heard, 2); }); }); describe("the Display setting", () => { let app: Server; let base = ""; let sub = "admin"; before(async () => { await db.insert(users).values([ { oidcSub: "admin", name: "Ada", role: "admin" }, { oidcSub: "viewer", name: "Vic", role: "viewer" }, ]); const a = express(); a.use(express.json()); a.use((req, _res, next) => { (req as unknown as { session: unknown }).session = { user: { sub } }; next(); }); a.use("/api/settings", settingsRouter); app = await new Promise((resolve) => { const x = a.listen(0, "127.0.0.1", () => resolve(x)); }); base = `http://127.0.0.1:${(app.address() as AddressInfo).port}/api/settings`; }); after(() => { app.close(); try { rmSync(dir, { recursive: true, force: true }); } catch { // Windows keeps the database file locked until the process exits. } }); const display = async () => ((await (await fetch(`${base}/display`)).json()) as { display: { hideUnusedPages: boolean; pageSize: number } }).display; const put = (body: unknown) => fetch(base, { method: "PUT", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }); test("it is off until somebody switches it on, and anyone signed in can read it", async () => { sub = "viewer"; assert.equal((await display()).hideUnusedPages, false); sub = "admin"; }); test("an administrator switches it on and off, and saving the other display settings does not undo it", async () => { assert.equal((await put({ display: { hideUnusedPages: true } })).status, 200); assert.equal((await display()).hideUnusedPages, true); assert.equal((await put({ display: { pageSize: 50 } })).status, 200); const d = await display(); assert.deepEqual([d.hideUnusedPages, d.pageSize], [true, 50]); assert.equal((await put({ display: { hideUnusedPages: false } })).status, 200); assert.equal((await display()).hideUnusedPages, false); }); test("it must be true or false, and a viewer may not change it", async () => { assert.equal((await put({ display: { hideUnusedPages: "yes" } })).status, 400); sub = "viewer"; assert.equal((await put({ display: { hideUnusedPages: true } })).status, 403); sub = "admin"; assert.equal((await display()).hideUnusedPages, false); }); });