feat(library): surface possible duplicate albums
GET /api/library/duplicates groups library items that look like the same release held more than once — sharing a MusicBrainz release-group, or the same artist + edition-normalized title (stripEditionQualifiers), unioned so transitive matches collapse into one group. The (artist,album) unique constraint rules out exact dupes, so these near-dupes are what's worth flagging. /library shows a "Possible duplicates" section; each entry opens the album modal to edit or delete. web 175 tests. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { GET } from "./route";
|
||||
|
||||
async function li(artist: string, album: string, rgMbid: string | null, path = "/m") {
|
||||
return prisma.libraryItem.create({
|
||||
data: { artist, album, path, source: "scan", format: "FLAC", qualityClass: 2, rgMbid },
|
||||
});
|
||||
}
|
||||
|
||||
describe("library duplicates API", () => {
|
||||
it("groups items sharing a release-group", async () => {
|
||||
await li("Adele", "21", "rg-21");
|
||||
await li("Adele", "21 (Deluxe)", "rg-21"); // same rg → duplicate
|
||||
await li("Adele", "25", "rg-25"); // unrelated
|
||||
const { duplicates } = await (await GET()).json();
|
||||
expect(duplicates).toHaveLength(1);
|
||||
expect(duplicates[0].map((d: { album: string }) => d.album).sort()).toEqual(["21", "21 (Deluxe)"]);
|
||||
});
|
||||
|
||||
it("groups items with the same edition-normalized title even without a shared rgMbid", async () => {
|
||||
await li("Nirvana", "Nevermind", null);
|
||||
await li("Nirvana", "Nevermind (Deluxe Edition)", "rg-x");
|
||||
const { duplicates } = await (await GET()).json();
|
||||
expect(duplicates).toHaveLength(1);
|
||||
expect(duplicates[0]).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("returns nothing when there are no duplicates", async () => {
|
||||
await li("A", "One", "rg-1");
|
||||
await li("B", "Two", "rg-2");
|
||||
const { duplicates } = await (await GET()).json();
|
||||
expect(duplicates).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,61 @@
|
||||
import { prisma } from "@/lib/db";
|
||||
import { stripEditionQualifiers } from "@/lib/musicbrainz";
|
||||
import { yearFromPath } from "@/lib/library-path";
|
||||
|
||||
// GET /api/library/duplicates — groups of library items that look like the same album held
|
||||
// more than once: sharing a MusicBrainz release-group, or the same artist + edition-normalized
|
||||
// title (e.g. "Album" vs "Album (Deluxe Edition)"). The (artist, album) unique constraint
|
||||
// rules out exact dupes, so these are the near-dupes worth surfacing.
|
||||
function normTitleKey(artist: string, album: string): string {
|
||||
return `${artist.trim().toLowerCase()}|${stripEditionQualifiers(album).trim().toLowerCase()}`;
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
const items = await prisma.libraryItem.findMany({ orderBy: [{ artist: "asc" }, { album: "asc" }] });
|
||||
|
||||
// Union-find: connect items that share a non-null rgMbid or the same normalized title key.
|
||||
const parent = new Map<string, string>();
|
||||
const find = (x: string): string => {
|
||||
let r = x;
|
||||
while (parent.get(r) !== r) r = parent.get(r)!;
|
||||
while (parent.get(x) !== r) { const n = parent.get(x)!; parent.set(x, r); x = n; }
|
||||
return r;
|
||||
};
|
||||
const union = (a: string, b: string) => { parent.set(find(a), find(b)); };
|
||||
|
||||
for (const it of items) parent.set(it.id, it.id);
|
||||
const byRg = new Map<string, string>();
|
||||
const byTitle = new Map<string, string>();
|
||||
for (const it of items) {
|
||||
if (it.rgMbid) {
|
||||
const seen = byRg.get(it.rgMbid);
|
||||
if (seen) union(seen, it.id); else byRg.set(it.rgMbid, it.id);
|
||||
}
|
||||
const tk = normTitleKey(it.artist, it.album);
|
||||
const seenT = byTitle.get(tk);
|
||||
if (seenT) union(seenT, it.id); else byTitle.set(tk, it.id);
|
||||
}
|
||||
|
||||
const groups = new Map<string, typeof items>();
|
||||
for (const it of items) {
|
||||
const root = find(it.id);
|
||||
(groups.get(root) ?? groups.set(root, []).get(root)!).push(it);
|
||||
}
|
||||
|
||||
const duplicates = [...groups.values()]
|
||||
.filter((g) => g.length > 1)
|
||||
.map((g) =>
|
||||
g.map((it) => ({
|
||||
id: it.id,
|
||||
artist: it.artist,
|
||||
album: it.album,
|
||||
format: it.format,
|
||||
qualityClass: it.qualityClass,
|
||||
rgMbid: it.rgMbid,
|
||||
year: yearFromPath(it.path),
|
||||
path: it.path,
|
||||
})),
|
||||
);
|
||||
|
||||
return Response.json({ duplicates });
|
||||
}
|
||||
Reference in New Issue
Block a user