command-keybind.test.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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("matchKeybind supports bracket keys", () => {
  30. const keybinds = parseKeybind("mod+alt+[, mod+alt+]")
  31. const prev = keybinds[0]
  32. const next = keybinds[1]
  33. expect(
  34. matchKeybind(
  35. keybinds,
  36. new KeyboardEvent("keydown", { key: "[", ctrlKey: prev?.ctrl, metaKey: prev?.meta, altKey: true }),
  37. ),
  38. ).toBe(true)
  39. expect(
  40. matchKeybind(
  41. keybinds,
  42. new KeyboardEvent("keydown", { key: "]", ctrlKey: next?.ctrl, metaKey: next?.meta, altKey: true }),
  43. ),
  44. ).toBe(true)
  45. })
  46. test("formatKeybind returns human readable output", () => {
  47. const display = formatKeybind("ctrl+alt+arrowup")
  48. expect(display).toContain("↑")
  49. expect(display.includes("Ctrl") || display.includes("⌃")).toBe(true)
  50. expect(display.includes("Alt") || display.includes("⌥")).toBe(true)
  51. expect(formatKeybind("none")).toBe("")
  52. })
  53. test("formatKeybind prefers the first combo", () => {
  54. const display = formatKeybind("mod+k,mod+p")
  55. expect(display.includes("K") || display.includes("k")).toBe(true)
  56. expect(display.includes("P") || display.includes("p")).toBe(false)
  57. })
  58. })