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