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

+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);
});
});