// @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(" { 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); }); });