import { getPayload, Payload } from "payload"; import config from "@/payload.config"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { resolveAttachments } from "@/lib/npc-dialogue/attachments"; let payload: Payload; const TIMEOUT = 30_000; /** Create a tiny media doc and return its id. */ async function createMedia(name: string): Promise { const doc = await payload.create({ collection: "media", data: { alt: name }, file: { data: Buffer.from(`attachment fixture: ${name}`), mimetype: "text/plain", name, size: 24, }, overrideAccess: true, }); return doc.id; } describe("resolveAttachments", () => { beforeAll(async () => { payload = await getPayload({ config }); }, TIMEOUT); afterAll(async () => { // Nothing persists: each test cleans up its own docs below. }); it( "returns nothing for missing or empty input", async () => { expect(await resolveAttachments(payload, undefined)).toEqual([]); expect(await resolveAttachments(payload, [])).toEqual([]); }, TIMEOUT, ); it( "resolves pointers to renderable references, honouring custom labels", async () => { const id = await createMedia("b2-int-transcript.txt"); try { const [resolved] = await resolveAttachments(payload, [ { mediaId: id, label: "Intercept transcript" }, ]); expect(resolved).toMatchObject({ label: "Intercept transcript", url: "/api/media/file/b2-int-transcript.txt", filename: "b2-int-transcript.txt", mimeType: "text/plain", }); } finally { await payload.delete({ collection: "media", id, overrideAccess: true }); } }, TIMEOUT, ); it( "falls back to the stored filename when no label is authored", async () => { const id = await createMedia("b2-int-order.txt"); try { const [resolved] = await resolveAttachments(payload, [{ mediaId: id }]); expect(resolved?.label).toBe("b2-int-order.txt"); } finally { await payload.delete({ collection: "media", id, overrideAccess: true }); } }, TIMEOUT, ); it( "skips deleted media and malformed pointers instead of rendering dead chips", async () => { const doomed = await createMedia("b2-int-doomed.txt"); await payload.delete({ collection: "media", id: doomed, overrideAccess: true }); const resolved = await resolveAttachments(payload, [ { mediaId: doomed }, { mediaId: 0 }, { mediaId: "not-a-number" }, { mediaId: -3 }, ]); expect(resolved).toEqual([]); }, TIMEOUT, ); });