Files
Homelab-manager/server/test/notificationLog.test.ts
T
bobbanandClaude Sonnet 5.5 9126200295 Add a notification history
Every notification is written down with how it went through each channel
(or why it was not sent: held for quiet hours, a routing rule said nobody, no
channel on). Records -> Notifications lists them with a filter, a search and
Send again. Errors are cleaned of every secret in the settings; kept 30 days.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-10 19:18:08 +01:00

262 lines
14 KiB
TypeScript

/**
* The notification history: every notification written down with how each channel took it, or why it was not sent (held for quiet hours, a routing rule said nobody, no channel to
* send it to); nothing secret kept in an error; a notification sent again; who may read it; and how long it is kept.
*
* Run with: npm test --workspace server
*/
import { test, describe, before, after } from "node:test";
import assert from "node:assert/strict";
import { createServer as createHttpServer, type Server } from "node:http";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { AddressInfo } from "node:net";
const dir = mkdtempSync(join(tmpdir(), "hm-notiflog-"));
process.env.DATABASE_PATH = join(dir, "test.sqlite");
process.env.SESSION_DIR = join(dir, "sessions");
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
process.env.HEALTH_STARTUP_GRACE_MS = "0";
const express = (await import("express")).default;
const { db } = await import("../src/db/client.js");
const { users, auditLog, notificationLog } = await import("../src/db/schema.js");
const { runMigrations } = await import("../src/db/migrate.js");
const log = await import("../src/services/notificationLog.js");
const { notify, notifyAudience } = await import("../src/services/notify.js");
const { updateSettings } = await import("../src/services/settingsStore.js");
const { notificationLogRouter } = await import("../src/routes/notificationLog.js");
const { eq } = await import("drizzle-orm");
await runMigrations();
const NTFY_TOKEN = "tk_secret_ntfy_value";
describe("pure parts", () => {
test("how a notification went, from how each channel did", () => {
assert.equal(log.statusOf([]), "none");
assert.equal(log.statusOf([{ channel: "ntfy", ok: true, error: null }]), "sent");
assert.equal(log.statusOf([{ channel: "ntfy", ok: true, error: null }, { channel: "gotify", ok: false, error: "x" }]), "partly");
assert.equal(log.statusOf([{ channel: "ntfy", ok: false, error: "x" }, { channel: "gotify", ok: false, error: "y" }]), "failed");
});
test("a secret is wiped from a text, also when it is percent-encoded, and a short word is left alone", () => {
assert.equal(log.scrub("bad token abcdef123 here", ["abcdef123"]), "bad token ••• here");
assert.equal(log.scrub("GET /x?token=a%2Fb%2Fc%2Fd%2Fe", ["a/b/c/d/e"]), "GET /x?token=•••");
assert.equal(log.scrub("the cat sat", ["cat"]), "the cat sat", "under six characters is not a secret worth wiping");
assert.equal(log.scrub("one two", [null, undefined, ""]), "one two");
assert.equal(log.scrub("abcdef and abcdefghi", ["abcdef", "abcdefghi"]), "••• and •••", "the longer first, so none of it is left behind");
});
});
describe("what is written down", () => {
let okHook: Server;
let badHook: Server;
let okUrl = "";
let badUrl = "";
const received: string[] = [];
const entries = () => log.listLogged({ limit: 500 });
before(async () => {
okHook = createHttpServer((req, res) => {
let body = "";
req.on("data", (c) => (body += c));
req.on("end", () => {
received.push(body);
res.writeHead(200).end("ok");
});
});
// A channel that refuses, and says the token back in what it says.
badHook = createHttpServer((req, res) => {
req.resume();
res.writeHead(401).end(`unauthorised: ${NTFY_TOKEN} is not accepted`);
});
await Promise.all([okHook, badHook].map((s) => new Promise<void>((resolve) => s.listen(0, "127.0.0.1", resolve))));
okUrl = `http://127.0.0.1:${(okHook.address() as AddressInfo).port}`;
badUrl = `http://127.0.0.1:${(badHook.address() as AddressInfo).port}`;
});
after(async () => {
okHook.close();
badHook.close();
await updateSettings({ webhook: { enabled: false }, ntfy: { enabled: false }, routing: { rules: [] }, quietHours: { enabled: false, start: "22:00", end: "07:00", criticalThrough: false } });
});
test("with nothing switched on, a notification is written down as having nowhere to go", async () => {
await updateSettings({ webhook: { enabled: false }, ntfy: { enabled: false } });
await notify("Homelab Manager — Nothing on", "no channels");
const [e] = await entries();
assert.deepEqual([e.title, e.status, e.results], ["Homelab Manager — Nothing on", "none", []]);
});
test("one that went through is sent, with the channel that took it", async () => {
await updateSettings({ webhook: { enabled: true, url: `${okUrl}/hook` } });
await notify("Homelab Manager — Disk full", "pve1 is at 98%");
const [e] = await entries();
assert.deepEqual([e.title, e.message, e.status, e.results.map((r) => [r.channel, r.ok, r.error])], ["Homelab Manager — Disk full", "pve1 is at 98%", "sent", [["webhook", true, null]]]);
assert.equal(received.length >= 1, true);
});
test("a channel that refuses is failed, with what it said — and without the token it said back", async () => {
await updateSettings({ webhook: { enabled: false }, ntfy: { enabled: true, url: badUrl, topic: "lab", token: NTFY_TOKEN } });
await notify("Homelab Manager — Server down", "web-1 has not reported");
const [e] = await entries();
assert.equal(e.status, "failed");
assert.equal(e.results[0].channel, "ntfy");
assert.match(e.results[0].error ?? "", /ntfy error 401: unauthorised: ••• is not accepted/);
assert.ok(!JSON.stringify(e).includes(NTFY_TOKEN), "the token is nowhere in the entry");
});
test("one channel working and one refusing is partly sent, and each says how it did", async () => {
await updateSettings({ webhook: { enabled: true, url: `${okUrl}/hook` }, ntfy: { enabled: true, url: badUrl, topic: "lab", token: NTFY_TOKEN } });
await notify("Homelab Manager — Half", "one of two");
const [e] = await entries();
assert.equal(e.status, "partly");
assert.deepEqual(e.results.map((r) => [r.channel, r.ok]).sort(), [["ntfy", false], ["webhook", true]]);
});
test("a routing rule that says nobody is written down as muted, with why", async () => {
await updateSettings({ routing: { rules: [{ id: "q", name: "quiet", enabled: true, categories: ["backup"], minSeverity: null, tags: [], channels: [] }] } });
const before = received.length;
await notify("Homelab Manager — Backup late", "x", { category: "backup", severity: "warning" });
const [e] = await entries();
assert.deepEqual([e.title, e.status, e.results], ["Homelab Manager — Backup late", "muted", []]);
assert.match(e.note ?? "", /routing rule says nobody/);
assert.equal(received.length, before, "and nothing was sent");
await updateSettings({ routing: { rules: [] } });
});
test("one held for quiet hours is written down as held, and the digest that follows is its own entry", async () => {
const h = new Date().getUTCHours();
const pad = (n: number) => String(((n % 24) + 24) % 24).padStart(2, "0") + ":00";
await updateSettings({ webhook: { enabled: true, url: `${okUrl}/hook` }, ntfy: { enabled: false }, quietHours: { enabled: true, start: pad(h - 1), end: pad(h + 1), criticalThrough: false }, notifications: { timezone: "UTC" } as never });
await notify("Homelab Manager — Held one", "wait for morning", { severity: "warning" });
const [e] = await entries();
assert.deepEqual([e.title, e.status], ["Homelab Manager — Held one", "held"]);
assert.match(e.note ?? "", /quiet hours/);
await updateSettings({ quietHours: { enabled: false, start: "22:00", end: "07:00", criticalThrough: false } });
});
test("a message is cut at 4000 characters, a title at 300, and an error at 300", async () => {
const id = await log.recordNotification({ title: "T".repeat(500), message: "M".repeat(9000), results: [{ channel: "ntfy", ok: false, error: "E".repeat(900) }] });
const row = (await db.select().from(notificationLog).where(eq(notificationLog.id, id!)))[0];
assert.deepEqual([row.title.length, row.message.length, JSON.parse(row.results)[0].error.length], [300, 4000, 300]);
});
test("it is a list newest first that can be narrowed to one status or a word", async () => {
const all = await entries();
assert.ok(all.length >= 6);
assert.deepEqual(all.map((e) => e.at), [...all.map((e) => e.at)].sort().reverse(), "newest first");
const failed = await log.listLogged({ status: "failed" });
assert.ok(failed.length >= 1 && failed.every((e) => e.status === "failed"));
const word = await log.listLogged({ q: "Disk full" });
assert.deepEqual(word.map((e) => e.title), ["Homelab Manager — Disk full"]);
assert.deepEqual(await log.listLogged({ q: "no such words anywhere" }), []);
assert.equal((await log.listLogged({ q: "100%" })).length, 0, "a percent sign in a search is not a wildcard");
});
test("it never keeps more than 30 days, and never makes sending fail", async () => {
const now = new Date();
await db.insert(notificationLog).values({ at: new Date(now.getTime() - 40 * 86_400_000).toISOString(), title: "old", message: "old", status: "sent" });
await log.recordNotification({ title: "fresh", message: "x", results: [] }, now);
assert.equal((await db.select().from(notificationLog).where(eq(notificationLog.title, "old"))).length, 0, "the old one is gone");
// A notification that cannot be written down is still sent: the writer swallows its own failures.
assert.equal(await log.recordNotification({ title: "x", message: "y", results: [], status: "sent" }, new Date("not a date")), null);
});
});
describe("the page's route", () => {
let app: Server;
let base = "";
let who = "operator";
let hook: Server;
let hookUrl = "";
let hookHits = 0;
before(async () => {
await db.insert(users).values([
{ oidcSub: "admin", name: "Ada", role: "admin" },
{ oidcSub: "operator", name: "Olga", role: "operator" },
{ oidcSub: "viewer", name: "Vic", role: "viewer" },
]);
hook = createHttpServer((req, res) => {
req.resume();
hookHits++;
res.writeHead(200).end("ok");
});
await new Promise<void>((resolve) => hook.listen(0, "127.0.0.1", resolve));
hookUrl = `http://127.0.0.1:${(hook.address() as AddressInfo).port}/hook`;
await updateSettings({ webhook: { enabled: true, url: hookUrl }, ntfy: { enabled: false } });
const a = express();
a.use(express.json());
a.use((req, _res, next) => {
(req as unknown as { session: unknown }).session = { user: { sub: who } };
next();
});
a.use("/api/notification-log", notificationLogRouter);
app = await new Promise<Server>((resolve) => {
const s = a.listen(0, "127.0.0.1", () => resolve(s));
});
base = `http://127.0.0.1:${(app.address() as AddressInfo).port}/api/notification-log`;
});
after(async () => {
app.close();
hook.close();
await updateSettings({ webhook: { enabled: false } });
try {
rmSync(dir, { recursive: true, force: true });
} catch {
// Windows keeps the database file locked until the process exits.
}
});
test("operators and administrators read it; a viewer does not", async () => {
for (const [role, status] of [["viewer", 403], ["operator", 200], ["admin", 200]] as const) {
who = role;
assert.equal((await fetch(base)).status, status, role);
}
who = "operator";
const body = (await (await fetch(base)).json()) as { entries: { title: string; results: unknown[] }[]; statuses: string[] };
assert.ok(body.entries.length > 0);
assert.ok(Array.isArray(body.entries[0].results));
assert.deepEqual(body.statuses, ["sent", "partly", "failed", "none", "held", "muted"]);
});
test("the list can be narrowed by status and by a word, and an unknown status is ignored", async () => {
who = "operator";
const failed = (await (await fetch(`${base}?status=failed`)).json()) as { entries: { status: string }[] };
assert.ok(failed.entries.length > 0 && failed.entries.every((e) => e.status === "failed"));
const word = (await (await fetch(`${base}?q=Disk%20full`)).json()) as { entries: { title: string }[] };
assert.deepEqual(word.entries.map((e) => e.title), ["Homelab Manager — Disk full"]);
const ignored = (await (await fetch(`${base}?status=bogus`)).json()) as { entries: unknown[] };
assert.ok(ignored.entries.length > 5);
});
test("a notification can be sent again: to every channel that is on, written down as a new entry, and in the audit log", async () => {
who = "operator";
const first = (await log.listLogged({ q: "Disk full" }))[0];
const hits = hookHits;
const res = await fetch(`${base}/${first.id}/resend`, { method: "POST" });
assert.equal(res.status, 200);
const { entry } = (await res.json()) as { entry: number };
assert.equal(hookHits, hits + 1, "it went to the channel that is on");
const again = (await db.select().from(notificationLog).where(eq(notificationLog.id, entry)))[0];
assert.deepEqual([again.title, again.message, again.status, again.resentOf], [first.title, first.message, "sent", first.id]);
const audit = (await db.select().from(auditLog)).filter((e) => e.category === "notification" && e.action === "resend");
assert.equal(audit.length, 1);
});
test("a viewer cannot send one again, a notification that is not there is not found, and an id that is not one is refused", async () => {
who = "viewer";
assert.equal((await fetch(`${base}/1/resend`, { method: "POST" })).status, 403);
who = "operator";
assert.equal((await fetch(`${base}/999999/resend`, { method: "POST" })).status, 404);
assert.equal((await fetch(`${base}/abc/resend`, { method: "POST" })).status, 400);
});
test("notifyAudience with an audience that names no channel is written down as muted too (an escalation to nobody)", async () => {
await notifyAudience("Homelab Manager — Escalation", "to nobody", { channels: [], pushIds: null });
const [e] = await log.listLogged();
assert.deepEqual([e.title, e.status], ["Homelab Manager — Escalation", "muted"]);
});
});