import { test } from "node:test"; import assert from "node:assert/strict"; import { hostOf, integrationRow, integrationStatus } from "../../web/src/utils/integrationRows.js"; import { sortItems } from "../../web/src/hooks/useSortable.js"; import type { IntegrationSummary } from "../../web/src/api/client.js"; const make = (over: Partial = {}): IntegrationSummary => ({ id: 1, type: "proxmox", name: "pve", baseUrl: "https://pve.example.lan:8006/", enabled: true, createdAt: "2026-01-01T00:00:00Z", ...over, }); test("a disabled integration reads as disabled whatever its health says", () => { assert.equal(integrationStatus(make({ enabled: false, health: { status: "failing", consecutiveFailures: 4 } })).key, "disabled"); assert.equal(integrationStatus(make({ health: { status: "disabled", consecutiveFailures: 0 } })).key, "disabled"); }); test("failing says how many calls failed, in the singular too", () => { assert.equal(integrationStatus(make({ health: { status: "failing", consecutiveFailures: 1 } })).label, "Failing — last 1 call failed"); assert.equal(integrationStatus(make({ health: { status: "failing", consecutiveFailures: 3 } })).label, "Failing — last 3 calls failed"); }); test("working, and not checked yet when nothing has called it", () => { assert.equal(integrationStatus(make({ health: { status: "ok", consecutiveFailures: 0 } })).key, "working"); assert.equal(integrationStatus(make()).key, "unchecked"); assert.equal(integrationStatus(make({ health: { status: "unknown", consecutiveFailures: 0 } })).key, "unchecked"); }); test("the status ranks put what needs attention first", () => { const rank = (i: IntegrationSummary) => integrationStatus(i).rank; const failing = rank(make({ health: { status: "failing", consecutiveFailures: 2 } })); const unchecked = rank(make()); const working = rank(make({ health: { status: "ok", consecutiveFailures: 0 } })); const disabled = rank(make({ enabled: false })); assert.ok(failing < unchecked && unchecked < working && working < disabled); }); test("hostOf gives the host and port, and the text itself when it is not an address", () => { assert.equal(hostOf("https://pve.example.lan:8006/"), "pve.example.lan:8006"); assert.equal(hostOf("http://10.0.0.5"), "10.0.0.5"); assert.equal(hostOf("not a url"), "not a url"); assert.equal(hostOf(""), ""); }); test("a row carries the raw values the table sorts on", () => { const row = integrationRow(make({ type: "pbs", name: "backup", baseUrl: "" })); assert.equal(row.kind, "Proxmox Backup Server"); assert.equal(row.group, "Backups"); assert.equal(row.host, ""); assert.equal(row.name, "backup"); }); test("the table sorts by status with the failing ones first, and by name within the same status", () => { const rows = [ integrationRow(make({ id: 1, name: "b-ok", health: { status: "ok", consecutiveFailures: 0 } })), integrationRow(make({ id: 2, name: "a-ok", health: { status: "ok", consecutiveFailures: 0 } })), integrationRow(make({ id: 3, name: "c-down", health: { status: "failing", consecutiveFailures: 2 } })), integrationRow(make({ id: 4, name: "d-off", enabled: false })), ].sort((a, b) => a.name.localeCompare(b.name)); // arrives in name order, as the page gives it const sorted = sortItems(rows, "statusRank", "asc").map((r) => r.name); // sortItems is stable, so the name order survives inside each status assert.deepEqual(sorted, ["c-down", "a-ok", "b-ok", "d-off"]); assert.deepEqual(sortItems(rows, "kind", "asc").map((r) => r.id).length, 4); }); test("a kind nobody has placed in a group is listed under Other", () => { const row = integrationRow(make({ type: "nonexistent" as never })); assert.equal(row.group, "Other"); });