import type { Payload } from "payload"; import { beforeAll, describe, expect, it } from "vitest"; import { getTestPayload } from "../helpers/get-test-payload"; import { loadDialogueSettings } from "@/lib/npc-dialogue/generationSettings"; /** * The settings global is behaviour, not just a form: the hook normalises the text * an author pastes and decides when the revision moves, and the generator reads it * back through `loadDialogueSettings`. A round trip through real Payload is the * only way to know both halves agree. * * Assertions are relative: the global persists across the tests in this file, so * an absolute revision would only be asserting test order. */ let payload: Payload; let seq = 0; beforeAll(async () => { payload = await getTestPayload(); }); const update = (data: { voice?: string | null; extraRules?: string | null; presentation?: { typewriterDefault?: boolean | null; pacing?: string | null }; }) => payload.updateGlobal({ slug: "npc-dialogue-settings", data, overrideAccess: true, }); /** Text that has never been stored before, so the change is always a change. */ const unique = (prefix: string) => `${prefix} ${++seq}`; describe("npc-dialogue-settings global", () => { it("normalises dashes on save", async () => { const doc = await update({ voice: "Short \u2014 blunt.", extraRules: null }); expect(doc.voice).toBe("Short, blunt."); expect(typeof doc.revision).toBe("number"); }); it("does not bump the revision when the text is re-saved unchanged", async () => { const voice = unique("Stable text."); const first = await update({ voice, extraRules: null }); const second = await update({ voice, extraRules: null }); expect(second.revision).toBe(first.revision); }); it("bumps the revision by one when the text changes", async () => { const before = await update({ voice: unique("Voice"), extraRules: null }); const after = await update({ voice: unique("Voice"), extraRules: null }); expect(after.revision).toBe((before.revision ?? 0) + 1); }); it("counts house rules as a change, because they are part of the prompt", async () => { const voice = unique("Voice"); const before = await update({ voice, extraRules: null }); const after = await update({ voice, extraRules: unique("Rule") }); expect(after.revision).toBe((before.revision ?? 0) + 1); }); it("treats blank text as unset rather than storing whitespace", async () => { const doc = await update({ voice: " \n ", extraRules: "" }); expect(doc.voice ?? null).toBeNull(); expect(doc.extraRules ?? null).toBeNull(); }); it("keeps prompt history so a bad edit can be reverted", async () => { await update({ voice: unique("Version one.") }); await update({ voice: unique("Version two.") }); const versions = await payload.findGlobalVersions({ slug: "npc-dialogue-settings", limit: 10, overrideAccess: true, }); expect(versions.docs.length).toBeGreaterThanOrEqual(2); }); it("reads back through loadDialogueSettings, which is what the generator uses", async () => { const voice = unique("Read by the generator."); await update({ voice, extraRules: "Unit convention." }); const settings = await loadDialogueSettings(payload); expect(settings.voice).toBe(voice); expect(settings.extraRules).toBe("Unit convention."); expect(settings.revision).toBeGreaterThanOrEqual(1); }); it("falls back to the built-in defaults when the settings cannot be read", async () => { const settings = await loadDialogueSettings({ findGlobal: async () => { throw new Error("relation not found"); }, logger: { warn: () => undefined }, } as unknown as Payload); expect(settings).toEqual({ voice: null, extraRules: null, revision: null, typewriterDefault: true, pacing: "natural", }); }); it("carries the typewriter default without disturbing the prompt revision", async () => { const voice = unique("Unchanged by presentation."); const before = await update({ voice, presentation: { typewriterDefault: true } }); const revision = before.revision; // Turning the animation off is not a prompt change: stored drafts must not // look like they came from different instructions. const off = await update({ presentation: { typewriterDefault: false } }); expect(off.revision).toBe(revision); expect((await loadDialogueSettings(payload)).typewriterDefault).toBe(false); const on = await update({ presentation: { typewriterDefault: true } }); expect(on.revision).toBe(revision); expect((await loadDialogueSettings(payload)).typewriterDefault).toBe(true); }); it("carries the typing pace, and falls back to natural for a value it does not know", async () => { const voice = unique("Unchanged by pace."); const before = await update({ voice, presentation: { pacing: "pronounced" } }); expect((await loadDialogueSettings(payload)).pacing).toBe("pronounced"); // Pace is presentation, not prompt copy: stored drafts are unaffected. expect(before.revision).toBe((await loadDialogueSettings(payload)).revision); // A row written with a value this build does not know (an older or newer // admin) must still render at a real pace rather than none. const stale = await loadDialogueSettings({ findGlobal: async () => ({ presentation: { pacing: "molasses" } }), logger: { warn: () => undefined }, } as unknown as Payload); expect(stale.pacing).toBe("natural"); await update({ presentation: { pacing: "subtle" } }); expect((await loadDialogueSettings(payload)).pacing).toBe("subtle"); await update({ presentation: { pacing: "natural" } }); }); });