/** * Tests of the global keymap: parsing Lutra notation (`Mod` per platform), * matching by key code independent of what Alt and Shift type, building * the keymap from command descriptions and formatting shortcuts for * display. */ import { describe, expect, it } from "vitest"; import { buildKeymap, commandFor, eventKey, formatShortcut, matches, parseShortcut } from "../../src/commands/keymap"; /** * A key event. * * @param init - its properties. */ function key(init: KeyboardEventInit): KeyboardEvent { return new KeyboardEvent("keydown", init); } describe("parseShortcut", () => { it("maps Mod to Ctrl or Cmd", () => { expect(parseShortcut("Mod+Shift+P", false)).toEqual({ ctrl: true, meta: false, alt: false, shift: true, key: "p" }); expect(parseShortcut("Mod+Shift+P", true)).toEqual({ ctrl: false, meta: true, alt: false, shift: true, key: "p" }); }); it("reads punctuation, digits and the plus key", () => { expect(parseShortcut("Mod+,", false)?.key).toBe(","); expect(parseShortcut("Mod+1", false)?.key).toBe("1"); expect(parseShortcut("Mod++", false)).toEqual({ ctrl: true, meta: false, alt: false, shift: false, key: "+" }); }); it("rejects unknown modifiers and missing keys", () => { expect(parseShortcut("Hyper+S", false)).toBeNull(); expect(parseShortcut("Mod+", false)).toBeNull(); expect(parseShortcut("", false)).toBeNull(); expect(parseShortcut("Mod++S", false)).toBeNull(); }); }); describe("matching", () => { it("uses the key code for letters, digits and punctuation", () => { expect(eventKey(key({ key: "ß", code: "KeyS" }))).toBe("s"); expect(eventKey(key({ key: "!", code: "Digit1" }))).toBe("1"); expect(eventKey(key({ key: ";", code: "Comma" }))).toBe(","); expect(eventKey(key({ key: "F4", code: "F4" }))).toBe("f4"); }); it("requires the exact modifiers", () => { const shortcut = parseShortcut("Mod+Shift+P", false); expect(shortcut).not.toBeNull(); if (!shortcut) { return; } expect(matches(key({ key: "P", code: "KeyP", ctrlKey: true, shiftKey: true }), shortcut)).toBe(true); expect(matches(key({ key: "p", code: "KeyP", ctrlKey: true }), shortcut)).toBe(false); expect(matches(key({ key: "P", code: "KeyP", ctrlKey: true, shiftKey: true, altKey: true }), shortcut)).toBe(false); }); it("finds the command of a key press, the first listed winning", () => { const keymap = buildKeymap( [ { id: "palette.open", defaultShortcut: "Mod+Shift+P" }, { id: "settings.open", defaultShortcut: "Mod+," }, { id: "view.toggleProjectTree", defaultShortcut: "Mod+1" }, { id: "other.palette", defaultShortcut: "Mod+Shift+P" }, { id: "no.shortcut", defaultShortcut: null }, { id: "bad.shortcut", defaultShortcut: "Hyper+X" }, ], false, ); expect(keymap.map((b) => b.command)).toEqual(["palette.open", "settings.open", "view.toggleProjectTree", "other.palette"]); expect(commandFor(keymap, key({ key: "P", code: "KeyP", ctrlKey: true, shiftKey: true }))).toBe("palette.open"); expect(commandFor(keymap, key({ key: ",", code: "Comma", ctrlKey: true }))).toBe("settings.open"); expect(commandFor(keymap, key({ key: "1", code: "Digit1", ctrlKey: true }))).toBe("view.toggleProjectTree"); expect(commandFor(keymap, key({ key: "1", code: "Digit1", metaKey: true }))).toBeNull(); }); }); describe("formatShortcut", () => { it("names the modifiers of the platform", () => { expect(formatShortcut("Mod+Shift+P", false)).toBe("Ctrl+Shift+P"); expect(formatShortcut("Mod+Alt+S", true)).toBe("Option+Cmd+S"); expect(formatShortcut("Mod+,", false)).toBe("Ctrl+,"); expect(formatShortcut("Shift+F4", false)).toBe("Shift+F4"); expect(formatShortcut("Nope+X", false)).toBeNull(); }); });