|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { CommandRegistry, formatKeybinding } from "../src"; |
| 3 | + |
| 4 | +function createRegistry(): CommandRegistry { |
| 5 | + return new CommandRegistry(); |
| 6 | +} |
| 7 | + |
| 8 | +describe("CommandRegistry", () => { |
| 9 | + describe("command registration", () => { |
| 10 | + it("registers and retrieves a command via contributions", () => { |
| 11 | + const registry = createRegistry(); |
| 12 | + registry.registerContributions([ |
| 13 | + { |
| 14 | + command: "test.command", |
| 15 | + title: "Test Command", |
| 16 | + category: "Test", |
| 17 | + }, |
| 18 | + ]); |
| 19 | + |
| 20 | + const cmd = registry.getCommand("test.command"); |
| 21 | + expect(cmd).toBeDefined(); |
| 22 | + expect(cmd!.id).toBe("test.command"); |
| 23 | + expect(cmd!.title).toBe("Test Command"); |
| 24 | + expect(cmd!.category).toBe("Test"); |
| 25 | + }); |
| 26 | + |
| 27 | + it("registers multiple commands and returns all", () => { |
| 28 | + const registry = createRegistry(); |
| 29 | + registry.registerContributions([ |
| 30 | + { command: "cmd.a", title: "A" }, |
| 31 | + { command: "cmd.b", title: "B" }, |
| 32 | + ]); |
| 33 | + |
| 34 | + const all = registry.getAllCommands(); |
| 35 | + expect(all).toHaveLength(2); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe("executeCommand", () => { |
| 40 | + it("executes a registered command handler", async () => { |
| 41 | + const registry = createRegistry(); |
| 42 | + let executed = false; |
| 43 | + |
| 44 | + registry.registerCommand("test.run", () => { |
| 45 | + executed = true; |
| 46 | + }); |
| 47 | + |
| 48 | + await registry.executeCommand("test.run"); |
| 49 | + expect(executed).toBe(true); |
| 50 | + }); |
| 51 | + |
| 52 | + it("passes arguments to the handler", async () => { |
| 53 | + const registry = createRegistry(); |
| 54 | + let receivedArgs: unknown[] = []; |
| 55 | + |
| 56 | + registry.registerCommand("test.args", (...args) => { |
| 57 | + receivedArgs = args; |
| 58 | + }); |
| 59 | + |
| 60 | + await registry.executeCommand("test.args", "a", 42, { key: "val" }); |
| 61 | + expect(receivedArgs).toEqual(["a", 42, { key: "val" }]); |
| 62 | + }); |
| 63 | + |
| 64 | + it("executes async handlers", async () => { |
| 65 | + const registry = createRegistry(); |
| 66 | + let executed = false; |
| 67 | + |
| 68 | + registry.registerCommand("test.async", async () => { |
| 69 | + await new Promise((r) => setTimeout(r, 1)); |
| 70 | + executed = true; |
| 71 | + }); |
| 72 | + |
| 73 | + await registry.executeCommand("test.async"); |
| 74 | + expect(executed).toBe(true); |
| 75 | + }); |
| 76 | + |
| 77 | + it("does not throw when command is not found", async () => { |
| 78 | + const registry = createRegistry(); |
| 79 | + await expect( |
| 80 | + registry.executeCommand("nonexistent.command"), |
| 81 | + ).resolves.toBeUndefined(); |
| 82 | + }); |
| 83 | + |
| 84 | + it("prefers active-scoped handlers over inactive ones", async () => { |
| 85 | + const registry = createRegistry(); |
| 86 | + const results: string[] = []; |
| 87 | + |
| 88 | + registry.registerCommand("test.scope", () => results.push("default")); |
| 89 | + |
| 90 | + registry.registerCommand( |
| 91 | + "test.scope", |
| 92 | + () => results.push("active"), |
| 93 | + { isActive: () => true }, |
| 94 | + ); |
| 95 | + |
| 96 | + registry.registerCommand( |
| 97 | + "test.scope", |
| 98 | + () => results.push("inactive"), |
| 99 | + { isActive: () => false }, |
| 100 | + ); |
| 101 | + |
| 102 | + await registry.executeCommand("test.scope"); |
| 103 | + expect(results).toEqual(["active"]); |
| 104 | + }); |
| 105 | + |
| 106 | + it("falls back to the last registered handler when no active scope matches", async () => { |
| 107 | + const registry = createRegistry(); |
| 108 | + const results: string[] = []; |
| 109 | + |
| 110 | + registry.registerCommand( |
| 111 | + "test.fallback", |
| 112 | + () => results.push("inactive"), |
| 113 | + { isActive: () => false }, |
| 114 | + ); |
| 115 | + |
| 116 | + registry.registerCommand("test.fallback", () => |
| 117 | + results.push("fallback"), |
| 118 | + ); |
| 119 | + |
| 120 | + await registry.executeCommand("test.fallback"); |
| 121 | + expect(results).toEqual(["fallback"]); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + describe("keybindings", () => { |
| 126 | + it("returns empty keybindings by default", () => { |
| 127 | + const registry = createRegistry(); |
| 128 | + expect(registry.getKeybindings()).toEqual([]); |
| 129 | + }); |
| 130 | + |
| 131 | + it("adds and retrieves keybindings for a layer", () => { |
| 132 | + const registry = createRegistry(); |
| 133 | + registry.registerKeybinding( |
| 134 | + { command: "test.cmd", key: "ctrl+a" }, |
| 135 | + "default", |
| 136 | + ); |
| 137 | + const bindings = registry.getKeybindings(); |
| 138 | + expect(bindings).toHaveLength(1); |
| 139 | + expect(bindings[0]!.key).toBe("ctrl+a"); |
| 140 | + }); |
| 141 | + |
| 142 | + it("filters out keybindings with empty command", () => { |
| 143 | + const registry = createRegistry(); |
| 144 | + registry.registerKeybinding( |
| 145 | + { command: "", key: "ctrl+a" }, |
| 146 | + "default", |
| 147 | + ); |
| 148 | + expect(registry.getKeybindings()).toHaveLength(0); |
| 149 | + }); |
| 150 | + |
| 151 | + it("resolves keybindings for a specific layer", () => { |
| 152 | + const registry = createRegistry(); |
| 153 | + registry.registerKeybinding( |
| 154 | + { command: "cmd.a", key: "ctrl+x" }, |
| 155 | + "default", |
| 156 | + ); |
| 157 | + registry.registerKeybinding( |
| 158 | + { command: "cmd.b", key: "ctrl+y" }, |
| 159 | + "user", |
| 160 | + ); |
| 161 | + |
| 162 | + const defaultBindings = registry.getKeybindingsForLayer("default"); |
| 163 | + expect(defaultBindings).toHaveLength(1); |
| 164 | + expect(defaultBindings[0]!.command).toBe("cmd.a"); |
| 165 | + }); |
| 166 | + }); |
| 167 | + |
| 168 | + describe("context", () => { |
| 169 | + it("updates and retrieves context values", () => { |
| 170 | + const registry = createRegistry(); |
| 171 | + registry.setContext("panelActive", true); |
| 172 | + expect(registry.getContext("panelActive")).toBe(true); |
| 173 | + |
| 174 | + registry.setContext("readonly", false); |
| 175 | + expect(registry.getContext("readonly")).toBe(false); |
| 176 | + }); |
| 177 | + }); |
| 178 | +}); |
| 179 | + |
| 180 | +describe("formatKeybinding", () => { |
| 181 | + it("formats a simple key", () => { |
| 182 | + const result = formatKeybinding({ command: "test", key: "a" }); |
| 183 | + expect(result.toUpperCase()).toBe("A"); |
| 184 | + }); |
| 185 | + |
| 186 | + it("formats a keybinding with modifiers", () => { |
| 187 | + const result = formatKeybinding({ command: "test", key: "ctrl+shift+p" }); |
| 188 | + expect(result).toContain("P"); |
| 189 | + }); |
| 190 | + |
| 191 | + it("formats special keys", () => { |
| 192 | + expect(formatKeybinding({ command: "test", key: "enter" })).toContain("↵"); |
| 193 | + expect(formatKeybinding({ command: "test", key: "escape" })).toContain("Esc"); |
| 194 | + expect(formatKeybinding({ command: "test", key: "tab" })).toContain("Tab"); |
| 195 | + expect(formatKeybinding({ command: "test", key: "up" })).toContain("↑"); |
| 196 | + expect(formatKeybinding({ command: "test", key: "down" })).toContain("↓"); |
| 197 | + expect(formatKeybinding({ command: "test", key: "left" })).toContain("←"); |
| 198 | + expect(formatKeybinding({ command: "test", key: "right" })).toContain("→"); |
| 199 | + expect(formatKeybinding({ command: "test", key: "space" })).toContain("Space"); |
| 200 | + expect(formatKeybinding({ command: "test", key: "backspace" })).toContain("⌫"); |
| 201 | + expect(formatKeybinding({ command: "test", key: "delete" })).toContain("Del"); |
| 202 | + }); |
| 203 | + |
| 204 | + it("formats f-keys", () => { |
| 205 | + const result = formatKeybinding({ command: "test", key: "f5" }); |
| 206 | + expect(result).toBe("F5"); |
| 207 | + }); |
| 208 | + |
| 209 | + it("does not throw when called in node environment", () => { |
| 210 | + // formatKeybinding uses isMac() which falls back gracefully |
| 211 | + expect(() => formatKeybinding({ command: "test", key: "a" })).not.toThrow(); |
| 212 | + }); |
| 213 | +}); |
0 commit comments