// @vitest-environment node import { describe, expect, it } from "vitest"; import { MAX_PAUSE_MS, visibleSegments } from "@/lib/npc-dialogue/DialogueText"; import { BASE_UNIT_MS, DEFAULT_PACING_PRESET, MAX_LINE_MS, MIN_LINE_MS, PACING_PRESETS, beatMs, buildRevealSchedule, lineDurationMs, scaleSchedule, shownAt, } from "@/lib/npc-dialogue/pacing"; /** * The typing rhythm. A plain clock cannot pause at a comma, so the reveal runs * off a schedule: each visible unit has its own cost plus a pause after * punctuation and line breaks. These tests pin the shape of that schedule, not * the exact milliseconds of every character, so tuning a preset does not turn * into a wall of rewritten assertions. */ /** How long unit `i` waits before the next one starts. */ function gapAfter(arrival: readonly number[], i: number): number { return arrival[i + 1] - arrival[i]; } const sample = "Hold, trooper. The road ahead is watched."; describe("reveal pacing", () => { it("costs a base unit per visible character when there is no punctuation", () => { const schedule = buildRevealSchedule("abc"); expect(schedule.arrival).toEqual([0, BASE_UNIT_MS, BASE_UNIT_MS * 2]); // The last unit's own cost finishes the line; no trailing pause is waited on. expect(schedule.total).toBe(BASE_UNIT_MS * 3); }); it("pauses after a comma, longer after a sentence end, longest at a break", () => { const profile = PACING_PRESETS[DEFAULT_PACING_PRESET]; const schedule = buildRevealSchedule("a,b. c\nd"); const text = schedule.text; const at = (char: string, from = 0) => text.indexOf(char, from); // The unit right after each mark waits longer than a plain neighbour. expect(gapAfter(schedule.arrival, at(","))).toBe(BASE_UNIT_MS + profile.clauseMs); expect(gapAfter(schedule.arrival, at("."))).toBe(BASE_UNIT_MS + profile.sentenceMs); expect(gapAfter(schedule.arrival, at("\n"))).toBe(BASE_UNIT_MS + profile.newlineMs); expect(gapAfter(schedule.arrival, at("a"))).toBe(BASE_UNIT_MS); expect(profile.clauseMs).toBeLessThan(profile.sentenceMs); expect(profile.sentenceMs).toBeLessThan(profile.newlineMs); expect(profile.newlineMs).toBeLessThan(profile.paragraphMs); }); it("treats a blank line as one paragraph pause, not two line pauses", () => { const profile = PACING_PRESETS[DEFAULT_PACING_PRESET]; const schedule = buildRevealSchedule("a\n\nb"); // Units: a, \n, \n, b. The first break carries the paragraph pause, the // second is free, and the character before a break charges nothing, so the // break is never paid for twice. expect(gapAfter(schedule.arrival, 0)).toBe(BASE_UNIT_MS); expect(gapAfter(schedule.arrival, 1)).toBe(BASE_UNIT_MS + profile.paragraphMs); expect(gapAfter(schedule.arrival, 2)).toBe(BASE_UNIT_MS); }); it("does not add a punctuation pause before a line break", () => { // "End." then a newline: the break supplies the pause, so the sentence mark // must not charge again on top of it. const profile = PACING_PRESETS[DEFAULT_PACING_PRESET]; const schedule = buildRevealSchedule("End.\nNext"); const period = schedule.text.indexOf("."); expect(gapAfter(schedule.arrival, period)).toBe(BASE_UNIT_MS); const newline = schedule.text.indexOf("\n"); expect(gapAfter(schedule.arrival, newline)).toBe(BASE_UNIT_MS + profile.newlineMs); }); it("charges the pause after a closing quote, not inside it", () => { const profile = PACING_PRESETS[DEFAULT_PACING_PRESET]; const schedule = buildRevealSchedule('"Fine." Then'); const quote = schedule.text.indexOf('"', 1); expect(gapAfter(schedule.arrival, quote)).toBe(BASE_UNIT_MS + profile.sentenceMs); }); it("leaves a thousands separator inside a number unpaused", () => { const profile = PACING_PRESETS[DEFAULT_PACING_PRESET]; const schedule = buildRevealSchedule("1,000 rounds"); const comma = schedule.text.indexOf(","); expect(gapAfter(schedule.arrival, comma)).toBe(BASE_UNIT_MS); // A comma between a digit and a letter still reads as a clause break. const prose = buildRevealSchedule("50, or so"); expect(gapAfter(prose.arrival, prose.text.indexOf(","))).toBe( BASE_UNIT_MS + profile.clauseMs, ); }); it("scales to a target duration without changing the rhythm", () => { const schedule = buildRevealSchedule(sample); const doubled = scaleSchedule(schedule, schedule.total * 2); const plain = scaleSchedule(schedule, schedule.total); expect(plain.durationMs).toBeCloseTo(schedule.total, 5); // Every arrival moves by the same factor, so a long line paced to a voice // clip keeps its pauses in proportion instead of flattening them. const ratio = doubled.arrivalMs[5] / plain.arrivalMs[5]; expect(ratio).toBeCloseTo(2, 5); expect(doubled.tickMs[0] / plain.tickMs[0]).toBeCloseTo(2, 5); }); it("counts arrivals the same way the reveal loop does", () => { const schedule = buildRevealSchedule(sample); const scaled = scaleSchedule(schedule, schedule.total); const last = scaled.arrivalMs[scaled.arrivalMs.length - 1]; expect(shownAt(scaled.arrivalMs, -1)).toBe(0); expect(shownAt(scaled.arrivalMs, 0)).toBe(1); // The last unit arrives a base cost before the line is "done", so the line // never waits on a trailing pause once every character is visible. expect(last).toBeLessThan(scaled.durationMs); expect(shownAt(scaled.arrivalMs, last - 1)).toBe(schedule.arrival.length - 1); expect(shownAt(scaled.arrivalMs, last)).toBe(schedule.arrival.length); }); it("places ticks on visible characters, spaced out and ascending", () => { const schedule = buildRevealSchedule(sample, "natural"); expect(schedule.ticks.length).toBeGreaterThan(3); for (let i = 1; i < schedule.ticks.length; i += 1) { expect(schedule.ticks[i]).toBeGreaterThan(schedule.ticks[i - 1]); expect(schedule.ticks[i] - schedule.ticks[i - 1]).toBeGreaterThanOrEqual(95); } // A tick only ever lands on a unit that actually appears on screen. for (const tick of schedule.ticks) { const index = schedule.arrival.indexOf(tick); expect(index).toBeGreaterThanOrEqual(0); expect(schedule.text[index].trim().length).toBeGreaterThan(0); } }); it("keeps a line's rhythm stable across rebuilds", () => { // Seeded from the text, so a re-render cannot re-roll the ticks mid-line. const first = buildRevealSchedule(sample); const second = buildRevealSchedule(sample); expect(second.ticks).toEqual(first.ticks); expect(buildRevealSchedule(`${sample}?`).ticks).not.toEqual(first.ticks); }); it("has nothing to schedule for an empty line", () => { const empty = buildRevealSchedule(""); expect(empty).toMatchObject({ arrival: [], total: 0, ticks: [], text: "" }); expect(scaleSchedule(empty, 900)).toEqual({ arrivalMs: [], tickMs: [], durationMs: 900 }); }); it("follows the preset it is given, and falls back to natural", () => { const subtle = PACING_PRESETS.subtle; const pronounced = PACING_PRESETS.pronounced; const text = "Hold, trooper."; const comma = text.indexOf(","); // Each preset moves speed and pauses together, so a character and the pause // after a comma both cost its own base. expect(gapAfter(buildRevealSchedule(text, "subtle").arrival, comma)).toBe( subtle.baseMs + subtle.clauseMs, ); expect(gapAfter(buildRevealSchedule(text, "pronounced").arrival, comma)).toBe( pronounced.baseMs + pronounced.clauseMs, ); expect(subtle.baseMs).toBeLessThan(PACING_PRESETS.natural.baseMs); expect(PACING_PRESETS.natural.baseMs).toBeLessThan(pronounced.baseMs); expect(BASE_UNIT_MS).toBe(PACING_PRESETS.natural.baseMs); expect(beatMs("subtle")).toBeLessThan(beatMs("natural")); expect(beatMs("natural")).toBeLessThan(beatMs("pronounced")); expect(beatMs("unknown" as never)).toBe(beatMs("natural")); }); }); describe("authored pauses", () => { it("keeps the marker out of the line and reports where it sat", () => { const segments = visibleSegments("Hold{1.2s} there."); expect(segments.text).toBe("Hold there."); expect(segments.pauses).toEqual([{ at: 4, ms: 1200 }]); }); it("reads both units, and refuses nonsense", () => { expect(visibleSegments("a{400ms}b").pauses).toEqual([{ at: 1, ms: 400 }]); expect(visibleSegments("a{2S}b").pauses).toEqual([{ at: 1, ms: 2000 }]); // Not a pause token: it stays literal text, like any unknown brace run. expect(visibleSegments("a{1.2x}b").text).toBe("a{1.2x}b"); expect(visibleSegments("a{1.2x}b").pauses).toEqual([]); // Clamped, so a typo cannot park a player for minutes. expect(visibleSegments("a{90s}b").pauses).toEqual([{ at: 1, ms: MAX_PAUSE_MS }]); }); it("costs exactly the authored time, on top of the base pace", () => { const plain = buildRevealSchedule("ab"); const paused = buildRevealSchedule("a{1.2s}b"); expect(paused.text).toBe("ab"); expect(paused.explicitTotal).toBe(1200); expect(paused.pauses).toEqual([1200, 0]); expect(gapAfter(paused.arrival, 0)).toBe(BASE_UNIT_MS + 1200); expect(paused.total - plain.total).toBe(1200); }); it("stays absolute when the line is stretched or squeezed to fit", () => { const schedule = buildRevealSchedule("a{1.2s}bcd"); const stretched = scaleSchedule(schedule, schedule.total * 3); const squeezed = scaleSchedule(schedule, 2000); // Two neighbouring units across the pause: the extra 1.2s is still there. const gap = (arrival: readonly number[]) => arrival[1] - arrival[0]; expect(gap(stretched.arrivalMs) - BASE_UNIT_MS * 3).toBeGreaterThanOrEqual(1200); expect(gap(squeezed.arrivalMs)).toBeGreaterThanOrEqual(1200); }); }); describe("line duration", () => { it("does not compress a long line below the base pace", () => { // 400 characters at the old 3s ceiling ran at 7.5ms per character. const schedule = buildRevealSchedule("word ".repeat(80).trim()); const duration = lineDurationMs(schedule); expect(duration).toBe(schedule.total); expect(duration / schedule.text.length).toBeGreaterThanOrEqual(BASE_UNIT_MS - 1); }); it("still has a ceiling, and a floor for a very short line", () => { const wall = buildRevealSchedule("word ".repeat(400).trim()); expect(lineDurationMs(wall)).toBe(MAX_LINE_MS); expect(lineDurationMs(buildRevealSchedule("Hi."))).toBeGreaterThanOrEqual(MIN_LINE_MS); }); it("lets a clip set the target but never cuts an authored pause", () => { const schedule = buildRevealSchedule("a{1.2s}bcd"); expect(lineDurationMs(schedule, 4000)).toBe(4000); expect(lineDurationMs(schedule, 600)).toBeGreaterThanOrEqual(1200); }); });