|
| 1 | +import { describe, test, expect } from "bun:test" |
| 2 | +import { Keybind } from "../../src/util/keybind" |
| 3 | + |
| 4 | +describe("Keybind.parse", () => { |
| 5 | + test("returns empty array for 'none'", () => { |
| 6 | + expect(Keybind.parse("none")).toEqual([]) |
| 7 | + }) |
| 8 | + |
| 9 | + test("parses simple key", () => { |
| 10 | + const [info] = Keybind.parse("a") |
| 11 | + expect(info.name).toBe("a") |
| 12 | + expect(info.ctrl).toBe(false) |
| 13 | + expect(info.meta).toBe(false) |
| 14 | + expect(info.shift).toBe(false) |
| 15 | + expect(info.leader).toBe(false) |
| 16 | + }) |
| 17 | + |
| 18 | + test("parses ctrl+key combo", () => { |
| 19 | + const [info] = Keybind.parse("ctrl+s") |
| 20 | + expect(info.ctrl).toBe(true) |
| 21 | + expect(info.name).toBe("s") |
| 22 | + }) |
| 23 | + |
| 24 | + test("parses multi-modifier combo ctrl+shift+a", () => { |
| 25 | + const [info] = Keybind.parse("ctrl+shift+a") |
| 26 | + expect(info.ctrl).toBe(true) |
| 27 | + expect(info.shift).toBe(true) |
| 28 | + expect(info.name).toBe("a") |
| 29 | + }) |
| 30 | + |
| 31 | + test("recognizes 'alt', 'meta', and 'option' as meta modifier", () => { |
| 32 | + for (const alias of ["alt", "meta", "option"]) { |
| 33 | + const [info] = Keybind.parse(`${alias}+x`) |
| 34 | + expect(info.meta).toBe(true) |
| 35 | + expect(info.name).toBe("x") |
| 36 | + } |
| 37 | + }) |
| 38 | + |
| 39 | + test("parses super modifier", () => { |
| 40 | + const [info] = Keybind.parse("super+s") |
| 41 | + expect(info.super).toBe(true) |
| 42 | + expect(info.name).toBe("s") |
| 43 | + }) |
| 44 | + |
| 45 | + test("parses <leader> prefix", () => { |
| 46 | + const [info] = Keybind.parse("<leader>a") |
| 47 | + expect(info.leader).toBe(true) |
| 48 | + expect(info.name).toBe("a") |
| 49 | + }) |
| 50 | + |
| 51 | + test("normalizes 'esc' to 'escape'", () => { |
| 52 | + const [info] = Keybind.parse("esc") |
| 53 | + expect(info.name).toBe("escape") |
| 54 | + }) |
| 55 | + |
| 56 | + test("parses comma-separated multi-binding", () => { |
| 57 | + const bindings = Keybind.parse("ctrl+a,ctrl+b") |
| 58 | + expect(bindings).toHaveLength(2) |
| 59 | + expect(bindings[0].name).toBe("a") |
| 60 | + expect(bindings[0].ctrl).toBe(true) |
| 61 | + expect(bindings[1].name).toBe("b") |
| 62 | + expect(bindings[1].ctrl).toBe(true) |
| 63 | + }) |
| 64 | +}) |
| 65 | + |
| 66 | +describe("Keybind.toString", () => { |
| 67 | + test("returns empty string for undefined", () => { |
| 68 | + expect(Keybind.toString(undefined)).toBe("") |
| 69 | + }) |
| 70 | + |
| 71 | + test("formats ctrl+key", () => { |
| 72 | + const result = Keybind.toString({ |
| 73 | + ctrl: true, meta: false, shift: false, super: false, leader: false, name: "s", |
| 74 | + }) |
| 75 | + expect(result).toBe("ctrl+s") |
| 76 | + }) |
| 77 | + |
| 78 | + test("formats meta as 'alt'", () => { |
| 79 | + const result = Keybind.toString({ |
| 80 | + ctrl: false, meta: true, shift: false, super: false, leader: false, name: "x", |
| 81 | + }) |
| 82 | + expect(result).toBe("alt+x") |
| 83 | + }) |
| 84 | + |
| 85 | + test("formats super modifier", () => { |
| 86 | + const result = Keybind.toString({ |
| 87 | + ctrl: false, meta: false, shift: false, super: true, leader: false, name: "s", |
| 88 | + }) |
| 89 | + expect(result).toBe("super+s") |
| 90 | + }) |
| 91 | + |
| 92 | + test("formats leader prefix with key", () => { |
| 93 | + const result = Keybind.toString({ |
| 94 | + ctrl: false, meta: false, shift: false, super: false, leader: true, name: "a", |
| 95 | + }) |
| 96 | + expect(result).toBe("<leader> a") |
| 97 | + }) |
| 98 | + |
| 99 | + test("formats leader-only (no key)", () => { |
| 100 | + const result = Keybind.toString({ |
| 101 | + ctrl: false, meta: false, shift: false, super: false, leader: true, name: "", |
| 102 | + }) |
| 103 | + expect(result).toBe("<leader>") |
| 104 | + }) |
| 105 | + |
| 106 | + test("maps 'delete' to 'del'", () => { |
| 107 | + const result = Keybind.toString({ |
| 108 | + ctrl: false, meta: false, shift: false, super: false, leader: false, name: "delete", |
| 109 | + }) |
| 110 | + expect(result).toBe("del") |
| 111 | + }) |
| 112 | +}) |
| 113 | + |
| 114 | +describe("Keybind.match", () => { |
| 115 | + test("returns false for undefined first argument", () => { |
| 116 | + const b: Keybind.Info = { ctrl: true, meta: false, shift: false, super: false, leader: false, name: "s" } |
| 117 | + expect(Keybind.match(undefined, b)).toBe(false) |
| 118 | + }) |
| 119 | + |
| 120 | + test("matches identical bindings", () => { |
| 121 | + const a: Keybind.Info = { ctrl: true, meta: false, shift: false, super: false, leader: false, name: "s" } |
| 122 | + const b: Keybind.Info = { ctrl: true, meta: false, shift: false, super: false, leader: false, name: "s" } |
| 123 | + expect(Keybind.match(a, b)).toBe(true) |
| 124 | + }) |
| 125 | + |
| 126 | + test("treats missing super as false (normalization)", () => { |
| 127 | + // Simulate an Info object where super is undefined (e.g. from older code) |
| 128 | + const a = { ctrl: false, meta: false, shift: false, leader: false, name: "a" } as Keybind.Info |
| 129 | + const b: Keybind.Info = { ctrl: false, meta: false, shift: false, super: false, leader: false, name: "a" } |
| 130 | + expect(Keybind.match(a, b)).toBe(true) |
| 131 | + }) |
| 132 | + |
| 133 | + test("does not match different keys", () => { |
| 134 | + const a: Keybind.Info = { ctrl: true, meta: false, shift: false, super: false, leader: false, name: "s" } |
| 135 | + const b: Keybind.Info = { ctrl: true, meta: false, shift: false, super: false, leader: false, name: "x" } |
| 136 | + expect(Keybind.match(a, b)).toBe(false) |
| 137 | + }) |
| 138 | +}) |
| 139 | + |
| 140 | +describe("Keybind.parse → Keybind.toString roundtrip", () => { |
| 141 | + test("roundtrips ctrl+shift+a", () => { |
| 142 | + const [parsed] = Keybind.parse("ctrl+shift+a") |
| 143 | + expect(Keybind.toString(parsed)).toBe("ctrl+shift+a") |
| 144 | + }) |
| 145 | + |
| 146 | + test("meta aliases normalize to 'alt' on roundtrip", () => { |
| 147 | + // parse("meta+x") sets meta:true, toString emits "alt" — lossy but correct |
| 148 | + const [parsed] = Keybind.parse("meta+x") |
| 149 | + expect(Keybind.toString(parsed)).toBe("alt+x") |
| 150 | + |
| 151 | + const [parsed2] = Keybind.parse("option+x") |
| 152 | + expect(Keybind.toString(parsed2)).toBe("alt+x") |
| 153 | + }) |
| 154 | +}) |
0 commit comments