import { describe, it, expect } from "vitest"; import { prisma } from "@/lib/db"; import { POST } from "./route"; const ctx = (id: string) => ({ params: Promise.resolve({ id }) }); async function owned(artist: string, album: string, rgMbid: string | null) { return prisma.libraryItem.create({ data: { artist, album, path: "/m", source: "scan", format: "FLAC", qualityClass: 2, rgMbid }, }); } describe("POST /api/library/[id]/upgrade", () => { it("enqueues a forced re-acquire request against the matching release", async () => { await prisma.monitoredRelease.create({ data: { artistMbid: "a1", artistName: "Adele", rgMbid: "rg-25", album: "25", secondaryTypes: [], monitored: true, state: "fulfilled", currentQualityClass: 2 }, }); const item = await owned("Adele", "25", "rg-25"); const res = await POST(new Request("http://x", { method: "POST" }), ctx(item.id)); expect(res.status).toBe(202); expect((await res.json()).enqueued).toBe(true); const reqRow = await prisma.request.findFirst({ where: { album: "25" }, include: { job: true } }); expect(reqRow?.force).toBe(true); expect(reqRow?.monitoredReleaseId).toBeTruthy(); expect(reqRow?.job).toBeTruthy(); }); it("400s when there is no matching release", async () => { const item = await owned("Nobody", "Untracked", null); const res = await POST(new Request("http://x", { method: "POST" }), ctx(item.id)); expect(res.status).toBe(400); }); it("does not stack a second job while one is in flight", async () => { const mr = await prisma.monitoredRelease.create({ data: { artistMbid: "a1", artistName: "Adele", rgMbid: "rg-21", album: "21", secondaryTypes: [], monitored: true, state: "wanted" }, }); const item = await owned("Adele", "21", "rg-21"); await prisma.request.create({ data: { artist: "Adele", album: "21", monitoredReleaseId: mr.id, job: { create: { state: "downloading" } } }, }); const res = await POST(new Request("http://x", { method: "POST" }), ctx(item.id)); expect(res.status).toBe(202); expect((await res.json()).enqueued).toBe(false); }); it("404s an unknown library item", async () => { const res = await POST(new Request("http://x", { method: "POST" }), ctx("nope")); expect(res.status).toBe(404); }); });