1
0
Fork 0
polaris-task-force/tests/int/npc-dialogue-reveal.int.spec.ts
Z8MB1E 464ba2d5d4 feat(npc-dialogue): natural typing rhythm, a beat between lines, a typing sound
The reveal was one linear clock, so every character cost the same and a comma
had nowhere to breathe. It now runs off a schedule: each visible unit costs
BASE_UNIT_MS plus a pause after it, longer at a sentence end and longest at a
line or paragraph break. Pauses never stack (a break after a full stop owns the
pause) and a comma inside a number is not a clause. The schedule is built in
relative units and scaled to the target duration, so a line paced to a recorded
clip keeps the rhythm instead of flattening it.

Between two spoken lines there is now a beat with the speaker's typing dots, and
clicking during it starts the next line at once. The opening line of a cascade
gets no beat.

The typing sound is synthesized (one tick per 55ms at most) from precomputed,
seeded tick times, so a re-render cannot re-roll the rhythm. It stays silent on a
line with a recorded clip, when the header mute is on, and under reduced motion.
It follows a new account preference, users.preferences.dialogue.typewriterSound,
separate from the voice switch, with a migration.

visibleLength is now defined as visibleText(text).length, so the schedule and the
renderer cannot disagree about how long a line is.
2026-11-24 19:20:11 -05:00

90 lines
3.4 KiB
TypeScript

// @vitest-environment node
import React from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { describe, expect, it } from "vitest";
import { DialogueText, visibleLength, visibleText } 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");
});
it("flattens the same units the reveal counts", () => {
// The pacing schedule is built from `visibleText`, so the two must agree
// character for character; a disagreement would make a line finish early or
// hang one unit short.
const cases = [
"Careful, **{amber}the trail{/}** is watched.",
"- one\n- two\n\n1. three",
"Wait {cd|done} now",
"Look ![map](/x.png) here",
"A `code run` inside prose.",
"Line one\nline two",
"",
];
for (const source of cases) {
expect(visibleText(source).length).toBe(visibleLength(source));
}
expect(visibleText("Careful, **{amber}the trail{/}** is watched.")).toBe(
"Careful, the trail is watched.",
);
// A list marker is formatting, not text: it costs nothing and is not shown.
expect(visibleText("- one")).toBe("one");
});
it("gives an image and a countdown one placeholder unit each", () => {
expect(visibleText("Wait {cd|done} now")).toHaveLength("Wait now".length + 1);
expect(visibleText("Look ![map](/x.png) here")).toHaveLength("Look here".length + 1);
});
});