1
0
Fork 0
polaris-task-force/tests/int/npc-dialogue-pacing.int.spec.ts
Z8MB1E 1db89addf5 feat(npc-dialogue): hold the typing with an authored pause, and stop squashing long lines
Two things, both about the pace of a line arriving.

An author can now put a beat anywhere in a line with {1.2s} or {400ms}, clamped
to ten seconds. The marker renders nothing; the renderer records where it sat and
the pacing schedule charges that gap between the units either side. Pauses are
absolute, so matching a line to a voice clip stretches or squeezes the typed part
and leaves the authored beat alone. The editor's line help names the marker.

The speed complaint had a mechanical cause. Lines were capped at three seconds,
so anything past about 120 characters was silently compressed: 240 characters ran
at 13ms per character and 360 at 8, against a base of 30. Long lines deeper in a
conversation read as if the typing had sped up. The cap is now twenty seconds,
which only catches a wall of text, and the base unit went from 24ms to 30ms
(about 33 characters a second) because 24 measured as a blur in the app.

The duration rule moved into pacing.ts as lineDurationMs so it is testable
without a browser. One nearby bug fell out of the marker work: when a brace run
turned out not to be markup, the renderer had already spent budget on the text
inside it and then rendered the run literally, so those characters were counted
twice. It now restores what the failed attempt consumed.

Measured in the running app: a {1.2s} marker held the line 1237ms after the fourth
character and never appeared on screen; a 360 character line took 12.5s instead of
3s; a short line still lands at once. 253 tests across 24 files pass.
2026-12-01 13:00:50 -05:00

232 lines
10 KiB
TypeScript

// @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(",");
expect(gapAfter(buildRevealSchedule(text, "subtle").arrival, comma)).toBe(
BASE_UNIT_MS + subtle.clauseMs,
);
expect(gapAfter(buildRevealSchedule(text, "pronounced").arrival, comma)).toBe(
BASE_UNIT_MS + pronounced.clauseMs,
);
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);
});
});