UniFi: the controller's client list is matched to servers by IP address, so a server is connected through a switch or access point (and port). That is a dependency made by itself, shown on the server page, the Network devices page and the Connections map. IP Addresses can sync the devices' addresses. A server's page is split into Overview, Health, Network, History, Tasks and Settings, by ?view=; alerts link to the section they are about. My work lists what has your name on it: alerts you have, maintenance you started (extend, end with undo) and update runs you started, with a badge. Settings has a search box (and the command palette lists every setting). Removing a dependency, admin link, runbook, hand-made IP address, port opening or restore test is undone from the message instead of asked about first. Two backup tests used a fixed date and failed as days passed; they follow the real clock now. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
144 lines
6.9 KiB
TypeScript
144 lines
6.9 KiB
TypeScript
/**
|
|
* "My work": what has one person's name on it — alerts handed to them or acknowledged by them, their maintenance windows, their update runs — and which of those
|
|
* are about to run out.
|
|
*
|
|
* 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-mywork-"));
|
|
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, updateRuns } = await import("../src/db/schema.js");
|
|
const { runMigrations } = await import("../src/db/migrate.js");
|
|
const { setAck } = await import("../src/services/alertAcks.js");
|
|
const { startOrExtendWindow } = await import("../src/services/maintenance.js");
|
|
const { invalidateAlertsCache } = await import("../src/services/alerts.js");
|
|
const { buildMyWork, labelsOf } = await import("../src/services/myWork.js");
|
|
const { myWorkRouter } = await import("../src/routes/myWork.js");
|
|
|
|
await runMigrations();
|
|
|
|
const HOUR = 3600_000;
|
|
let app: Server;
|
|
let base = "";
|
|
let who = "olga";
|
|
const ids: Record<string, number> = {};
|
|
const olga = { id: 0, name: "Olga", email: "olga@example.lan", oidcSub: "olga" };
|
|
|
|
before(async () => {
|
|
const rows = await db
|
|
.insert(users)
|
|
.values([
|
|
{ oidcSub: "ada", name: "Ada", role: "admin" },
|
|
{ oidcSub: "olga", name: "Olga", email: "olga@example.lan", role: "operator" },
|
|
{ oidcSub: "vic", name: "Vic", role: "viewer" },
|
|
])
|
|
.returning({ id: users.id, oidcSub: users.oidcSub });
|
|
for (const r of rows) ids[r.oidcSub] = r.id;
|
|
olga.id = ids.olga;
|
|
// four servers that have stopped reporting: four standing alerts
|
|
for (const name of ["web", "db", "nas", "pi"]) {
|
|
ids[name] = (await db.insert(servers).values({ name, apiTokenHash: name, apiTokenPrefix: name, lastSeenAt: new Date(Date.now() - 6 * HOUR).toISOString() }).returning({ id: servers.id }))[0].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/my-work", myWorkRouter);
|
|
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/my-work`;
|
|
});
|
|
after(() => {
|
|
app.close();
|
|
try {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
} catch {
|
|
// Windows may still hold the database file
|
|
}
|
|
});
|
|
|
|
const alertOf = (name: string) => `offline:offline:server:${ids[name]}`;
|
|
|
|
describe("my work", () => {
|
|
test("the labels a person's actions are recorded under are their name, email and sign-in id", () => {
|
|
assert.deepEqual(labelsOf(olga).sort(), ["Olga", "olga", "olga@example.lan"].sort());
|
|
assert.deepEqual(labelsOf({ id: 1, name: null, email: null, oidcSub: "x" }), ["x"]);
|
|
});
|
|
|
|
test("nothing has their name on it: nothing is listed", async () => {
|
|
invalidateAlertsCache();
|
|
const mine = await buildMyWork(olga);
|
|
assert.deepEqual([mine.alerts.length, mine.windows.length, mine.updateRuns.length, mine.needsAttention], [0, 0, 0, 0]);
|
|
});
|
|
|
|
test("alerts handed to them, or acknowledged by them, and nobody else's; the one that runs out first is first", async () => {
|
|
await setAck({ alertId: alertOf("web"), note: "Looking at the PSU", assigneeId: ids.olga, hours: 72, actor: "Ada" });
|
|
await setAck({ alertId: alertOf("db"), note: "Waiting for the disk", assigneeId: null, hours: 4, actor: "Olga" });
|
|
await setAck({ alertId: alertOf("nas"), note: "Not mine", assigneeId: ids.ada, hours: 2, actor: "Ada" });
|
|
await setAck({ alertId: alertOf("pi"), note: "", assigneeId: ids.olga, hours: null, actor: "Olga" });
|
|
invalidateAlertsCache();
|
|
const mine = await buildMyWork(olga);
|
|
assert.deepEqual(
|
|
mine.alerts.map((a) => [a.alertId, a.role, a.endsSoon]),
|
|
[
|
|
[alertOf("db"), "acked", true],
|
|
[alertOf("web"), "handed", false],
|
|
[alertOf("pi"), "handed", false],
|
|
],
|
|
"ending in 4 hours, in 3 days, and never; the one for Ada is not here",
|
|
);
|
|
assert.equal(mine.alerts[0].note, "Waiting for the disk");
|
|
// handed to me (2) + one that runs out within a day (1, which is not also handed to me)
|
|
assert.equal(mine.needsAttention, 3);
|
|
});
|
|
|
|
test("a maintenance window they started, that ends soon; one that was started by somebody else is not theirs", async () => {
|
|
await startOrExtendWindow("server", ids.web, 45, "PSU swap", "Olga");
|
|
await startOrExtendWindow("server", ids.db, 600, "Disk replacement", "Olga");
|
|
await startOrExtendWindow("server", ids.nas, 30, "Somebody else's", "Ada");
|
|
invalidateAlertsCache();
|
|
const mine = await buildMyWork(olga);
|
|
assert.deepEqual(mine.windows.map((w) => [w.targetName, w.endsSoon, w.reason]), [["web", true, "PSU swap"], ["db", false, "Disk replacement"]], "the one that ends first first");
|
|
assert.equal(mine.needsAttention, 4);
|
|
});
|
|
|
|
test("update runs they started that are still going, or finished within a day; an older one, and somebody else's, are not listed", async () => {
|
|
const run = (serverId: number, startedBy: string, startedAt: Date, finishedAt: Date | null, status: string) =>
|
|
db.insert(updateRuns).values({ serverId, integrationId: 1, projectId: 1, templateId: 1, templateName: "Apply updates", hostLimit: "x", status, startedBy, startedAt: startedAt.toISOString(), finishedAt: finishedAt ? finishedAt.toISOString() : null, updatesBefore: 6, updatesAfter: finishedAt ? 0 : null });
|
|
const now = Date.now();
|
|
await run(ids.web, "Olga", new Date(now - 10 * 60_000), null, "running");
|
|
await run(ids.db, "Olga", new Date(now - 5 * HOUR), new Date(now - 4 * HOUR), "done");
|
|
await run(ids.nas, "Olga", new Date(now - 3 * 24 * HOUR), new Date(now - 3 * 24 * HOUR + HOUR), "done");
|
|
await run(ids.pi, "Ada", new Date(now - 10 * 60_000), null, "running");
|
|
const mine = await buildMyWork(olga);
|
|
assert.deepEqual(mine.updateRuns.map((r) => [r.serverName, r.running]), [["web", true], ["db", false]], "the running one first");
|
|
assert.equal(mine.needsAttention, 5);
|
|
});
|
|
|
|
test("over the route: only the signed-in person's own", async () => {
|
|
who = "olga";
|
|
const body = (await (await fetch(base)).json()) as any;
|
|
assert.equal(body.alerts.length, 3);
|
|
assert.equal(body.windows.length, 2);
|
|
who = "vic";
|
|
const vic = (await (await fetch(base)).json()) as any;
|
|
assert.deepEqual([vic.alerts.length, vic.windows.length, vic.updateRuns.length, vic.needsAttention], [0, 0, 0, 0]);
|
|
});
|
|
});
|