command-keybind.test.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { describe, expect, test } from "bun:test"
  2. import { formatKeybind, matchKeybind, parseKeybind } from "./command"
  3. describe("command keybind helpers", () => {
  4. test("parseKeybind handles aliases and multiple combos", () => {
  5. const keybinds = parseKeybind("control+option+k, mod+shift+comma")
  6. expect(keybinds).toHaveLength(2)
  7. expect(keybinds[0]).toEqual({
  8. key: "k",
  9. ctrl: true,
  10. meta: false,
  11. shift: false,
  12. alt: true,
  13. })
  14. expect(keybinds[1]?.shift).toBe(true)
  15. expect(keybinds[1]?.key).toBe("comma")
  16. expect(Boolean(keybinds[1]?.ctrl || keybinds[1]?.meta)).toBe(true)
  17. })
  18. test("parseKeybind treats none and empty as disabled", () => {
  19. expect(parseKeybind("none")).toEqual([])
  20. expect(parseKeybind("")).toEqual([])
  21. })
  22. test("matchKeybind normalizes punctuation keys", () => {
  23. const keybinds = parseKeybind("ctrl+comma, shift+plus, meta+space")
  24. expect(matchKeybind(keybinds, new KeyboardEvent("keydown", { key: ",", ctrlKey: true }))).toBe(true)
  25. expect(matchKeybind(keybinds, new KeyboardEvent("keydown", { key: "+", shiftKey: true }))).toBe(true)
  26. expect(matchKeybind(keybinds, new KeyboardEvent("keydown", { key: " ", metaKey: true }))).toBe(true)
  27. expect(matchKeybind(keybinds, new KeyboardEvent("keydown", { key: ",", ctrlKey: true, altKey: true }))).toBe(false)
  28. })
  29. test("formatKeybind returns human readable output", () => {
  30. const display = formatKeybind("ctrl+alt+arrowup")
  31. expect(display).toContain("↑")
  32. expect(display.includes("Ctrl") || display.includes("⌃")).toBe(true)
  33. expect(display.includes("Alt") || display.includes("⌥")).toBe(true)
  34. expect(formatKeybind("none")).toBe("")
  35. })
  36. })