1
0
Fork 0
polaris-task-force/tests/int/npc-dialogue-settings-global.int.spec.ts
Z8MB1E bf9fd09752 perf(npc-dialogue): stop re-parsing the markup preview on every keystroke
Typing a line in the editor felt sticky. The line box renders a live markup
preview, and that preview is a full parse and render of the authored string: it
ran on every character, so a long line with colours, bold and a link was re-parsed
per keypress while the whole edit dialog re-rendered around it.

The preview now waits 220ms after the last keystroke (the textarea itself stays
instant), and both the preview and DialogueText are memoised, so an unchanged
line never re-parses. DialogueText is shared with the canvas, which re-renders
every card on any diagram change.

Measured in the running app on a 24 node diagram and a 150 character line, 40
programmatic keystrokes through the browser's own metrics: script 319ms to 221ms
(8.0 to 5.5 per key), total main-thread task 767ms to 633ms, style recalc 386ms
to 338ms. The preview still updates 220ms after typing stops.

What is left is the dialog re-render itself: on this file a keystroke still
re-renders every section of the edit dialog. Removing that means giving the line
box its own state and committing on a debounce, which needs the Save path to
flush, and belongs after the in-progress poll work in NodeEditDialog lands.
2026-12-01 14:12:09 -05:00

154 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?: "subtle" | "natural" | "pronounced" | 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" } });
});
});