The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | import type { KeybindingCommand } from "@t3tools/contracts"; |
| 3 | import { compileResolvedKeybindingsConfig } from "@t3tools/shared/keybindings"; |
| 4 | import { resolveShortcutCommand } from "./keybindings"; |
| 5 | import * as editableFocus from "./lib/editableFocus"; |
| 6 | |
| 7 | function setupFocus() { |
| 8 | const editor = new EventTarget(); |
| 9 | const other = new EventTarget(); |
| 10 | vi.stubGlobal("document", { activeElement: editor }); |
| 11 | const focus = vi |
| 12 | .spyOn(editableFocus, "isEditableFocused") |
| 13 | .mockImplementation((target) => target === editor); |
| 14 | return { |
| 15 | editor, |
| 16 | other, |
| 17 | [Symbol.dispose]() { |
| 18 | focus.mockRestore(); |
| 19 | vi.unstubAllGlobals(); |
| 20 | }, |
| 21 | }; |
| 22 | } |
| 23 | |
| 24 | function keydown(target?: EventTarget) { |
| 25 | return { |
| 26 | key: "k", |
| 27 | ctrlKey: true, |
| 28 | metaKey: false, |
| 29 | altKey: false, |
| 30 | shiftKey: false, |
| 31 | ...(target ? { target } : {}), |
| 32 | }; |
| 33 | } |
| 34 | |
| 35 | function bindings(command: KeybindingCommand, when = "!editableFocus") { |
| 36 | return compileResolvedKeybindingsConfig([{ key: "mod+k", command, when }]); |
| 37 | } |
| 38 | |
| 39 | describe("editable shortcut conditions", () => { |
| 40 | it.each(["commandPalette.toggle", "sidebar.toggle", "thread.next", "thread.jump.1"] as const)( |
| 41 | "honors !editableFocus for %s without a caller-provided focus context", |
| 42 | (command) => { |
| 43 | using focus = setupFocus(); |
| 44 | const rules = bindings(command); |
| 45 | |
| 46 | expect( |
| 47 | resolveShortcutCommand(keydown(focus.editor), rules, { |
| 48 | platform: "Linux", |
| 49 | context: { terminalFocus: false }, |
| 50 | }), |
| 51 | ).toBeNull(); |
| 52 | expect(resolveShortcutCommand(keydown(focus.other), rules, { platform: "Linux" })).toBe( |
| 53 | command, |
| 54 | ); |
| 55 | }, |
| 56 | ); |
| 57 | |
| 58 | it("uses the active element when a forwarded shortcut has no target", () => { |
| 59 | using _focus = setupFocus(); |
| 60 | expect( |
| 61 | resolveShortcutCommand(keydown(), bindings("commandPalette.toggle"), { platform: "Linux" }), |
| 62 | ).toBeNull(); |
| 63 | }); |
| 64 | |
| 65 | it("honors a positive editableFocus condition", () => { |
| 66 | using focus = setupFocus(); |
| 67 | const rules = bindings("commandPalette.toggle", "editableFocus"); |
| 68 | expect(resolveShortcutCommand(keydown(focus.editor), rules, { platform: "Linux" })).toBe( |
| 69 | "commandPalette.toggle", |
| 70 | ); |
| 71 | expect(resolveShortcutCommand(keydown(focus.other), rules, { platform: "Linux" })).toBeNull(); |
| 72 | }); |
| 73 | |
| 74 | it("preserves explicit focus overrides", () => { |
| 75 | using focus = setupFocus(); |
| 76 | const rules = bindings("commandPalette.toggle"); |
| 77 | |
| 78 | expect( |
| 79 | resolveShortcutCommand(keydown(focus.editor), rules, { |
| 80 | platform: "Linux", |
| 81 | context: { editableFocus: false }, |
| 82 | }), |
| 83 | ).toBe("commandPalette.toggle"); |
| 84 | expect( |
| 85 | resolveShortcutCommand(keydown(focus.other), rules, { |
| 86 | platform: "Linux", |
| 87 | context: { editableFocus: true }, |
| 88 | }), |
| 89 | ).toBeNull(); |
| 90 | }); |
| 91 | |
| 92 | it("keeps shortcuts without an editable-focus condition active while editing", () => { |
| 93 | using focus = setupFocus(); |
| 94 | expect( |
| 95 | resolveShortcutCommand(keydown(focus.editor), bindings("commandPalette.toggle", "true"), { |
| 96 | platform: "Linux", |
| 97 | }), |
| 98 | ).toBe("commandPalette.toggle"); |
| 99 | }); |
| 100 | }); |