Files
Homelab-manager/server/test/maintenanceBulk.test.ts
T
bobbanandClaude Sonnet 5.5 bff0c2169e Start maintenance on everything, all servers or all integrations at once
The Start maintenance list gets a "Many at once" group: Everything, All
servers, All integrations, All DNS providers (enabled ones only). Each target
still gets its own window, existing ones are extended, and the action is one
audit entry. "Everything" asks for confirmation first, and "End all" on the
Active list ends every running window together. The top banner and the wall
display summarise long lists instead of printing every name.

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

148 lines
6.4 KiB
TypeScript

/**
* Putting everything in maintenance at once, and ending it all again.
*
* 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-bulk-"));
process.env.DATABASE_PATH = join(dir, "test.sqlite");
process.env.SESSION_DIR = join(dir, "sessions");
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
const express = (await import("express")).default;
const { db } = await import("../src/db/client.js");
const { users, servers, integrations, dnsProviders, maintenanceWindows, auditLog } = await import("../src/db/schema.js");
const { runMigrations } = await import("../src/db/migrate.js");
const { maintenanceRouter } = await import("../src/routes/maintenance.js");
const { listActiveWindows } = await import("../src/services/maintenance.js");
await runMigrations();
describe("bulk maintenance", () => {
let app: Server;
let base = "";
let who = "operator";
before(async () => {
await db.insert(users).values([
{ oidcSub: "operator", name: "Olga", role: "operator" },
{ oidcSub: "viewer", name: "Vic", role: "viewer" },
]);
await db.insert(servers).values([
{ name: "elsa", apiTokenHash: "a", apiTokenPrefix: "a" },
{ name: "anna", apiTokenHash: "b", apiTokenPrefix: "b" },
{ name: "zoe", apiTokenHash: "c", apiTokenPrefix: "c" },
]);
await db.insert(integrations).values([
{ type: "proxmox", name: "Felicia", baseUrl: "https://pve.lab", config: "{}" },
{ type: "gitea", name: "Gitea", baseUrl: "https://git.lab", config: "{}" },
{ type: "synology", name: "Old NAS", baseUrl: "https://nas.lab", config: "{}", enabled: false },
]);
await db.insert(dnsProviders).values([
{ providerType: "technitium", name: "Ginny", config: "{}" },
{ providerType: "pihole", name: "Off pihole", config: "{}", 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/maintenance", maintenanceRouter);
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/maintenance`;
});
after(() => {
app.close();
try {
rmSync(dir, { recursive: true, force: true });
} catch {
// Windows keeps the database file locked until the process exits.
}
});
const post = (path: string, body?: unknown) =>
fetch(`${base}${path}`, { method: "POST", headers: { "content-type": "application/json" }, body: body === undefined ? undefined : JSON.stringify(body) });
const byType = async () => {
const counts: Record<string, number> = {};
for (const w of await listActiveWindows()) counts[w.targetType] = (counts[w.targetType] ?? 0) + 1;
return counts;
};
test("all servers starts one window per server and nothing else", async () => {
const res = await post("/bulk", { scope: "servers", minutes: 60 });
assert.equal(res.status, 201);
assert.deepEqual(await res.json(), { started: 3, extended: 0, total: 3 });
assert.deepEqual(await byType(), { server: 3 });
});
test("everything adds the enabled integrations and DNS providers, and extends the servers already in maintenance", async () => {
const res = await post("/bulk", { scope: "everything", minutes: 120, reason: "Power work" });
assert.deepEqual(await res.json(), { started: 3, extended: 3, total: 6 }, "2 integrations + 1 provider are new; the 3 servers are extended, not doubled");
assert.deepEqual(await byType(), { server: 3, integration: 2, dns_provider: 1 });
const windows = await listActiveWindows();
assert.ok(windows.every((w) => w.reason === "Power work" || w.reason === "Everything in maintenance"));
assert.ok(windows.every((w) => w.createdBy === "Olga"));
});
test("disabled integrations and providers are left out", async () => {
const ids = new Set((await listActiveWindows()).filter((w) => w.targetType === "integration").map((w) => w.targetId));
assert.equal(ids.size, 2);
});
test("it is one entry in the audit log, saying what it covered", async () => {
const entries = (await db.select().from(auditLog)).filter((e) => e.action === "start_bulk");
assert.equal(entries.length, 2);
assert.match(entries[1].detail ?? "", /"scope":"everything"/);
});
test("a viewer can't, and a bad request is refused", async () => {
who = "viewer";
assert.equal((await post("/bulk", { scope: "servers", minutes: 60 })).status, 403);
assert.equal((await post("/end-all")).status, 403);
who = "operator";
assert.equal((await post("/bulk", { scope: "the moon", minutes: 60 })).status, 400);
assert.equal((await post("/bulk", { scope: "servers", minutes: 1 })).status, 400);
assert.equal((await post("/bulk", { scope: "servers", minutes: 60 * 24 * 30 })).status, 400);
});
test("end-all ends what is running, and leaves a scheduled window alone", async () => {
const [anyServer] = await db.select().from(servers);
await db.insert(maintenanceWindows).values({
targetType: "server",
targetId: anyServer.id,
reason: "tonight",
startedAt: new Date(Date.now() + 5 * 3600_000).toISOString(),
endsAt: new Date(Date.now() + 6 * 3600_000).toISOString(),
});
const res = await post("/end-all");
assert.deepEqual(await res.json(), { ended: 6 });
assert.equal((await listActiveWindows()).length, 0);
const upcoming = (await db.select().from(maintenanceWindows)).filter((w) => w.reason === "tonight");
assert.equal(upcoming.length, 1);
assert.ok(upcoming[0].endsAt > upcoming[0].startedAt, "the scheduled one is untouched");
});
test("end-all with nothing running is fine", async () => {
assert.deepEqual(await (await post("/end-all")).json(), { ended: 0 });
});
test("a group with nothing in it says so instead of doing nothing silently", async () => {
await db.delete(dnsProviders);
const res = await post("/bulk", { scope: "dns_providers", minutes: 60 });
assert.equal(res.status, 400);
assert.equal((await res.json()).error, "nothing_to_silence");
});
});