Away mode, and critical alerts through quiet hours
For a one-person homelab, "on call" is when you can be reached. Settings -> Notifications -> When -> Away holds notifications while you are away (a banner on every page says so) and sends one digest when it ends, at its time or when you say you are back. Critical alerts can still come through. Quiet hours get the same "still send critical alerts" option. Escalation steps marked to send in quiet hours are sent while away too. What is held for one reason is not released while the other still holds it. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3b31b16796
commit
16b24f5436
21 files changed
+737
-50
No files matched your search
@@ -0,0 +1,254 @@
|
||||
/**
|
||||
* Away mode and what lets critical alerts through: when a notification is held (quiet hours, away), who it is let through for, what is sent when the hold ends, and the route
|
||||
* that starts, plans and ends an away stretch.
|
||||
*
|
||||
* 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-away-"));
|
||||
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 } = await import("../src/db/schema.js");
|
||||
const { runMigrations } = await import("../src/db/migrate.js");
|
||||
const avail = await import("../src/services/availability.js");
|
||||
const { endAwayIfOver } = await import("../src/services/awayMonitor.js");
|
||||
const { notify, notifyAudience, flushQuietHoursQueue, notifyHealthIssues } = await import("../src/services/notify.js");
|
||||
const { listQueuedNotifications, clearQueuedNotifications } = await import("../src/services/notificationQueue.js");
|
||||
const { awayRouter, momentOf } = await import("../src/routes/away.js");
|
||||
const { getSettings, updateSettings } = await import("../src/services/settingsStore.js");
|
||||
const { eq } = await import("drizzle-orm");
|
||||
|
||||
await runMigrations();
|
||||
|
||||
const NOW = new Date("2026-10-20T12:00:00Z");
|
||||
const HOUR = 3_600_000;
|
||||
const iso = (ms: number) => new Date(ms).toISOString();
|
||||
|
||||
describe("when is a notification held", () => {
|
||||
const away = (from: number | null, until: number | null, criticalThrough = true) => ({ from: from === null ? null : iso(from), until: until === null ? null : iso(until), criticalThrough, note: "" });
|
||||
const quiet = (criticalThrough = false) => ({ enabled: true, start: "22:00", end: "07:00", criticalThrough });
|
||||
|
||||
test("an away stretch is none, planned, active or over", () => {
|
||||
const t = NOW.getTime();
|
||||
assert.equal(avail.awayState(away(null, null), NOW), "none");
|
||||
assert.equal(avail.awayState(away(null, t + HOUR), NOW), "active", "no start means it began when it was set");
|
||||
assert.equal(avail.awayState(away(t - HOUR, t + HOUR), NOW), "active");
|
||||
assert.equal(avail.awayState(away(t + HOUR, t + 2 * HOUR), NOW), "planned");
|
||||
assert.equal(avail.awayState(away(null, t - 1), NOW), "none", "over");
|
||||
assert.equal(avail.awayState({ from: null, until: "nonsense" }, NOW), "none");
|
||||
});
|
||||
|
||||
test("quiet hours: a window that wraps past midnight, one that doesn't, and the zone it is on", () => {
|
||||
const at = (h: string, z = "UTC") => avail.inQuietWindow(quiet(), z, new Date(`2026-10-20T${h}:00Z`));
|
||||
assert.deepEqual([at("23:00"), at("03:00"), at("07:00"), at("12:00"), at("21:59")], [true, true, false, false, false]);
|
||||
assert.equal(avail.inQuietWindow({ enabled: false, start: "22:00", end: "07:00" }, "UTC", new Date("2026-10-20T23:00:00Z")), false);
|
||||
assert.equal(avail.inQuietWindow({ enabled: true, start: "08:00", end: "08:00" }, "UTC", NOW), false, "a window of no length is never on");
|
||||
assert.equal(avail.inQuietWindow({ enabled: true, start: "09:00", end: "17:00" }, "UTC", NOW), true, "an ordinary same-day window");
|
||||
assert.equal(at("21:30", "Europe/Stockholm"), true, "21:30 UTC is 23:30 there (summer time)");
|
||||
assert.equal(at("04:30", "Europe/Stockholm"), true, "04:30 UTC is 06:30 there: the window ends at 07:00");
|
||||
assert.equal(at("05:30", "Europe/Stockholm"), false, "05:30 UTC is 07:30 there");
|
||||
assert.equal(at("21:30", "America/New_York"), false, "21:30 UTC is 17:30 in New York");
|
||||
});
|
||||
|
||||
test("away holds everything, or everything but what is critical; quiet hours the same; away is looked at first", () => {
|
||||
const night = new Date("2026-10-20T23:30:00Z");
|
||||
const s = (a: ReturnType<typeof away>, q = quiet()) => ({ away: a, quietHours: q, timezone: "UTC" });
|
||||
const t = night.getTime();
|
||||
assert.equal(avail.holdFor(s(away(null, t + HOUR)), false, night), "away");
|
||||
assert.equal(avail.holdFor(s(away(null, t + HOUR, true)), true, night), "quiet", "critical gets past away, and meets the quiet hours that are also on");
|
||||
assert.equal(avail.holdFor(s(away(null, t + HOUR, true), quiet(true)), true, night), null, "…unless they let it through too");
|
||||
assert.equal(avail.holdFor(s(away(null, t + HOUR, false), quiet(true)), true, night), "away", "away that lets nothing through holds a critical one");
|
||||
assert.equal(avail.holdFor(s(away(null, null)), false, night), "quiet");
|
||||
assert.equal(avail.holdFor(s(away(null, null), quiet(true)), true, night), null);
|
||||
assert.equal(avail.holdFor(s(away(null, null)), true, night), "quiet", "critical is held by quiet hours unless they say otherwise");
|
||||
assert.equal(avail.holdFor(s(away(t + HOUR, t + 2 * HOUR), { ...quiet(), enabled: false }), false, night), null, "an away stretch that has not begun holds nothing");
|
||||
});
|
||||
|
||||
test("a time typed for the notification time zone is that zone's clock; an ISO moment is itself", () => {
|
||||
assert.equal(momentOf("2026-10-25T18:00", "UTC")?.toISOString(), "2026-10-25T18:00:00.000Z");
|
||||
assert.equal(momentOf("2026-10-25T18:00", "Europe/Stockholm")?.toISOString(), "2026-10-25T17:00:00.000Z", "the clocks went back that morning: UTC+1");
|
||||
assert.equal(momentOf("2026-10-20T18:00", "Europe/Stockholm")?.toISOString(), "2026-10-20T16:00:00.000Z", "UTC+2 before");
|
||||
assert.equal(momentOf("2026-10-25T18:00:00.000Z", "Europe/Stockholm")?.toISOString(), "2026-10-25T18:00:00.000Z");
|
||||
assert.equal(momentOf("tomorrow", "UTC"), null);
|
||||
assert.equal(momentOf("2026-10-25 18:00", "UTC"), null);
|
||||
});
|
||||
});
|
||||
|
||||
describe("held, let through, and sent when it ends", () => {
|
||||
let hook: Server;
|
||||
let app: Server;
|
||||
let base = "";
|
||||
let who = "admin";
|
||||
const messages: string[] = [];
|
||||
const titles = () => messages.map((m) => /^\*\*(.*?)\*\*/.exec(m)?.[1] ?? m);
|
||||
|
||||
before(async () => {
|
||||
await db.insert(users).values([
|
||||
{ oidcSub: "admin", name: "Ada", role: "admin" },
|
||||
{ oidcSub: "viewer", name: "Vic", role: "viewer" },
|
||||
]);
|
||||
hook = createHttpServer((req, res) => {
|
||||
const chunks: Buffer[] = [];
|
||||
req.on("data", (c) => chunks.push(c));
|
||||
req.on("end", () => {
|
||||
messages.push(String(JSON.parse(Buffer.concat(chunks).toString()).content));
|
||||
res.end("{}");
|
||||
});
|
||||
});
|
||||
await new Promise<void>((r) => hook.listen(0, "127.0.0.1", r));
|
||||
await updateSettings({ webhook: { enabled: true, url: `http://127.0.0.1:${(hook.address() as AddressInfo).port}/` } });
|
||||
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/away", awayRouter);
|
||||
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/away`;
|
||||
});
|
||||
after(() => {
|
||||
app.close();
|
||||
hook.close();
|
||||
try {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
} catch {
|
||||
// Windows keeps the database file locked until the process exits.
|
||||
}
|
||||
});
|
||||
|
||||
const send = (method: string, body?: unknown) => fetch(base, { method, headers: { "content-type": "application/json", connection: "close" }, body: body === undefined ? undefined : JSON.stringify(body) });
|
||||
const tomorrow = () => iso(Date.now() + 24 * HOUR);
|
||||
|
||||
test("away: a warning is held, a critical alert gets through, and coming back sends what was held as one digest", async () => {
|
||||
who = "admin";
|
||||
const res = await send("PUT", { until: tomorrow(), criticalThrough: true, note: "Holiday" });
|
||||
assert.equal(res.status, 200);
|
||||
assert.equal(((await res.json()) as any).away.state, "active");
|
||||
messages.length = 0;
|
||||
await notify("Disk warning", "nas: / is 91% full", { category: "disk", severity: "warning" });
|
||||
await notify("Disk critical", "nas: / is 99% full", { category: "disk", severity: "critical" });
|
||||
assert.deepEqual(titles(), ["Disk critical"], "only the critical one went out");
|
||||
assert.equal((await listQueuedNotifications()).length, 1);
|
||||
const v = (await (await send("GET")).json()) as any;
|
||||
assert.deepEqual([v.away.state, v.away.note, v.held], ["active", "Holiday", 1]);
|
||||
|
||||
messages.length = 0;
|
||||
const back = await send("DELETE");
|
||||
assert.equal(back.status, 200);
|
||||
assert.equal(((await back.json()) as any).sent, 1);
|
||||
assert.deepEqual(titles(), ["Homelab Manager — Notifications from while you were away"]);
|
||||
assert.match(messages[0], /1 notification was held while you were away:[\s\S]*nas: \/ is 91% full/);
|
||||
assert.equal((await listQueuedNotifications()).length, 0);
|
||||
assert.deepEqual((await getSettings()).away.until, null);
|
||||
assert.equal((await send("DELETE")).status, 404, "nobody is away now");
|
||||
const audited = await db.select().from(auditLog).where(eq(auditLog.category, "away"));
|
||||
assert.deepEqual(audited.map((a) => a.action), ["start", "end"]);
|
||||
});
|
||||
|
||||
test("away that lets nothing through holds even a critical alert, and a health alert follows the same rule", async () => {
|
||||
await send("PUT", { until: tomorrow(), criticalThrough: false });
|
||||
messages.length = 0;
|
||||
await notify("Disk critical", "nas: / is 99% full", { category: "disk", severity: "critical" });
|
||||
await notifyHealthIssues([{ key: "offline:server:9", message: "pve hasn't reported for 2 h", severity: "critical" }]);
|
||||
assert.deepEqual(messages, []);
|
||||
assert.equal((await listQueuedNotifications()).length, 2);
|
||||
await send("DELETE");
|
||||
assert.equal(messages.length, 1);
|
||||
assert.match(messages[0], /2 notifications were held while you were away/);
|
||||
});
|
||||
|
||||
test("an escalation step that says to send even in quiet hours is sent while away too", async () => {
|
||||
await send("PUT", { until: tomorrow(), criticalThrough: false });
|
||||
messages.length = 0;
|
||||
await notifyAudience("Still not acknowledged", "pve is still down", { channels: null, pushIds: null }, { quietHours: false });
|
||||
assert.deepEqual(titles(), ["Still not acknowledged"]);
|
||||
await send("DELETE");
|
||||
await clearQueuedNotifications();
|
||||
});
|
||||
|
||||
test("an away stretch planned for later holds nothing until it begins", async () => {
|
||||
const res = await send("PUT", { from: iso(Date.now() + 6 * HOUR), until: iso(Date.now() + 30 * HOUR) });
|
||||
assert.equal(((await res.json()) as any).away.state, "planned");
|
||||
messages.length = 0;
|
||||
await notify("Disk warning", "nas: / is 91% full", { category: "disk", severity: "warning" });
|
||||
assert.equal(messages.length, 1, "sent as ever");
|
||||
assert.equal((await send("DELETE")).status, 200, "a planned one can be cancelled");
|
||||
assert.equal(messages.length, 1, "and nothing was waiting to be sent");
|
||||
});
|
||||
|
||||
test("when its time comes it ends by itself and sends the digest", async () => {
|
||||
await updateSettings({ away: { from: null, until: iso(Date.now() + 5 * 60_000), criticalThrough: true, note: "" } });
|
||||
messages.length = 0;
|
||||
await notify("Disk warning", "nas: / is 91% full", { category: "disk", severity: "warning" });
|
||||
assert.equal(messages.length, 0, "held");
|
||||
assert.equal(await endAwayIfOver(), false, "not yet");
|
||||
assert.equal(await endAwayIfOver(new Date(Date.now() + 10 * 60_000)), true, "ten minutes on, it is over");
|
||||
assert.deepEqual(titles(), ["Homelab Manager — Notifications from while you were away"]);
|
||||
assert.equal((await getSettings()).away.until, null);
|
||||
const last = (await db.select().from(auditLog).where(eq(auditLog.category, "away"))).at(-1)!;
|
||||
assert.deepEqual([last.action, JSON.parse(last.detail!).automatic, last.actorLabel], ["end", true, "system"]);
|
||||
});
|
||||
|
||||
test("what is held for one reason is not let out while the other still holds it", async () => {
|
||||
// quiet hours around now, and away on top of them
|
||||
const h = new Date().getUTCHours();
|
||||
const pad = (n: number) => `${String(n % 24).padStart(2, "0")}:00`;
|
||||
await updateSettings({ quietHours: { enabled: true, start: pad(h + 23), end: pad(h + 1), criticalThrough: false }, notifications: { timezone: "UTC" } as never });
|
||||
await send("PUT", { until: tomorrow(), criticalThrough: true });
|
||||
messages.length = 0;
|
||||
await notify("Disk warning", "nas: / is 91% full", { category: "disk", severity: "warning" });
|
||||
assert.equal(await flushQuietHoursQueue(), 0, "quiet hours ending in the middle of a holiday release nothing");
|
||||
assert.equal(await flushQuietHoursQueue({ reason: "away" }), 0, "and coming back during quiet hours releases nothing yet");
|
||||
await send("DELETE");
|
||||
assert.equal(messages.length, 0, "still quiet");
|
||||
assert.equal((await listQueuedNotifications()).length, 1);
|
||||
assert.equal(await flushQuietHoursQueue({ force: true }), 1, "by hand it goes");
|
||||
assert.match(messages[0], /held during quiet hours/);
|
||||
});
|
||||
|
||||
test("quiet hours can let critical alerts through, and say so", async () => {
|
||||
const h = new Date().getUTCHours();
|
||||
const pad = (n: number) => `${String(n % 24).padStart(2, "0")}:00`;
|
||||
await updateSettings({ quietHours: { enabled: true, start: pad(h + 23), end: pad(h + 1), criticalThrough: true } });
|
||||
messages.length = 0;
|
||||
await notify("Disk warning", "held", { category: "disk", severity: "warning" });
|
||||
await notify("Disk critical", "sent", { category: "disk", severity: "critical" });
|
||||
assert.deepEqual(titles(), ["Disk critical"]);
|
||||
await clearQueuedNotifications();
|
||||
await updateSettings({ quietHours: { enabled: false, start: "22:00", end: "07:00", criticalThrough: false } });
|
||||
});
|
||||
|
||||
test("anyone signed in can see whether somebody is away; only an administrator sets, plans and ends it, with a reason when it is wrong", async () => {
|
||||
who = "viewer";
|
||||
assert.equal((await send("GET")).status, 200);
|
||||
assert.equal((await send("PUT", { until: tomorrow() })).status, 403);
|
||||
assert.equal((await send("DELETE")).status, 403);
|
||||
who = "admin";
|
||||
const bad = async (body: unknown, error: string) => {
|
||||
const r = await send("PUT", body);
|
||||
assert.deepEqual([r.status, ((await r.json()) as any).error], [400, error], JSON.stringify(body));
|
||||
};
|
||||
await bad({ until: iso(Date.now() - HOUR) }, "in_the_past");
|
||||
await bad({ until: iso(Date.now() + 200 * 24 * HOUR) }, "too_long");
|
||||
await bad({ until: "next week" }, "invalid_until");
|
||||
await bad({ from: "soon", until: tomorrow() }, "invalid_from");
|
||||
await bad({ from: iso(Date.now() + 48 * HOUR), until: tomorrow() }, "bad_range");
|
||||
await bad({}, "invalid_body");
|
||||
assert.equal((await send("PUT", { until: "2999-01-01T00:00" })).status, 400, "a thousand years is too long");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user