import { beforeEach, expect, it } from "vitest"; import { nextEntityCode, nextMapCode, nextMarkerCode, nextRelationCode, nextScheduleCode } from "@/lib/ids"; import { describeDb, makeBook, makeUser, prisma, resetAll } from "../helpers/db"; describeDb("löpande ID", () => { let bookId: string; let otherBookId: string; beforeEach(async () => { await resetAll(); const user = await makeUser(); bookId = (await makeBook(user.id, "A")).id; otherBookId = (await makeBook(user.id, "B")).id; }); it("räknar upp per typ i varje bok", async () => { expect(await nextEntityCode(bookId, "CHARACTER")).toBe("KI-0001"); expect(await nextEntityCode(bookId, "CHARACTER")).toBe("KI-0002"); expect(await nextEntityCode(bookId, "PET")).toBe("DI-0001"); expect(await nextEntityCode(bookId, "CHARACTER")).toBe("KI-0003"); }); it("börjar om i en ny bok", async () => { await nextEntityCode(bookId, "CHARACTER"); await nextEntityCode(bookId, "CHARACTER"); expect(await nextEntityCode(otherBookId, "CHARACTER")).toBe("KI-0001"); }); it("ger inget ID åt typer utan ID", async () => { expect(await nextEntityCode(bookId, "APP")).toBeNull(); expect(await nextEntityCode(bookId, "WEBSITE")).toBeNull(); expect(await prisma.idCounter.count({ where: { bookId } })).toBe(0); }); it("använder egna räknare för relationer, placeringar, scheman och kartor", async () => { expect(await nextRelationCode(bookId)).toBe("REI-0001"); expect(await nextMarkerCode(bookId)).toBe("PI-0001"); expect(await nextScheduleCode(bookId)).toBe("SCI-0001"); expect(await nextMapCode(bookId, "GEO")).toBe("KD-R_0001"); expect(await nextMapCode(bookId, "IMAGE")).toBe("KD-E_0001"); expect(await nextMapCode(bookId, "GEO")).toBe("KD-R_0002"); }); it("ger unika nummer även när många skapas samtidigt", async () => { const codes = await Promise.all(Array.from({ length: 25 }, () => nextEntityCode(bookId, "CHARACTER"))); expect(new Set(codes).size).toBe(25); expect(codes.sort()[0]).toBe("KI-0001"); expect(codes.sort()[24]).toBe("KI-0025"); }); });