Files
Homelab-manager/server/test/serverPageLayout.test.ts
T
bobbanandClaude Sonnet 5.5 551eb1597a AdGuard Home, Grafana, Docker image age, foldable server page cards
New integration kinds adguard (status, statistics, filter lists and a real DNS question
over UDP; alerts for DNS down/not running, protection left off, stale lists) and grafana
(alert rules firing or failing, data-source health). Optional warning for running Docker
images built more than N days ago (off by default). A server's overview cards can be
folded and arranged, kept per user. Migrations 0075-0077; docs, metrics and tests included.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-27 22:13:03 +01:00

96 lines
5.0 KiB
TypeScript

/**
* How a person arranged a server's overview page (the cards they folded and the order of the blocks): kept on their own user record, returned with /api/me, replaced by PUT /api/me/server-page.
*
* Run with: npm test --workspace server
*/
import { test, describe, before, after } from "node:test";
import assert from "node:assert/strict";
import 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-pagelayout-"));
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 } = await import("../src/db/schema.js");
const { runMigrations } = await import("../src/db/migrate.js");
const { meRouter, parsePageLayout } = await import("../src/routes/me.js");
const { eq } = await import("drizzle-orm");
await runMigrations();
describe("reading what is stored", () => {
test("a layout is read back; nothing, nonsense and a layout with a bad name are the page as drawn", () => {
assert.deepEqual(parsePageLayout(JSON.stringify({ folded: ["notes"], order: { notes: 10, runbooks: 0 } })), { folded: ["notes"], order: { notes: 10, runbooks: 0 } });
const none = { folded: [], order: {} };
assert.deepEqual(parsePageLayout(null), none);
assert.deepEqual(parsePageLayout("not json"), none);
assert.deepEqual(parsePageLayout(JSON.stringify({ folded: ["Not A Name"], order: {} })), none);
assert.deepEqual(parsePageLayout(JSON.stringify({ folded: [], order: { notes: -1 } })), none);
});
});
describe("the routes", () => {
let app: Server;
let base = "";
const call = (sub: string, method: string, path: string, body?: unknown) => fetch(`${base}${path}`, { method, headers: { "content-type": "application/json", "x-sub": sub }, body: body === undefined ? undefined : JSON.stringify(body) });
before(async () => {
await db.insert(users).values([{ oidcSub: "ann", name: "Ann", role: "viewer" }, { oidcSub: "bo", name: "Bo", role: "operator" }]);
const a = express();
a.use(express.json());
a.use((req, _res, next) => {
(req as unknown as { session: unknown }).session = { user: { sub: String(req.headers["x-sub"]) } };
next();
});
a.use("/api/me", meRouter);
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/me`;
});
after(() => {
app.close();
try {
rmSync(dir, { recursive: true, force: true });
} catch {
// Windows keeps the database file locked until the process exits.
}
});
const mine = async (sub: string) => ((await (await call(sub, "GET", "")).json()) as { user: { serverPage: { folded: string[]; order: Record<string, number> } } }).user.serverPage;
test("a person who has not arranged anything gets the page as drawn", async () => {
assert.deepEqual(await mine("ann"), { folded: [], order: {} });
});
test("anyone signed in saves their own arrangement, and gets it back with their account — not someone else's", async () => {
const put = await call("ann", "PUT", "/server-page", { folded: ["admin-links", "admin-links", "notes"], order: { runbooks: 0, notes: 10 } });
assert.equal(put.status, 200);
assert.deepEqual(((await put.json()) as { serverPage: unknown }).serverPage, { folded: ["admin-links", "notes"], order: { runbooks: 0, notes: 10 } }, "a block named twice is folded once");
assert.deepEqual(await mine("ann"), { folded: ["admin-links", "notes"], order: { runbooks: 0, notes: 10 } });
assert.deepEqual(await mine("bo"), { folded: [], order: {} });
});
test("a name that is not a block's, a position out of range and too many blocks are refused, and change nothing", async () => {
assert.equal((await call("ann", "PUT", "/server-page", { folded: ["Admin Links"], order: {} })).status, 400);
assert.equal((await call("ann", "PUT", "/server-page", { folded: [], order: { notes: 5000 } })).status, 400);
assert.equal((await call("ann", "PUT", "/server-page", { folded: Array.from({ length: 61 }, (_, i) => `b${i}`), order: {} })).status, 400);
assert.equal((await call("ann", "PUT", "/server-page", { nonsense: true })).status, 400);
assert.deepEqual(await mine("ann"), { folded: ["admin-links", "notes"], order: { runbooks: 0, notes: 10 } });
});
test("putting the page back as drawn stores nothing at all", async () => {
assert.equal((await call("ann", "PUT", "/server-page", { folded: [], order: {} })).status, 200);
const [row] = await db.select({ layout: users.serverPageLayout }).from(users).where(eq(users.oidcSub, "ann"));
assert.equal(row.layout, null);
assert.deepEqual(await mine("ann"), { folded: [], order: {} });
});
});