keybind-plugin.test.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { describe, expect, test } from "bun:test"
  2. import type { ParsedKey } from "@opentui/core"
  3. import { createPluginKeybind } from "../../../src/cli/cmd/tui/context/plugin-keybinds"
  4. describe("createPluginKeybind", () => {
  5. const defaults = {
  6. open: "ctrl+o",
  7. close: "escape",
  8. }
  9. test("uses defaults when overrides are missing", () => {
  10. const api = {
  11. match: () => false,
  12. print: (key: string) => key,
  13. }
  14. const bind = createPluginKeybind(api, defaults)
  15. expect(bind.all).toEqual(defaults)
  16. expect(bind.get("open")).toBe("ctrl+o")
  17. expect(bind.get("close")).toBe("escape")
  18. })
  19. test("applies valid overrides", () => {
  20. const api = {
  21. match: () => false,
  22. print: (key: string) => key,
  23. }
  24. const bind = createPluginKeybind(api, defaults, {
  25. open: "ctrl+alt+o",
  26. close: "q",
  27. })
  28. expect(bind.all).toEqual({
  29. open: "ctrl+alt+o",
  30. close: "q",
  31. })
  32. })
  33. test("ignores invalid overrides", () => {
  34. const api = {
  35. match: () => false,
  36. print: (key: string) => key,
  37. }
  38. const bind = createPluginKeybind(api, defaults, {
  39. open: " ",
  40. close: 1,
  41. extra: "ctrl+x",
  42. })
  43. expect(bind.all).toEqual(defaults)
  44. expect(bind.get("extra")).toBe("extra")
  45. })
  46. test("resolves names for match", () => {
  47. const list: string[] = []
  48. const api = {
  49. match: (key: string) => {
  50. list.push(key)
  51. return true
  52. },
  53. print: (key: string) => key,
  54. }
  55. const bind = createPluginKeybind(api, defaults, {
  56. open: "ctrl+shift+o",
  57. })
  58. bind.match("open", { name: "x" } as ParsedKey)
  59. bind.match("ctrl+k", { name: "x" } as ParsedKey)
  60. expect(list).toEqual(["ctrl+shift+o", "ctrl+k"])
  61. })
  62. test("resolves names for print", () => {
  63. const list: string[] = []
  64. const api = {
  65. match: () => false,
  66. print: (key: string) => {
  67. list.push(key)
  68. return `print:${key}`
  69. },
  70. }
  71. const bind = createPluginKeybind(api, defaults, {
  72. close: "q",
  73. })
  74. expect(bind.print("close")).toBe("print:q")
  75. expect(bind.print("ctrl+p")).toBe("print:ctrl+p")
  76. expect(list).toEqual(["q", "ctrl+p"])
  77. })
  78. })