import { getTestPayload } from "../helpers/get-test-payload"; import type { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { computeNoteLayout, noteNodeId, parseNoteNodeId, } from "@/components/admin/tech-tree/notes"; import { COLUMN_STEP, ROW_STEP } from "@/components/admin/tech-tree/types"; const RUN = `ttnote-${Date.now().toString(36)}`; const TIMEOUT = 30_000; describe("tech tree note node layout (pure)", () => { const note = ( id: number, treePosition: { x: number; y: number } | null = null, ): Parameters[0][number] => ({ id, title: `note ${id}`, body: "", showOnFrontend: false, treePosition, updatedAt: "", }); it("parks untouched notes in a column one step right of the tree", () => { const layout = computeNoteLayout([note(1), note(2)], { columns: 3 }); expect(layout).toEqual([ { noteId: 1, x: 3 * COLUMN_STEP, y: 0 }, { noteId: 2, x: 3 * COLUMN_STEP, y: ROW_STEP }, ]); }); it("uses dragged positions without shifting the automatic stack", () => { const layout = computeNoteLayout( [note(1, { x: 40, y: 400 }), note(2), note(3)], { columns: 2 }, ); expect(layout).toEqual([ { noteId: 1, x: 40, y: 400 }, { noteId: 2, x: 2 * COLUMN_STEP, y: 0 }, { noteId: 3, x: 2 * COLUMN_STEP, y: ROW_STEP }, ]); }); it("starts at column 0 when the tree has no technologies", () => { expect(computeNoteLayout([note(9)], { columns: 0 })).toEqual([ { noteId: 9, x: 0, y: 0 }, ]); }); it("note node ids can never collide with technology ids", () => { expect(noteNodeId(4)).toBe("note-4"); expect(parseNoteNodeId("note-4")).toBe(4); expect(parseNoteNodeId("4")).toBeNull(); expect(parseNoteNodeId("note-x")).toBeNull(); expect(parseNoteNodeId("note-0")).toBeNull(); }); }); describe("tech-tree-notes collection", () => { let payload: Payload; const ids: number[] = []; beforeAll(async () => { payload = await getTestPayload(); }, TIMEOUT); afterAll(async () => { for (const id of ids) { await payload .delete({ collection: "tech-tree-notes", id, overrideAccess: true }) .catch(() => undefined); } }, TIMEOUT); it( "defaults to a backend-only note with no canvas position", async () => { const created = await payload.create({ collection: "tech-tree-notes", data: { title: `${RUN}-default` }, overrideAccess: true, depth: 0, }); ids.push(created.id); expect(created.showOnFrontend).toBe(false); expect(created.body ?? null).toBeNull(); expect(created.treePosition?.x ?? null).toBeNull(); expect(created.treePosition?.y ?? null).toBeNull(); }, TIMEOUT, ); it( "requires a title", async () => { await expect( payload.create({ collection: "tech-tree-notes", // Deliberately missing the required title. data: {} as never, overrideAccess: true, }), ).rejects.toThrow(); }, TIMEOUT, ); it( "publishes only the notes flagged for the frontend", async () => { const hidden = await payload.create({ collection: "tech-tree-notes", data: { title: `${RUN}-hidden`, showOnFrontend: false }, overrideAccess: true, depth: 0, }); const shown = await payload.create({ collection: "tech-tree-notes", data: { title: `${RUN}-shown`, showOnFrontend: true }, overrideAccess: true, depth: 0, }); ids.push(hidden.id, shown.id); const published = await payload.find({ collection: "tech-tree-notes", where: { and: [ { title: { like: RUN } }, // The member-facing page's exact publication gate. { showOnFrontend: { equals: true } }, ], }, depth: 0, limit: 0, overrideAccess: true, }); const publishedIds = published.docs.map((doc) => doc.id); expect(publishedIds).toContain(shown.id); expect(publishedIds).not.toContain(hidden.id); }, TIMEOUT, ); it( "keeps body line breaks and updates in place", async () => { const created = await payload.create({ collection: "tech-tree-notes", data: { title: `${RUN}-edit`, body: "line one\nline two", showOnFrontend: true, }, overrideAccess: true, depth: 0, }); ids.push(created.id); const updated = await payload.update({ collection: "tech-tree-notes", id: created.id, data: { showOnFrontend: false, title: `${RUN}-edit-renamed` }, overrideAccess: true, depth: 0, }); expect(updated.showOnFrontend).toBe(false); expect(updated.title).toBe(`${RUN}-edit-renamed`); // An unrelated field update must not drop the body. expect(updated.body).toBe("line one\nline two"); }, TIMEOUT, ); });