/** * Sonarr, Radarr and Prowlarr: reading what their API says (the same one for all three), what is stored, what is wrong (their own health findings, stuck downloads, a disk nearly full), the * health pass, the alerts, the page's data and /metrics. * * Run with: npm test --workspace server */ import { test, describe, before, after } from "node:test"; import assert from "node:assert/strict"; import { createServer as createHttpServer, 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-arr-")); 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 } = await import("../src/db/schema.js"); const { runMigrations } = await import("../src/db/migrate.js"); const adapter = await import("../src/integrations/arr/adapter.js"); const arr = await import("../src/services/arrSync.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 { arrRouter } = await import("../src/routes/arr.js"); const { buildMetrics } = await import("../src/services/metrics.js"); const { setInternalFlag } = await import("../src/services/settingsStore.js"); const { eq } = await import("drizzle-orm"); await runMigrations(); const MIN = 60_000; const HOUR = 60 * MIN; const GB = 1024 ** 3; const iso = (ms: number) => new Date(ms).toISOString(); type Item = import("../src/integrations/arr/adapter.js").ArrQueueItem; type Read = import("../src/integrations/arr/adapter.js").ArrRead; const item = (extra: Partial = {}): Item => ({ id: 1, title: "Show S01E01", status: "downloading", tracked: "ok", state: "downloading", messages: [], size: 1000, sizeLeft: 500, added: iso(Date.now() - 30 * MIN), etaAt: null, protocol: "torrent", client: "qBittorrent", indexer: "Tracker", ...extra }); const read = (extra: Partial = {}): Read => ({ version: "4.0.9", health: [], queue: { total: 0, items: [] }, disks: [{ path: "/tv", label: null, free: 500 * GB, total: 1000 * GB }], missing: 3, indexers: null, ...extra }); // What the applications send. const apiItem = (id: number, extra: Record = {}) => ({ id, title: `Show S01E0${id}`, status: "downloading", trackedDownloadStatus: "ok", trackedDownloadState: "downloading", statusMessages: [], size: 1000, sizeleft: 400, added: "2026-06-01T10:00:00Z", estimatedCompletionTime: "2026-06-01T12:00:00Z", protocol: "torrent", downloadClient: "qBittorrent", indexer: "Tracker", ...extra }); describe("what the *arr API says", () => { test("health: what each check found, with its type; an entry with no message is skipped", () => { const h = adapter.parseHealth([{ source: "DownloadClientCheck", type: "error", message: "No download client is available", wikiUrl: "x" }, { source: "UpdateCheck", type: "notice", message: "New update is available" }, { source: "Empty", type: "warning" }, "junk"]); assert.deepEqual(h.map((x) => [x.source, x.type, x.message]), [["DownloadClientCheck", "error", "No download client is available"], ["UpdateCheck", "notice", "New update is available"]]); assert.deepEqual(adapter.parseHealth({ message: "no" }), []); }); test("a queue item: its status and how the download is judged, what it says is wrong (the error first), size, times, client and indexer", () => { const q = adapter.parseQueueItem(apiItem(4, { status: "warning", trackedDownloadStatus: "warning", trackedDownloadState: "importBlocked", errorMessage: "Import failed", statusMessages: [{ title: "Show.S01E04.mkv", messages: ["Not an upgrade", "Not an upgrade"] }, { title: "No files found", messages: [] }] }))!; assert.deepEqual([q.id, q.status, q.tracked, q.state, q.size, q.sizeLeft, q.client, q.indexer, q.added, q.etaAt], [4, "warning", "warning", "importBlocked", 1000, 400, "qBittorrent", "Tracker", "2026-06-01T10:00:00.000Z", "2026-06-01T12:00:00.000Z"]); assert.deepEqual(q.messages, ["Import failed", "Show.S01E04.mkv: Not an upgrade", "No files found"]); }); test("an item without an id is skipped; a page is read with the total the application says", () => { assert.equal(adapter.parseQueueItem({ title: "x" }), null); const q = adapter.parseQueue({ page: 1, pageSize: 100, totalRecords: 250, records: [apiItem(1), { title: "no id" }, apiItem(2)] }); assert.deepEqual([q.total, q.items.map((i) => i.id)], [250, [1, 2]]); assert.deepEqual(adapter.parseQueue(null), { total: 0, items: [] }); }); test("disks: where, how much is free and how big", () => { const d = adapter.parseDisks([{ path: "/tv", label: "TV", freeSpace: 10, totalSpace: 100 }, { path: "/x" }, "junk"]); assert.deepEqual(d, [{ path: "/tv", label: "TV", free: 10, total: 100 }]); }); test("Prowlarr's indexers: the ones it has switched off after failures say until when", () => { const list = adapter.parseIndexers([{ id: 2, name: "B", enable: true, protocol: "torrent" }, { id: 1, name: "A", enable: false, protocol: "usenet" }, { id: 3, name: "C" }], [{ indexerId: 2, disabledTill: "2026-06-02T00:00:00Z", mostRecentFailure: "2026-06-01T00:00:00Z" }]); assert.deepEqual(list.map((i) => [i.name, i.enabled, i.disabledTill]), [["A", false, null], ["B", true, "2026-06-02T00:00:00.000Z"], ["C", true, null]]); }); describe("the adapter against applications that answer", () => { let fake: Server; let url = ""; let keyOk = true; let queueStatus = 200; let appName = "Sonarr"; const seen: { method: string | undefined; path: string; key: string | string[] | undefined }[] = []; before(async () => { fake = createHttpServer((req, res) => { const u = new URL(req.url ?? "", "http://x"); seen.push({ method: req.method, path: u.pathname + u.search, key: req.headers["x-api-key"] }); const send = (code: number, body: unknown) => { res.writeHead(code, { "content-type": "application/json" }); res.end(JSON.stringify(body)); }; if (!keyOk) return send(401, { error: "Unauthorized" }); const p = u.pathname.replace(/^\/api\/v[13]/, ""); if (p === "/system/status") return send(200, { appName, version: "4.0.9" }); if (p === "/health") return send(200, [{ source: "IndexerStatusCheck", type: "warning", message: "Indexers unavailable due to failures: A" }]); if (p === "/queue") return queueStatus === 200 ? send(200, { totalRecords: 1, records: [apiItem(1)] }) : send(queueStatus, {}); if (p === "/diskspace") return send(200, [{ path: "/tv", freeSpace: 100, totalSpace: 1000 }]); if (p === "/wanted/missing") return send(200, { totalRecords: 7, records: [] }); if (p === "/indexer") return send(200, [{ id: 1, name: "A", enable: true, protocol: "torrent" }]); if (p === "/indexerstatus") return send(200, [{ indexerId: 1, disabledTill: "2026-06-02T00:00:00Z" }]); send(404, {}); }); await new Promise((resolve) => fake.listen(0, "127.0.0.1", resolve)); url = `http://127.0.0.1:${(fake.address() as AddressInfo).port}`; }); after(() => fake.close()); test("Sonarr: version, health, the queue (asked with its own include), the disks and the missing — with the key in X-Api-Key, and only ever GET", async () => { seen.length = 0; const r = await adapter.createArrAdapter("sonarr", { url, apiKey: "k1" }).read(); assert.deepEqual([r.version, r.health.length, r.queue?.total, r.disks?.length, r.missing, r.indexers], ["4.0.9", 1, 1, 1, 7, null]); assert.ok(seen.every((s) => s.method === "GET" && s.key === "k1")); assert.ok(seen.some((s) => s.path.startsWith("/api/v3/queue?page=1&pageSize=100&includeUnknownSeriesItems=true"))); }); test("Radarr asks for movies; Prowlarr is under v1, has no queue, and gives its indexers", async () => { seen.length = 0; await adapter.createArrAdapter("radarr", { url, apiKey: "k" }).read(); assert.ok(seen.some((s) => s.path.includes("includeUnknownMovieItems=true"))); seen.length = 0; const r = await adapter.createArrAdapter("prowlarr", { url, apiKey: "k" }).read(); assert.deepEqual([r.queue, r.missing, r.indexers?.[0].name, r.indexers?.[0].disabledTill], [null, null, "A", "2026-06-02T00:00:00.000Z"]); assert.ok(seen.every((s) => s.path.startsWith("/api/v1/"))); assert.ok(!seen.some((s) => s.path.includes("/queue"))); }); test("a part that cannot be read is null, not empty; the status and the health are what must work", async () => { queueStatus = 500; const r = await adapter.createArrAdapter("sonarr", { url, apiKey: "k" }).read(); assert.deepEqual([r.queue, r.disks?.length], [null, 1]); queueStatus = 200; }); test("a key that is refused, an address that is not the application, and the wrong kind are said", async () => { keyOk = false; const ping = await adapter.createArrAdapter("sonarr", { url, apiKey: "bad" }).ping(); assert.equal(ping.ok, false); assert.match(ping.error ?? "", /Sonarr did not accept the API key \(HTTP 401\)/); keyOk = true; appName = "Radarr"; assert.match((await adapter.createArrAdapter("sonarr", { url, apiKey: "k" }).ping()).error ?? "", /That is Radarr, not Sonarr/); appName = "Sonarr"; assert.equal((await adapter.createArrAdapter("sonarr", { url, apiKey: "k" }).ping()).ok, true); assert.equal((await adapter.createArrAdapter("sonarr", { url: "http://127.0.0.1:1", apiKey: "k" }).ping()).ok, false); await assert.rejects(() => adapter.createArrAdapter("sonarr", { url: `${url}/nowhere`, apiKey: "k" }).read(), /no API at this address/); }); }); }); describe("what is wrong", () => { const NOW = Date.parse("2026-06-10T12:00:00Z"); const state = (extra: Partial = {}) => ({ version: "4", health: [], queue: { total: 0, items: [] }, disks: null, missing: 0, indexers: null, updatedAt: iso(NOW), ...extra }); const keys = (list: ReturnType) => list.map((p) => `${p.key}=${p.severity}`); const old = (extra: Partial = {}) => item({ added: iso(NOW - 3 * HOUR), ...extra }); test("an application that is well says nothing", () => { assert.deepEqual(arr.arrProblems("sonarr", 1, "TV", state({ queue: { total: 1, items: [item({ added: iso(NOW - 10 * MIN) })] }, disks: [{ path: "/tv", label: null, free: 500 * GB, total: 1000 * GB }] }), NOW), []); }); describe("what it says itself", () => { test("a warning or an error is raised, a notice is not; the message says who says it", () => { const p = arr.arrProblems("sonarr", 1, "TV", state({ health: [{ source: "DownloadClientCheck", type: "error", message: "No download client is available" }, { source: "UpdateCheck", type: "notice", message: "New update available" }, { source: "RootFolderCheck", type: "warning", message: "Missing root folder: /tv " }] }), NOW); assert.equal(p.length, 2); assert.match(p[0].message, /^TV: Sonarr says no download client is available$/); assert.match(p[1].message, /Sonarr says missing root folder: \/tv$/); assert.ok(p.every((x) => x.severity === "warning" && x.key.startsWith("sonarr-health:1:"))); }); test("a finding keeps its key when only its numbers change, and has another when its words do", () => { const key = (m: string) => arr.arrProblems("prowlarr", 3, "P", state({ health: [{ source: "IndexerStatusCheck", type: "warning", message: m }] }), NOW)[0].key; assert.equal(key("3 indexers are unavailable"), key("4 indexers are unavailable")); assert.notEqual(key("3 indexers are unavailable"), key("Download client missing")); assert.match(key("x"), /^prowlarr-health:3:indexerstatuscheck-/); }); }); describe("downloads that are stuck", () => { test("judged wrong for two hours is stuck; younger is left alone; one judged fine is not stuck however old", () => { assert.equal(arr.isStuck(old({ tracked: "warning" }), NOW), true); assert.equal(arr.isStuck(item({ tracked: "warning", added: iso(NOW - 30 * MIN) }), NOW), false); assert.equal(arr.isStuck(old({ tracked: "ok", state: "downloading" }), NOW), false); }); test("failed, failing and blocked from importing count, whatever the tracked status says", () => { for (const state of ["failed", "failedPending", "importBlocked"]) assert.equal(arr.isStuck(old({ tracked: "ok", state }), NOW), true, state); assert.equal(arr.isStuck(old({ tracked: null, state: null, status: "failed" }), NOW), true); assert.equal(arr.isStuck(old({ tracked: "error", added: null }), NOW), false, "one with no time cannot be called old"); }); test("one alert for the lot: how many, the first three by name, the oldest's age and why", () => { const items = [old({ id: 1, title: "A", tracked: "error", messages: ["No files found"], added: iso(NOW - 30 * HOUR) }), old({ id: 2, title: "B", tracked: "warning" }), old({ id: 3, title: "C", state: "failed" }), old({ id: 4, title: "D", tracked: "warning" }), item({ id: 5, title: "fine" })]; const p = arr.arrProblems("radarr", 2, "Films", state({ queue: { total: 5, items } }), NOW); assert.deepEqual(keys(p), ["radarr-queue:2=warning"]); assert.equal(p[0].message, "Films: 4 downloads are stuck — A; B; C; and 1 more (the oldest for 30 hours) — No files found"); assert.match(arr.arrProblems("radarr", 2, "Films", state({ queue: { total: 1, items: [items[0]] } }), NOW)[0].message, /^Films: 1 download is stuck — A \(the oldest for 30 hours\)/); }); test("a queue that could not be read says nothing; Prowlarr has none", () => { assert.deepEqual(arr.arrProblems("sonarr", 1, "TV", state({ queue: null }), NOW), []); }); }); describe("disks", () => { const disk = (path: string, free: number, total = 1000) => ({ path, label: null, free: free * GB, total: total * GB }); test("under 10% free is a warning, under 5% critical; the message says how much", () => { assert.deepEqual(arr.arrProblems("sonarr", 1, "TV", state({ disks: [disk("/tv", 150)] }), NOW), []); const warn = arr.arrProblems("sonarr", 1, "TV", state({ disks: [disk("/tv", 80)] }), NOW); assert.deepEqual(keys(warn), ["sonarr-disk:1:tv=warning"]); assert.equal(warn[0].message, "TV: /tv has 80.0 GB free of 1000 GB (8%)"); assert.deepEqual(keys(arr.arrProblems("sonarr", 1, "TV", state({ disks: [disk("/tv", 30)] }), NOW)), ["sonarr-disk:1:tv=critical"]); }); test("folders on one disk are one disk (the shortest path names it); a tiny disk is not a media disk; an unread list says nothing", () => { const d = arr.distinctDisks([disk("/data/tv/anime", 30), disk("/data", 30), disk("/data/tv", 30), disk("/boot", 0.1, 1), disk("/movies", 600)]); assert.deepEqual(d.map((x) => x.path), ["/data", "/movies"]); assert.deepEqual(keys(arr.arrProblems("sonarr", 1, "TV", state({ disks: [disk("/data/tv", 30), disk("/data", 30)] }), NOW)), ["sonarr-disk:1:data=critical"]); assert.deepEqual(arr.arrProblems("sonarr", 1, "TV", state({ disks: null }), NOW), []); }); }); }); describe("what is stored", () => { let id = 0; before(async () => { id = (await db.insert(integrations).values({ type: "sonarr", name: "TV", baseUrl: "http://sonarr", config: JSON.stringify({}) }).returning({ id: integrations.id }))[0].id; }); const row = async () => (await db.select().from(arrState).where(eq(arrState.integrationId, id)))[0]; after(async () => { await arr.forgetArr(id); await db.delete(integrations).where(eq(integrations.id, id)); }); test("the state is replaced at each read; a part that could not be read is stored as null", async () => { await arr.storeRead(id, read({ queue: null, disks: null }), new Date()); let s = arr.storedOf(await row()); assert.deepEqual([s.queue, s.disks, s.missing, s.health], [null, null, 3, []]); await arr.storeRead(id, read({ queue: { total: 1, items: [item()] }, health: [{ source: "x", type: "warning", message: "m" }] }), new Date()); s = arr.storedOf(await row()); assert.deepEqual([s.queue?.total, s.health.length], [1, 1]); }); test("a read that failed is said, and what was stored is left alone", async () => { const ok = await arr.syncArr(id, new Date(), async () => read({ version: "kept" })); assert.equal(ok.ok, true); const failed = await arr.syncArr(id, new Date(), async () => { throw new Error("connection refused"); }); assert.deepEqual([failed.ok, failed.error], [false, "connection refused"]); assert.equal(failed.at, ok.at); assert.equal(arr.storedOf(await row()).version, "kept"); assert.equal(arr.isArrFresh((await arr.arrSyncStatuses())[id], Date.now()), false); }); test("the kind is what the read is asked for", async () => { const asked: string[] = []; await arr.syncArr(id, new Date(), async (kind) => { asked.push(kind); return read(); }); assert.deepEqual(asked, ["sonarr"]); }); }); describe("the health pass, the page and /metrics", () => { let tvId = 0; let pId = 0; let app: Server; let base = ""; const fresh = () => setInternalFlag("arrSync", JSON.stringify({ [tvId]: { at: new Date().toISOString(), ok: true, error: null }, [pId]: { at: new Date().toISOString(), ok: true, error: null } })); const stale = () => setInternalFlag("arrSync", JSON.stringify({ [tvId]: { at: new Date(Date.now() - 30 * MIN).toISOString(), ok: true, error: null }, [pId]: { at: new Date().toISOString(), ok: true, error: null } })); before(async () => { await db.insert(users).values([{ oidcSub: "viewer", name: "Vic", role: "viewer" }]); const mk = async (type: "sonarr" | "prowlarr", name: string) => (await db.insert(integrations).values({ type, name, baseUrl: `http://${name}`, config: JSON.stringify({}) }).returning({ id: integrations.id }))[0].id; tvId = await mk("sonarr", "TV"); pId = await mk("prowlarr", "Indexers"); const now = Date.now(); await arr.storeRead( tvId, read({ health: [{ source: "DownloadClientCheck", type: "error", message: "No download client is available" }], queue: { total: 3, items: [item({ id: 1, tracked: "error", added: iso(now - 5 * HOUR), messages: ["No files found"] }), item({ id: 2 }), item({ id: 3, state: "importBlocked", added: iso(now - 4 * HOUR) })] }, disks: [{ path: "/tv", label: null, free: 20 * GB, total: 1000 * GB }], }), new Date(), ); await arr.storeRead(pId, read({ queue: null, missing: null, disks: null, indexers: [{ id: 1, name: "A", enabled: true, protocol: "torrent", disabledTill: iso(now + HOUR), lastFailure: iso(now) }], health: [{ source: "IndexerStatusCheck", type: "warning", message: "Indexers unavailable due to failures: A" }] }), new Date()); await fresh(); const a = express(); a.use((req, _res, next) => { (req as unknown as { session: unknown }).session = { user: { sub: "viewer" } }; next(); }); a.use("/api/arr", arrRouter); 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/arr`; }); after(() => { app.close(); try { rmSync(dir, { recursive: true, force: true }); } catch { // Windows keeps the database file locked until the process exits. } }); test("the health pass judges each from what is stored, and holds one whose last read is old", async () => { let { snapshot, held } = await collectSnapshot(); assert.deepEqual(snapshot.arr?.map((a) => [a.kind, a.integrationId]).sort(), [["prowlarr", pId], ["sonarr", tvId]].sort()); const list = evaluateHealth(snapshot, { serverOfflineMinutes: 60, diskUsagePercent: 90 }, Date.now(), { skipServers: true }); const mine = list.filter((x) => /^(sonarr|prowlarr)-/.test(x.key)); assert.deepEqual(mine.map((x) => x.key.replace(/^([a-z]+-[a-z]+:[0-9]+).*$/, "$1")).sort(), [`prowlarr-health:${pId}`, `sonarr-disk:${tvId}`, `sonarr-health:${tvId}`, `sonarr-queue:${tvId}`].sort()); const disk = mine.find((x) => x.key === `sonarr-disk:${tvId}:tv`)!; assert.deepEqual([disk.severity, disk.source], ["critical", `sonarr:${tvId}`]); for (const k of ["health", "queue", "disk"]) assert.equal(subjectOfConditionKey(`sonarr-${k}:${tvId}:x`), `integration:${tvId}`); await stale(); ({ snapshot, held } = await collectSnapshot()); assert.deepEqual([snapshot.arr?.map((a) => a.integrationId), held.has(`sonarr:${tvId}`)], [[pId], true]); await fresh(); }); test("the Alerts page says where to look: the disk under Disk, the rest under Services, and the link is the media page", async () => { invalidateAlertsCache(); const report = await getAlerts(true); const q = report.alerts.find((x) => x.id.endsWith(`sonarr-queue:${tvId}`))!; const d = report.alerts.find((x) => x.id.endsWith(`sonarr-disk:${tvId}:tv`))!; assert.deepEqual([q.category, q.source, q.link, q.target?.type, d.category], ["services", "Sonarr", "/media", "integration", "disk"]); assert.match(q.message ?? "", /2 downloads are stuck/); }); test("the page: each application with its kind, what it says, its queue (the stuck ones marked), its disks and, for Prowlarr, its indexers", async () => { const body = (await (await fetch(base)).json()) as { apps: { id: number; kind: string; fresh: boolean; problems: { key: string }[]; state: { stuck: number[]; queue: { total: number } | null; disks: { path: string }[]; indexers: { name: string }[] | null; health: unknown[] } }[] }; const tv = body.apps.find((a) => a.id === tvId)!; assert.deepEqual([tv.kind, tv.fresh, tv.state.queue?.total, tv.state.stuck.sort(), tv.state.disks.map((d) => d.path), tv.state.health.length], ["sonarr", true, 3, [1, 3], ["/tv"], 1]); assert.equal(tv.problems.length, 3); const p = body.apps.find((a) => a.id === pId)!; assert.deepEqual([p.kind, p.state.queue, p.state.indexers?.map((i) => i.name), p.problems.length], ["prowlarr", null, ["A"], 1]); await stale(); const old = (await (await fetch(base)).json()) as typeof body; assert.deepEqual(old.apps.find((a) => a.id === tvId)!.problems, [], "an old reading says nothing is wrong"); await fresh(); }); test("/metrics has the health findings, the queue and what is stuck, the missing and each disk's free space — only while the reading is recent", async () => { let text = await buildMetrics(new Date()); assert.match(text, /homelab_arr_health_issues\{app="sonarr",name="TV"\} 1/); assert.match(text, /homelab_arr_queue_items\{app="sonarr",name="TV"\} 3/); assert.match(text, /homelab_arr_queue_stuck_items\{app="sonarr",name="TV"\} 2/); assert.match(text, /homelab_arr_wanted_missing\{app="sonarr",name="TV"\} 3/); assert.match(text, /homelab_arr_disk_free_bytes\{app="sonarr",name="TV",path="\/tv"\} 21474836480/); assert.doesNotMatch(text, /homelab_arr_queue_items\{app="prowlarr"/, "Prowlarr has no queue"); await stale(); text = await buildMetrics(new Date()); assert.doesNotMatch(text, /homelab_arr_[a-z_]+\{app="sonarr"/); await fresh(); }); test("taking the integration away takes what was read with it", async () => { await arr.forgetArr(tvId); assert.equal((await arr.arrSyncStatuses())[tvId], undefined); await db.delete(integrations).where(eq(integrations.id, tvId)); assert.deepEqual(await db.select().from(arrState).where(eq(arrState.integrationId, tvId)), []); }); });