feat: /api/scan — request a library scan + read its result

Type the POST request param (_request: Request) to match the test's
call signature and keep tsc --noEmit clean, per Next.js route handler
convention already used elsewhere in the codebase.
This commit is contained in:
Jonathan
2026-07-11 17:11:42 +02:00
parent c31c8b5249
commit 7cb99361a5
2 changed files with 46 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
import { describe, it, expect } from "vitest";
import { prisma } from "@/lib/db";
import { POST, GET } from "./route";
describe("scan API", () => {
it("POST sets scan.requested and GET reflects it", async () => {
const res = await POST(new Request("http://localhost/api/scan", { method: "POST" }));
expect(res.status).toBe(202);
expect((await res.json()).requested).toBe(true);
const row = await prisma.config.findUnique({ where: { key: "scan.requested" } });
expect(row?.value).toBe("true");
const g = await GET();
const body = await g.json();
expect(body.requested).toBe(true);
expect(body.result).toBeNull(); // no scan.result yet
});
it("GET returns the worker's scan.result when present", async () => {
await prisma.config.create({ data: { key: "scan.result", value: "imported 3, skipped 1" } });
const body = await (await GET()).json();
expect(body.result).toBe("imported 3, skipped 1");
});
});
+21
View File
@@ -0,0 +1,21 @@
import { prisma } from "@/lib/db";
export async function POST(_request: Request) {
await prisma.config.upsert({
where: { key: "scan.requested" },
create: { key: "scan.requested", value: "true", secret: false },
update: { value: "true" },
});
return Response.json({ requested: true }, { status: 202 });
}
export async function GET() {
const [requested, result] = await Promise.all([
prisma.config.findUnique({ where: { key: "scan.requested" } }),
prisma.config.findUnique({ where: { key: "scan.result" } }),
]);
return Response.json({
requested: requested?.value === "true",
result: result?.value ?? null,
});
}