/** * Brand colours and logos for the kinds of integration and DNS providers: every kind has a colour that is a real colour, the logos that exist are paths that can be drawn, a card's label says which kind * it is about, going back to a brand's colour is saved in a way the settings can keep, and the menu and the tags use them. * * Run with: npm test --workspace server */ import { test, describe, before, after } from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { mkdtempSync, 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-brands-")); 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 b = await import("../../web/src/utils/brands.js"); const { TYPE_LABELS, TYPE_PAGES } = await import("../../web/src/utils/integrationTypes.js"); await runMigrations(); const web = (...parts: string[]) => readFileSync(join(import.meta.dirname, "..", "..", "web", "src", ...parts), "utf8"); const PROVIDERS = ["cloudflare", "loopia", "pihole", "azure", "cpanel", "technitium"]; describe("the table", () => { test("every kind of integration has a colour, and a logo is a path that can be drawn", () => { for (const kind of Object.keys(TYPE_LABELS)) { const brand = (b.KIND_BRANDS as Record)[kind]; assert.ok(brand, `${kind} has no brand`); assert.match(brand.color, /^#[0-9a-f]{6}$/, kind); if (brand.path !== null) assert.match(brand.path, /^M[\d.\- ,a-zA-Z]+$/, `${kind}'s logo is not a path`); } assert.equal(Object.keys(b.KIND_BRANDS).length, Object.keys(TYPE_LABELS).length, "no brand for a kind that does not exist"); }); test("every DNS provider has one too", () => { for (const p of PROVIDERS) assert.match((b.PROVIDER_BRANDS as Record)[p].color, /^#[0-9a-f]{6}$/, p); assert.equal(Object.keys(b.PROVIDER_BRANDS).length, PROVIDERS.length); }); test("the well-known ones have their logo, and the few the open set lacks keep their generic icon", () => { for (const k of ["proxmox", "synology", "tailscale", "gitea", "jellyfin", "immich", "nextcloud", "grafana", "prometheus", "homeassistant", "sonarr", "radarr", "qbittorrent", "hetzner", "cloudflare", "pihole"]) assert.ok(b.brandOf(k)?.path, `${k} has a logo`); for (const k of ["netbox", "prowlarr", "lidarr", "readarr", "sabnzbd", "technitium", "osticket", "phpipam", "fragnet", "dockhand", "semaphore", "ups", "azure", "loopia"]) assert.equal(b.brandOf(k)?.path, null, `${k} has none`); }); test("a kind that shares a brand shares its colour (Hetzner Cloud and Robot, Proxmox and its Backup Server)", () => { assert.equal(b.brandOf("hetzner")?.color, b.brandOf("hetznerrobot")?.color); assert.equal(b.brandOf("proxmox")?.color, b.brandOf("pbs")?.color); assert.equal(b.PROVIDER_BRANDS.technitium.color, b.KIND_BRANDS.technitium.color); }); test("something that is no kind has no brand", () => { assert.equal(b.brandOf("martian"), null); assert.equal(b.brandOf(""), null); }); }); describe("what the page's helpers say", () => { test("a card's label says which kind it is about, with or without the name of the one integration", () => { assert.equal(b.kindOfLabel("Jellyfin"), "jellyfin"); assert.equal(b.kindOfLabel("Jellyfin: Media"), "jellyfin"); assert.equal(b.kindOfLabel("Hetzner Cloud: Projekt 1"), "hetzner"); assert.equal(b.kindOfLabel("Technitium DNS"), "technitium"); assert.equal(b.kindOfLabel("Servers down"), null); assert.equal(b.kindOfLabel(""), null); }); test("a colour is #rrggbb and nothing else — an empty one is 'none chosen'", () => { for (const ok of ["#00a4dc", "#FFFFFF"]) assert.equal(b.isHexColor(ok), true, ok); for (const bad of ["", "00a4dc", "#abc", "#12345g", "red", null, undefined, 5]) assert.equal(b.isHexColor(bad), false, String(bad)); }); }); describe("going back to a brand's colour is kept", () => { let app: Server; let base = ""; before(async () => { await db.insert(users).values({ oidcSub: "admin", name: "Ada", role: "admin" }); const a = express(); a.use(express.json()); a.use((req, _res, next) => { (req as unknown as { session: unknown }).session = { user: { sub: "admin" } }; next(); }); a.use("/api/settings", settingsRouter); app = await new Promise((resolve) => { const s = a.listen(0, "127.0.0.1", () => resolve(s)); }); 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 put = (body: unknown) => fetch(base, { method: "PUT", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }); const colors = async () => ((await (await fetch(`${base}/integration-colors`)).json()) as { integrationColors: Record }).integrationColors; test("a colour is chosen, and given up by saving an empty one (the settings merge key by key, so a key cannot be taken away)", async () => { assert.equal((await put({ integrationColors: { proxmox: "#112233", jellyfin: "#445566" } })).status, 200); assert.deepEqual(await colors(), { proxmox: "#112233", jellyfin: "#445566" }); assert.equal((await put({ integrationColors: { proxmox: "" } })).status, 200); const now = await colors(); assert.equal(b.isHexColor(now.proxmox), false, "an empty one is none chosen, so the brand's is used"); assert.equal(now.jellyfin, "#445566", "the others are as they were"); }); }); describe("where they are used", () => { test("the Integrations page, the Servers page and the DNS page draw their tags with the brand tag, and no page is left with the old plain style", () => { for (const f of ["Integrations.tsx", "Servers.tsx", "Dns.tsx"]) { const s = web("pages", f); assert.match(s, / { const nav = web("layout", "nav.tsx"); assert.match(nav, /export const NAV: NavEntry\[\] = withLogos\(PLAIN_NAV\)/); assert.match(nav, /kinds\?\.length === 1 && hasLogo\(kinds\[0\]\)/); // a page of several kinds (Media managers, Download clients) stays generic assert.ok((nav.match(/PAGE_KINDS\[/g) ?? []).length >= 1); assert.equal((TYPE_PAGES as Record).sonarr, (TYPE_PAGES as Record).radarr, "they share a page, so it has no single logo"); }); test("the menu's groups show an icon before each page, switched off per browser by an attribute on the page, and the dashboard cards and kind picker carry the logos", () => { assert.match(web("layout", "SidebarNav.tsx"), /className="menu-item-icon"[\s\S]*?\{leaf\.icon\}/); const css = web("styles.css"); assert.match(css, /html\[data-menu-icons="off"\] \.dropdown-item \.menu-item-icon\s*\{\s*display:\s*none/); assert.match(web("main.tsx"), /applyStoredMenuIcons\(\)/); assert.match(web("utils", "menuIcons.ts"), /data-menu-icons/); assert.match(web("pages", "Dashboard.tsx"), /kindOfLabel\(label\)/); assert.match(web("components", "IntegrationTypePicker.tsx"), / { const css = web("styles.css"); assert.match(css, /\.badge\.brand-tag\s*\{[\s\S]*?color-mix\(in srgb, var\(--b\) \d+%, black\)/); assert.match(css, /\[data-bs-theme="dark"\] \.badge\.brand-tag\s*\{[\s\S]*?color-mix\(in srgb, var\(--b\) \d+%, white\)/); assert.match(css, /\.brand-fg\s*\{[\s\S]*?black\)/); // and where the browser can hold the lightness, it does: no darker than 0.48 on the light theme, no lighter than 0.76 on the dark one assert.match(css, /@supports \(color: oklch\(from red l c h\)\)[\s\S]*?min\(l, 0\.48\)[\s\S]*?max\(l, 0\.76\)/); }); });