/** * The Map page's graph: Proxmox hosts → servers → DNS names and monitors, with nothing guessed. * * 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 http from "node:http"; import type { AddressInfo } from "node:net"; const dir = mkdtempSync(join(tmpdir(), "hm-topology-")); process.env.DATABASE_PATH = join(dir, "test.sqlite"); process.env.SESSION_DIR = join(dir, "sessions"); process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32); const { db } = await import("../src/db/client.js"); const { servers, integrations, dnsProviders, dnsZonesCache, dnsRecordsCache } = await import("../src/db/schema.js"); const { runMigrations } = await import("../src/db/migrate.js"); const { buildTopology } = await import("../src/services/topology.js"); await runMigrations(); const metric = (id: number, host: string, status: number) => `monitor_status{monitor_id="${id}",monitor_name="m${id}",monitor_type="ping",monitor_url="",monitor_hostname="${host}",monitor_port=""} ${status}`; describe("buildTopology", () => { let kuma: http.Server; let kumaStatus = 200; before(async () => { kuma = await new Promise((resolve) => { const s = http.createServer((req, res) => { res.statusCode = kumaStatus; res.end(req.url === "/metrics" ? [metric(1, "elsa.lab.example", 1), metric(2, "elsa.lab.example", 0), metric(3, "unknown.example", 1)].join("\n") : ""); }); s.listen(0, "127.0.0.1", () => resolve(s)); }); const kumaUrl = `http://127.0.0.1:${(kuma.address() as AddressInfo).port}`; const [pve] = await db.insert(integrations).values({ type: "proxmox", name: "Felicia", baseUrl: "https://pve.lab", config: "{}" }).returning(); await db.insert(integrations).values({ type: "uptimekuma", name: "Kuma", baseUrl: kumaUrl, config: JSON.stringify({ url: kumaUrl, password: "k" }) }); const recent = new Date().toISOString(); await db.insert(servers).values([ { name: "elsa", hostname: "elsa.lab.example", apiTokenHash: "a", apiTokenPrefix: "a", lastSeenAt: recent, ipAddresses: JSON.stringify(["10.0.0.5"]), proxmoxIntegrationId: pve.id, proxmoxNode: "pve1", proxmoxGuestType: "qemu", proxmoxVmid: 100 }, { name: "anna", hostname: null, apiTokenHash: "b", apiTokenPrefix: "b", lastSeenAt: new Date(Date.now() - 5 * 24 * 3600_000).toISOString(), ipAddresses: JSON.stringify(["10.0.0.6"]) }, { name: "never", hostname: null, apiTokenHash: "c", apiTokenPrefix: "c" }, ]); const [provider] = await db.insert(dnsProviders).values({ providerType: "technitium", name: "Ginny", config: "{}" }).returning(); await db.insert(dnsZonesCache).values({ providerId: provider.id, zoneId: "z1", zoneName: "lab.example" }); await db.insert(dnsRecordsCache).values([ { providerId: provider.id, zoneId: "z1", recordId: "1", type: "A", name: "elsa", content: "10.0.0.5" }, { providerId: provider.id, zoneId: "z1", recordId: "2", type: "A", name: "www.lab.example", content: "10.0.0.5" }, { providerId: provider.id, zoneId: "z1", recordId: "3", type: "A", name: "@", content: "10.0.0.6" }, { providerId: provider.id, zoneId: "z1", recordId: "4", type: "A", name: "stranger", content: "10.9.9.9" }, { providerId: provider.id, zoneId: "z1", recordId: "5", type: "TXT", name: "txt", content: "10.0.0.5" }, ]); }); after(() => { kuma.close(); try { rmSync(dir, { recursive: true, force: true }); } catch { // Windows keeps the database file locked until the process exits. } }); const has = (map: { edges: { from: string; to: string }[] }, from: string, to: string) => map.edges.some((e) => e.from === from && e.to === to); test("every server is a node, with its state; a Proxmox link adds the host above it", async () => { const map = await buildTopology(); const status = (label: string) => map.nodes.find((n) => n.kind === "server" && n.label === label)!.status; assert.equal(status("elsa"), "ok"); assert.equal(status("anna"), "bad", "last heard from five days ago"); assert.equal(status("never"), "unknown", "never reported"); const host = map.nodes.find((n) => n.kind === "host")!; assert.equal(host.label, "pve1"); assert.equal(host.sub, "Felicia"); const elsa = map.nodes.find((n) => n.label === "elsa" && n.kind === "server")!; assert.ok(has(map, host.id, elsa.id)); }); test("a DNS name is shown only where an A or AAAA record holds the server's own address, written out in full", async () => { const map = await buildTopology(); const dns = map.nodes.filter((n) => n.kind === "dns").map((n) => n.label).sort(); assert.deepEqual(dns, ["elsa.lab.example", "lab.example", "www.lab.example"]); const elsa = map.nodes.find((n) => n.kind === "server" && n.label === "elsa")!; assert.ok(has(map, elsa.id, "dns:elsa.lab.example")); assert.ok(has(map, elsa.id, "dns:www.lab.example")); assert.ok(has(map, map.nodes.find((n) => n.kind === "server" && n.label === "anna")!.id, "dns:lab.example"), "an apex record is the zone's own name"); assert.ok(!map.nodes.some((n) => n.label.includes("stranger")), "an address no server has is left out"); }); test("monitors watching a server hang off it, with their status; ones watching nothing known are left out", async () => { const map = await buildTopology(); const monitors = map.nodes.filter((n) => n.kind === "monitor"); assert.deepEqual(monitors.map((m) => [m.label, m.status]).sort(), [["m1", "ok"], ["m2", "bad"]]); for (const m of monitors) assert.ok(map.edges.some((e) => e.to === m.id && e.from.startsWith("server:"))); }); test("when Uptime Kuma can't be read the rest of the map is still there, with a note", async () => { kumaStatus = 500; const map = await buildTopology(); assert.equal(map.nodes.filter((n) => n.kind === "monitor").length, 0); assert.ok(map.nodes.filter((n) => n.kind === "server").length === 3); assert.match(map.notes.join(" "), /Couldn't read Uptime Kuma “Kuma”/); kumaStatus = 200; }); });