feat(core): works catalogue and matching
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d392a65aa7
commit
a46fb60eee
4 files changed
+674
No files matched your search
@@ -0,0 +1,102 @@
|
||||
import { expect, test } from "bun:test";
|
||||
import type { Db } from "@sttracker/db/db";
|
||||
import { newId, nowIso } from "@sttracker/db/ids";
|
||||
import { forEachDialect } from "@sttracker/db/testing";
|
||||
import { fuzzyScore, matchWork } from "../src/matching.ts";
|
||||
import { createWork, getWork } from "../src/works.ts";
|
||||
|
||||
async function addUser(db: Db): Promise<string> {
|
||||
const id = newId();
|
||||
const now = nowIso();
|
||||
await db.k
|
||||
.insertInto("users")
|
||||
.values({ id, username: `u${id}`, display_name: "U", password_hash: "x", role: "member", preferences: "{}", created_at: now, updated_at: now })
|
||||
.execute();
|
||||
return id;
|
||||
}
|
||||
|
||||
test("fuzzyScore", () => {
|
||||
expect(fuzzyScore({ title: "The Expanse: Leviathan Wakes" }, { title: "Leviathan Wakes" })).toBeGreaterThan(0.6);
|
||||
expect(fuzzyScore({ title: "Dune" }, { title: "Dune Messiah" })).toBeLessThan(0.6);
|
||||
expect(fuzzyScore({ title: "Pokémon" }, { title: "Pokemon" })).toBe(1);
|
||||
expect(
|
||||
fuzzyScore({ title: "Pokémon", year: 1996, creators: [{ name: "Satoshi" }] }, { title: "pokemon!", year: 1996, creators: [{ name: "satoshi" }] }),
|
||||
).toBe(1);
|
||||
// year off by one counts half, creator mismatch counts zero
|
||||
expect(fuzzyScore({ title: "A B", year: 2000, creators: [{ name: "x" }] }, { title: "A B", year: 2001, creators: [{ name: "y" }] })).toBeCloseTo(0.8, 5);
|
||||
});
|
||||
|
||||
forEachDialect("matching", (getDb) => {
|
||||
test("match by isbn13 links existing", async () => {
|
||||
const db = getDb();
|
||||
const u = await addUser(db);
|
||||
const id = await createWork(db, { hint: { title: "Dune", mediaType: "book", externalIds: { isbn13: "9780441013593" } } }, "t");
|
||||
const r = await matchWork(db, u, { title: "Dune (Ace)", mediaType: "book", year: 1990, externalIds: { isbn13: "9780441013593", asin: "B1" } }, "plug");
|
||||
expect(r).toEqual({ workId: id, created: false, draft: false });
|
||||
expect((await getWork(db, id))?.year).toBe(1990);
|
||||
});
|
||||
|
||||
test("unseen external id creates shared work", async () => {
|
||||
const db = getDb();
|
||||
const u = await addUser(db);
|
||||
const r = await matchWork(db, u, { title: "Heat", mediaType: "movie", externalIds: { tmdb_movie: "949" } }, "p");
|
||||
expect(r.created).toBe(true);
|
||||
expect(r.draft).toBe(false);
|
||||
expect((await getWork(db, r.workId))?.owner_user_id).toBeNull();
|
||||
});
|
||||
|
||||
const chain = () => ({
|
||||
title: "Pilot",
|
||||
mediaType: "episode" as const,
|
||||
externalIds: {},
|
||||
position: { episode: 1, season: 1 },
|
||||
parent: {
|
||||
title: "Season 1",
|
||||
mediaType: "season" as const,
|
||||
externalIds: {},
|
||||
position: { season: 1 },
|
||||
parent: { title: "Lost", mediaType: "show" as const, externalIds: { tmdb_show: "4607" } },
|
||||
},
|
||||
});
|
||||
|
||||
test("episode hint creates show→season→episode chain once", async () => {
|
||||
const db = getDb();
|
||||
const u = await addUser(db);
|
||||
const r1 = await matchWork(db, u, chain(), "p");
|
||||
const r2 = await matchWork(db, u, chain(), "p");
|
||||
expect(r1.created).toBe(true);
|
||||
expect(r2).toEqual({ workId: r1.workId, created: false, draft: false });
|
||||
expect(await db.k.selectFrom("works").select("id").execute()).toHaveLength(3);
|
||||
const ep = await getWork(db, r1.workId);
|
||||
expect(ep?.position).toEqual({ season: 1, episode: 1 });
|
||||
expect(ep?.parent_id).not.toBeNull();
|
||||
const season = await getWork(db, ep!.parent_id!);
|
||||
expect(season?.media_type).toBe("season");
|
||||
expect((await getWork(db, season!.parent_id!))?.media_type).toBe("show");
|
||||
});
|
||||
|
||||
test("position key order does not matter", async () => {
|
||||
const db = getDb();
|
||||
const u = await addUser(db);
|
||||
const a = await matchWork(db, u, chain(), "p");
|
||||
const h = chain();
|
||||
h.position = { season: 1, episode: 1 };
|
||||
expect((await matchWork(db, u, h, "p")).workId).toBe(a.workId);
|
||||
});
|
||||
|
||||
test("hint without ids creates private draft with suggestions", async () => {
|
||||
const db = getDb();
|
||||
const u = await addUser(db);
|
||||
const shared = await createWork(db, { hint: { title: "Leviathan Wakes", mediaType: "book", externalIds: { isbn13: "1" } } }, "t");
|
||||
await createWork(db, { hint: { title: "Leviathan Wakes", mediaType: "game", externalIds: { igdb: "1" } } }, "t");
|
||||
await createWork(db, { hint: { title: "Unrelated", mediaType: "book", externalIds: { isbn13: "2" } } }, "t");
|
||||
const r = await matchWork(db, u, { title: "The Expanse: Leviathan Wakes", mediaType: "book", externalIds: {} }, "p");
|
||||
expect(r.created).toBe(true);
|
||||
expect(r.draft).toBe(true);
|
||||
expect((await getWork(db, r.workId))?.owner_user_id).toBe(u);
|
||||
const sug = await db.k.selectFrom("match_suggestions").selectAll().execute();
|
||||
expect(sug).toHaveLength(1);
|
||||
expect(sug[0]).toMatchObject({ user_id: u, work_id: r.workId, candidate_work_id: shared, status: "pending" });
|
||||
expect(sug[0]!.score).toBeGreaterThan(0.6);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,112 @@
|
||||
import { expect, test } from "bun:test";
|
||||
import type { Db } from "@sttracker/db/db";
|
||||
import { newId, nowIso } from "@sttracker/db/ids";
|
||||
import { fromJson } from "@sttracker/db/json";
|
||||
import { forEachDialect } from "@sttracker/db/testing";
|
||||
import { AuthError } from "../src/errors.ts";
|
||||
import {
|
||||
adminMergeWorks,
|
||||
createWork,
|
||||
findWorkByExternalIds,
|
||||
getWork,
|
||||
resolveAlias,
|
||||
searchWorks,
|
||||
updateWorkFromHint,
|
||||
} from "../src/works.ts";
|
||||
|
||||
async function addUser(db: Db, role: "admin" | "member" = "member"): Promise<string> {
|
||||
const id = newId();
|
||||
const now = nowIso();
|
||||
await db.k
|
||||
.insertInto("users")
|
||||
.values({ id, username: `u${id}`, display_name: "U", password_hash: "x", role, preferences: "{}", created_at: now, updated_at: now })
|
||||
.execute();
|
||||
return id;
|
||||
}
|
||||
|
||||
async function addEntry(db: Db, userId: string, workId: string): Promise<string> {
|
||||
const id = newId();
|
||||
const now = nowIso();
|
||||
await db.k
|
||||
.insertInto("entries")
|
||||
.values({
|
||||
id, user_id: userId, work_id: workId, status: "none", rating: null, review: null, private_notes: null,
|
||||
favorite: 0, tags: "[]", custom_fields: "{}", visibility: "private", overrides: "{}",
|
||||
media_type_override: null, field_sources: "{}", created_at: now, updated_at: now, deleted_at: null,
|
||||
})
|
||||
.execute();
|
||||
return id;
|
||||
}
|
||||
|
||||
forEachDialect("works", (getDb) => {
|
||||
test("createWork stores external ids; findWorkByExternalIds finds any namespace", async () => {
|
||||
const db = getDb();
|
||||
const id = await createWork(db, { hint: { title: "Dune", mediaType: "book", year: 1965, externalIds: { isbn13: "9780441013593", hardcover_book: "7" } } }, "test");
|
||||
expect(await findWorkByExternalIds(db, { nope: "1", hardcover_book: "7" })).toBe(id);
|
||||
expect(await findWorkByExternalIds(db, { isbn13: "0" })).toBeNull();
|
||||
expect((await getWork(db, id))?.year).toBe(1965);
|
||||
});
|
||||
|
||||
test("updateWorkFromHint updates shared work, logs history, never touches private", async () => {
|
||||
const db = getDb();
|
||||
const u = await addUser(db);
|
||||
const shared = await createWork(db, { hint: { title: "Dune", mediaType: "book", externalIds: { a: "1" } } }, "t");
|
||||
await updateWorkFromHint(db, shared, { title: "Dune", mediaType: "book", year: 1965, description: "Sand", externalIds: { a: "1", b: "2" } }, "plug");
|
||||
const w = await getWork(db, shared);
|
||||
expect(w?.year).toBe(1965);
|
||||
expect(w?.description).toBe("Sand");
|
||||
expect(await findWorkByExternalIds(db, { b: "2" })).toBe(shared);
|
||||
const hist = await db.k.selectFrom("work_history").selectAll().where("work_id", "=", shared).where("actor", "=", "plug").execute();
|
||||
expect(hist).toHaveLength(1);
|
||||
expect(fromJson<Record<string, unknown>>(hist[0]!.before, {}).year).toBeNull();
|
||||
|
||||
const priv = await createWork(db, { hint: { title: "Mine", mediaType: "book", externalIds: {} }, ownerUserId: u }, "t");
|
||||
await updateWorkFromHint(db, priv, { title: "Changed", mediaType: "book", externalIds: {} }, "plug");
|
||||
expect((await getWork(db, priv))?.title).toBe("Mine");
|
||||
});
|
||||
|
||||
test("private works invisible to other users in search", async () => {
|
||||
const db = getDb();
|
||||
const u1 = await addUser(db);
|
||||
const u2 = await addUser(db);
|
||||
await createWork(db, { hint: { title: "Secret 100%", mediaType: "book", externalIds: {} }, ownerUserId: u1 }, "t");
|
||||
await createWork(db, { hint: { title: "Public Thing", mediaType: "book", externalIds: { x: "1" } } }, "t");
|
||||
expect((await searchWorks(db, u1, "secret 100%")).map((w) => w.title)).toEqual(["Secret 100%"]);
|
||||
expect(await searchWorks(db, u2, "secret")).toEqual([]);
|
||||
expect((await searchWorks(db, u2, "PUBLIC")).map((w) => w.title)).toEqual(["Public Thing"]);
|
||||
expect(await searchWorks(db, u2, "public", { mediaType: "game" })).toEqual([]);
|
||||
});
|
||||
|
||||
test("adminMergeWorks requires admin", async () => {
|
||||
const db = getDb();
|
||||
const member = await addUser(db);
|
||||
const a = await createWork(db, { hint: { title: "A", mediaType: "book", externalIds: { x: "1" } } }, "t");
|
||||
const b = await createWork(db, { hint: { title: "B", mediaType: "book", externalIds: { x: "2" } } }, "t");
|
||||
await expect(adminMergeWorks(db, member, a, b)).rejects.toBeInstanceOf(AuthError);
|
||||
});
|
||||
|
||||
test("adminMergeWorks tombstones, moves ids, repoints entries, emits merged event on conflict", async () => {
|
||||
const db = getDb();
|
||||
const admin = await addUser(db, "admin");
|
||||
const u1 = await addUser(db);
|
||||
const u2 = await addUser(db);
|
||||
const from = await createWork(db, { hint: { title: "Old", mediaType: "book", externalIds: { x: "1" } } }, "t");
|
||||
const to = await createWork(db, { hint: { title: "New", mediaType: "book", externalIds: { x: "2" } } }, "t");
|
||||
const e1 = await addEntry(db, u1, from);
|
||||
const e2from = await addEntry(db, u2, from);
|
||||
await addEntry(db, u2, to);
|
||||
|
||||
await adminMergeWorks(db, admin, from, to);
|
||||
|
||||
expect((await getWork(db, from))?.id).toBe(to);
|
||||
expect(await findWorkByExternalIds(db, { x: "1" })).toBe(to);
|
||||
expect(await resolveAlias(db, u1, from)).toBe(to);
|
||||
expect((await db.k.selectFrom("entries").select("work_id").where("id", "=", e1).executeTakeFirstOrThrow()).work_id).toBe(to);
|
||||
expect((await db.k.selectFrom("entries").select("work_id").where("id", "=", e2from).executeTakeFirstOrThrow()).work_id).toBe(from);
|
||||
const ev = await db.k.selectFrom("change_events").selectAll().execute();
|
||||
expect(ev).toHaveLength(1);
|
||||
expect(ev[0]).toMatchObject({ user_id: u2, type: "merged", status: "pending", target_entity: "entry", target_id: e2from });
|
||||
expect(fromJson(ev[0]!.payload, {})).toEqual({ fromWorkId: from, toWorkId: to });
|
||||
expect(await db.k.selectFrom("work_history").select("id").where("work_id", "=", from).where("actor", "=", admin).execute()).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user