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.
168 lines
7.4 KiB
TypeScript
168 lines
7.4 KiB
TypeScript
// @vitest-environment node
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
BASE_UNIT_MS,
|
|
DEFAULT_PACING_PRESET,
|
|
PACING_PRESETS,
|
|
beatMs,
|
|
buildRevealSchedule,
|
|
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).toEqual({ 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"));
|
|
});
|
|
});
|