The system prompt was a single constant, so tuning how drafted dialogue sounds meant editing code and redeploying. Split it into the half that is copy and the half that is a contract. VOICE and optional HOUSE RULES are now editable from a new `npc-dialogue-settings` global, and per conversation through an `voiceOverride` field on `npc-dialogues`. Precedence is dialogue, then global, then the built-in default, resolved by the pure `resolveVoice`. STRUCTURE, FIELDS, STATE, OUTPUT and the worked example stay in `LOCKED_RULES`: the draft validator enforces the same rules, so an edit there would make every draft fail for reasons an author cannot trace. Both surfaces render the assembled prompt through the same pure composer the server calls, so the preview cannot drift from what the model receives. The global normalises dashes on save, bumps `revision` only on a real text change, and keeps 20 versions for rollback. Provenance gains `promptSource` and `promptRevision` so a stored draft still names the voice that shaped it. A draft that fails twice while a custom voice is active now says so, instead of looking like a model bug. `composeDialogueSystemPrompt()` with no arguments reproduces the old prompt byte for byte, pinned by a golden fixture.
105 lines
3.7 KiB
TypeScript
105 lines
3.7 KiB
TypeScript
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 }) =>
|
|
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 });
|
|
});
|
|
});
|