Heartbeats, Coming up and suggested checks; Operations menu tidied

Heartbeats: a cron job or script calls /api/heartbeat/<token> (and /start, /fail); the app
alerts when the call is late or the job says it failed. Coming up: one agenda of everything
dated (expiries, warranties, end of support, planned and recurring maintenance, restore tests,
waiting updates), with a dashboard card and an iCalendar feed behind a share link. Suggested
checks: web ports and certificates the app already knows about, added with a click.

Menu: Incidents become Alerts -> History (old /incidents links redirect); new Monitoring group
(Endpoints, Heartbeats, Uptime Kuma); osTicket under Automation, Admin Links under
Infrastructure, Generator under Administration. Also makes the restore-test metrics test follow
the real clock.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-19 21:42:34 +02:00
1 parent c05c847d17
commit 4e0c6927b2
44 files changed
+7092 -75

No files matched your search

+300
View File
@@ -0,0 +1,300 @@
/**
* "Coming up": what the agenda gathers and how it dates it, the calendar feed it can be written as, and the routes — the list for people signed in
* and the feed behind a share link.
*
* Run with: npm test --workspace server
*/
import { test, describe, before, after } from "node:test";
import assert from "node:assert/strict";
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-agenda-"));
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, servers, secrets, domains, assets, endpointChecks, maintenanceWindows, maintenanceSchedules, restoreTests } = await import("../src/db/schema.js");
const { runMigrations } = await import("../src/db/migrate.js");
const { agendaFromData, buildIcs, foldLine, localDate, addDays, buildAgenda, clearAgendaCacheForTests } = await import("../src/services/agenda.js");
const { agendaRouter } = await import("../src/routes/agenda.js");
const { publicCalendarRouter } = await import("../src/routes/publicCalendar.js");
const { shareLinksRouter } = await import("../src/routes/shareLinks.js");
const { createShareLink } = await import("../src/services/shareLinks.js");
const { updateSettings } = await import("../src/services/settingsStore.js");
await runMigrations();
type Data = Parameters<typeof agendaFromData>[0];
const NOW = new Date("2026-10-19T12:00:00Z");
const empty = (extra: Partial<Data> = {}): Data => ({ secrets: [], domains: [], endpoints: [], tailscale: [], assets: [], windows: [], schedules: [], restoreTests: [], restoreLimitDays: 0, updates: [], notes: [], ...extra });
const titles = (data: Data, days?: number, tz = "UTC") => agendaFromData(data, NOW, tz, days).items.map((i) => [i.daysLeft, i.kind, i.title]);
describe("what is gathered, and how it is dated", () => {
test("a day that something expires on, with how many days away it is; a secret that is a certificate is a certificate", () => {
const data = empty({
secrets: [
{ id: 1, name: "API key", type: "api_key", expiryDate: "2026-10-30" },
{ id: 2, name: "Wildcard", type: "ssl_certificate", expiryDate: "2026-11-02" },
{ id: 3, name: "Never", type: "generic", expiryDate: null },
{ id: 4, name: "Garbage", type: "generic", expiryDate: "2026-02-30" },
],
domains: [{ id: 1, name: "example.se", expiresAt: "2027-01-05" }],
});
assert.deepEqual(titles(data), [
[11, "secret", "Secret “API key” expires"],
[14, "certificate", "Certificate “Wildcard” expires"],
[78, "domain", "Domain example.se expires"],
]);
});
test("what has just passed is shown as passed, for thirty days; what is further off than asked for is not shown", () => {
const data = empty({ secrets: [
{ id: 1, name: "Yesterday", type: "generic", expiryDate: "2026-10-18" },
{ id: 2, name: "Today", type: "generic", expiryDate: "2026-10-19" },
{ id: 3, name: "Last month", type: "generic", expiryDate: "2026-09-19" },
{ id: 4, name: "Long ago", type: "generic", expiryDate: "2026-09-18" },
{ id: 5, name: "In a year", type: "generic", expiryDate: "2027-10-19" },
] });
assert.deepEqual(titles(data, 90), [[-30, "secret", "Secret “Last month” expired"], [-1, "secret", "Secret “Yesterday” expired"], [0, "secret", "Secret “Today” expires"]]);
assert.equal(titles(data, 400).length, 4, "a year ahead, when asked for");
});
test("certificates that endpoint checks have seen, hardware warranties and end of support — each linking to where they are kept", () => {
const agenda = agendaFromData(
empty({
endpoints: [
{ id: 1, name: "Gitea", enabled: true, certExpiresAt: "2026-11-01T08:00:00.000Z", serverId: 5 },
{ id: 2, name: "Paused", enabled: false, certExpiresAt: "2026-11-01T08:00:00.000Z", serverId: null },
{ id: 3, name: "No cert", enabled: true, certExpiresAt: null, serverId: null },
],
assets: [{ id: 9, name: "Synology DS920+", warrantyUntil: "2026-12-01", endOfSupport: "2026-12-20", serverId: 2 }],
}),
NOW,
"UTC",
);
assert.deepEqual(agenda.items.map((i) => [i.kind, i.title, i.link, i.serverId]), [
["certificate", "The certificate for Gitea expires", "/endpoints", 5],
["warranty", "Warranty on Synology DS920+ ends", "/inventory?asset=9", 2],
["support", "Support for Synology DS920+ ends", "/inventory?asset=9", 2],
]);
});
test("Tailscale keys: only those the integration says are expiring, dated by days left", () => {
assert.deepEqual(titles(empty({ tailscale: [{ integrationId: 1, integrationName: "Home", deviceLabel: "laptop", daysLeft: 6 }] })), [[6, "tailscale", "Tailscale key for laptop expires"]]);
});
test("a restore test is due a set time after the last good one — only with a limit set, and never for one that failed", () => {
const tests = [
{ subjectKey: "guest:1:100", label: "elsa", testedAt: "2026-08-01", result: "ok" },
{ subjectKey: "guest:1:101", label: "anna", testedAt: "2026-08-01", result: "failed" },
{ subjectKey: "custom:photos", label: "Photos share", testedAt: "2026-07-03", result: "partial" },
];
assert.deepEqual(titles(empty({ restoreTests: tests, restoreLimitDays: 0 })), []);
assert.deepEqual(titles(empty({ restoreTests: tests, restoreLimitDays: 90 })), [[-18, "restore", "Restore test was due: Photos share"], [11, "restore", "Restore test due: elsa"]]);
});
test("updates that are waiting are one line, from the longest wait", () => {
const agenda = agendaFromData(empty({ updates: [{ name: "a", count: 5, security: 2, since: "2026-10-10T08:00:00.000Z" }, { name: "b", count: 1, security: 0, since: "2026-10-17T08:00:00.000Z" }, { name: "c", count: 3, security: null, since: null }] }), NOW, "UTC");
assert.equal(agenda.items.length, 1);
assert.deepEqual([agenda.items[0].title, agenda.items[0].detail, agenda.items[0].daysLeft, agenda.items[0].link], ["Updates waiting on 3 servers (1 with security fixes)", "the longest wait is 9 days", -9, "/updates"]);
});
test("maintenance that is set up is grouped by when it is, the names kept short; windows a schedule made are left to the schedule, which is read forward", () => {
const agenda = agendaFromData(
empty({
windows: [
{ startedAt: "2026-10-24T01:00:00.000Z", endsAt: "2026-10-24T03:00:00.000Z", reason: "Firmware", source: null, targetName: "nas" },
{ startedAt: "2026-10-24T01:00:00.000Z", endsAt: "2026-10-24T03:00:00.000Z", reason: "Firmware", source: null, targetName: "switch" },
{ startedAt: "2026-10-21T02:00:00.000Z", endsAt: "2026-10-21T04:00:00.000Z", reason: "Patch night", source: "schedule:1:2026-10-21", targetName: "nas" },
],
schedules: [{ id: 1, name: "Patch night", reason: null, days: JSON.stringify([3]), startTime: "02:00", durationMinutes: 120, timezone: "UTC", targetNames: ["nas", "elsa"] }],
}),
NOW,
"UTC",
30,
);
const m = agenda.items.filter((i) => i.kind === "maintenance");
assert.deepEqual(m.map((i) => [i.title, i.at.slice(0, 16), i.allDay, i.endsAt?.slice(11, 16)]), [
["Recurring maintenance: Patch night", "2026-10-21T02:00", false, "04:00"],
["Maintenance: nas, switch", "2026-10-24T01:00", false, "03:00"],
["Recurring maintenance: Patch night", "2026-10-28T02:00", false, "04:00"],
["Recurring maintenance: Patch night", "2026-11-04T02:00", false, "04:00"],
["Recurring maintenance: Patch night", "2026-11-11T02:00", false, "04:00"],
["Recurring maintenance: Patch night", "2026-11-18T02:00", false, "04:00"],
]);
assert.equal(m.filter((i) => i.at.startsWith("2026-10-21")).length, 1, "the window the schedule made and the schedule itself are one entry, not two");
assert.equal(m[1].detail, "Firmware");
assert.equal(m[0].detail, "elsa, nas");
});
test("a day is the day on the clock set for notifications: a window at 23:30 UTC is already tomorrow in Stockholm", () => {
const windows = [{ startedAt: "2026-10-19T23:30:00.000Z", endsAt: "2026-10-20T01:00:00.000Z", reason: null, source: null, targetName: "nas" }];
assert.equal(agendaFromData(empty({ windows }), NOW, "UTC").items[0].daysLeft, 0);
assert.equal(agendaFromData(empty({ windows }), NOW, "Europe/Stockholm").items[0].daysLeft, 1);
assert.equal(localDate(new Date("2026-10-19T23:30:00Z"), "Europe/Stockholm"), "2026-10-20");
assert.equal(addDays("2026-10-31", 1), "2026-11-01");
});
});
describe("the calendar feed", () => {
const data = empty({
secrets: [{ id: 1, name: "API key, with; odd\\chars", type: "api_key", expiryDate: "2026-10-30" }],
tailscale: [{ integrationId: 1, integrationName: "Home", deviceLabel: "bobs-laptop", daysLeft: 6 }],
windows: [{ startedAt: "2026-10-24T01:00:00.000Z", endsAt: "2026-10-24T03:00:00.000Z", reason: "Firmware", source: null, targetName: "nas" }],
secretsOld: undefined,
updates: [{ name: "a", count: 1, security: 0, since: null }],
} as unknown as Partial<Data>);
const agenda = agendaFromData(data, NOW, "UTC");
const ics = buildIcs(agenda, { name: "Homelab; manager", detail: "names", baseUrl: "https://hm.lan/" });
test("it is a calendar with CRLF lines: all-day events for days, a start and an end for maintenance, nothing for waiting updates", () => {
assert.ok(ics.startsWith("BEGIN:VCALENDAR\r\nVERSION:2.0\r\n") && ics.endsWith("END:VCALENDAR\r\n"));
assert.equal(ics.includes("\n") && ics.split("\n").every((l, i, a) => i === a.length - 1 || l.endsWith("\r")), true);
assert.equal((ics.match(/BEGIN:VEVENT/g) ?? []).length, 3, "the secret, the key and the window — not the updates");
assert.match(ics, /DTSTART;VALUE=DATE:20261030\r\nDTEND;VALUE=DATE:20261031/);
assert.match(ics, /DTSTART:20261024T010000Z\r\nDTEND:20261024T030000Z/);
assert.match(ics, /X-WR-CALNAME:Homelab\\; manager/);
assert.match(ics, /TRANSP:TRANSPARENT/);
});
test("text is escaped, and a line is never more than 75 bytes, without splitting a character", () => {
assert.match(ics.replace(/\r\n /g, ""), /SUMMARY:Secret “API key\\, with\\; odd\\\\chars” expires/);
const long = foldLine(`SUMMARY:${"å".repeat(100)}`);
for (const l of long.split("\r\n")) assert.ok(Buffer.byteLength(l, "utf8") <= 75, `${Buffer.byteLength(l, "utf8")} bytes`);
assert.equal(long.replace(/\r\n /g, ""), `SUMMARY:${"å".repeat(100)}`, "and it unfolds to what it was");
for (const l of ics.split("\r\n")) assert.ok(Buffer.byteLength(l, "utf8") <= 75);
});
test("with names, an event carries the name and a link back; each has a steady id that does not give the name away", () => {
const unfolded = ics.replace(/\r\n /g, "");
assert.match(unfolded, /DESCRIPTION:Firmware\\nhttps:\/\/hm\.lan\/maintenance/);
const uids = [...ics.matchAll(/UID:(\S+)\r\n/g)].map((m) => m[1]);
assert.equal(new Set(uids).size, 3);
assert.ok(uids.every((u) => /^[0-9a-f]{24}@homelab-manager$/.test(u)));
assert.deepEqual(uids, [...buildIcs(agenda, { name: "x", detail: "names" }).matchAll(/UID:(\S+)\r\n/g)].map((m) => m[1]), "the same on every fetch, or a subscribed calendar would duplicate everything");
});
test("with no names, an event says only what kind of thing it is — and nothing in it gives away a name", () => {
const plain = buildIcs(agenda, { name: "Homelab", detail: "summary", baseUrl: "https://hm.lan" });
const unfolded = plain.replace(/\r\n /g, "");
assert.match(unfolded, /SUMMARY:A secret expires/);
assert.match(unfolded, /SUMMARY:A Tailscale key expires/);
assert.match(unfolded, /SUMMARY:Planned maintenance/);
assert.equal(/API key|bobs-laptop|nas|Firmware|hm\.lan|DESCRIPTION/.test(unfolded), false);
});
test("an event more than a week old is dropped, one from this week is kept", () => {
const old = agendaFromData(empty({ secrets: [{ id: 1, name: "Old", type: "generic", expiryDate: "2026-10-10" }, { id: 2, name: "Recent", type: "generic", expiryDate: "2026-10-15" }] }), NOW, "UTC");
assert.equal((buildIcs(old, { name: "x", detail: "names" }).match(/BEGIN:VEVENT/g) ?? []).length, 1);
});
});
describe("from what is stored, through the routes", () => {
let app: Server;
let base = "";
let who = "admin";
const day = (n: number) => new Date(Date.now() + n * 86_400_000).toISOString().slice(0, 10);
before(async () => {
await db.insert(users).values([
{ oidcSub: "admin", name: "Ada", role: "admin" },
{ oidcSub: "viewer", name: "Vic", role: "viewer" },
]);
const [s] = await db
.insert(servers)
.values({ name: "nas", apiTokenHash: "n", apiTokenPrefix: "n", pendingUpdates: JSON.stringify({ manager: "apt", count: 4, security: 1, rebootRequired: false, reportedAt: new Date().toISOString() }), updatesPendingSince: new Date(Date.now() - 3 * 86_400_000).toISOString() })
.returning({ id: servers.id });
await db.insert(secrets).values({ name: "Deploy key", type: "api_key", expiryDate: day(10) } as never);
await db.insert(domains).values({ name: "example.se", expiresAt: day(40) });
await db.insert(assets).values({ name: "Switch", kind: "network", warrantyUntil: day(20), createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() } as never);
await db.insert(endpointChecks).values({ name: "Gitea", kind: "http", target: "https://git.lab/", certExpiresAt: new Date(Date.now() + 5 * 86_400_000).toISOString(), serverId: s.id });
await db.insert(maintenanceWindows).values({ targetType: "server", targetId: s.id, reason: "Disk swap", startedAt: new Date(Date.now() + 2 * 86_400_000).toISOString(), endsAt: new Date(Date.now() + 2 * 86_400_000 + 3600_000).toISOString() });
await db.insert(maintenanceSchedules).values({ name: "Patch night", targets: JSON.stringify([{ type: "server", id: s.id }]), days: JSON.stringify([0, 1, 2, 3, 4, 5, 6]), startTime: "03:00", durationMinutes: 60, timezone: "UTC" });
await db.insert(restoreTests).values({ subjectKey: "custom:photos", label: "Photos share", testedAt: day(-80), result: "ok" });
await updateSettings({ healthChecks: { restoreTestDays: 90 } });
clearAgendaCacheForTests();
const a = express();
a.use(express.json());
a.use("/api/public/calendar", publicCalendarRouter);
a.use((req, _res, next) => {
(req as unknown as { session: unknown }).session = { user: { sub: who } };
next();
});
a.use("/api/agenda", agendaRouter);
a.use("/api/share-links", shareLinksRouter);
app = await new Promise<Server>((resolve) => {
const x = a.listen(0, "127.0.0.1", () => resolve(x));
});
base = `http://127.0.0.1:${(app.address() as AddressInfo).port}`;
});
after(() => {
app.close();
try {
rmSync(dir, { recursive: true, force: true });
} catch {
// Windows keeps the database file locked until the process exits.
}
});
test("everything with a date, from every source, for anyone signed in; the days asked for narrow it", async () => {
who = "viewer";
const res = await fetch(`${base}/api/agenda`);
assert.equal(res.status, 200);
const body = await res.json();
const kinds = new Set(body.items.map((i: { kind: string }) => i.kind));
for (const k of ["secret", "domain", "warranty", "certificate", "maintenance", "restore", "updates"]) assert.ok(kinds.has(k), k);
assert.ok(body.items.some((i: { title: string }) => i.title === "Restore test due: Photos share"), "tested 80 days ago with a 90-day limit");
assert.ok(body.items.filter((i: { title: string }) => i.title.startsWith("Recurring maintenance")).length >= 60 / 1, "a schedule is read forward, not only the week that has windows");
const near = await (await fetch(`${base}/api/agenda?days=15`)).json();
assert.equal(near.items.some((i: { kind: string }) => i.kind === "domain"), false, "40 days out is beyond 15");
assert.equal(near.items.some((i: { kind: string }) => i.kind === "secret"), true);
const bad = await (await fetch(`${base}/api/agenda?days=banana`)).json();
assert.equal(bad.items.length > 0, true, "an unreadable range is the default one");
const direct = await buildAgenda();
assert.equal(direct.items.length, body.items.length, "the route and the service agree");
});
test("a calendar link is made like any share link, by an administrator, and its address is the feed", async () => {
who = "viewer";
assert.equal((await fetch(`${base}/api/share-links`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ label: "Phone", kind: "calendar", detail: "names", expiresInDays: null }) })).status, 403);
who = "admin";
const res = await fetch(`${base}/api/share-links`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ label: "Phone", kind: "calendar", detail: "names", expiresInDays: null }) });
assert.equal(res.status, 201);
const made = await res.json();
assert.match(made.path, /^\/api\/public\/calendar\/[A-Za-z0-9_-]{20,}\.ics$/);
const feed = await fetch(`${base}${made.path}`);
assert.equal(feed.status, 200);
assert.match(feed.headers.get("content-type") ?? "", /^text\/calendar; charset=utf-8/);
const text = (await feed.text()).replace(/\r\n /g, "");
assert.ok(text.startsWith("BEGIN:VCALENDAR"));
assert.match(text, /SUMMARY:Secret “Deploy key” expires/);
assert.match(text, /SUMMARY:Domain example\.se expires/);
const bare = made.path.replace(/\.ics$/, "");
assert.equal((await fetch(`${base}${bare}`)).status, 200, "the extension is for calendar apps; the address works without it");
});
test("a link for no names gives the feed no names; one for something else, a wrong one and an ended one all say the same thing", async () => {
const { token } = await createShareLink({ label: "Private", kind: "calendar", detail: "summary", expiresInDays: null, createdBy: "t" });
const text = (await (await fetch(`${base}/api/public/calendar/${token}.ics`)).text()).replace(/\r\n /g, "");
assert.match(text, /SUMMARY:A secret expires/);
assert.equal(/Deploy key|example\.se|Gitea|nas/.test(text), false);
const wall = await createShareLink({ label: "Wall", kind: "wall", detail: "summary", expiresInDays: null, createdBy: "t" });
const ended = await createShareLink({ label: "Old", kind: "calendar", detail: "names", expiresInDays: 1, createdBy: "t", now: new Date(Date.now() - 3 * 86_400_000) });
const answers = await Promise.all([`${wall.token}.ics`, "nonsense.ics", `${"a".repeat(30)}.ics`, `${ended.token}.ics`].map(async (p) => {
const r = await fetch(`${base}/api/public/calendar/${p}`);
return [r.status, await r.text()];
}));
assert.deepEqual(new Set(answers.map((a) => JSON.stringify(a))).size, 1, "never made, ended, and a wall's: indistinguishable");
assert.equal(answers[0][0], 404);
});
});
+1 -1
View File
@@ -115,7 +115,7 @@ describe("through the routes", () => {
assert.ok(!r.entries.some((e: { title: string }) => /login|export/i.test(e.title)));
const maint = r.entries[0];
assert.deepEqual([maint.kind, maint.actor, maint.link, maint.serverId], ["maintenance", "Ada", `/servers/${nas}`, nas]);
assert.equal(r.entries.find((e: { title: string }) => e.title.startsWith("Alert raised")).link.startsWith("/incidents/"), true);
assert.equal(r.entries.find((e: { title: string }) => e.title.startsWith("Alert raised")).link.startsWith("/alerts/incidents/"), true);
assert.equal(r.kinds.length, 10);
});
+176
View File
@@ -0,0 +1,176 @@
/**
* Suggested endpoint checks: what is offered from servers' open web ports, DNS names and secrets, what is left out, and the routes that add or dismiss them.
*
* Run with: npm test --workspace server
*/
import { test, describe, before, after } from "node:test";
import assert from "node:assert/strict";
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-suggest-"));
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, servers, secrets, dnsProviders, dnsRecordsCache, endpointChecks } = await import("../src/db/schema.js");
const { runMigrations } = await import("../src/db/migrate.js");
const { suggestChecks } = await import("../src/services/endpointSuggestions.js");
const { endpointsRouter } = await import("../src/routes/endpoints.js");
const { eq } = await import("drizzle-orm");
await runMigrations();
const nas = { id: 1, name: "nas", hostname: "nas.lan", ips: ["10.0.0.5"], ports: [{ protocol: "tcp", port: 443, address: "0.0.0.0" }, { protocol: "tcp", port: 22, address: "0.0.0.0" }] };
const input = (extra: Partial<Parameters<typeof suggestChecks>[0]> = {}) => ({ servers: [nas], dns: [], secrets: [], existing: [], dismissed: new Set<string>(), ...extra });
describe("what is offered", () => {
test("a web port a server listens on, by the name DNS gives it, linked to the server", () => {
const { suggestions } = suggestChecks(input({ dns: [{ name: "git.example.lan", type: "A", content: "10.0.0.5" }, { name: "other.example.lan", type: "A", content: "10.0.0.9" }] }));
assert.deepEqual(suggestions.map((s) => [s.kind, s.target, s.name, s.serverId, s.serverName]), [["http", "https://git.example.lan/", "git.example.lan", 1, "nas"]]);
assert.match(suggestions[0].reason, /nas is listening on port 443, and git\.example\.lan points at it in DNS/);
});
test("an alias to one of those names, or to the server's own hostname, is offered too; wildcards, reverse names and short names are not", () => {
const { suggestions } = suggestChecks(
input({
dns: [
{ name: "git.example.lan", type: "A", content: "10.0.0.5" },
{ name: "code.example.lan", type: "CNAME", content: "git.example.lan." },
{ name: "files.example.lan", type: "CNAME", content: "nas.lan" },
{ name: "*.example.lan", type: "A", content: "10.0.0.5" },
{ name: "5.0.0.10.in-addr.arpa", type: "A", content: "10.0.0.5" },
{ name: "nas", type: "A", content: "10.0.0.5" },
{ name: "unrelated.example.lan", type: "CNAME", content: "elsewhere.lan" },
],
}),
);
assert.deepEqual(suggestions.map((s) => s.target), ["https://code.example.lan/", "https://files.example.lan/", "https://git.example.lan/"]);
});
test("with no DNS name the server's own hostname or its address is used; with neither, nothing", () => {
assert.deepEqual(suggestChecks(input()).suggestions.map((s) => s.target), ["https://nas.lan/"]);
const bare = { ...nas, hostname: "nas", ips: ["169.254.1.1", "10.0.0.5"] };
assert.deepEqual(suggestChecks(input({ servers: [bare] })).suggestions.map((s) => s.target), ["https://10.0.0.5/"]);
assert.deepEqual(suggestChecks(input({ servers: [{ ...bare, ips: [] }] })).suggestions, []);
});
test("only web ports count, only for TCP, and a port that is open on this machine alone does not", () => {
const odd = { ...nas, ports: [{ protocol: "udp", port: 443, address: "0.0.0.0" }, { protocol: "tcp", port: 22, address: "0.0.0.0" }, { protocol: "tcp", port: 8443, address: "127.0.0.1" }, { protocol: "tcp", port: 3000, address: "0.0.0.0" }] };
assert.deepEqual(suggestChecks(input({ servers: [odd] })).suggestions, []);
const mixed = { ...nas, ports: [{ protocol: "tcp", port: 8080, address: "127.0.0.1" }, { protocol: "tcp", port: 8080, address: "10.0.0.5" }] };
assert.deepEqual(suggestChecks(input({ servers: [mixed] })).suggestions.map((s) => s.target), ["http://nas.lan:8080/"], "one socket on a real address is enough, and a port that is not the default is in the address");
});
test("plain and secure on the same server: the secure one is offered, since it reads the certificate and the plain one is most likely its redirect", () => {
const both = { ...nas, ports: [{ protocol: "tcp", port: 80, address: "::" }, { protocol: "tcp", port: 443, address: "::" }, { protocol: "tcp", port: 8080, address: "::" }] };
assert.deepEqual(suggestChecks(input({ servers: [both] })).suggestions.map((s) => s.target), ["http://nas.lan:8080/", "https://nas.lan/"]);
});
test("a secret that already reads a certificate is offered as a certificate check", () => {
const { suggestions } = suggestChecks(input({ servers: [], secrets: [{ name: "Wildcard cert", checkHost: "Git.Example.lan.", checkPort: 443 }, { name: "Typed in", checkHost: null, checkPort: null }] }));
assert.deepEqual(suggestions.map((s) => [s.kind, s.target, s.port, s.name, s.serverId]), [["tls", "git.example.lan", 443, "Certificate: Wildcard cert", null]]);
});
});
describe("what is left out", () => {
const dns = [{ name: "git.example.lan", type: "A", content: "10.0.0.5" }];
test("what a check already covers, however its address is written", () => {
assert.equal(suggestChecks(input({ dns, existing: [{ kind: "http", target: "https://Git.example.lan:443/login", port: null }] })).suggestions.length, 0);
assert.equal(suggestChecks(input({ dns, existing: [{ kind: "http", target: "https://git.example.lan", port: null }] })).suggestions.length, 0);
assert.equal(suggestChecks(input({ dns, existing: [{ kind: "http", target: "http://git.example.lan/", port: null }] })).suggestions.length, 1, "a plain-http check does not cover the secure port");
assert.equal(suggestChecks(input({ dns, existing: [{ kind: "tcp", target: "git.example.lan", port: 443 }] })).suggestions.length, 1, "nor does a port check, which doesn't speak to the page");
});
test("a secret's certificate is covered by a certificate check, or by a web check on https that reads it anyway", () => {
const sec = [{ name: "Cert", checkHost: "git.example.lan", checkPort: 443 }];
assert.equal(suggestChecks(input({ servers: [], secrets: sec, existing: [{ kind: "tls", target: "git.example.lan", port: 443 }] })).suggestions.length, 0);
assert.equal(suggestChecks(input({ servers: [], secrets: sec, existing: [{ kind: "http", target: "https://git.example.lan/", port: null }] })).suggestions.length, 0);
assert.equal(suggestChecks(input({ servers: [], secrets: sec, existing: [{ kind: "tls", target: "git.example.lan", port: 8443 }] })).suggestions.length, 1);
});
test("what was dismissed is listed apart, not offered", () => {
const r = suggestChecks(input({ dns, dismissed: new Set(["http|https://git.example.lan/"]) }));
assert.deepEqual([r.suggestions.length, r.dismissed.map((d) => d.target)], [0, ["https://git.example.lan/"]]);
});
});
describe("the routes", () => {
let app: Server;
let base = "";
let who = "admin";
before(async () => {
await db.insert(users).values([
{ oidcSub: "admin", name: "Ada", role: "admin" },
{ oidcSub: "operator", name: "Olga", role: "operator" },
]);
const [s] = await db
.insert(servers)
.values({ name: "nas", hostname: "nas.lan", apiTokenHash: "n", apiTokenPrefix: "n", ipAddresses: JSON.stringify(["10.0.0.5"]), listeningPorts: JSON.stringify([{ protocol: "tcp", port: 443, address: "0.0.0.0", process: "nginx" }, { protocol: "tcp", port: 8080, address: "0.0.0.0" }]) })
.returning({ id: servers.id });
const [p] = await db.insert(dnsProviders).values({ providerType: "pihole", name: "Pi-hole" }).returning({ id: dnsProviders.id });
await db.insert(dnsRecordsCache).values({ providerId: p.id, zoneId: "z", recordId: "r1", type: "A", name: "git.example.lan", content: "10.0.0.5" });
await db.insert(secrets).values({ name: "Mail cert", type: "ssl_certificate", checkHost: "mail.example.lan", checkPort: 443 } as never);
void s;
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/endpoints", endpointsRouter);
app = await new Promise<Server>((resolve) => {
const x = a.listen(0, "127.0.0.1", () => resolve(x));
});
base = `http://127.0.0.1:${(app.address() as AddressInfo).port}/api/endpoints`;
});
after(() => {
app.close();
try {
rmSync(dir, { recursive: true, force: true });
} catch {
// Windows keeps the database file locked until the process exits.
}
});
const send = (method: string, path: string, body?: unknown) => fetch(`${base}${path}`, { method, headers: { "content-type": "application/json" }, body: body === undefined ? undefined : JSON.stringify(body) });
test("administrators see what is offered, and nobody else", async () => {
who = "operator";
assert.equal((await send("GET", "/suggestions")).status, 403);
assert.equal((await send("POST", "/suggestions/add", { keys: ["x"] })).status, 403);
who = "admin";
const body = await (await send("GET", "/suggestions")).json();
assert.deepEqual(body.suggestions.map((s: { target: string }) => s.target), ["http://git.example.lan:8080/", "https://git.example.lan/", "mail.example.lan"]);
});
test("adding makes the checks the server itself offered — a key that was not offered adds nothing — linked to their server, and they stop being offered", async () => {
const res = await send("POST", "/suggestions/add", { keys: ["https://git.example.lan/", "http|https://git.example.lan/", "http|http://evil.example/", "http|https://git.example.lan/"] });
assert.equal(res.status, 201);
assert.equal((await res.json()).added.length, 1);
const [made] = await db.select().from(endpointChecks).where(eq(endpointChecks.target, "https://git.example.lan/"));
assert.deepEqual([made.kind, made.name, made.serverId !== null, made.intervalMinutes, made.enabled], ["http", "git.example.lan", true, 5, true]);
const body = await (await send("GET", "/suggestions")).json();
assert.equal(body.suggestions.some((s: { target: string }) => s.target === "https://git.example.lan/"), false);
assert.equal((await send("POST", "/suggestions/add", { keys: [] })).status, 400);
});
test("dismissing takes one out of the offer, and restoring brings every dismissed one back", async () => {
let body = await (await send("POST", "/suggestions/dismiss", { keys: ["tls|mail.example.lan|443"] })).json();
assert.equal(body.suggestions.some((s: { kind: string }) => s.kind === "tls"), false);
assert.equal(body.dismissed.length, 1);
body = await (await send("POST", "/suggestions/restore")).json();
assert.equal(body.suggestions.some((s: { kind: string }) => s.kind === "tls"), true);
assert.equal(body.dismissed.length, 0);
});
});
+292
View File
@@ -0,0 +1,292 @@
/**
* Heartbeats: what a job reporting in (or not) amounts to, the address it calls, what the pass announces, and who may set them up.
*
* 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-heartbeats-"));
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, servers, heartbeats, heartbeatPings } = await import("../src/db/schema.js");
const { runMigrations } = await import("../src/db/migrate.js");
const { dueBy, heartbeatState, heartbeatConditions, cleanMessage, parseStamp, recordPing, newToken, TOKEN_PATTERN } = await import("../src/services/heartbeats.js");
const { heartbeatsRouter } = await import("../src/routes/heartbeats.js");
const { heartbeatPingRouter } = await import("../src/routes/heartbeatPing.js");
const { runEndpointPass } = await import("../src/services/endpointMonitor.js");
const { getAlerts, invalidateAlertsCache } = await import("../src/services/alerts.js");
const { startOrExtendWindow } = await import("../src/services/maintenance.js");
const { metaOfConditionKey } = await import("../src/services/notifyRouting.js");
const { subjectOfConditionKey } = await import("../src/services/maintenance.js");
const { updateSettings } = await import("../src/services/settingsStore.js");
const { buildMetrics } = await import("../src/services/metrics.js");
const { eq } = await import("drizzle-orm");
await runMigrations();
type Row = typeof heartbeats.$inferSelect;
const NOW = new Date("2026-10-19T03:00:00Z");
const row = (extra: Partial<Row> = {}): Row => ({
id: 1, name: "Nightly backup", token: "hb_x", periodMinutes: 1440, graceMinutes: 60, serverId: null, enabled: true, notes: null, lastPingAt: null, lastStatus: null, lastMessage: null,
lastDurationMs: null, lastStartAt: null, pingCount: 0, createdAt: "2026-10-18 03:00:00", ...extra,
});
const minutes = (n: number) => new Date(NOW.getTime() + n * 60_000);
describe("when a job is due, and how it is doing", () => {
test("it is due a period and the grace after its last call — or after it was made, if it never called", () => {
assert.equal(dueBy(row({ lastPingAt: "2026-10-19T02:00:00.000Z" })).toISOString(), "2026-10-20T03:00:00.000Z");
assert.equal(dueBy(row()).toISOString(), "2026-10-19T04:00:00.000Z", "SQLite's own timestamp format is read as UTC");
assert.equal(parseStamp("2026-10-18 03:00:00"), Date.parse("2026-10-18T03:00:00Z"));
});
test("new, up, running, late, failed and switched off", () => {
const called = { lastPingAt: NOW.toISOString(), lastStatus: "ok" };
assert.equal(heartbeatState(row({ createdAt: "2026-10-19 02:00:00" }), NOW), "waiting", "made recently, has not called, not yet due");
assert.equal(heartbeatState(row(), NOW), "waiting", "due by 04:00 today");
assert.equal(heartbeatState(row(), minutes(61)), "late", "never called, and now it is overdue");
assert.equal(heartbeatState(row(called), minutes(60 * 24)), "up");
assert.equal(heartbeatState(row(called), minutes(60 * 25)), "up", "inside the grace");
assert.equal(heartbeatState(row(called), minutes(60 * 25 + 1)), "late");
assert.equal(heartbeatState(row({ ...called, lastStartAt: minutes(5).toISOString() }), minutes(10)), "running", "it began after it last finished");
assert.equal(heartbeatState(row({ ...called, lastStartAt: minutes(-5).toISOString() }), minutes(10)), "up", "a start older than the last finish is not a run in progress");
assert.equal(heartbeatState(row({ ...called, lastStatus: "fail", lastMessage: "disk full" }), minutes(10)), "failed", "said so, and does not wait for the time to pass");
assert.equal(heartbeatState(row({ enabled: false }), minutes(9999)), "off");
});
test("the problems: late is a warning with the time it was due, a reported failure is critical; each keyed by the server it runs on", () => {
const list = heartbeatConditions(
[
row({ id: 1, serverId: 7 }),
row({ id: 2, name: "Photos sync", lastPingAt: NOW.toISOString(), lastStatus: "fail", lastMessage: "exit 3" }),
row({ id: 3, name: "Fine", lastPingAt: NOW.toISOString(), lastStatus: "ok" }),
row({ id: 4, name: "Paused", enabled: false }),
],
minutes(120),
);
assert.deepEqual(list.map((c) => [c.key, c.severity, c.category, c.serverId]), [["heartbeat:1:server:7", "warning", "automation", 7], ["heartbeat-fail:2", "critical", "automation", null]]);
assert.match(list[0].message, /Nightly backup hasn't reported — it was due by 2026-10-19 04:00 UTC \(it has never reported\)/);
assert.match(list[1].message, /Photos sync reported a failure — exit 3/);
assert.deepEqual(metaOfConditionKey("heartbeat:1:server:7", "warning"), { category: "automation", severity: "warning", serverId: 7 }, "tags and categories route it");
assert.equal(subjectOfConditionKey("heartbeat-fail:2:server:9"), "server:9", "and maintenance on the server holds it back");
});
test("a note from a job is one short line", () => {
assert.equal(cleanMessage("3.2 GB\n\tcopied in 41 s\u0000"), "3.2 GB copied in 41 s");
assert.equal(cleanMessage(" "), null);
assert.equal(cleanMessage(42), null);
assert.equal(cleanMessage("x".repeat(500))?.length, 200);
});
test("an address carries a long random token, and only that shape is looked up", () => {
const t = newToken();
assert.match(t, TOKEN_PATTERN);
assert.notEqual(t, newToken());
for (const bad of ["", "hb_", "hb_short", "nope_" + "a".repeat(24), "hb_" + "a/b".repeat(10), "hb_" + "a".repeat(100)]) assert.equal(TOKEN_PATTERN.test(bad), false, bad);
});
});
describe("the address, the routes and the pass", () => {
let app: Server;
let hook: Server;
let base = "";
let who = "admin";
let serverId = 0;
let id = 0;
let path = "";
const messages: string[] = [];
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" },
]);
serverId = (await db.insert(servers).values({ name: "nas", apiTokenHash: "n", apiTokenPrefix: "n", lastSeenAt: new Date().toISOString() }).returning({ id: servers.id }))[0].id;
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();
// The ping address comes first, as in the real app: no session, no JSON parser, no sign-in.
a.use("/api/heartbeat", heartbeatPingRouter);
a.use(express.json());
a.use((req, _res, next) => {
(req as unknown as { session: unknown }).session = { user: { sub: who } };
next();
});
a.use("/api/heartbeats", heartbeatsRouter);
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}`;
});
after(() => {
app.close();
hook.close();
try {
rmSync(dir, { recursive: true, force: true });
} catch {
// Windows keeps the database file locked until the process exits.
}
});
const admin = (method: string, p: string, body?: unknown) => fetch(`${base}/api/heartbeats${p}`, { method, headers: { "content-type": "application/json" }, body: body === undefined ? undefined : JSON.stringify(body) });
const ping = (p: string, init?: RequestInit) => fetch(`${base}${p}`, init);
test("an administrator makes one; a wrong one is refused with a reason; nobody else can; everyone can look", async () => {
const res = await admin("POST", "/", { name: "Nightly backup", periodMinutes: 1440, graceMinutes: 60, serverId });
assert.equal(res.status, 201);
const made = (await res.json()).heartbeat;
id = made.id;
path = made.path;
assert.match(path, /^\/api\/heartbeat\/hb_[A-Za-z0-9_-]{20,}$/);
for (const bad of [{ periodMinutes: 5 }, { name: "x" }, { name: "x", periodMinutes: 0 }, { name: "x", periodMinutes: 60 * 24 * 31 }, { name: "x", periodMinutes: 5, graceMinutes: 99999 }, { name: "x", periodMinutes: 5, serverId: 99999 }, { name: " ", periodMinutes: 5 }]) {
assert.equal((await admin("POST", "/", bad)).status, 400, JSON.stringify(bad));
}
who = "operator";
assert.equal((await admin("POST", "/", { name: "y", periodMinutes: 5 })).status, 403);
assert.equal((await admin("DELETE", `/${id}`)).status, 403);
const list = await (await admin("GET", "/")).json();
assert.equal(list.heartbeats[0].path, null, "the address is for administrators: it is what lets anyone say \"I'm alive\"");
assert.equal(list.baseUrl, null);
assert.equal(list.heartbeats[0].state, "waiting");
assert.equal(list.heartbeats[0].server.name, "nas");
who = "admin";
const adminList = await (await admin("GET", "/")).json();
assert.equal(adminList.heartbeats[0].path, path);
assert.ok(adminList.baseUrl);
assert.equal("token" in adminList.heartbeats[0], false, "the token is not a field of its own");
});
test("a call is noted: success, a note in the body or the query, and how long a run took when it said it started", async () => {
assert.equal((await ping(path)).status, 200);
assert.equal(await (await ping(path, { method: "HEAD" })).text(), "");
let [h] = await db.select().from(heartbeats).where(eq(heartbeats.id, id));
assert.deepEqual([h.lastStatus, h.pingCount], ["ok", 2]);
assert.equal(heartbeatState(h, new Date()), "up");
assert.equal((await ping(`${path}/start`, { method: "POST" })).status, 200);
await new Promise((r) => setTimeout(r, 30));
const done = await ping(path, { method: "POST", body: "3.2 GB\ncopied", headers: { "content-type": "text/plain" } });
assert.equal(await done.text(), "OK");
[h] = await db.select().from(heartbeats).where(eq(heartbeats.id, id));
assert.equal(h.lastMessage, "3.2 GB copied");
assert.ok(h.lastDurationMs !== null && h.lastDurationMs >= 20, "timed from the start call");
await ping(`${path}?msg=from%20the%20query`);
[h] = await db.select().from(heartbeats).where(eq(heartbeats.id, id));
assert.equal(h.lastMessage, "from the query");
assert.equal(h.lastDurationMs, null, "no start before this one, so no duration");
const rows = await db.select().from(heartbeatPings).where(eq(heartbeatPings.heartbeatId, id));
assert.deepEqual(rows.map((r) => r.kind), ["ok", "ok", "start", "ok", "ok"]);
});
test("a token that does not exist, a path that is not one of the three and a method that is not a call all get no information", async () => {
const a = await ping("/api/heartbeat/hb_" + "z".repeat(24));
const b = await ping(`${path}/bogus`);
const c = await ping("/api/heartbeat/nonsense");
assert.deepEqual([a.status, b.status, c.status], [404, 404, 404]);
assert.equal(await a.text(), await b.text(), "never made, and a wrong path, read the same");
assert.equal((await ping(path, { method: "DELETE" })).status, 405);
assert.equal((await ping(path)).headers.get("cache-control"), "no-store");
});
test("a job that says it failed is an alert at once, until it next succeeds; the pass says so once, and once when it is over", async () => {
messages.length = 0;
await db.update(heartbeats).set({ serverId: null }).where(eq(heartbeats.id, id));
assert.equal((await ping(`${path}/fail`, { method: "POST", body: "exit 3: disk full" })).status, 200);
invalidateAlertsCache();
const alert = (await getAlerts(true)).alerts.find((x) => x.source === "Heartbeat")!;
assert.match(alert.message, /Nightly backup reported a failure — exit 3: disk full/);
assert.deepEqual([alert.severity, alert.category, alert.link], ["critical", "automation", "/heartbeats"]);
let r = await runEndpointPass(new Date());
assert.equal(r.announced, 1);
assert.match(messages.join("\n"), /Nightly backup reported a failure/);
const sent = messages.length;
r = await runEndpointPass(new Date());
assert.equal(r.announced + r.recovered, 0);
assert.equal(messages.length, sent, "not said again");
await ping(path);
r = await runEndpointPass(new Date());
assert.equal(r.recovered, 1);
assert.match(messages[messages.length - 1], /Nightly backup reported a failure/, "the recovery names what cleared");
invalidateAlertsCache();
assert.equal((await getAlerts(true)).alerts.filter((x) => x.source === "Heartbeat").length, 0);
});
test("a call that does not come in time is a warning once it is overdue, silenced by maintenance on the server, and cleared by the call", async () => {
messages.length = 0;
await db.update(heartbeats).set({ serverId, periodMinutes: 60, graceMinutes: 10 }).where(eq(heartbeats.id, id));
const t = (min: number) => new Date(Date.now() + min * 60_000);
let r = await runEndpointPass(t(30));
assert.equal(r.announced, 0, "not due yet");
r = await runEndpointPass(t(75));
assert.equal(r.announced, 1, "an hour and ten minutes with no call");
assert.match(messages.join("\n"), /Nightly backup hasn't reported — it was due by/);
// The Alerts page reads the real clock, so the last call is moved back instead of the clock forward.
await db.update(heartbeats).set({ lastPingAt: new Date(Date.now() - 120 * 60_000).toISOString() }).where(eq(heartbeats.id, id));
invalidateAlertsCache();
const late = (await getAlerts(true)).alerts.find((x) => x.source === "Heartbeat")!;
assert.equal(late.severity, "warning");
assert.deepEqual(late.target && [late.target.type, late.target.id], ["server", serverId]);
assert.equal(late.silenced, false);
await startOrExtendWindow("server", serverId, 60, "work", "Ada");
invalidateAlertsCache();
assert.equal((await getAlerts(true)).alerts.find((x) => x.source === "Heartbeat")!.silenced, true, "maintenance on the server it runs on silences it");
assert.match(await buildMetrics(t(75)), /homelab_heartbeat_up\{name="Nightly backup"\} 0/);
await recordPing((await db.select().from(heartbeats).where(eq(heartbeats.id, id)))[0].token, "ok", null, t(76));
r = await runEndpointPass(t(77));
assert.equal(r.recovered, 1);
assert.match(await buildMetrics(t(77)), /homelab_heartbeat_up\{name="Nightly backup"\} 1/);
assert.match(await buildMetrics(t(77)), /homelab_heartbeat_last_ping_timestamp_seconds\{name="Nightly backup"\} \d+/);
});
test("a switched-off heartbeat still takes calls, says nothing, and starts a fresh clock when it is switched on again", async () => {
await admin("PUT", `/${id}`, { name: "Nightly backup", periodMinutes: 60, graceMinutes: 10, serverId: null, enabled: false });
const [h] = await db.select().from(heartbeats).where(eq(heartbeats.id, id));
assert.equal((await ping(`/api/heartbeat/${h.token}`)).status, 200, "pausing a job's heartbeat does not break its script");
assert.equal(heartbeatConditions([{ ...h, lastPingAt: "2020-01-01T00:00:00.000Z", lastStatus: "ok" }], new Date()).length, 0);
await admin("PUT", `/${id}`, { name: "Nightly backup", periodMinutes: 60, graceMinutes: 10, serverId: null, enabled: true });
const [after_] = await db.select().from(heartbeats).where(eq(heartbeats.id, id));
assert.equal(heartbeatState(after_, new Date()), "up");
});
test("a new address replaces the old one at once; removing one takes its history", async () => {
const res = await admin("POST", `/${id}/new-address`);
assert.equal(res.status, 200);
const fresh = (await res.json()).path as string;
assert.notEqual(fresh, path);
assert.equal((await ping(path)).status, 404, "the old one is dead");
assert.equal((await ping(fresh)).status, 200);
who = "viewer";
assert.equal((await admin("POST", `/${id}/new-address`)).status, 403);
who = "admin";
assert.equal((await admin("DELETE", `/${id}`)).status, 204);
assert.equal((await db.select().from(heartbeatPings).where(eq(heartbeatPings.heartbeatId, id))).length, 0);
assert.equal((await ping(fresh)).status, 404);
assert.equal((await admin("DELETE", `/${id}`)).status, 404);
});
});
+3 -1
View File
@@ -194,7 +194,9 @@ describe("through the routes, the page, the alerts and the figures", () => {
test("metrics say how old each test is and whether it passed", async () => {
const text = await buildMetrics();
assert.match(text, /homelab_restore_test_age_days\{subject="elsa"\} 0/);
assert.match(text, /homelab_restore_test_age_days\{subject="Photos share"\} 40/);
// The test's dates come from a fixed "now" and the metrics from the real clock, so what is expected is worked out the way the metric does.
const realAge = Math.floor((Date.now() - Date.parse(`${daysAgo(40)}T12:00:00Z`)) / 86_400_000);
assert.match(text, new RegExp(`homelab_restore_test_age_days\\{subject="Photos share"\\} ${realAge}`));
assert.match(text, /homelab_restore_test_passed\{subject="elsa"\} 1/);
assert.doesNotMatch(text, /subject="anna"/, "'not needed' isn't measured");
});