feat: artists API — follow (persist discography) + list
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import { describe, it, expect, vi, afterEach } from "vitest";
|
||||
|
||||
vi.mock("@/lib/musicbrainz", () => ({ browseReleaseGroups: vi.fn() }));
|
||||
import { browseReleaseGroups } from "@/lib/musicbrainz";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { GET, POST } from "./route";
|
||||
|
||||
const mockBrowse = vi.mocked(browseReleaseGroups);
|
||||
afterEach(() => mockBrowse.mockReset());
|
||||
|
||||
function postReq(body: unknown) {
|
||||
return new Request("http://localhost/api/artists", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
}
|
||||
|
||||
describe("artists collection API", () => {
|
||||
it("follows an artist and persists the discography unmonitored", async () => {
|
||||
mockBrowse.mockResolvedValue([
|
||||
{ rgMbid: "rg1", title: "Continuum", primaryType: "Album", secondaryTypes: [], firstReleaseDate: "2006-09-12" },
|
||||
{ rgMbid: "rg2", title: "Battle Studies", primaryType: "Album", secondaryTypes: [], firstReleaseDate: "2009" },
|
||||
]);
|
||||
const res = await POST(postReq({ mbid: "a1", name: "John Mayer" }));
|
||||
expect(res.status).toBe(201);
|
||||
expect((await res.json()).releaseCount).toBe(2);
|
||||
|
||||
const rels = await prisma.monitoredRelease.findMany({ where: { artistMbid: "a1" } });
|
||||
expect(rels).toHaveLength(2);
|
||||
expect(rels.every((r) => r.monitored === false && r.state === "wanted")).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects a bad body and a duplicate follow", async () => {
|
||||
expect((await POST(postReq({ mbid: "a1" }))).status).toBe(400);
|
||||
mockBrowse.mockResolvedValue([]);
|
||||
expect((await POST(postReq({ mbid: "a1", name: "John Mayer" }))).status).toBe(201);
|
||||
expect((await POST(postReq({ mbid: "a1", name: "John Mayer" }))).status).toBe(409);
|
||||
});
|
||||
|
||||
it("502s when the discography fetch fails and creates nothing", async () => {
|
||||
mockBrowse.mockRejectedValue(new Error("boom"));
|
||||
expect((await POST(postReq({ mbid: "a9", name: "X" }))).status).toBe(502);
|
||||
expect(await prisma.watchedArtist.findUnique({ where: { mbid: "a9" } })).toBeNull();
|
||||
});
|
||||
|
||||
it("lists followed artists with counts", async () => {
|
||||
mockBrowse.mockResolvedValue([
|
||||
{ rgMbid: "rg1", title: "A", primaryType: "Album", secondaryTypes: [], firstReleaseDate: "2001" },
|
||||
]);
|
||||
await POST(postReq({ mbid: "a1", name: "John Mayer" }));
|
||||
// mark the one release as monitored + have, to exercise the counts
|
||||
await prisma.monitoredRelease.updateMany({ where: { artistMbid: "a1" }, data: { monitored: true, currentQualityClass: 2 } });
|
||||
|
||||
const res = await GET();
|
||||
expect(res.status).toBe(200);
|
||||
const a = (await res.json()).artists[0];
|
||||
expect(a).toMatchObject({ mbid: "a1", name: "John Mayer", releaseCount: 1, monitoredCount: 1, haveCount: 1 });
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user