1
0
Fork 0
polaris-task-force/tests/int/npc-dialogue-keys.int.spec.ts
Z8MB1E b71e94b23d feat(npc-dialogue): announce lines to screen readers and pick choices by number
The conversation had no live region and no keyboard path, so a screen reader
user opened it and heard nothing while the only way to answer was to Tab through
buttons.

The log is now a polite live region with aria-busy while a line types. A polite
region would otherwise announce every mutation of the truncating text, so the
visible copy is hidden from assistive tech during a reveal and the whole line
sits once in an sr-only sibling that never changes.

Digits 1-9 take a choice, numbered over the selectable list so a locked option
does not consume a digit, with the number shown on the button and exposed as
aria-keyshortcuts. Enter or Space finishes the typing line or starts the next one
during the beat; Escape closes, from Radix.

Measured: sr-only copy present at full length while the visible copy read 'Hol',
removed once settled, aria-busy true then false; pressing 2 took 'Follow the
river'; Enter cut a 2587ms line to 382ms.
2026-11-24 20:17:04 -05:00

46 lines
1.7 KiB
TypeScript

// @vitest-environment node
import { describe, expect, it } from "vitest";
import { choiceIndexFromKey, choiceShortcutLabel, isSkipKey } from "@/lib/npc-dialogue/keys";
/**
* Digit shortcuts for choices. The number shown on a button and the number the
* keyboard accepts must agree, and a locked choice must not shift the numbering,
* so both are computed from the selectable list rather than the server index.
*/
describe("conversation keyboard picks", () => {
it("maps 1-9 onto the selectable choices", () => {
expect(choiceIndexFromKey("1", 4)).toBe(0);
expect(choiceIndexFromKey("2", 4)).toBe(1);
expect(choiceIndexFromKey("4", 4)).toBe(3);
});
it("ignores digits past the number of choices", () => {
expect(choiceIndexFromKey("5", 4)).toBeNull();
expect(choiceIndexFromKey("1", 0)).toBeNull();
});
it("ignores anything that is not a digit 1-9", () => {
for (const key of ["0", "a", "Enter", " ", "Escape", "ArrowDown", "10"]) {
expect(choiceIndexFromKey(key, 9)).toBeNull();
}
});
it("labels choices with the same numbers the keyboard accepts", () => {
expect([0, 1, 2].map((i) => choiceShortcutLabel(i))).toEqual(["1", "2", "3"]);
expect(choiceShortcutLabel(8)).toBe("9");
expect(choiceShortcutLabel(9)).toBeNull();
expect(choiceShortcutLabel(-1)).toBeNull();
// Every label the button shows resolves to the same choice on keypress.
for (let i = 0; i < 9; i += 1) {
expect(choiceIndexFromKey(choiceShortcutLabel(i) as string, 9)).toBe(i);
}
});
it("treats only Enter and Space as skip keys", () => {
expect(isSkipKey("Enter")).toBe(true);
expect(isSkipKey(" ")).toBe(true);
expect(isSkipKey("a")).toBe(false);
expect(isSkipKey("Escape")).toBe(false);
});
});