1
0
Fork 0
polaris-task-force/tests/int/npc-dialogue-settings-global.int.spec.ts
Z8MB1E 4ed5b4ee79 feat(npc-dialogue): choose the conversation typing pace in the admin panel
The pace was a code constant, so changing it meant a build and a deploy. It is
now a select on the NPC Dialogue Prompt global (Presentation, "Typing pace") with
three presets, and a change applies to the next conversation opened.

Each preset moves the character cost and the punctuation pauses together, because
a slow character rate with brisk pauses reads as broken rather than deliberate:
subtle 22ms a character (about 45 a second), natural 30ms (about 33), pronounced
42ms (about 24). natural stays the default. The per-unit cost moved into
PauseProfile, and BASE_UNIT_MS is now derived from natural, so the existing
schedule tests keep their meaning.

loadDialogueSettings reads presentation.pacing through normalizePacingPreset: an
unrecognised stored value (an older row, a newer admin, a typo) falls back to
natural instead of leaving the conversation with no pacing at all. The contact
page reads the global once for both presentation defaults and passes the preset to
NpcConversation, which uses it for the reveal schedule and the beat between lines.

Measured in the running app on one fixed 71 character line: subtle 2377ms, natural
2880ms, pronounced 3627ms, and the admin select renders with Natural selected.
Migration 20261201_180344 applied to dev. 220 npc-dialogue tests pass.
2026-12-01 13:09:17 -05:00

151 lines
5.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;
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" } });
});
});