import { describe, it, expect } from "vitest"; import { prisma } from "@/lib/db"; import { GET, PATCH } from "./route"; function patch(body: unknown) { return PATCH(new Request("http://localhost/api/monitor/config", { method: "PATCH", headers: { "content-type": "application/json" }, body: JSON.stringify(body), })); } describe("monitor config API", () => { it("GET returns defaults when nothing is stored", async () => { const body = await (await GET()).json(); expect(body).toMatchObject({ "monitor.enabled": "false", "monitor.pollIntervalHours": "24", "monitor.retryIntervalHours": "6", "monitor.qualityCutoff": "2", "monitor.upgradeWindowDays": "14", }); }); it("PATCH upserts known keys and ignores unknown ones", async () => { const res = await patch({ "monitor.enabled": "true", "monitor.pollIntervalHours": 12, "nope": "x" }); expect((await res.json()).updated).toBe(2); const body = await (await GET()).json(); expect(body["monitor.enabled"]).toBe("true"); expect(body["monitor.pollIntervalHours"]).toBe("12"); expect(await prisma.config.findUnique({ where: { key: "nope" } })).toBeNull(); }); it("ignores prototype-chain keys (__proto__, constructor) and writes no Config row", async () => { const before = await prisma.config.count(); const res = await patch({ __proto__: "x", constructor: "y" }); expect((await res.json()).updated).toBe(0); expect(await prisma.config.count()).toBe(before); }); }); describe("monitor config — floor.jobDelaySeconds", () => { it("defaults jobDelaySeconds to 0 and round-trips a new value", async () => { const before = await (await GET()).json(); expect(before["floor.jobDelaySeconds"]).toBe("0"); await PATCH( new Request("http://localhost/api/monitor/config", { method: "PATCH", headers: { "content-type": "application/json" }, body: JSON.stringify({ "floor.jobDelaySeconds": "45" }), }), ); const stored = await prisma.config.findUnique({ where: { key: "floor.jobDelaySeconds" } }); expect(stored?.value).toBe("45"); const after = await (await GET()).json(); expect(after["floor.jobDelaySeconds"]).toBe("45"); }); });