Add Read now to every integration

POST /api/integrations/:id/read-now reads one integration at once (operators and administrators; not again within 20 s): a kind with a stored reading is read and judged again, a kind read live is asked whether it answers. The three per-kind lists in the integrations routes (add, change, delete) are now one map in services/integrationSyncs.ts. Button on the Integrations cards and the Media managers, Download clients and Hetzner Robot pages.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-11-04 23:40:31 +01:00
1 parent 01acbaf831
commit b29c0b7d04
13 files changed
+434 -128

No files matched your search

+17 -86
View File
@@ -27,30 +27,9 @@ import { createPbsAdapter } from "../integrations/pbs/adapter.js";
import { createOsTicketAdapter } from "../integrations/osticket/adapter.js";
import { attachMatchedServers, summarizeMonitors, toMatchableServers } from "../services/uptimeKumaMatch.js";
import { getSourceStatus } from "../services/integrationHealthMonitor.js";
import { removeIntegrationData, syncUnifiIntegration } from "../services/unifiSync.js";
import { removeHomeAssistantData, syncHomeAssistant } from "../services/homeAssistantSync.js";
import { forgetUps, syncUps } from "../services/upsSync.js";
import { forgetJellyfin, syncJellyfin } from "../services/jellyfinSync.js";
import { forgetAuthentik, syncAuthentik } from "../services/authentikSync.js";
import { forgetPrometheus, syncPrometheus } from "../services/prometheusSync.js";
import { forgetImmich, syncImmich } from "../services/immichSync.js";
import { forgetPaperless, syncPaperless } from "../services/paperlessSync.js";
import { forgetN8n, syncN8n } from "../services/n8nSync.js";
import { forgetNetBox, syncNetBox } from "../services/netboxSync.js";
import { forgetHetzner, syncHetzner } from "../services/hetznerSync.js";
import { forgetSpeedtest, syncSpeedtest } from "../services/speedtestSync.js";
import { forgetFragnet, syncFragnet } from "../services/fragnetSync.js";
import { forgetArr, syncArr } from "../services/arrSync.js";
import { ARR_KINDS } from "../integrations/arr/adapter.js";
import { forgetAdguard, syncAdguard } from "../services/adguardSync.js";
import { forgetDownload, syncDownload } from "../services/downloadsSync.js";
import { DOWNLOAD_KINDS } from "../integrations/downloads/adapter.js";
import { forgetRobot, syncRobot } from "../services/hetznerRobotSync.js";
import { forgetTechnitium, syncTechnitium } from "../services/technitiumSync.js";
import { forgetGrafana, syncGrafana } from "../services/grafanaSync.js";
import { forgetNextcloud, syncNextcloud } from "../services/nextcloudSync.js";
import { dockerVulnSyncStatuses, findingsOf, forgetDockerVulns, imageAgeDays, imagesOf, isDockerVulnFresh, syncDockerVulns } from "../services/dockerVulnSync.js";
import { dockerVulnSyncStatuses, findingsOf, imageAgeDays, imagesOf, isDockerVulnFresh } from "../services/dockerVulnSync.js";
import { invalidateAlertsCache } from "../services/alerts.js";
import { forgetReading, readAfterChange, readIntegrationNow } from "../services/integrationSyncs.js";
import { getSettings, updateSettings } from "../services/settingsStore.js";
import { asyncHandler } from "../utils/asyncHandler.js";
import { ID_PATTERNS, isValidId } from "../utils/pathSegment.js";
@@ -149,27 +128,7 @@ integrationsRouter.post("/", requireRole("admin"), asyncHandler(async (req, res)
});
// A new UniFi controller is read at once, so its equipment is there when the person opens the page (not waited for: a slow controller must not slow this).
if (type === "unifi") void syncUnifiIntegration(created.id).then(() => invalidateAlertsCache());
if (type === "homeassistant") void syncHomeAssistant(created.id).then(() => invalidateAlertsCache());
if (type === "ups") void syncUps(created.id).then(() => invalidateAlertsCache());
if (type === "jellyfin") void syncJellyfin(created.id).then(() => invalidateAlertsCache());
if (type === "authentik") void syncAuthentik(created.id).then(() => invalidateAlertsCache());
if (type === "prometheus") void syncPrometheus(created.id).then(() => invalidateAlertsCache());
if (type === "immich") void syncImmich(created.id).then(() => invalidateAlertsCache());
if (type === "paperless") void syncPaperless(created.id).then(() => invalidateAlertsCache());
if (type === "n8n") void syncN8n(created.id).then(() => invalidateAlertsCache());
if (type === "netbox") void syncNetBox(created.id).then(() => invalidateAlertsCache());
if (type === "hetzner") void syncHetzner(created.id).then(() => invalidateAlertsCache());
if (type === "speedtest") void syncSpeedtest(created.id).then(() => invalidateAlertsCache());
if (type === "fragnet") void syncFragnet(created.id).then(() => invalidateAlertsCache());
if ((ARR_KINDS as string[]).includes(type)) void syncArr(created.id).then(() => invalidateAlertsCache());
if ((DOWNLOAD_KINDS as string[]).includes(type)) void syncDownload(created.id).then(() => invalidateAlertsCache());
if (type === "hetznerrobot") void syncRobot(created.id).then(() => invalidateAlertsCache());
if (type === "adguard") void syncAdguard(created.id).then(() => invalidateAlertsCache());
if (type === "technitium") void syncTechnitium(created.id).then(() => invalidateAlertsCache());
if (type === "grafana") void syncGrafana(created.id).then(() => invalidateAlertsCache());
if (type === "nextcloud") void syncNextcloud(created.id).then(() => invalidateAlertsCache());
if (type === "dockhand") void syncDockerVulns(created.id).then(() => invalidateAlertsCache());
readAfterChange(type, created.id);
res.status(201).json({
integration: {
@@ -270,27 +229,7 @@ integrationsRouter.patch("/:id", requireRole("admin"), asyncHandler(async (req,
},
});
if (updated.type === "unifi" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncUnifiIntegration(updated.id).then(() => invalidateAlertsCache());
if (updated.type === "homeassistant" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncHomeAssistant(updated.id).then(() => invalidateAlertsCache());
if (updated.type === "ups" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncUps(updated.id).then(() => invalidateAlertsCache());
if (updated.type === "jellyfin" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncJellyfin(updated.id).then(() => invalidateAlertsCache());
if (updated.type === "authentik" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncAuthentik(updated.id).then(() => invalidateAlertsCache());
if (updated.type === "prometheus" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncPrometheus(updated.id).then(() => invalidateAlertsCache());
if (updated.type === "immich" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncImmich(updated.id).then(() => invalidateAlertsCache());
if (updated.type === "paperless" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncPaperless(updated.id).then(() => invalidateAlertsCache());
if (updated.type === "n8n" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncN8n(updated.id).then(() => invalidateAlertsCache());
if (updated.type === "netbox" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncNetBox(updated.id).then(() => invalidateAlertsCache());
if (updated.type === "hetzner" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncHetzner(updated.id).then(() => invalidateAlertsCache());
if (updated.type === "speedtest" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncSpeedtest(updated.id).then(() => invalidateAlertsCache());
if (updated.type === "fragnet" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncFragnet(updated.id).then(() => invalidateAlertsCache());
if ((ARR_KINDS as string[]).includes(updated.type) && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncArr(updated.id).then(() => invalidateAlertsCache());
if ((DOWNLOAD_KINDS as string[]).includes(updated.type) && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncDownload(updated.id).then(() => invalidateAlertsCache());
if (updated.type === "hetznerrobot" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncRobot(updated.id).then(() => invalidateAlertsCache());
if (updated.type === "adguard" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncAdguard(updated.id).then(() => invalidateAlertsCache());
if (updated.type === "technitium" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncTechnitium(updated.id).then(() => invalidateAlertsCache());
if (updated.type === "grafana" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncGrafana(updated.id).then(() => invalidateAlertsCache());
if (updated.type === "nextcloud" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncNextcloud(updated.id).then(() => invalidateAlertsCache());
if (updated.type === "dockhand" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncDockerVulns(updated.id).then(() => invalidateAlertsCache());
if (updated.enabled && (parsed.data.config || parsed.data.enabled === true)) readAfterChange(updated.type, updated.id);
res.json({
integration: {
@@ -352,6 +291,18 @@ integrationsRouter.post("/:id/test", requireRole("admin"), asyncHandler(async (r
res.json(result);
}));
// Reads one integration now, instead of waiting for its next turn: a kind with a stored reading is read the way its schedule reads it (and what it says is judged again at once); a kind that is read live
// is asked whether it answers. Operators and administrators — it only reads, but some systems limit how often they may be asked, so the same integration is not read again within 20 seconds.
integrationsRouter.post("/:id/read-now", requireRole("operator"), asyncHandler(async (req, res) => {
const id = Number(req.params.id);
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
const r = await readIntegrationNow(id);
if (r.state === "not_found") return res.status(404).json({ error: "not_found" });
if (r.state === "disabled") return res.status(400).json({ error: "integration_disabled", message: "It is switched off." });
if (r.state === "too_soon") return res.status(429).set("Retry-After", String(r.retryAfterSeconds)).json({ error: "too_soon", message: `It was read a moment ago — try again in ${r.retryAfterSeconds} s.`, retryAfterSeconds: r.retryAfterSeconds });
res.json({ kind: r.kind, ok: r.ok, error: r.error, at: r.at, latencyMs: r.latencyMs });
}));
/** How long an inspection may take: a read makes a handful of requests, each with a timeout of its own. */
const INSPECT_LIMIT_MS = 90_000;
@@ -398,27 +349,7 @@ integrationsRouter.delete("/:id", requireRole("admin"), asyncHandler(async (req,
.where(eq(servers.proxmoxIntegrationId, id))
.returning({ id: servers.id });
if (existing.type === "unifi") await removeIntegrationData(id);
if (existing.type === "homeassistant") await removeHomeAssistantData(id);
if (existing.type === "ups") await forgetUps(id);
if (existing.type === "jellyfin") await forgetJellyfin(id);
if (existing.type === "authentik") await forgetAuthentik(id);
if (existing.type === "prometheus") await forgetPrometheus(id);
if (existing.type === "immich") await forgetImmich(id);
if (existing.type === "paperless") await forgetPaperless(id);
if (existing.type === "n8n") await forgetN8n(id);
if (existing.type === "netbox") await forgetNetBox(id);
if (existing.type === "hetzner") await forgetHetzner(id);
if (existing.type === "speedtest") await forgetSpeedtest(id);
if (existing.type === "fragnet") await forgetFragnet(id);
if ((ARR_KINDS as string[]).includes(existing.type)) await forgetArr(id);
if ((DOWNLOAD_KINDS as string[]).includes(existing.type)) await forgetDownload(id);
if (existing.type === "hetznerrobot") await forgetRobot(id);
if (existing.type === "adguard") await forgetAdguard(id);
if (existing.type === "technitium") await forgetTechnitium(id);
if (existing.type === "grafana") await forgetGrafana(id);
if (existing.type === "nextcloud") await forgetNextcloud(id);
if (existing.type === "dockhand") await forgetDockerVulns(id);
await forgetReading(existing.type, id);
await db.delete(integrations).where(eq(integrations.id, id));
if (existing.credentialId) {
await db.delete(integrationCredentials).where(eq(integrationCredentials.id, existing.credentialId));
+120
View File
@@ -0,0 +1,120 @@
/**
* What each kind of integration has that is read on a schedule and stored (a "sync") — in one place, so that adding an integration, changing one, taking one away and asking for it to be read *now* all
* agree on which kinds have one. A kind that is not here (Proxmox, Synology, Tailscale, Gitea, Semaphore, Uptime Kuma, Proxmox Backup Server, osTicket, phpIPAM, Nginx Proxy Manager) has nothing stored:
* its pages and the health pass read it live, every time.
*/
import type { IntegrationType } from "../db/schema.js";
import { loadIntegrationConfig } from "../integrations/loadIntegration.js";
import { createIntegrationAdapter } from "../integrations/registry.js";
import { ARR_KINDS } from "../integrations/arr/adapter.js";
import { DOWNLOAD_KINDS } from "../integrations/downloads/adapter.js";
import { invalidateAlertsCache } from "./alerts.js";
import { forgetAdguard, syncAdguard } from "./adguardSync.js";
import { forgetArr, syncArr } from "./arrSync.js";
import { forgetAuthentik, syncAuthentik } from "./authentikSync.js";
import { forgetDockerVulns, syncDockerVulns } from "./dockerVulnSync.js";
import { forgetDownload, syncDownload } from "./downloadsSync.js";
import { forgetFragnet, syncFragnet } from "./fragnetSync.js";
import { forgetGrafana, syncGrafana } from "./grafanaSync.js";
import { forgetHetzner, syncHetzner } from "./hetznerSync.js";
import { forgetRobot, syncRobot } from "./hetznerRobotSync.js";
import { removeHomeAssistantData, syncHomeAssistant } from "./homeAssistantSync.js";
import { forgetImmich, syncImmich } from "./immichSync.js";
import { forgetJellyfin, syncJellyfin } from "./jellyfinSync.js";
import { forgetN8n, syncN8n } from "./n8nSync.js";
import { forgetNetBox, syncNetBox } from "./netboxSync.js";
import { forgetNextcloud, syncNextcloud } from "./nextcloudSync.js";
import { forgetPaperless, syncPaperless } from "./paperlessSync.js";
import { forgetPrometheus, syncPrometheus } from "./prometheusSync.js";
import { forgetSpeedtest, syncSpeedtest } from "./speedtestSync.js";
import { forgetTechnitium, syncTechnitium } from "./technitiumSync.js";
import { removeIntegrationData, syncUnifiIntegration } from "./unifiSync.js";
import { forgetUps, syncUps } from "./upsSync.js";
/** How a read went, as every sync says it (some say more, which is not needed here). */
export interface ReadStatus {
at?: string;
ok: boolean;
error: string | null;
}
interface Entry {
sync: (id: number) => Promise<ReadStatus>;
forget: (id: number) => Promise<void>;
}
const BY_TYPE: Partial<Record<IntegrationType, Entry>> = {
unifi: { sync: syncUnifiIntegration, forget: removeIntegrationData },
homeassistant: { sync: syncHomeAssistant, forget: removeHomeAssistantData },
ups: { sync: syncUps, forget: forgetUps },
jellyfin: { sync: syncJellyfin, forget: forgetJellyfin },
authentik: { sync: syncAuthentik, forget: forgetAuthentik },
prometheus: { sync: syncPrometheus, forget: forgetPrometheus },
immich: { sync: syncImmich, forget: forgetImmich },
paperless: { sync: syncPaperless, forget: forgetPaperless },
n8n: { sync: syncN8n, forget: forgetN8n },
netbox: { sync: syncNetBox, forget: forgetNetBox },
hetzner: { sync: syncHetzner, forget: forgetHetzner },
hetznerrobot: { sync: syncRobot, forget: forgetRobot },
speedtest: { sync: syncSpeedtest, forget: forgetSpeedtest },
fragnet: { sync: syncFragnet, forget: forgetFragnet },
adguard: { sync: syncAdguard, forget: forgetAdguard },
technitium: { sync: syncTechnitium, forget: forgetTechnitium },
grafana: { sync: syncGrafana, forget: forgetGrafana },
nextcloud: { sync: syncNextcloud, forget: forgetNextcloud },
dockhand: { sync: (id) => syncDockerVulns(id), forget: forgetDockerVulns },
};
for (const k of ARR_KINDS) BY_TYPE[k] = { sync: (id) => syncArr(id), forget: forgetArr };
for (const k of DOWNLOAD_KINDS) BY_TYPE[k] = { sync: (id) => syncDownload(id), forget: forgetDownload };
/** Whether this kind of integration has a reading that is stored. */
export const hasStoredReading = (type: IntegrationType): boolean => BY_TYPE[type] !== undefined;
/** A new integration, or one whose settings changed or that was switched on, is read at once (not waited for: a slow system must not slow the request), so what it shows and says is there when the page is opened. */
export function readAfterChange(type: IntegrationType, id: number): void {
const entry = BY_TYPE[type];
if (entry) void entry.sync(id).then(() => invalidateAlertsCache());
}
/** An integration that is being taken away: what was read from it goes too. */
export async function forgetReading(type: IntegrationType, id: number): Promise<void> {
await BY_TYPE[type]?.forget(id);
}
/** Wait this long between two reads of the same integration asked for by hand: some systems limit how often they may be asked (Hetzner Robot: 200 requests an hour). */
export const READ_NOW_COOLDOWN_MS = 20_000;
const lastAsked = new Map<number, number>();
export type ReadNowResult =
| { state: "read"; kind: "stored" | "live"; ok: boolean; error: string | null; at: string; latencyMs: number | null }
| { state: "not_found" }
| { state: "disabled" }
| { state: "too_soon"; retryAfterSeconds: number };
/**
* Reads one integration now. A kind with a stored reading is read the way its schedule reads it, and what it says is judged again at once; a kind that is read live (nothing stored) is asked whether it
* answers — the same question *Test* asks — which also brings its Working/Failing status up to date, and the alerts, which read it live, are worked out again at the next look. Never throws.
*/
export async function readIntegrationNow(id: number, now: number = Date.now()): Promise<ReadNowResult> {
const loaded = await loadIntegrationConfig(id);
if (!loaded) return { state: "not_found" };
if (!loaded.integration.enabled) return { state: "disabled" };
const before = lastAsked.get(id);
if (before !== undefined && now - before < READ_NOW_COOLDOWN_MS) return { state: "too_soon", retryAfterSeconds: Math.ceil((READ_NOW_COOLDOWN_MS - (now - before)) / 1000) };
lastAsked.set(id, now);
const type = loaded.integration.type;
const entry = BY_TYPE[type];
const started = Date.now();
try {
if (entry) {
const status = await entry.sync(id);
invalidateAlertsCache();
return { state: "read", kind: "stored", ok: status.ok, error: status.error, at: status.at ?? new Date().toISOString(), latencyMs: Date.now() - started };
}
const result = await createIntegrationAdapter(type, loaded.config).ping();
invalidateAlertsCache();
return { state: "read", kind: "live", ok: result.ok, error: result.ok ? null : (result.error ?? "no answer"), at: new Date().toISOString(), latencyMs: result.latencyMs ?? Date.now() - started };
} catch (err) {
return { state: "read", kind: entry ? "stored" : "live", ok: false, error: err instanceof Error ? err.message : String(err), at: new Date().toISOString(), latencyMs: null };
}
}
+191
View File
@@ -0,0 +1,191 @@
/**
* "Read now": one integration read at once instead of at its next turn — a kind with a stored reading is read the way its schedule reads it, a kind that is read live is asked whether it answers; the
* same one not again within twenty seconds; operators and administrators only; and the one list of which kinds have a stored reading.
*
* 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-readnow-"));
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, integrationTypes, downloadState } = await import("../src/db/schema.js");
const { runMigrations } = await import("../src/db/migrate.js");
const syncs = await import("../src/services/integrationSyncs.js");
const { downloadSyncStatuses } = await import("../src/services/downloadsSync.js");
const { integrationsRouter } = await import("../src/routes/integrations.js");
const { eq } = await import("drizzle-orm");
await runMigrations();
describe("which kinds have a stored reading", () => {
test("every kind is either read on a schedule and stored, or read live — so a new kind is placed on purpose", () => {
const live = integrationTypes.filter((t) => !syncs.hasStoredReading(t)).sort();
assert.deepEqual(live, ["gitea", "npm", "osticket", "pbs", "phpipam", "proxmox", "semaphore", "synology", "tailscale", "uptimekuma"]);
for (const t of ["sonarr", "lidarr", "qbittorrent", "sabnzbd", "hetznerrobot", "dockhand", "unifi", "homeassistant", "ups", "grafana"] as const) assert.equal(syncs.hasStoredReading(t), true, t);
});
});
describe("reading one now", () => {
let fake: Server;
let url = "";
let sabStatus = 200;
let giteaStatus = 200;
let sabId = 0;
let giteaId = 0;
let offId = 0;
before(async () => {
fake = createHttpServer((req, res) => {
const u = new URL(req.url ?? "", "http://x");
const send = (code: number, v: unknown) => {
res.writeHead(code, { "content-type": "application/json" });
res.end(JSON.stringify(v));
};
if (u.pathname === "/api") {
if (sabStatus !== 200) return send(sabStatus, {});
const mode = u.searchParams.get("mode");
if (mode === "queue") return send(200, { queue: { version: "4.3.2", paused: false, kbpersec: "100", slots: [{ nzo_id: "a", filename: "A", status: "Downloading", mb: "10", mbleft: "5" }] } });
if (mode === "history") return send(200, { history: { slots: [] } });
return send(200, { warnings: [] });
}
if (u.pathname === "/api/v1/user/repos") return giteaStatus === 200 ? send(200, []) : send(giteaStatus, { message: "no" });
send(404, {});
});
await new Promise<void>((resolve) => fake.listen(0, "127.0.0.1", resolve));
url = `http://127.0.0.1:${(fake.address() as AddressInfo).port}`;
const mk = async (type: "sabnzbd" | "gitea", name: string, config: Record<string, unknown>, enabled = true) => (await db.insert(integrations).values({ type, name, baseUrl: url, config: JSON.stringify(config), enabled }).returning({ id: integrations.id }))[0].id;
sabId = await mk("sabnzbd", "Usenet", { url, apiKey: "k" });
giteaId = await mk("gitea", "Git", { url, token: "t" });
offId = await mk("sabnzbd", "Usenet off", { url, apiKey: "k" }, false);
});
after(() => fake.close());
test("a kind with a stored reading is read and stored, and the answer says it was a stored one", async () => {
const r = await syncs.readIntegrationNow(sabId);
assert.equal(r.state, "read");
if (r.state !== "read") return;
assert.deepEqual([r.kind, r.ok, r.error], ["stored", true, null]);
assert.ok(Date.parse(r.at) > Date.now() - 60_000);
assert.equal((await downloadSyncStatuses())[sabId]?.ok, true);
assert.equal((await db.select().from(downloadState).where(eq(downloadState.integrationId, sabId))).length, 1);
});
test("the same one is not read again within twenty seconds — and is, after", async () => {
const t0 = Date.now() + 1_000_000;
assert.equal((await syncs.readIntegrationNow(sabId, t0)).state, "read");
const soon = await syncs.readIntegrationNow(sabId, t0 + 5_000);
assert.deepEqual(soon, { state: "too_soon", retryAfterSeconds: 15 });
assert.equal((await syncs.readIntegrationNow(sabId, t0 + syncs.READ_NOW_COOLDOWN_MS)).state, "read");
assert.equal((await syncs.readIntegrationNow(giteaId, t0 + 5_000)).state, "read", "another integration is not held up by it");
});
test("a read that fails says why, and what was stored is kept", async () => {
sabStatus = 500;
const r = await syncs.readIntegrationNow(sabId, Date.now() + 2_000_000);
assert.equal(r.state, "read");
if (r.state !== "read") return;
assert.deepEqual([r.kind, r.ok], ["stored", false]);
assert.match(r.error ?? "", /SABnzbd error: HTTP 500/);
const status = (await downloadSyncStatuses())[sabId];
assert.equal(status?.ok, false);
assert.equal((await db.select().from(downloadState).where(eq(downloadState.integrationId, sabId))).length, 1);
sabStatus = 200;
});
test("a kind that is read live is asked whether it answers, like Test", async () => {
const ok = await syncs.readIntegrationNow(giteaId, Date.now() + 3_000_000);
assert.equal(ok.state, "read");
if (ok.state !== "read") return;
assert.deepEqual([ok.kind, ok.ok, ok.error], ["live", true, null]);
assert.equal(typeof ok.latencyMs, "number");
giteaStatus = 401;
const bad = await syncs.readIntegrationNow(giteaId, Date.now() + 4_000_000);
assert.equal(bad.state, "read");
if (bad.state !== "read") return;
assert.deepEqual([bad.kind, bad.ok], ["live", false]);
assert.ok((bad.error ?? "").length > 0);
giteaStatus = 200;
});
test("one that is switched off, and one that is not there, are said", async () => {
assert.deepEqual(await syncs.readIntegrationNow(offId), { state: "disabled" });
assert.deepEqual(await syncs.readIntegrationNow(999_999), { state: "not_found" });
});
});
describe("the route", () => {
let app: Server;
let base = "";
let sessionSub = "operator";
let id = 0;
let fake: Server;
before(async () => {
fake = createHttpServer((req, res) => {
const u = new URL(req.url ?? "", "http://x");
res.writeHead(200, { "content-type": "application/json" });
res.end(JSON.stringify(u.searchParams.get("mode") === "queue" ? { queue: { version: "4", slots: [] } } : u.searchParams.get("mode") === "history" ? { history: { slots: [] } } : { warnings: [] }));
});
await new Promise<void>((resolve) => fake.listen(0, "127.0.0.1", resolve));
const furl = `http://127.0.0.1:${(fake.address() as AddressInfo).port}`;
await db.insert(users).values([
{ oidcSub: "operator", name: "Olle", role: "operator" },
{ oidcSub: "viewer", name: "Vic", role: "viewer" },
]);
id = (await db.insert(integrations).values({ type: "sabnzbd", name: "Route", baseUrl: furl, config: JSON.stringify({ url: furl, apiKey: "k" }) }).returning({ id: integrations.id }))[0].id;
const a = express();
a.use((req, _res, next) => {
(req as unknown as { session: unknown }).session = { user: { sub: sessionSub } };
next();
});
a.use("/api/integrations", integrationsRouter);
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/integrations`;
});
after(() => {
app.close();
fake.close();
try {
rmSync(dir, { recursive: true, force: true });
} catch {
// Windows keeps the database file locked until the process exits.
}
});
test("an operator reads it and is told how it went; a viewer is not allowed", async () => {
sessionSub = "viewer";
assert.equal((await fetch(`${base}/${id}/read-now`, { method: "POST" })).status, 403);
sessionSub = "operator";
const res = await fetch(`${base}/${id}/read-now`, { method: "POST" });
assert.equal(res.status, 200);
const body = (await res.json()) as { kind: string; ok: boolean; error: string | null; at: string };
assert.deepEqual([body.kind, body.ok, body.error], ["stored", true, null]);
});
test("asked again at once it says to wait, with the time in the answer and in Retry-After", async () => {
const res = await fetch(`${base}/${id}/read-now`, { method: "POST" });
assert.equal(res.status, 429);
const body = (await res.json()) as { error: string; retryAfterSeconds: number; message: string };
assert.equal(body.error, "too_soon");
assert.ok(body.retryAfterSeconds >= 1 && body.retryAfterSeconds <= 20);
assert.equal(res.headers.get("retry-after"), String(body.retryAfterSeconds));
assert.match(body.message, /try again in \d+ s/);
});
test("an integration that is not there is a 404, and an id that is not a number a 400", async () => {
assert.equal((await fetch(`${base}/999999/read-now`, { method: "POST" })).status, 404);
assert.equal((await fetch(`${base}/abc/read-now`, { method: "POST" })).status, 400);
});
});