/** * The strip under a server's name: whether it reports, maintenance, what is open about it, what is waiting to be installed, its disk, how busy it is and how long it has run — worked out from what the page * already has (and the alerts and maintenance windows), never made up where nothing is known. * * Run with: npm test --workspace server */ import { test, describe } from "node:test"; import assert from "node:assert/strict"; const { serverStrip, worstDisk, uptimeText } = await import("../../web/src/utils/serverStrip.js"); type Input = Parameters[0]; type Server = Input["server"]; type Hardware = Input["hardware"]; type Alert = NonNullable[number]; type Window = NonNullable[number]; const NOW = Date.parse("2026-06-10T12:00:00Z"); const MIN = 60_000; const HOUR = 60 * MIN; const iso = (ms: number) => new Date(ms).toISOString(); const defaults = { serverOfflineMinutes: 15, diskUsagePercent: 90 } as Input["defaults"]; const server = (extra: Partial = {}): Server => ({ id: 7, name: "web-1", lastSeenAt: iso(NOW - 2 * MIN), pendingUpdates: null, thresholds: null, ...extra }) as Server; const hardware = (extra: Partial = {}): Hardware => ({ source: "agent", ...extra }) as Hardware; const alert = (extra: Partial = {}): Alert => ({ id: "a", kind: "disk", type: "server", severity: "warning", category: "disk", source: "Servers", message: "m", link: null, silenced: false, target: { type: "server", id: 7, name: "web-1" }, ...extra }) as Alert; const win = (extra: Partial = {}): Window => ({ id: 1, targetType: "server", targetId: 7, targetName: "web-1", targetKind: "server", reason: null, startedAt: iso(NOW - HOUR), endsAt: iso(NOW + 45 * MIN), createdBy: null, source: null, ...extra }) as Window; const strip = (extra: Partial = {}) => serverStrip({ server: server(), hardware: hardware(), defaults, alerts: [], windows: [], now: NOW, ...extra }); const tile = (tiles: ReturnType, id: string) => tiles.find((t) => t.id === id); describe("whether it reports", () => { test("a recent report is Online, with when; a report older than the limit is Offline, with when it was", () => { const on = tile(strip(), "status")!; assert.deepEqual([on.value, on.tone, on.sub], ["Online", "ok", "reported 2 min ago"]); const off = tile(strip({ server: server({ lastSeenAt: iso(NOW - 3 * HOUR) }) }), "status")!; assert.deepEqual([off.value, off.tone, off.sub], ["Offline", "bad", "last report 3 h ago"]); }); test("the server's own limit decides, not the default", () => { const s = server({ lastSeenAt: iso(NOW - 20 * MIN), thresholds: { serverOfflineMinutes: 60 } }); assert.equal(tile(strip({ server: s }), "status")!.value, "Online"); assert.equal(tile(strip({ server: server({ lastSeenAt: iso(NOW - 20 * MIN) }) }), "status")!.value, "Offline", "the default is 15 minutes"); }); test("an offline agent whose guest Proxmox says is running is said to be that, so it is not read as the machine being down", () => { const t = tile(strip({ server: server({ lastSeenAt: iso(NOW - 2 * HOUR) }), hardware: hardware({ source: "proxmox", status: "running" }) }), "status")!; assert.match(t.sub ?? "", /last report 2 h ago — Proxmox says it is running/); }); test("no report at all: what Proxmox says if it says, else that nothing has reported — never Online", () => { const viaProxmox = tile(strip({ server: server({ lastSeenAt: null }), hardware: hardware({ source: "proxmox", status: "stopped" }) }), "status")!; assert.deepEqual([viaProxmox.value, viaProxmox.tone, viaProxmox.sub], ["Stopped", "warn", "from Proxmox — no agent report"]); const running = tile(strip({ server: server({ lastSeenAt: null }), hardware: hardware({ source: "proxmox", status: "running" }) }), "status")!; assert.deepEqual([running.value, running.tone], ["Running", "ok"]); const none = tile(strip({ server: server({ lastSeenAt: null }) }), "status")!; assert.deepEqual([none.value, none.tone], ["No report yet", "muted"]); }); }); describe("maintenance", () => { test("a window on this server that is on now is a tile, saying when it ends and why; the nearest end if there are two", () => { const t = tile(strip({ windows: [win({ endsAt: iso(NOW + 3 * HOUR) }), win({ id: 2, endsAt: iso(NOW + 45 * MIN), reason: "kernel update" })] }), "maintenance")!; assert.deepEqual([t.value, t.tone, t.sub, t.to], ["On now", "info", "ends in 45 min — kernel update", "/maintenance"]); }); test("another server's window, one that has ended, one that has not begun and a window on something else are not this server's", () => { const windows = [win({ targetId: 8 }), win({ endsAt: iso(NOW - MIN) }), win({ startedAt: iso(NOW + HOUR), endsAt: iso(NOW + 2 * HOUR) }), win({ targetType: "integration" })]; assert.equal(tile(strip({ windows }), "maintenance"), undefined); assert.equal(tile(strip({ windows: null }), "maintenance"), undefined, "not loaded: nothing is said"); }); }); describe("what is open about it", () => { test("only this server's alerts, by how bad: critical first in the tone, both counted", () => { const alerts = [alert({ id: "1", severity: "critical" }), alert({ id: "2" }), alert({ id: "3" }), alert({ id: "x", target: { type: "server", id: 8, name: "other" } }), alert({ id: "y", target: { type: "integration", id: 7, name: "int" } }), alert({ id: "i", severity: "info" })]; const t = tile(strip({ alerts }), "alerts")!; assert.deepEqual([t.value, t.tone, t.sub, t.to], ["3", "bad", "1 critical · 2 warnings", "/alerts"]); assert.equal(tile(strip({ alerts: [alert()] }), "alerts")!.tone, "warn"); }); test("what a maintenance window, a snooze or somebody has put aside is not counted, but is said", () => { const alerts = [alert({ id: "1", silenced: true }), alert({ id: "2", snoozed: { until: iso(NOW + HOUR), by: "x" } }), alert({ id: "3", closed: { by: "x", at: iso(NOW), note: null } }), alert({ id: "4" })]; const t = tile(strip({ alerts }), "alerts")!; assert.deepEqual([t.value, t.sub], ["1", "1 warning · 3 set aside"]); const onlyAside = tile(strip({ alerts: [alerts[0]] }), "alerts")!; assert.deepEqual([onlyAside.value, onlyAside.tone, onlyAside.sub], ["None", "ok", "1 set aside"]); }); test("none is None and fine; alerts that could not be loaded say so, not None", () => { const none = tile(strip({ alerts: [] }), "alerts")!; assert.deepEqual([none.value, none.tone, none.sub], ["None", "ok", "nothing open"]); const unknown = tile(strip({ alerts: null }), "alerts")!; assert.deepEqual([unknown.value, unknown.tone, unknown.sub], ["—", "muted", "not loaded"]); }); }); describe("what is waiting to be installed", () => { const pending = (extra: Partial>) => server({ pendingUpdates: { manager: "apt", count: 0, security: 0, rebootRequired: false, reportedAt: iso(NOW), ...extra } }); test("not reported, up to date, some, security ones, and a reboot", () => { assert.deepEqual(((t) => [t.value, t.tone])(tile(strip(), "updates")!), ["Not reported", "muted"]); assert.deepEqual(((t) => [t.value, t.tone, t.sub])(tile(strip({ server: pending({}) }), "updates")!), ["Up to date", "ok", null]); assert.deepEqual(((t) => [t.value, t.tone, t.sub])(tile(strip({ server: pending({ count: 12, security: 0 }) }), "updates")!), ["12 waiting", "info", "0 security"]); assert.deepEqual(((t) => [t.value, t.tone, t.sub])(tile(strip({ server: pending({ count: 12, security: 3 }) }), "updates")!), ["12 waiting", "warn", "3 security"]); assert.deepEqual(((t) => [t.value, t.tone, t.sub])(tile(strip({ server: pending({ count: 2, security: null, rebootRequired: true }) }), "updates")!), ["2 waiting", "warn", "reboot needed"]); assert.equal(tile(strip({ server: pending({ count: 0, rebootRequired: true }) }), "updates")!.sub, "reboot needed", "a reboot is needed even with nothing waiting"); }); }); describe("the disk", () => { const disks = (...d: [string, number, number][]): Hardware => hardware({ disks: d.map(([mount, used, size]) => ({ mount, usedBytes: used, sizeBytes: size })) }); test("the fullest, against the limit: fine, to be watched within ten points, wrong at the limit", () => { const t = (used: number) => tile(strip({ hardware: disks(["/", used, 100]) }), "disk")!; assert.deepEqual([t(50).value, t(50).tone, t(50).sub], ["50%", "ok", "/"]); assert.equal(t(80).tone, "warn"); assert.equal(t(89).tone, "warn"); assert.equal(t(90).tone, "bad"); }); test("with several, the one closest to its own limit is shown, and the others are counted", () => { const t = tile(strip({ hardware: disks(["/", 60, 100], ["/data", 85, 100], ["/boot", 10, 100]) }), "disk")!; assert.deepEqual([t.value, t.sub], ["85%", "/data — fullest of 3"]); }); test("a mount's own limit decides for it, and an ignored mount is left out (so is one with no size)", () => { const s = server({ thresholds: { diskUsagePercent: 95, mounts: { "/data": { percent: 70 }, "/scratch": { ignore: true } } } }); const t = tile(strip({ server: s, hardware: disks(["/", 80, 100], ["/data", 75, 100], ["/scratch", 99, 100], ["/zero", 0, 0]) }), "disk")!; assert.deepEqual([t.value, t.tone, t.sub], ["75%", "bad", "/data — fullest of 2"], "75% is over /data's 70, though / at 80% is under the 95 of the server"); assert.deepEqual(worstDisk(disks(["/zero", 0, 0]), { thresholds: null }, { diskUsagePercent: 90 }), null); }); test("no disks reported, no tile", () => { assert.equal(tile(strip(), "disk"), undefined); assert.equal(tile(strip({ hardware: hardware({ disks: [] }) }), "disk"), undefined); }); }); describe("how busy it is, and how long it has run", () => { test("processor and memory as percentages, to be watched at 90; either alone; neither, no tile", () => { const both = tile(strip({ hardware: hardware({ cpuUsagePercent: 12.4, memTotalBytes: 1000, memUsedBytes: 610 }) }), "load")!; assert.deepEqual([both.value, both.tone], ["12% · 61%", "muted"]); assert.equal(tile(strip({ hardware: hardware({ cpuLoadPercent: 95, memTotalBytes: 1000, memUsedBytes: 10 }) }), "load")!.tone, "warn", "the load figure stands in for the usage one"); assert.equal(tile(strip({ hardware: hardware({ memTotalBytes: 1000, memUsedBytes: 950 }) }), "load")!.value, "— · 95%"); assert.equal(tile(strip(), "load"), undefined); }); test("uptime, in days and hours or hours and minutes; none, no tile", () => { assert.equal(uptimeText(12 * 86400 + 4 * 3600 + 59), "12d 4h"); assert.equal(uptimeText(3 * 3600 + 20 * 60), "3h 20m"); assert.equal(tile(strip({ hardware: hardware({ uptime: 90_000 }) }), "uptime")!.value, "1d 1h"); assert.equal(tile(strip({ hardware: hardware({ uptime: null }) }), "uptime"), undefined); assert.equal(tile(strip({ hardware: hardware({ uptime: 0 }) }), "uptime"), undefined); }); test("the tiles come in a fixed order: status, maintenance, alerts, updates, disk, load, uptime", () => { const tiles = strip({ windows: [win()], hardware: hardware({ disks: [{ mount: "/", usedBytes: 1, sizeBytes: 10 }], cpuUsagePercent: 5, memTotalBytes: 10, memUsedBytes: 1, uptime: 5000 }) }); assert.deepEqual(tiles.map((t) => t.id), ["status", "maintenance", "alerts", "updates", "disk", "load", "uptime"]); }); });