Lidarr/Readarr, integration inspector, app versions, grouped alerts
Lidarr and Readarr join the shared *arr reader. Integrations get an Inspect button that runs the reader once and shows what each request returned as shapes (never values) and where the result is empty. Operations -> App versions compares each application's version with its newest GitHub release (lookup off until an admin switches it on; optional alert). The Alerts page can group by source, kind of problem or type of alert. The shared HTTP helper no longer reuses connections (stale-socket resets). Migration 0078; docs and tests included. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
551eb1597a
commit
93aaf5476d
45 files changed
+10033
-65
No files matched your search
@@ -0,0 +1,211 @@
|
||||
/**
|
||||
* App versions: comparing what each integration last read with the newest release (kept from GitHub, only while switched on), the page's data and its switch, the optional alert and /metrics.
|
||||
*
|
||||
* 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-versions-"));
|
||||
process.env.DATABASE_PATH = join(dir, "test.sqlite");
|
||||
process.env.SESSION_DIR = join(dir, "sessions");
|
||||
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
|
||||
process.env.HEALTH_STARTUP_GRACE_MS = "0";
|
||||
|
||||
const express = (await import("express")).default;
|
||||
const { db } = await import("../src/db/client.js");
|
||||
const { users, integrations, arrState, grafanaState, adguardState, auditLog } = await import("../src/db/schema.js");
|
||||
const { runMigrations } = await import("../src/db/migrate.js");
|
||||
const av = await import("../src/services/appVersions.js");
|
||||
const { versionsRouter } = await import("../src/routes/versions.js");
|
||||
const { evaluateHealth, collectSnapshot } = await import("../src/services/healthMonitor.js");
|
||||
const { subjectOfConditionKey } = await import("../src/services/maintenance.js");
|
||||
const { getAlerts, invalidateAlertsCache } = await import("../src/services/alerts.js");
|
||||
const { buildMetrics } = await import("../src/services/metrics.js");
|
||||
const { getSettings, updateSettings } = await import("../src/services/settingsStore.js");
|
||||
const { eq } = await import("drizzle-orm");
|
||||
|
||||
await runMigrations();
|
||||
|
||||
const DAY = 86_400_000;
|
||||
const iso = (ms: number) => new Date(ms).toISOString();
|
||||
|
||||
describe("reading a version", () => {
|
||||
test("the first three numbers, after the prefixes projects put on theirs", () => {
|
||||
assert.deepEqual(av.parseVersion("v4.0.9.2244"), [4, 0, 9]);
|
||||
assert.deepEqual(av.parseVersion("4.0.9.2244"), [4, 0, 9]);
|
||||
assert.deepEqual(av.parseVersion("n8n@1.60.0"), [1, 60, 0]);
|
||||
assert.deepEqual(av.parseVersion("version/2024.8.3"), [2024, 8, 3]);
|
||||
assert.deepEqual(av.parseVersion("v0.107.52"), [0, 107, 52]);
|
||||
assert.deepEqual(av.parseVersion("2.12.0-rc1"), [2, 12, 0]);
|
||||
assert.deepEqual(av.parseVersion("v10"), [10, 0, 0]);
|
||||
for (const none of [null, undefined, "", "latest", "nightly"]) assert.equal(av.parseVersion(none), null, String(none));
|
||||
});
|
||||
|
||||
test("how far behind: the first number is major, the second minor, the third a patch; a build number is not a difference; newer than the newest is ahead", () => {
|
||||
assert.equal(av.lagOf("4.0.9.2244", "v4.0.9.2300"), "same");
|
||||
assert.equal(av.lagOf("4.0.9.2244", "v4.0.10.2300"), "patch");
|
||||
assert.equal(av.lagOf("11.2.0", "v11.3.1"), "minor");
|
||||
assert.equal(av.lagOf("10.9.11", "v11.0.0"), "major");
|
||||
assert.equal(av.lagOf("2024.8.3", "version/2025.2.0"), "major", "a new year of Authentik");
|
||||
assert.equal(av.lagOf("v0.107.52", "v0.107.51"), "ahead");
|
||||
assert.equal(av.lagOf("1.0.0", "v1.0.0"), "same");
|
||||
assert.equal(av.lagOf(null, "v1.0.0"), "unknown");
|
||||
assert.equal(av.lagOf("1.0.0", ""), "unknown");
|
||||
});
|
||||
});
|
||||
|
||||
describe("what is kept and shown", () => {
|
||||
const ids: Record<string, number> = {};
|
||||
const mk = async (type: string, name: string, enabled = true) => (await db.insert(integrations).values({ type: type as "sonarr", name, baseUrl: "http://x", enabled, config: JSON.stringify({}) }).returning({ id: integrations.id }))[0].id;
|
||||
const asked: string[] = [];
|
||||
let failing = new Set<string>();
|
||||
const published = (daysAgo: number) => iso(Date.now() - daysAgo * DAY);
|
||||
|
||||
before(async () => {
|
||||
ids.sonarr = await mk("sonarr", "TV");
|
||||
ids.sonarr2 = await mk("sonarr", "TV 4K");
|
||||
ids.grafana = await mk("grafana", "Dashboards");
|
||||
ids.adguard = await mk("adguard", "DNS");
|
||||
ids.radarr = await mk("radarr", "Films"); // never read: no version
|
||||
ids.proxmox = await mk("proxmox", "Cluster"); // no release to compare
|
||||
ids.off = await mk("grafana", "Old grafana", false);
|
||||
const at = new Date().toISOString();
|
||||
await db.insert(arrState).values([{ integrationId: ids.sonarr, version: "4.0.9.2244", health: "[]", updatedAt: at }, { integrationId: ids.sonarr2, version: "4.0.9.2244", health: "[]", updatedAt: at }]);
|
||||
await db.insert(grafanaState).values({ integrationId: ids.grafana, version: "11.2.0", updatedAt: at });
|
||||
await db.insert(adguardState).values({ integrationId: ids.adguard, version: "v0.107.52", updatedAt: at });
|
||||
av.setReleaseFetcherForTests(async (repo) => {
|
||||
asked.push(repo);
|
||||
if (failing.has(repo)) throw new Error("GitHub's limit on requests without a sign-in was reached — it is tried again later");
|
||||
const tags: Record<string, [string, number]> = { "Sonarr/Sonarr": ["v4.0.10.2400", 90], "grafana/grafana": ["v11.2.0", 200], "AdguardTeam/AdGuardHome": ["v0.107.57", 10] };
|
||||
const t = tags[repo];
|
||||
if (!t) throw new Error("GitHub lists no releases for it");
|
||||
return { tag: t[0], publishedAt: published(t[1]), url: `https://github.com/${repo}/releases/tag/${t[0]}` };
|
||||
});
|
||||
});
|
||||
after(() => av.setReleaseFetcherForTests(null));
|
||||
|
||||
test("nothing is listed that has no release to compare with, no version read, or is switched off; before any lookup the newest is not known", async () => {
|
||||
const rows = await av.versionRows();
|
||||
assert.deepEqual(rows.map((r) => [r.label, r.name, r.running, r.latest, r.lag]), [["AdGuard Home", "DNS", "v0.107.52", null, "unknown"], ["Grafana", "Dashboards", "11.2.0", null, "unknown"], ["Sonarr", "TV", "4.0.9.2244", null, "unknown"], ["Sonarr", "TV 4K", "4.0.9.2244", null, "unknown"]]);
|
||||
});
|
||||
|
||||
test("a lookup asks once for each repository — two Sonarrs are one question — and keeps what it learns", async () => {
|
||||
asked.length = 0;
|
||||
const r = await av.refreshReleases(new Date(), true);
|
||||
assert.deepEqual([r.checked, r.failed], [3, 0]);
|
||||
assert.deepEqual([...asked].sort(), ["AdguardTeam/AdGuardHome", "Sonarr/Sonarr", "grafana/grafana"]);
|
||||
const rows = await av.versionRows();
|
||||
const by = (n: string) => rows.find((x) => x.name === n)!;
|
||||
assert.deepEqual([by("TV").lag, by("TV").latest?.tag, by("TV").daysOut], ["patch", "v4.0.10.2400", 90]);
|
||||
assert.deepEqual([by("Dashboards").lag, by("Dashboards").daysOut], ["same", null]);
|
||||
assert.deepEqual([by("DNS").lag, by("DNS").daysOut], ["patch", 10], "0.107.52 against 0.107.57");
|
||||
});
|
||||
|
||||
test("a repository looked up lately is not asked again; with force it is", async () => {
|
||||
asked.length = 0;
|
||||
await av.refreshReleases(new Date());
|
||||
assert.deepEqual(asked, []);
|
||||
await av.refreshReleases(new Date(), true);
|
||||
assert.equal(asked.length, 3);
|
||||
});
|
||||
|
||||
test("a lookup that fails keeps the release it had and says why; a later pass tries it again at once", async () => {
|
||||
failing = new Set(["grafana/grafana"]);
|
||||
const r = await av.refreshReleases(new Date(), true);
|
||||
assert.deepEqual([r.checked, r.failed], [2, 1]);
|
||||
const g = (await av.versionRows()).find((x) => x.name === "Dashboards")!;
|
||||
assert.deepEqual([g.latest?.tag, g.lookupError], ["v11.2.0", "GitHub's limit on requests without a sign-in was reached — it is tried again later"]);
|
||||
failing = new Set();
|
||||
asked.length = 0;
|
||||
await av.refreshReleases(new Date());
|
||||
assert.deepEqual(asked, ["grafana/grafana"], "only the one that had failed");
|
||||
assert.equal((await av.versionRows()).find((x) => x.name === "Dashboards")!.lookupError, null);
|
||||
});
|
||||
|
||||
test("the alert: a newer release out for the number of days or more; not before; never at 0", async () => {
|
||||
const rows = await av.versionRows();
|
||||
assert.deepEqual(av.versionProblems(rows, 0), []);
|
||||
const p = av.versionProblems(rows, 60);
|
||||
assert.deepEqual(p.map((x) => x.key), [`version-behind:${ids.sonarr}`, `version-behind:${ids.sonarr2}`]);
|
||||
assert.equal(p[0].message, "TV: Sonarr 4.0.9.2244 is running; v4.0.10.2400 came out 90 days ago (a patch)");
|
||||
assert.deepEqual(av.versionProblems(rows, 91), []);
|
||||
assert.equal(av.versionProblems(rows, 5).length, 3, "the two Sonarrs and AdGuard Home (Grafana is up to date)");
|
||||
});
|
||||
|
||||
describe("the page, its switch, the health pass, the Alerts page and /metrics", () => {
|
||||
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: "viewer", name: "Vic", role: "viewer" }, { oidcSub: "admin", name: "Ada", role: "admin" }]);
|
||||
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/versions", versionsRouter);
|
||||
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/versions`;
|
||||
});
|
||||
after(() => {
|
||||
app.close();
|
||||
try {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
} catch {
|
||||
// Windows keeps the database file locked until the process exits.
|
||||
}
|
||||
});
|
||||
|
||||
test("it is off by default; everyone signed in sees the list, only an administrator switches it, and a refresh while it is off is refused", async () => {
|
||||
const off = (await (await call("viewer", "GET", "")).json()) as { enabled: boolean; alertAfterDays: number; rows: unknown[] };
|
||||
assert.deepEqual([off.enabled, off.alertAfterDays, off.rows.length], [false, 0, 4]);
|
||||
assert.equal((await call("viewer", "PUT", "/settings", { enabled: true, alertAfterDays: 0 })).status, 403);
|
||||
assert.equal((await call("admin", "POST", "/refresh")).status, 400);
|
||||
assert.equal((await call("viewer", "POST", "/refresh")).status, 403);
|
||||
});
|
||||
|
||||
test("it must be given the whole of what it asks; switching on looks the releases up at once, and is in the audit log", async () => {
|
||||
assert.equal((await call("admin", "PUT", "/settings", { enabled: true })).status, 400);
|
||||
assert.equal((await call("admin", "PUT", "/settings", { enabled: true, alertAfterDays: 999 })).status, 400);
|
||||
asked.length = 0;
|
||||
const on = (await (await call("admin", "PUT", "/settings", { enabled: true, alertAfterDays: 60 })).json()) as { enabled: boolean; alertAfterDays: number; lastCheckedAt: string | null; rows: { name: string; lag: string }[] };
|
||||
assert.deepEqual([on.enabled, on.alertAfterDays, on.lastCheckedAt !== null], [true, 60, true]);
|
||||
assert.equal(on.rows.find((r) => r.name === "TV")!.lag, "patch");
|
||||
assert.equal(asked.length, 3, "looked up on switching on");
|
||||
assert.equal((await db.select().from(auditLog)).filter((e) => e.targetType === "versionChecks").length, 1);
|
||||
});
|
||||
|
||||
test("with the lookup and a number set, the health pass raises it, the Alerts page puts it under Updates with a link, and maintenance on the integration holds it", async () => {
|
||||
const { snapshot } = await collectSnapshot();
|
||||
assert.deepEqual(snapshot.versions?.map((p) => p.key), [`version-behind:${ids.sonarr}`, `version-behind:${ids.sonarr2}`]);
|
||||
const list = evaluateHealth(snapshot, { serverOfflineMinutes: 60, diskUsagePercent: 90 }, Date.now(), { skipServers: true });
|
||||
assert.equal(list.find((x) => x.key === `version-behind:${ids.sonarr}`)!.source, "versions");
|
||||
assert.equal(subjectOfConditionKey(`version-behind:${ids.sonarr}`), `integration:${ids.sonarr}`);
|
||||
invalidateAlertsCache();
|
||||
const a = (await getAlerts(true)).alerts.find((x) => x.id.endsWith(`version-behind:${ids.sonarr}`))!;
|
||||
assert.deepEqual([a.category, a.source, a.link, a.target?.type, a.kind], ["updates", "Versions", "/versions", "integration", "version-behind"]);
|
||||
});
|
||||
|
||||
test("/metrics says which have a newer release — only with the lookup on — and with a number of 0 no alert is raised", async () => {
|
||||
const text = await buildMetrics(new Date());
|
||||
assert.match(text, new RegExp(`homelab_app_update_available\\{app="sonarr",name="TV",running="4\\.0\\.9\\.2244",latest="v4\\.0\\.10\\.2400"\\} 1`));
|
||||
assert.match(text, /homelab_app_update_available\{app="grafana",name="Dashboards",running="11\.2\.0",latest="v11\.2\.0"\} 0/);
|
||||
const checks = (await getSettings()).versionChecks;
|
||||
await updateSettings({ versionChecks: { ...checks, alertAfterDays: 0 } });
|
||||
assert.equal((await collectSnapshot()).snapshot.versions, undefined);
|
||||
await updateSettings({ versionChecks: { ...checks, enabled: false } });
|
||||
assert.doesNotMatch(await buildMetrics(new Date()), /homelab_app_update_available/);
|
||||
assert.equal((await collectSnapshot()).snapshot.versions, undefined);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user