| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | | import { describe, expect, it } from "vitest"; |
| 1 | import { assert, describe, it } from "vitest"; |
| 2 | 2 | |
| 3 | 3 | import type { KeybindingsConfig } from "@t3tools/contracts"; |
| 4 | 4 | import { |
| ... | ... | @@ -34,43 +34,47 @@ const DEFAULT_BINDINGS: KeybindingsConfig = [ |
| 34 | 34 | |
| 35 | 35 | describe("isTerminalToggleShortcut", () => { |
| 36 | 36 | it("matches Cmd+J on macOS", () => { |
| 37 | | expect(isTerminalToggleShortcut(event({ metaKey: true }), DEFAULT_BINDINGS, { platform: "MacIntel" })).toBe(true); |
| 37 | assert.isTrue( |
| 38 | isTerminalToggleShortcut(event({ metaKey: true }), DEFAULT_BINDINGS, { platform: "MacIntel" }), |
| 39 | ); |
| 38 | 40 | }); |
| 39 | 41 | |
| 40 | 42 | it("matches Ctrl+J on non-macOS", () => { |
| 41 | | expect(isTerminalToggleShortcut(event({ ctrlKey: true }), DEFAULT_BINDINGS, { platform: "Win32" })).toBe(true); |
| 43 | assert.isTrue( |
| 44 | isTerminalToggleShortcut(event({ ctrlKey: true }), DEFAULT_BINDINGS, { platform: "Win32" }), |
| 45 | ); |
| 42 | 46 | }); |
| 43 | 47 | }); |
| 44 | 48 | |
| 45 | 49 | describe("split/new terminal shortcuts", () => { |
| 46 | 50 | it("requires terminalFocus for default split/new bindings", () => { |
| 47 | | expect( |
| 51 | assert.isFalse( |
| 48 | 52 | isTerminalSplitShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, { |
| 49 | 53 | platform: "MacIntel", |
| 50 | 54 | context: { terminalFocus: false }, |
| 51 | 55 | }), |
| 52 | | ).toBe(false); |
| 53 | | expect( |
| 56 | ); |
| 57 | assert.isFalse( |
| 54 | 58 | isTerminalNewShortcut(event({ key: "d", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, { |
| 55 | 59 | platform: "Linux", |
| 56 | 60 | context: { terminalFocus: false }, |
| 57 | 61 | }), |
| 58 | | ).toBe(false); |
| 62 | ); |
| 59 | 63 | }); |
| 60 | 64 | |
| 61 | 65 | it("matches split/new when terminalFocus is true", () => { |
| 62 | | expect( |
| 66 | assert.isTrue( |
| 63 | 67 | isTerminalSplitShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, { |
| 64 | 68 | platform: "MacIntel", |
| 65 | 69 | context: { terminalFocus: true }, |
| 66 | 70 | }), |
| 67 | | ).toBe(true); |
| 68 | | expect( |
| 71 | ); |
| 72 | assert.isTrue( |
| 69 | 73 | isTerminalNewShortcut(event({ key: "d", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, { |
| 70 | 74 | platform: "Linux", |
| 71 | 75 | context: { terminalFocus: true }, |
| 72 | 76 | }), |
| 73 | | ).toBe(true); |
| 77 | ); |
| 74 | 78 | }); |
| 75 | 79 | |
| 76 | 80 | it("supports when expressions", () => { |
| ... | ... | @@ -87,24 +91,24 @@ describe("split/new terminal shortcuts", () => { |
| 87 | 91 | }, |
| 88 | 92 | { key: "mod+j", command: "terminal.toggle" }, |
| 89 | 93 | ]; |
| 90 | | expect( |
| 94 | assert.isTrue( |
| 91 | 95 | isTerminalSplitShortcut(event({ key: "\\", ctrlKey: true }), keybindings, { |
| 92 | 96 | platform: "Win32", |
| 93 | 97 | context: { terminalOpen: true, terminalFocus: false }, |
| 94 | 98 | }), |
| 95 | | ).toBe(true); |
| 96 | | expect( |
| 99 | ); |
| 100 | assert.isFalse( |
| 97 | 101 | isTerminalSplitShortcut(event({ key: "\\", ctrlKey: true }), keybindings, { |
| 98 | 102 | platform: "Win32", |
| 99 | 103 | context: { terminalOpen: false, terminalFocus: false }, |
| 100 | 104 | }), |
| 101 | | ).toBe(false); |
| 102 | | expect( |
| 105 | ); |
| 106 | assert.isTrue( |
| 103 | 107 | isTerminalNewShortcut(event({ key: "n", ctrlKey: true, shiftKey: true }), keybindings, { |
| 104 | 108 | platform: "Win32", |
| 105 | 109 | context: { terminalOpen: true, terminalFocus: false }, |
| 106 | 110 | }), |
| 107 | | ).toBe(true); |
| 111 | ); |
| 108 | 112 | }); |
| 109 | 113 | }); |
| 110 | 114 | |
| ... | ... | @@ -114,12 +118,13 @@ describe("shortcutLabelForCommand", () => { |
| 114 | 118 | { key: "mod+\\", command: "terminal.split", when: "terminalFocus" }, |
| 115 | 119 | { key: "mod+shift+\\", command: "terminal.split", when: "!terminalFocus" }, |
| 116 | 120 | ]; |
| 117 | | expect(shortcutLabelForCommand(bindings, "terminal.split", "Linux")).toBe("Ctrl+Shift+\\"); |
| 121 | assert.strictEqual(shortcutLabelForCommand(bindings, "terminal.split", "Linux"), "Ctrl+Shift+\\"); |
| 118 | 122 | }); |
| 119 | 123 | |
| 120 | 124 | it("returns labels for non-terminal commands", () => { |
| 121 | | expect(shortcutLabelForCommand(DEFAULT_BINDINGS, "chat.new", "MacIntel")).toBe("⇧⌘O"); |
| 122 | | expect(shortcutLabelForCommand(DEFAULT_BINDINGS, "editor.openFavorite", "Linux")).toBe( |
| 125 | assert.strictEqual(shortcutLabelForCommand(DEFAULT_BINDINGS, "chat.new", "MacIntel"), "⇧⌘O"); |
| 126 | assert.strictEqual( |
| 127 | shortcutLabelForCommand(DEFAULT_BINDINGS, "editor.openFavorite", "Linux"), |
| 123 | 128 | "Ctrl+O", |
| 124 | 129 | ); |
| 125 | 130 | }); |
| ... | ... | @@ -127,49 +132,103 @@ describe("shortcutLabelForCommand", () => { |
| 127 | 132 | |
| 128 | 133 | describe("chat/editor shortcuts", () => { |
| 129 | 134 | it("matches chat.new shortcut", () => { |
| 130 | | expect( |
| 135 | assert.isTrue( |
| 131 | 136 | isChatNewShortcut(event({ key: "o", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, { |
| 132 | 137 | platform: "MacIntel", |
| 133 | 138 | }), |
| 134 | | ).toBe(true); |
| 135 | | expect( |
| 139 | ); |
| 140 | assert.isTrue( |
| 136 | 141 | isChatNewShortcut(event({ key: "o", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, { |
| 137 | 142 | platform: "Linux", |
| 138 | 143 | }), |
| 139 | | ).toBe(true); |
| 144 | ); |
| 140 | 145 | }); |
| 141 | 146 | |
| 142 | 147 | it("matches editor.openFavorite shortcut", () => { |
| 143 | | expect( |
| 148 | assert.isTrue( |
| 144 | 149 | isOpenFavoriteEditorShortcut(event({ key: "o", metaKey: true }), DEFAULT_BINDINGS, { |
| 145 | 150 | platform: "MacIntel", |
| 146 | 151 | }), |
| 147 | | ).toBe(true); |
| 148 | | expect( |
| 152 | ); |
| 153 | assert.isTrue( |
| 149 | 154 | isOpenFavoriteEditorShortcut(event({ key: "o", ctrlKey: true }), DEFAULT_BINDINGS, { |
| 150 | 155 | platform: "Linux", |
| 151 | 156 | }), |
| 152 | | ).toBe(true); |
| 157 | ); |
| 158 | }); |
| 159 | }); |
| 160 | |
| 161 | describe("cross-command precedence", () => { |
| 162 | it("uses when + order so a later focused rule overrides a global rule", () => { |
| 163 | const keybindings: KeybindingsConfig = [ |
| 164 | { key: "mod+n", command: "chat.new" }, |
| 165 | { key: "mod+n", command: "terminal.new", when: "terminalFocus" }, |
| 166 | ]; |
| 167 | |
| 168 | assert.isTrue( |
| 169 | isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, { |
| 170 | platform: "MacIntel", |
| 171 | context: { terminalFocus: true }, |
| 172 | }), |
| 173 | ); |
| 174 | assert.isFalse( |
| 175 | isChatNewShortcut(event({ key: "n", metaKey: true }), keybindings, { |
| 176 | platform: "MacIntel", |
| 177 | context: { terminalFocus: true }, |
| 178 | }), |
| 179 | ); |
| 180 | assert.isFalse( |
| 181 | isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, { |
| 182 | platform: "MacIntel", |
| 183 | context: { terminalFocus: false }, |
| 184 | }), |
| 185 | ); |
| 186 | assert.isTrue( |
| 187 | isChatNewShortcut(event({ key: "n", metaKey: true }), keybindings, { |
| 188 | platform: "MacIntel", |
| 189 | context: { terminalFocus: false }, |
| 190 | }), |
| 191 | ); |
| 192 | }); |
| 193 | |
| 194 | it("still lets a later global rule win when both rules match", () => { |
| 195 | const keybindings: KeybindingsConfig = [ |
| 196 | { key: "mod+n", command: "terminal.new", when: "terminalFocus" }, |
| 197 | { key: "mod+n", command: "chat.new" }, |
| 198 | ]; |
| 199 | |
| 200 | assert.isFalse( |
| 201 | isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, { |
| 202 | platform: "Linux", |
| 203 | context: { terminalFocus: true }, |
| 204 | }), |
| 205 | ); |
| 206 | assert.isTrue( |
| 207 | isChatNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, { |
| 208 | platform: "Linux", |
| 209 | context: { terminalFocus: true }, |
| 210 | }), |
| 211 | ); |
| 153 | 212 | }); |
| 154 | 213 | }); |
| 155 | 214 | |
| 156 | 215 | describe("formatShortcutLabel", () => { |
| 157 | 216 | it("formats labels for macOS", () => { |
| 158 | | expect(formatShortcutLabel("mod+shift+d", "MacIntel")).toBe("⇧⌘D"); |
| 217 | assert.strictEqual(formatShortcutLabel("mod+shift+d", "MacIntel"), "⇧⌘D"); |
| 159 | 218 | }); |
| 160 | 219 | |
| 161 | 220 | it("formats labels for non-macOS", () => { |
| 162 | | expect(formatShortcutLabel("mod+shift+d", "Linux")).toBe("Ctrl+Shift+D"); |
| 221 | assert.strictEqual(formatShortcutLabel("mod+shift+d", "Linux"), "Ctrl+Shift+D"); |
| 163 | 222 | }); |
| 164 | 223 | }); |
| 165 | 224 | |
| 166 | 225 | describe("isTerminalClearShortcut", () => { |
| 167 | 226 | it("matches Ctrl+L on all platforms", () => { |
| 168 | | expect(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "Linux")).toBe(true); |
| 169 | | expect(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "MacIntel")).toBe(true); |
| 227 | assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "Linux")); |
| 228 | assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "MacIntel")); |
| 170 | 229 | }); |
| 171 | 230 | |
| 172 | 231 | it("matches Cmd+K on macOS", () => { |
| 173 | | expect(isTerminalClearShortcut(event({ key: "k", metaKey: true }), "MacIntel")).toBe(true); |
| 232 | assert.isTrue(isTerminalClearShortcut(event({ key: "k", metaKey: true }), "MacIntel")); |
| 174 | 233 | }); |
| 175 | 234 | }); |