import { describe, it, expect } from "vitest"; import { GET, PATCH } from "./route"; import { prisma } from "@/lib/db"; function patch(body: Record) { return new Request("http://localhost/api/qobuz/pacing", { method: "PATCH", headers: { "content-type": "application/json" }, body: JSON.stringify(body), }); } describe("qobuz pacing config API", () => { it("returns defaults when nothing is stored", async () => { const cfg = await (await GET()).json(); expect(cfg["qobuz.pacing.enabled"]).toBe("true"); expect(cfg["qobuz.pacing.activeStartHour"]).toBe("8"); expect(cfg["qobuz.pacing.activeEndHour"]).toBe("23"); expect(cfg["qobuz.pacing.dailyCap"]).toBe("40"); expect(cfg["qobuz.pacing.warmupStartDate"]).toBe(""); }); it("round-trips allow-listed settings and persists them", async () => { await PATCH(patch({ "qobuz.pacing.dailyCap": "25", "qobuz.pacing.activeStartHour": "9" })); const stored = await prisma.config.findUnique({ where: { key: "qobuz.pacing.dailyCap" } }); expect(stored?.value).toBe("25"); const cfg = await (await GET()).json(); expect(cfg["qobuz.pacing.dailyCap"]).toBe("25"); expect(cfg["qobuz.pacing.activeStartHour"]).toBe("9"); }); it("ignores keys not in the allow-list (e.g. worker-managed counters)", async () => { await PATCH(patch({ "qobuz.pacing.countToday": "999", "qobuz.pacing.dailyCap": "30" })); expect(await prisma.config.findUnique({ where: { key: "qobuz.pacing.countToday" } })).toBeNull(); expect((await prisma.config.findUnique({ where: { key: "qobuz.pacing.dailyCap" } }))?.value).toBe("30"); }); });