Conversation lines appeared whole after a length-based pause with bouncing
dots. They now type themselves out, and the player can finish a line by
clicking it.
Control is two-tier: npc-dialogue-settings.presentation.typewriterDefault sets
the unit default, users.preferences.dialogue.typewriter (default/on/off) lets a
player override it either way. prefers-reduced-motion turns the animation off
regardless. Changing the unit setting does not bump the prompt revision, since
it is not prompt text.
The reveal truncates the rendered strings rather than the source, so a
half-typed line can never flash {amber} or an unclosed **. visibleLength()
gives pacing the same count, and the choice footer waits for the reveal to
land so nobody answers a line they have not seen.
130 lines
4.6 KiB
TypeScript
130 lines
4.6 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 };
|
|
}) =>
|
|
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,
|
|
});
|
|
});
|
|
|
|
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);
|
|
});
|
|
});
|