Files
Homelab-manager/server/test/dataUsageAndDeliveries.test.ts
bobbanandClaude Sonnet 5.5 e114567d64 How big the app's data is, and a webhook delivery log with Send again
Settings -> Data -> Storage measures the database, backups and sessions and every
table (size, rows, growth, how long it is kept, how far back it reaches), and flags
histories that are not being trimmed or have no limit.

Event webhooks now keep a delivery log (last 100 per webhook, 14 days): what was
sent, how it went, the message itself, and Send again, which resends it exactly as it
was (same id and signature). Resends are audited and in the Change log.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-21 20:58:57 +02:00

270 lines
16 KiB
TypeScript

/**
* How big the app's own data is and whether it is being trimmed, and the delivery log of the event webhooks with its retry.
*
* Run with: npm test --workspace server
*/
import { test, describe, before, after } from "node:test";
import assert from "node:assert/strict";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { createServer, type IncomingMessage, type Server } from "node:http";
import type { AddressInfo } from "node:net";
const dir = mkdtempSync(join(tmpdir(), "hm-usage-"));
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, serverMetrics, endpointChecks, endpointResults, auditLog, eventWebhooks, webhookDeliveries } = await import("../src/db/schema.js");
const { runMigrations } = await import("../src/db/migrate.js");
const usage = await import("../src/services/dataUsage.js");
const hooks = await import("../src/services/eventWebhooks.js");
const { updateSettings } = await import("../src/services/settingsStore.js");
const { dataUsageRouter } = await import("../src/routes/dataUsage.js");
const { eventWebhooksRouter } = await import("../src/routes/eventWebhooks.js");
const { eq } = await import("drizzle-orm");
await runMigrations();
mkdirSync(join(dir, "sessions"), { recursive: true });
mkdirSync(join(dir, "backups"), { recursive: true });
after(() => {
try {
rmSync(dir, { recursive: true, force: true });
} catch {
// Windows may still hold the database file
}
});
const DAY = 86_400_000;
const NOW = new Date("2026-10-20T12:00:00Z");
const ago = (days: number) => new Date(NOW.getTime() - days * DAY).toISOString();
describe("is a history being trimmed?", () => {
test("it reaches back as far as it should, or it is still filling, or it is holding far too much, or it has no limit at all", () => {
assert.deepEqual(usage.judge({ keepDays: 35, spanDays: 34.5, rows: 900 }).state, "limit");
assert.deepEqual(usage.judge({ keepDays: 35, spanDays: 36, rows: 900 }).state, "limit", "a day or two over is the trimming not having run yet today");
assert.equal(usage.judge({ keepDays: 35, spanDays: 12, rows: 300 }).state, "filling");
assert.match(usage.judge({ keepDays: 35, spanDays: 12, rows: 300 }).text, /Holds 12 of its 35 days so far/);
const bad = usage.judge({ keepDays: 35, spanDays: 90, rows: 5000 });
assert.equal(bad.state, "not_trimmed");
assert.match(bad.text, /Reaches back 90 days, but should keep only 35/);
assert.equal(usage.judge({ keepDays: null, spanDays: 400, rows: 1000, logsOff: true }).state, "unlimited");
assert.equal(usage.judge({ keepDays: null, spanDays: 10, rows: 10 }).state, "plain", "no limit and no promise of one");
assert.equal(usage.judge({ keepDays: 14, spanDays: null, rows: 0 }).text, "Empty.");
});
test("the times the database writes, in all three forms, become moments", () => {
assert.equal(usage.toIso("2026-10-03 14:05:09"), "2026-10-03T14:05:09.000Z");
assert.equal(usage.toIso("2026-10-03T14:05:09.123Z"), "2026-10-03T14:05:09.123Z");
assert.equal(usage.toIso("2026-10-03"), "2026-10-03T00:00:00.000Z");
assert.equal(usage.toIso("nonsense"), null);
});
});
describe("what the data folder holds", () => {
before(async () => {
const [s] = await db.insert(servers).values({ name: "web", apiTokenHash: "h", apiTokenPrefix: "p", ipAddresses: "[]" }).returning();
// Server samples going back 60 days, though only 35 are meant to be kept: the trimming has not run.
for (let d = 60; d >= 0; d -= 2) await db.insert(serverMetrics).values({ serverId: s.id, metric: "cpu", value: 10, recordedAt: ago(d) });
// An endpoint check's answers for only five days of its fourteen.
const [c] = await db.insert(endpointChecks).values({ name: "Wiki", kind: "http", target: "https://x/" } as never).returning();
for (let h = 0; h < 5 * 24; h += 6) await db.insert(endpointResults).values({ checkId: c.id, at: new Date(NOW.getTime() - h * 3600_000).toISOString(), ok: true, ms: 40, message: "ok" } as never);
// The audit log, in SQLite's own time format, going back 200 days.
for (let d = 200; d >= 0; d -= 20) await db.insert(auditLog).values({ category: "settings", action: "update", createdAt: ago(d).slice(0, 19).replace("T", " ") });
writeFileSync(join(dir, "backups", "homelab-manager-2026-10-19-03-30-00.sqlite"), Buffer.alloc(1500));
writeFileSync(join(dir, "backups", "homelab-manager-2026-10-18-03-30-00.sqlite"), Buffer.alloc(500));
writeFileSync(join(dir, "sessions", "abc.json"), "{}");
});
test("the file, the backups and the sessions beside it, and the tables by what they hold", async () => {
const u = await usage.getDataUsage(NOW);
assert.ok(u.database.bytes > 100_000 && u.database.pages * u.database.pageSize >= u.database.bytes - 8192, "the file is as big as its pages");
assert.deepEqual([u.backups.files, u.backups.bytes, u.sessions.files], [2, 2000, 1]);
assert.equal(u.totalBytes, u.database.bytes + u.database.walBytes + 2000 + u.sessions.bytes);
assert.ok(u.database.tables >= 60 && u.tables.length === u.database.tables);
const t = Object.fromEntries(u.tables.map((x) => [x.name, x]));
assert.equal(t.server_metrics.rows, 31);
assert.ok(t.server_metrics.bytes! > 0 && t.users.bytes! > 0, "sizes come from the database's own page count, indexes included");
assert.ok(u.tables.every((x, i) => i === 0 || (u.tables[i - 1].bytes ?? 0) >= (x.bytes ?? 0)), "the biggest first");
});
test("a history that holds more than it should is said to be untrimmed; one still filling says how far it has got", async () => {
const t = Object.fromEntries((await usage.getDataUsage(NOW)).tables.map((x) => [x.name, x]));
assert.deepEqual([t.server_metrics.state, t.server_metrics.keepDays, Math.round(t.server_metrics.spanDays!)], ["not_trimmed", 35, 60]);
assert.match(t.server_metrics.stateText, /should keep only 35/);
assert.equal(t.server_metrics.oldest, ago(60));
assert.deepEqual([t.endpoint_results.state, t.endpoint_results.keepDays], ["filling", 14]);
assert.match(t.endpoint_results.stateText, /Holds 5 of its 14 days/);
assert.ok(t.endpoint_results.perDay! >= 4, "four readings a day, over the five days");
assert.equal(t.server_metrics.perDay, 0.6, "four samples (every other day) in the last week, over seven days");
});
test("the logs have no limit until purging is switched on, and then are judged by it", async () => {
await updateSettings({ logRetention: { enabled: false, retentionDays: 90, intervalHours: 24 } });
let u = await usage.getDataUsage(NOW);
let audit = u.tables.find((x) => x.name === "audit_log")!;
assert.deepEqual([audit.state, audit.keepDays], ["unlimited", null]);
assert.ok(u.attention.some((a) => a.table === "audit_log" && /switched off/.test(a.text)));
assert.ok(u.attention.some((a) => a.table === "server_metrics"), "and what is not trimmed is on the list too");
assert.ok(!u.attention.some((a) => a.table === "endpoint_results"), "a table that is merely filling needs no look");
await updateSettings({ logRetention: { enabled: true, retentionDays: 90, intervalHours: 24 } });
u = await usage.getDataUsage(NOW);
audit = u.tables.find((x) => x.name === "audit_log")!;
assert.deepEqual([audit.state, audit.keepDays], ["not_trimmed", 90]);
assert.match(audit.stateText, /Reaches back 200 days, but should keep only 90/);
assert.equal(audit.perDay, 0.1, "the week is counted in the form this table writes its times in");
});
test("only an administrator can ask", async () => {
await db.insert(users).values([
{ oidcSub: "admin", name: "Ada", role: "admin" },
{ oidcSub: "viewer", name: "Vic", role: "viewer" },
]);
let who = "viewer";
const a = express();
a.use((req, _res, next) => {
(req as unknown as { session: unknown }).session = { user: { sub: who } };
next();
});
a.use("/api/data-usage", dataUsageRouter);
const server = await new Promise<Server>((resolve) => {
const s = a.listen(0, "127.0.0.1", () => resolve(s));
});
const base = `http://127.0.0.1:${(server.address() as AddressInfo).port}/api/data-usage`;
assert.equal((await fetch(base, { headers: { connection: "close" } })).status, 403);
who = "admin";
const res = await fetch(base, { headers: { connection: "close" } });
assert.equal(res.status, 200);
const body = (await res.json()) as any;
assert.ok(body.tables.length > 0 && body.database.bytes > 0 && !JSON.stringify(body).includes(dir.replace(/\\/g, "\\\\")), "no folder path leaves the server");
server.close();
});
});
describe("the webhook delivery log", () => {
let receiver: Server;
let receiverUrl = "";
const got: { headers: IncomingMessage["headers"]; body: string }[] = [];
let mode: "ok" | "fail" = "ok";
let app: Server;
let base = "";
let who = "admin";
let hookId = 0;
before(async () => {
receiver = await new Promise<Server>((resolve) => {
const s = createServer((req, res) => {
let b = "";
req.on("data", (c) => (b += c));
req.on("end", () => {
got.push({ headers: req.headers, body: b });
res.statusCode = mode === "ok" ? 200 : 500;
res.end("x");
});
}).listen(0, "127.0.0.1", () => resolve(s));
});
receiverUrl = `http://127.0.0.1:${(receiver.address() as AddressInfo).port}/hook`;
const [hook] = await db.insert(eventWebhooks).values({ label: "Receiver", url: receiverUrl, secret: hooks.sealSecret("s3cret"), events: JSON.stringify(["alert.opened"]) }).returning();
hookId = hook.id;
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/event-webhooks", eventWebhooksRouter);
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/event-webhooks`;
});
after(() => {
receiver.close();
app.close();
});
const call = (method: string, path: string) => fetch(`${base}${path}`, { method, headers: { connection: "close" } });
const rowOf = async () => (await db.select().from(eventWebhooks).where(eq(eventWebhooks.id, hookId)))[0];
test("every message is logged with how it went: one try that worked, and a failure that was tried twice", async () => {
const hook = await rowOf();
mode = "ok";
assert.equal((await hooks.deliver(hook, "alert.opened", { alert: { key: "offline:server:1", message: "web is down" } }, { retryAfterMs: 1 })).ok, true);
mode = "fail";
assert.equal((await hooks.deliver(hook, "alert.opened", { alert: { key: "offline:server:2", message: "pi is down" } }, { retryAfterMs: 1 })).ok, false);
const rows = await db.select().from(webhookDeliveries).orderBy(webhookDeliveries.id);
assert.deepEqual(rows.map((r) => [r.event, r.attempts, r.ok, r.result]), [["alert.opened", 1, true, "200"], ["alert.opened", 2, false, "500"]]);
assert.equal(got.length, 3, "one, then two");
assert.equal(JSON.parse(rows[1].body).data.alert.message, "pi is down");
assert.equal(rows[1].deliveryId, JSON.parse(rows[1].body).id);
assert.equal(rows[1].retryOf, null);
});
test("the log is for administrators; it lists newest first, with the message and whether it can be sent again", async () => {
who = "viewer";
assert.equal((await call("GET", `/${hookId}/deliveries`)).status, 403, "operators and viewers cannot read what was sent");
who = "admin";
const res = await call("GET", `/${hookId}/deliveries`);
assert.equal(res.status, 200);
const list = ((await res.json()) as any).deliveries;
assert.deepEqual(list.map((d: any) => [d.ok, d.attempts, d.retryable]), [[false, 2, true], [true, 1, true]]);
assert.equal(JSON.parse(list[0].body).data.alert.key, "offline:server:2");
assert.equal((await call("GET", "/99999/deliveries")).status, 404);
assert.equal((await call("GET", "/abc/deliveries")).status, 400);
});
test("retrying sends the very same message — the same body, the same id, the same signature — and logs it as a delivery of its own", async () => {
const [failed] = await db.select().from(webhookDeliveries).where(eq(webhookDeliveries.ok, false));
mode = "ok";
got.length = 0;
const res = await call("POST", `/${hookId}/deliveries/${failed.id}/retry`);
assert.deepEqual([res.status, ((await res.json()) as any).ok], [200, true]);
assert.equal(got.length, 1);
assert.equal(got[0].body, failed.body, "the exact bytes");
assert.equal(got[0].headers["x-hlm-delivery"], failed.deliveryId);
assert.equal(got[0].headers["x-hlm-signature"], hooks.signBody("s3cret", failed.body), "so a receiver checking the signature is satisfied");
const rows = await db.select().from(webhookDeliveries).orderBy(webhookDeliveries.id);
assert.deepEqual([rows.length, rows[2].retryOf, rows[2].ok, rows[2].deliveryId], [3, failed.id, true, failed.deliveryId]);
assert.equal((await rowOf()).lastResult, "200", "and the webhook's own last result follows");
const [audit] = await db.select().from(auditLog).where(eq(auditLog.action, "retry"));
assert.deepEqual([audit.category, audit.targetId, JSON.parse(audit.detail!).event], ["webhook", String(hookId), "alert.opened"]);
assert.equal((await call("POST", `/${hookId}/deliveries/99999/retry`)).status, 404);
assert.equal((await call("POST", `/${hookId + 1}/deliveries/${failed.id}/retry`)).status, 404, "a delivery of another webhook is not this one's");
who = "viewer";
assert.equal((await call("POST", `/${hookId}/deliveries/${failed.id}/retry`)).status, 403);
who = "admin";
});
test("a message too big to have been kept cannot be sent again, and says so", async () => {
const [big] = await db.insert(webhookDeliveries).values({ webhookId: hookId, event: "alert.opened", deliveryId: "big", attempts: 1, ok: false, result: "500", body: "x".repeat(hooks.MAX_STORED_BODY + 1), createdAt: new Date().toISOString() }).returning();
const list = ((await (await call("GET", `/${hookId}/deliveries`)).json()) as any).deliveries;
const row = list.find((d: any) => d.id === big.id);
assert.deepEqual([row.retryable, row.body], [false, null]);
const res = await call("POST", `/${hookId}/deliveries/${big.id}/retry`);
assert.equal(res.status, 409);
await db.delete(webhookDeliveries).where(eq(webhookDeliveries.id, big.id));
});
test("only the last hundred are kept for a webhook, and none older than two weeks; removing the webhook removes its log", async () => {
const hook = await rowOf();
const base0 = Date.now();
for (let i = 0; i < 110; i++) await db.insert(webhookDeliveries).values({ webhookId: hookId, event: "alert.opened", deliveryId: `d${i}`, attempts: 1, ok: true, result: "200", body: "{}", createdAt: new Date(base0 - i * 1000).toISOString() });
await db.insert(webhookDeliveries).values({ webhookId: hookId, event: "alert.opened", deliveryId: "old", attempts: 1, ok: true, result: "200", body: "{}", createdAt: new Date(base0 - 20 * DAY).toISOString() });
mode = "ok";
await hooks.deliver(hook, "alert.opened", { alert: { key: "k", message: "m" } }, { retryAfterMs: 1 });
const rows = await db.select().from(webhookDeliveries).where(eq(webhookDeliveries.webhookId, hookId));
assert.equal(rows.length, hooks.KEEP_DELIVERIES);
assert.ok(!rows.some((r) => r.deliveryId === "old"), "two weeks and more is gone");
assert.ok(rows.some((r) => r.deliveryId === "d0") && !rows.some((r) => r.deliveryId === "d109"), "the newest kept, the oldest dropped");
const removed = await call("DELETE", `/${hookId}`);
assert.equal(removed.status, 204);
assert.equal((await db.select().from(webhookDeliveries).where(eq(webhookDeliveries.webhookId, hookId))).length, 0);
});
});