1
0
Fork 0
polaris-task-force/tests/int/npc-dialogue-reveal.int.spec.ts
Z8MB1E 66a4337ba5 feat(npc-dialogue): type conversation lines out, with unit and player control
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.
2026-11-24 17:50:11 -05:00

62 lines
2.3 KiB
TypeScript

// @vitest-environment node
import React from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { describe, expect, it } from "vitest";
import { DialogueText, visibleLength } from "@/lib/npc-dialogue/DialogueText";
/**
* The reveal is what makes a conversation line type itself out. It must never
* expose markup, so the source is parsed in full and only the rendered strings
* are truncated: a half-typed line shows `{amber}the tra` as plain text, not
* `{amb`.
*/
function render(text: string, reveal?: number): string {
return renderToStaticMarkup(React.createElement(DialogueText, { text, reveal }));
}
/** Rendered text content (the test inputs carry no entities or attributes). */
function textOf(html: string): string {
return html.replace(/<[^>]*>/g, "");
}
describe("DialogueText reveal", () => {
it("counts visible characters only, ignoring markup", () => {
expect(visibleLength("Careful, **{amber}the trail{/}** is watched.")).toBe(
"Careful, the trail is watched.".length,
);
expect(visibleLength("")).toBe(0);
// An inline image and a countdown are each one visible unit.
expect(visibleLength("Wait {cd|done} now")).toBe("Wait now".length + 1);
expect(visibleLength("Look ![map](/x.png) here")).toBe("Look here".length + 1);
});
it("reveals a prefix of the visible text at every step, never raw markup", () => {
const source = "Careful, **{amber}the trail{/}** is watched.";
const visible = "Careful, the trail is watched.";
for (let reveal = 0; reveal <= visible.length; reveal += 1) {
const shown = textOf(render(source, reveal));
expect(shown).toBe(visible.slice(0, reveal));
expect(shown).not.toContain("{");
expect(shown).not.toContain("*");
}
// Past the end the whole line renders, styled, with nothing lost.
const full = render(source, visible.length + 10);
expect(textOf(full)).toBe(visible);
expect(full).toContain("<strong");
expect(full).toContain("text-amber-400");
});
it("holds back components until the reveal reaches them", () => {
const source = "Wait {cd|done} now";
expect(render(source, 5)).not.toContain("ready");
expect(render(source, 6)).toContain("ready");
});
it("renders everything when no reveal is given", () => {
expect(textOf(render("Plain **line**"))).toBe("Plain line");
});
});