- Racks page and a network diagram (UniFi tree with servers, printable) - Notes on servers and devices; To do list shown in Coming up and My work - Agents report the operating system (Linux 7, Windows 6); end-of-life badge and alert - Suggested dependencies from where a service is reached - Nginx Proxy Manager integration: proxy hosts page, certificate and host alerts, suggested checks - Home Assistant integration: watch chosen entities, history chart, limit and unavailable alerts - TimeChart gets a "value" scale that fits negatives; docs updated Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
138 lines
7.4 KiB
TypeScript
138 lines
7.4 KiB
TypeScript
/**
|
|
* Suggested dependencies: an integration reached at a server's own address is offered as needing that server; what is already so, would make a loop, or was
|
|
* said no to is left out; and adding one writes an ordinary dependency.
|
|
*
|
|
* 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-depsug-"));
|
|
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, dependencies, auditLog } = await import("../src/db/schema.js");
|
|
const { runMigrations } = await import("../src/db/migrate.js");
|
|
const { suggestDependencies, hostOfIntegration } = await import("../src/services/dependencySuggestions.js");
|
|
const { dependenciesRouter } = await import("../src/routes/dependencies.js");
|
|
const { eq } = await import("drizzle-orm");
|
|
|
|
await runMigrations();
|
|
|
|
describe("what is offered", () => {
|
|
const servers = [
|
|
{ id: 1, name: "nas", hostname: "nas.lan", ips: ["10.0.0.5"] },
|
|
{ id: 2, name: "elsa", hostname: null, ips: ["10.0.0.9"] },
|
|
];
|
|
const integ = (id: number, name: string, type: string, host: string | null, enabled = true) => ({ id, name, type, enabled, host });
|
|
const none = new Map<string, string[]>();
|
|
|
|
test("an integration at a server's own address (its IP, hostname or short name) needs that server", () => {
|
|
const { suggestions } = suggestDependencies({
|
|
integrations: [integ(1, "Gitea", "gitea", "10.0.0.5"), integ(2, "Kuma", "uptimekuma", "nas.lan"), integ(3, "IPAM", "phpipam", "elsa"), integ(4, "Cloud", "tailscale", "api.tailscale.com")],
|
|
servers,
|
|
edges: none,
|
|
dismissed: new Set(),
|
|
});
|
|
assert.deepEqual(suggestions.map((x) => x.key), ["integration:1>server:1", "integration:3>server:2", "integration:2>server:1"], "by name (Gitea, IPAM, Kuma); the one in the cloud matches nothing");
|
|
assert.ok(suggestions.every((x) => x.reason.includes("most likely runs there")));
|
|
});
|
|
|
|
test("a disabled integration, one with no address, and one with an unknown host are not offered", () => {
|
|
const { suggestions } = suggestDependencies({ integrations: [integ(1, "Off", "gitea", "10.0.0.5", false), integ(2, "None", "gitea", null), integ(3, "Elsewhere", "gitea", "10.9.9.9")], servers, edges: none, dismissed: new Set() });
|
|
assert.equal(suggestions.length, 0);
|
|
});
|
|
|
|
test("what is already so, and what would make a loop, is left out", () => {
|
|
const already = new Map([["integration:1", ["server:1"]]]);
|
|
assert.equal(suggestDependencies({ integrations: [integ(1, "Gitea", "gitea", "10.0.0.5")], servers, edges: already, dismissed: new Set() }).suggestions.length, 0);
|
|
// the server needs the integration already: the other way round would make them wait for each other
|
|
const loop = new Map([["server:1", ["integration:1"]]]);
|
|
assert.equal(suggestDependencies({ integrations: [integ(1, "Gitea", "gitea", "10.0.0.5")], servers, edges: loop, dismissed: new Set() }).suggestions.length, 0);
|
|
});
|
|
|
|
test("what somebody said no to is kept apart, and can come back", () => {
|
|
const r = suggestDependencies({ integrations: [integ(1, "Gitea", "gitea", "10.0.0.5")], servers, edges: none, dismissed: new Set(["integration:1>server:1"]) });
|
|
assert.deepEqual([r.suggestions.length, r.dismissed.length], [0, 1]);
|
|
});
|
|
|
|
test("where an integration is reached: a URL's host, or osTicket's database host", () => {
|
|
assert.equal(hostOfIntegration("https://git.example.lan:3000/", "{}"), "git.example.lan");
|
|
assert.equal(hostOfIntegration("", JSON.stringify({ host: "osticket-db.lan" })), "osticket-db.lan");
|
|
assert.equal(hostOfIntegration("not a url", null), null);
|
|
assert.equal(hostOfIntegration(null, "not json"), null);
|
|
});
|
|
});
|
|
|
|
describe("over the routes", () => {
|
|
let app: Server;
|
|
let base = "";
|
|
let who = "viewer";
|
|
before(async () => {
|
|
await db.insert(users).values([
|
|
{ oidcSub: "viewer", name: "Vic", role: "viewer" },
|
|
{ oidcSub: "operator", name: "Olga", role: "operator" },
|
|
]);
|
|
await db.insert(servers).values({ name: "nas", hostname: "nas.lan", apiTokenHash: "n", apiTokenPrefix: "n", ipAddresses: JSON.stringify(["10.0.0.5"]) });
|
|
await db.insert(integrations).values({ type: "gitea", name: "Gitea", baseUrl: "http://10.0.0.5:3000", config: JSON.stringify({ url: "http://10.0.0.5:3000" }) });
|
|
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/dependencies", dependenciesRouter);
|
|
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/dependencies`;
|
|
});
|
|
after(() => {
|
|
app.close();
|
|
try {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
} catch {
|
|
// Windows may still hold the database file
|
|
}
|
|
});
|
|
const post = (path: string, body: unknown = {}) => fetch(`${base}${path}`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
|
|
|
|
test("anyone signed in sees them; only an operator adds or dismisses", async () => {
|
|
who = "viewer";
|
|
const body = (await (await fetch(`${base}/suggestions`)).json()) as any;
|
|
assert.deepEqual(body.suggestions.map((s: any) => s.key), ["integration:1>server:1"]);
|
|
assert.equal((await post("/suggestions/add", { keys: ["integration:1>server:1"] })).status, 403);
|
|
assert.equal((await post("/suggestions/dismiss", { keys: ["integration:1>server:1"] })).status, 403);
|
|
});
|
|
|
|
test("dismissing hides it, restoring brings it back, and a bad body is refused", async () => {
|
|
who = "operator";
|
|
const hidden = (await (await post("/suggestions/dismiss", { keys: ["integration:1>server:1"] })).json()) as any;
|
|
assert.deepEqual([hidden.suggestions.length, hidden.dismissed.length], [0, 1]);
|
|
const back = (await (await post("/suggestions/restore")).json()) as any;
|
|
assert.equal(back.suggestions.length, 1);
|
|
assert.equal((await post("/suggestions/add", { keys: [] })).status, 400);
|
|
});
|
|
|
|
test("adding writes an ordinary dependency, written down, and it is no longer offered", async () => {
|
|
who = "operator";
|
|
const res = (await (await post("/suggestions/add", { keys: ["integration:1>server:1", "integration:9>server:9"] })).json()) as any;
|
|
assert.deepEqual(res.added.map((a: any) => [a.from, a.to]), [["Gitea", "nas"]], "a key that is not offered adds nothing");
|
|
assert.equal(res.suggestions.length, 0);
|
|
const rows = await db.select().from(dependencies);
|
|
assert.deepEqual(rows.map((r) => [r.fromType, r.fromId, r.toType, r.toId]), [["integration", 1, "server", 1]]);
|
|
const written = (await db.select().from(auditLog).where(eq(auditLog.category, "dependency")))[0];
|
|
assert.equal(written.action, "create");
|
|
const list = (await (await fetch(base)).json()) as any;
|
|
assert.equal(list.dependencies.length, 1, "and the page lists it with the rest");
|
|
});
|
|
});
|