| 1 | import { MAX_KEYBINDING_VALUE_LENGTH, type KeybindingCommand } from "@t3tools/contracts"; |
| 2 | import { describe, expect, it } from "vite-plus/test"; |
| 3 | |
| 4 | import { commandForProjectScript } from "../projectScripts"; |
| 5 | import { |
| 6 | decodeProjectScriptKeybindingRule, |
| 7 | keybindingValueForCommand, |
| 8 | PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE, |
| 9 | } from "./projectScriptKeybindings"; |
| 10 | |
| 11 | describe("projectScriptKeybindings", () => { |
| 12 | it("decodes and trims valid keybinding rules", () => { |
| 13 | const rule = decodeProjectScriptKeybindingRule({ |
| 14 | keybinding: " mod+k ", |
| 15 | command: commandForProjectScript("lint"), |
| 16 | }); |
| 17 | |
| 18 | expect(rule).toEqual({ |
| 19 | key: "mod+k", |
| 20 | command: "script.lint.run", |
| 21 | }); |
| 22 | }); |
| 23 | |
| 24 | it("returns null when keybinding is empty", () => { |
| 25 | expect( |
| 26 | decodeProjectScriptKeybindingRule({ |
| 27 | keybinding: " ", |
| 28 | command: commandForProjectScript("lint"), |
| 29 | }), |
| 30 | ).toBeNull(); |
| 31 | }); |
| 32 | |
| 33 | it("rejects invalid keybinding values", () => { |
| 34 | expect(() => |
| 35 | decodeProjectScriptKeybindingRule({ |
| 36 | keybinding: "k".repeat(MAX_KEYBINDING_VALUE_LENGTH + 1), |
| 37 | command: commandForProjectScript("lint"), |
| 38 | }), |
| 39 | ).toThrowError(PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE); |
| 40 | }); |
| 41 | |
| 42 | it("rejects invalid commands", () => { |
| 43 | expect(() => |
| 44 | decodeProjectScriptKeybindingRule({ |
| 45 | keybinding: "mod+k", |
| 46 | command: "script.BAD.run" as KeybindingCommand, |
| 47 | }), |
| 48 | ).toThrowError(PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE); |
| 49 | }); |
| 50 | |
| 51 | it("can edit or delete a legacy script without a shortcut", () => { |
| 52 | const command = commandForProjectScript("install-javascript-dependencies"); |
| 53 | expect(keybindingValueForCommand([], command)).toBeNull(); |
| 54 | expect(decodeProjectScriptKeybindingRule({ keybinding: null, command })).toBeNull(); |
| 55 | expect(() => decodeProjectScriptKeybindingRule({ keybinding: "mod+k", command })).toThrowError( |
| 56 | PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE, |
| 57 | ); |
| 58 | }); |
| 59 | |
| 60 | it("reads latest matching keybinding value for a command", () => { |
| 61 | const command = "script.test.run" as const; |
| 62 | const value = keybindingValueForCommand( |
| 63 | [ |
| 64 | { |
| 65 | command, |
| 66 | shortcut: { |
| 67 | key: "escape", |
| 68 | metaKey: false, |
| 69 | ctrlKey: false, |
| 70 | shiftKey: false, |
| 71 | altKey: false, |
| 72 | modKey: true, |
| 73 | }, |
| 74 | }, |
| 75 | { |
| 76 | command, |
| 77 | shortcut: { |
| 78 | key: "k", |
| 79 | metaKey: false, |
| 80 | ctrlKey: false, |
| 81 | shiftKey: true, |
| 82 | altKey: false, |
| 83 | modKey: true, |
| 84 | }, |
| 85 | }, |
| 86 | ], |
| 87 | command, |
| 88 | ); |
| 89 | |
| 90 | expect(value).toBe("mod+shift+k"); |
| 91 | }); |
| 92 | }); |