import { getTestPayload } from "../helpers/get-test-payload"; import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { Role, User } from "@/payload-types"; import { canSeeTestingDialogues, dialogueVisibleTo, loadPlayerDialogue, } from "@/lib/npc-dialogue/visibility"; let payload: Payload; const RUN = `vis-${Date.now().toString(36)}`; const TIMEOUT = 30_000; const FLOW = { nodes: [], edges: [] }; describe("dialogue visibility (testing flag)", () => { const roleIds: number[] = []; const userIds: number[] = []; const dialogueIds: number[] = []; let npcId: number; let otherNpcId: number; let superUser: User; let plainUser: User; let testingId: number; let liveId: number; const makeRole = async (label: string, isSuperuser = false): Promise => (await payload.create({ collection: "roles", data: { name: `${RUN}-${label}`, slug: `${RUN}-${label}`, isSuperuser }, overrideAccess: true, depth: 0, })) as unknown as Role; const makeUser = async (label: string, roleId: number): Promise => { const user = (await payload.create({ collection: "users", data: { username: `${RUN}-${label}`, discordUsername: `${RUN}-${label}`, displayName: label.toUpperCase(), steamId: `7656119${Math.floor(Math.random() * 1e9)}`, password: "Test123", roleDocs: [roleId], }, overrideAccess: true, depth: 0, })) as unknown as User; userIds.push(user.id); return user; }; const makeNpc = async (label: string): Promise => ( (await payload.create({ collection: "game-npcs", data: { name: `${RUN}-${label}`, canMessage: true }, overrideAccess: true, depth: 0, })) as unknown as { id: number } ).id; const makeDialogue = async ( label: string, npc: number, data: { priority: number; testing?: boolean }, ): Promise => { const doc = (await payload.create({ collection: "npc-dialogues", data: { name: `${RUN}-${label}`, npc, flowData: FLOW, ...data }, overrideAccess: true, depth: 0, })) as unknown as { id: number }; dialogueIds.push(doc.id); return doc.id; }; /** The real path: the reader's tier decides what the claim may resolve. */ const offerTo = async (user: User, npc: number, dialogueId?: number) => loadPlayerDialogue(payload, { npcId: npc, userId: user.id, canSeeTesting: await canSeeTestingDialogues(payload, user), dialogueId, }); beforeAll(async () => { payload = await getTestPayload(); const superRole = await makeRole("super", true); roleIds.push(superRole.id); const plainRole = await makeRole("plain"); roleIds.push(plainRole.id); superUser = await makeUser("super", superRole.id); plainUser = await makeUser("plain", plainRole.id); npcId = await makeNpc("npc"); otherNpcId = await makeNpc("other"); testingId = await makeDialogue("testing", npcId, { priority: 10, testing: true }); liveId = await makeDialogue("live", npcId, { priority: 0 }); }, TIMEOUT); afterAll(async () => { if (!payload) return; for (const id of dialogueIds) { await payload.delete({ collection: "npc-dialogues", id, overrideAccess: true }).catch(() => {}); } for (const id of [npcId, otherNpcId]) { if (!id) continue; await payload.delete({ collection: "game-npcs", id, overrideAccess: true }).catch(() => {}); } for (const id of userIds) { const profiles = await payload .find({ collection: "profiles", where: { user: { equals: id } }, limit: 5, depth: 0, overrideAccess: true, }) .catch(() => null); for (const profile of profiles?.docs ?? []) { await payload .delete({ collection: "profiles", id: profile.id, overrideAccess: true }) .catch(() => {}); } await payload.delete({ collection: "users", id, overrideAccess: true }).catch(() => {}); } for (const id of roleIds) { await payload.delete({ collection: "roles", id, overrideAccess: true }).catch(() => {}); } }); it("treats only an explicit true as unreleased", () => { expect(dialogueVisibleTo({ testing: true }, false)).toBe(false); expect(dialogueVisibleTo({ testing: false }, false)).toBe(true); expect(dialogueVisibleTo({}, false)).toBe(true); // Rows written before the flag existed read back as null, not false. expect(dialogueVisibleTo({ testing: null }, false)).toBe(true); expect(dialogueVisibleTo({ testing: true }, true)).toBe(true); }); it( "reserves the superuser tier, not every author", async () => { expect(await canSeeTestingDialogues(payload, superUser)).toBe(true); expect(await canSeeTestingDialogues(payload, plainUser)).toBe(false); expect(await canSeeTestingDialogues(payload, null)).toBe(false); }, TIMEOUT, ); it( "offers a player the live conversation even when a testing one outranks it", async () => { const offered = await loadPlayerDialogue(payload, { npcId, userId: plainUser.id, canSeeTesting: await canSeeTestingDialogues(payload, plainUser), }); expect(offered?.id).toBe(liveId); }, TIMEOUT, ); it( "offers a superuser the testing conversation", async () => { const offered = await loadPlayerDialogue(payload, { npcId, userId: superUser.id, canSeeTesting: await canSeeTestingDialogues(payload, superUser), }); expect(offered?.id).toBe(testingId); }, TIMEOUT, ); it( "refuses a testing conversation named by a player, and allows a superuser", async () => { expect(await offerTo(plainUser, npcId, testingId)).toBeNull(); expect((await offerTo(superUser, npcId, testingId))?.id).toBe(testingId); // A live conversation is still readable by its id for anyone. expect((await offerTo(plainUser, npcId, liveId))?.id).toBe(liveId); }, TIMEOUT, ); it( "rejects a conversation that belongs to another NPC", async () => { expect(await offerTo(superUser, otherNpcId, testingId)).toBeNull(); }, TIMEOUT, ); it( "rejects a conversation id that does not exist", async () => { expect(await offerTo(superUser, npcId, 2_000_000_000)).toBeNull(); }, TIMEOUT, ); });