/** * What the integration cards say about each integration: working, failing, not checked yet, or disabled. * * 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-int-status-")); 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, integrations } = await import("../src/db/schema.js"); const { runMigrations } = await import("../src/db/migrate.js"); const { integrationsRouter } = await import("../src/routes/integrations.js"); const { trackIntegrationHealth, getSourceStatus } = await import("../src/services/integrationHealthMonitor.js"); await runMigrations(); describe("integration health on the list", () => { let app: Server; let base = ""; before(async () => { await db.insert(users).values({ oidcSub: "tester", name: "Tester", role: "admin" }); await db.insert(integrations).values([ { type: "proxmox", name: "Felicia", baseUrl: "https://pve.lab:8006", config: "{}" }, { type: "gitea", name: "Gitea", baseUrl: "https://git.lab", config: "{}" }, { type: "synology", name: "NAS", baseUrl: "https://nas.lab", config: "{}" }, { type: "tailscale", name: "Off", baseUrl: "https://api.tailscale.com", config: "{}", enabled: false }, ]); 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/integrations", integrationsRouter); 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/integrations`; }); after(() => { app.close(); try { rmSync(dir, { recursive: true, force: true }); } catch { // Windows keeps the database file locked until the process exits. } }); test("a service nothing has called yet is unknown, not healthy", () => { assert.deepEqual(getSourceStatus("never-called"), { status: "unknown", consecutiveFailures: 0 }); }); test("failing, working, not checked yet and disabled are told apart", async () => { await trackIntegrationHealth("proxmox", false); await trackIntegrationHealth("proxmox", false); await trackIntegrationHealth("gitea", true); const list = (await (await fetch(base)).json()).integrations as { name: string; health: { status: string; consecutiveFailures: number } }[]; const by = (name: string) => list.find((i) => i.name === name)!.health; assert.deepEqual(by("Felicia"), { status: "failing", consecutiveFailures: 2 }); assert.deepEqual(by("Gitea"), { status: "ok", consecutiveFailures: 0 }); assert.equal(by("NAS").status, "unknown"); assert.equal(by("Off").status, "disabled"); }); test("a call that goes through ends the failing streak", async () => { await trackIntegrationHealth("proxmox", true); assert.deepEqual(getSourceStatus("proxmox"), { status: "ok", consecutiveFailures: 0 }); }); });