/** * The Silence button on an alert: the alert says what a maintenance window would have to be on, and starting one through the * maintenance route shows in the very next look at the alerts (not a minute later). * * 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-silence-")); 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, serverMetrics } = await import("../src/db/schema.js"); const { runMigrations } = await import("../src/db/migrate.js"); const { getAlerts } = await import("../src/services/alerts.js"); const { maintenanceRouter } = await import("../src/routes/maintenance.js"); const { updateSettings } = await import("../src/services/settingsStore.js"); await runMigrations(); const DAY = 24 * 3600_000; describe("silencing an alert", () => { let app: Server; let base = ""; let serverId = 0; before(async () => { await db.insert(users).values({ oidcSub: "tester", name: "Tester", role: "operator" }); serverId = (await db.insert(servers).values({ name: "elsa", apiTokenHash: "a", apiTokenPrefix: "a" }).returning())[0].id; const now = Date.now(); for (let i = 0; i <= 32; i++) { await db.insert(serverMetrics).values({ serverId, metric: "disk:/data", value: 50 + i * 0.5, recordedAt: new Date(now - (32 - i) * 0.25 * DAY).toISOString() }); } await updateSettings({ healthChecks: { diskForecastDays: 30 } }); const a = express(); a.use(express.json()); a.use((req, _res, next) => { (req as unknown as { session: unknown }).session = { user: { sub: "tester" } }; next(); }); a.use("/api/maintenance", maintenanceRouter); app = await new Promise((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 forecast = async () => (await getAlerts(false)).alerts.find((a) => a.id.startsWith("disk:disk-forecast:"))!; test("the alert names what a window would go on", async () => { const alert = await forecast(); assert.deepEqual(alert.target, { type: "server", id: serverId, name: "elsa" }); assert.equal(alert.silenced, false); }); test("starting a window through the route shows in the next look, and ending it does too", async () => { const alert = await forecast(); const started = await fetch(base, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ targetType: alert.target!.type, targetId: alert.target!.id, minutes: 60, reason: "Silenced from the Alerts page" }), }); assert.equal(started.status, 201); const window = (await started.json()).window; assert.equal((await forecast()).silenced, true, "silenced at once — the stored result was dropped"); assert.equal((await fetch(`${base}/${window.id}`, { method: "DELETE" })).status, 204); assert.equal((await forecast()).silenced, false, "and active again at once when it is ended"); }); });