The best way to control Claude Code, Codex and more from any computer or phone

main
ts 372 lines 12.7 KB
Raw
1 import { assert, describe, expect, it } from "vite-plus/test";
2 import type { ResolvedKeybindingsConfig } from "@t3tools/contracts";
3 import {
4 compileResolvedKeybindingsConfig,
5 parseKeybindingShortcut,
6 } from "@t3tools/shared/keybindings";
7
8 import { resolveShortcutCommand } from "../../keybindings";
9
10 import {
11 buildKeybindingRows,
12 buildWhenVariableOptions,
13 keybindingConflictLabels,
14 keybindingFromKeyboardEvent,
15 parseWhenExpressionDraft,
16 shortcutToKeybindingInput,
17 unknownWhenVariables,
18 whenAstToExpression,
19 } from "./KeybindingsSettings.logic";
20
21 describe("KeybindingsSettings.logic", () => {
22 it("keeps sibling row identities when a saved binding moves to the end", () => {
23 const bindings = compileResolvedKeybindingsConfig([
24 { command: "chat.new", key: "mod+n" },
25 { command: "terminal.toggle", key: "mod+j" },
26 { command: "terminal.toggle", key: "mod+k", when: "terminalFocus" },
27 ]);
28 const before = buildKeybindingRows(bindings);
29 const after = buildKeybindingRows([
30 ...bindings.slice(1),
31 ...compileResolvedKeybindingsConfig([{ command: "chat.new", key: "mod+shift+n" }]),
32 ]);
33
34 expect(after.filter((row) => row.command === "terminal.toggle").map((row) => row.id)).toEqual(
35 before.filter((row) => row.command === "terminal.toggle").map((row) => row.id),
36 );
37 expect(after.find((row) => row.command === "chat.new")?.id).not.toBe(
38 before.find((row) => row.command === "chat.new")?.id,
39 );
40 });
41
42 it("shares a stable editor for resolved duplicates but separates commands, shortcuts, and conditions", () => {
43 const bindings = compileResolvedKeybindingsConfig([
44 { command: "terminal.toggle", key: "mod+esc", when: "terminalFocus" },
45 { command: "terminal.toggle", key: "mod+escape", when: "(terminalFocus)" },
46 { command: "terminal.toggle", key: "mod+esc", when: "!terminalFocus" },
47 { command: "terminal.toggle", key: "mod+j", when: "terminalFocus" },
48 { command: "chat.new", key: "mod+esc", when: "terminalFocus" },
49 ]);
50 const rows = buildKeybindingRows(bindings);
51 expect(rows.map(({ command, key, when }) => ({ command, key, when }))).toEqual([
52 { command: "chat.new", key: "mod+esc", when: "terminalFocus" },
53 { command: "terminal.toggle", key: "mod+esc", when: "terminalFocus" },
54 { command: "terminal.toggle", key: "mod+esc", when: "!terminalFocus" },
55 { command: "terminal.toggle", key: "mod+j", when: "terminalFocus" },
56 ]);
57 expect(new Set(rows.map((row) => row.id)).size).toBe(4);
58 expect(rows.find((row) => row.command === "chat.new")?.conflicts).toEqual(["Terminal: Toggle"]);
59 expect(buildKeybindingRows(bindings.slice(1)).map((row) => row.id)).toEqual(
60 rows.map((row) => row.id),
61 );
62 });
63
64 it("marks only bindings shadowed by later unconditional or matching conditions", () => {
65 const bindings = compileResolvedKeybindingsConfig([
66 { command: "sidebar.toggle", key: "mod+b" },
67 { command: "terminal.toggle", key: "meta+b", when: "true" },
68 { command: "chat.new", key: "mod+n", when: "terminalFocus" },
69 { command: "terminal.new", key: "mod+n", when: "(terminalFocus)" },
70 { command: "chat.new", key: "mod+j" },
71 { command: "terminal.toggle", key: "mod+j", when: "terminalFocus" },
72 { command: "chat.new", key: "mod+k", when: "!terminalFocus" },
73 { command: "terminal.toggle", key: "mod+k", when: "terminalFocus" },
74 { command: "chat.new", key: "mod+l", when: "terminalFocus" },
75 { command: "terminal.toggle", key: "mod+l" },
76 ]);
77 const rows = buildKeybindingRows(bindings, "MacIntel");
78 expect(rows.filter((row) => row.previewLabels.length === 0).map((row) => row.binding)).toEqual([
79 bindings[8],
80 bindings[2],
81 bindings[0],
82 ]);
83 expect(rows.filter((row) => row.previewLabels.length > 0)).toHaveLength(7);
84 const sendBindings = compileResolvedKeybindingsConfig([
85 { command: "composer.sendAlternate", key: "mod+enter" },
86 { command: "chat.new", key: "mod+enter" },
87 ]);
88 for (const sendShortcut of ["enter", "mod-enter", "mod-enter-multiline"] as const) {
89 expect(
90 buildKeybindingRows(sendBindings, "MacIntel", sendShortcut).find(
91 (row) => row.command === "composer.sendAlternate",
92 )?.previewLabels.length === 0,
93 ).toBe(sendShortcut === "enter");
94 }
95 for (const command of ["composer.sendAlternate", "composer.sendBackground"] as const) {
96 const binding = compileResolvedKeybindingsConfig([{ command, key: "mod+enter" }]);
97 const labels = (mode: "enter" | "mod-enter" | "mod-enter-multiline") =>
98 buildKeybindingRows(binding, "Win32", mode)[0]?.previewLabels;
99 expect(labels("enter")).toEqual(["Ctrl+Enter"]);
100 expect(labels("mod-enter")).toEqual(["Ctrl+Shift+Enter"]);
101 expect(labels("mod-enter-multiline")).toEqual(["Ctrl+Enter", "Ctrl+Shift+Enter"]);
102 }
103 const shiftedBindings = compileResolvedKeybindingsConfig([
104 { command: "chat.new", key: "mod+shift+enter" },
105 { command: "composer.sendAlternate", key: "mod+enter" },
106 ]);
107 for (const sendShortcut of ["enter", "mod-enter", "mod-enter-multiline"] as const) {
108 expect(
109 buildKeybindingRows(shiftedBindings, "MacIntel", sendShortcut).find(
110 (row) => row.command === "chat.new",
111 )?.previewLabels.length === 0,
112 ).toBe(sendShortcut === "mod-enter");
113 }
114 expect(
115 buildKeybindingRows(bindings, "Win32").find((row) => row.command === "sidebar.toggle")
116 ?.previewLabels.length === 0,
117 ).toBe(false);
118 });
119
120 it("captures platform-specific mod shortcuts", () => {
121 expect(
122 keybindingFromKeyboardEvent(
123 { key: "K", code: "KeyK", metaKey: true, ctrlKey: false, altKey: false, shiftKey: true },
124 "MacIntel",
125 ),
126 ).toBe("mod+shift+k");
127 expect(
128 keybindingFromKeyboardEvent(
129 { key: "K", code: "KeyK", metaKey: false, ctrlKey: true, altKey: false, shiftKey: true },
130 "Win32",
131 ),
132 ).toBe("mod+shift+k");
133 });
134
135 it("waits for a key when only a modifier is pressed", () => {
136 expect(
137 keybindingFromKeyboardEvent(
138 {
139 key: "Meta",
140 code: "MetaLeft",
141 metaKey: true,
142 ctrlKey: false,
143 altKey: false,
144 shiftKey: false,
145 },
146 "MacIntel",
147 ),
148 ).toBeNull();
149 });
150
151 it.each([
152 ["@", "Digit2", "mod+shift+2"],
153 ['"', "Digit2", "mod+shift+2"],
154 ["@", "Quote", "mod+shift+'"],
155 ])("captures %s at %s by physical key", (key, code, expected) => {
156 expect(
157 keybindingFromKeyboardEvent(
158 {
159 key,
160 code,
161 metaKey: true,
162 ctrlKey: false,
163 altKey: false,
164 shiftKey: true,
165 },
166 "MacIntel",
167 ),
168 ).toBe(expected);
169 });
170
171 it("records layout punctuation while preserving physical Alt shortcuts; saved bindings still trigger", () => {
172 for (const { key, code, altKey = false, shiftKey, expected, defaultInput } of [
173 { key: "#", code: "Backslash", shiftKey: false, expected: "#", defaultInput: "\\" },
174 {
175 key: "'",
176 code: "Backslash",
177 shiftKey: true,
178 expected: "shift+'",
179 defaultInput: "shift+\\",
180 },
181 {
182 key: "¡",
183 code: "Digit1",
184 altKey: true,
185 shiftKey: false,
186 expected: "alt+1",
187 defaultInput: "alt+1",
188 },
189 ]) {
190 const pressed = {
191 key,
192 code,
193 metaKey: false,
194 ctrlKey: false,
195 altKey,
196 shiftKey,
197 };
198 expect(keybindingFromKeyboardEvent(pressed, "MacIntel")).toBe(defaultInput);
199 const input = keybindingFromKeyboardEvent(pressed, "MacIntel", { layoutCharacter: true });
200 expect(input).toBe(expected);
201 assert(input);
202 const shortcut = parseKeybindingShortcut(input);
203 assert(shortcut);
204 expect(
205 resolveShortcutCommand(pressed, [{ command: "chat.new", shortcut }], {
206 platform: "MacIntel",
207 }),
208 ).toBe("chat.new");
209 }
210 });
211
212 it("captures Latin layout keys instead of their punctuation position", () => {
213 expect(
214 keybindingFromKeyboardEvent(
215 {
216 key: "m",
217 code: "Semicolon",
218 metaKey: true,
219 ctrlKey: false,
220 altKey: false,
221 shiftKey: false,
222 },
223 "MacIntel",
224 ),
225 ).toBe("mod+m");
226 });
227
228 it("serializes shortcuts and when expressions for upserts", () => {
229 expect(
230 shortcutToKeybindingInput({
231 key: " ",
232 modKey: true,
233 metaKey: false,
234 ctrlKey: false,
235 altKey: true,
236 shiftKey: false,
237 }),
238 ).toBe("mod+alt+space");
239
240 expect(
241 whenAstToExpression({
242 type: "and",
243 left: { type: "identifier", name: "editorFocus" },
244 right: {
245 type: "not",
246 node: { type: "identifier", name: "terminalFocus" },
247 },
248 }),
249 ).toBe("editorFocus && !terminalFocus");
250
251 expect(parseWhenExpressionDraft("editorFocus && (!terminalFocus || modelPickerOpen)")).toEqual({
252 ok: true,
253 value: {
254 type: "and",
255 left: { type: "identifier", name: "editorFocus" },
256 right: {
257 type: "or",
258 left: {
259 type: "not",
260 node: { type: "identifier", name: "terminalFocus" },
261 },
262 right: { type: "identifier", name: "modelPickerOpen" },
263 },
264 },
265 });
266 const previewOpenDraft = parseWhenExpressionDraft("previewOpen && !previewFocus");
267 assert(previewOpenDraft.ok);
268 expect(unknownWhenVariables(previewOpenDraft.value)).toEqual([]);
269 expect(buildWhenVariableOptions()).toContain("previewOpen");
270 expect(parseWhenExpressionDraft("editorFocus &&")).toEqual({
271 ok: false,
272 message: "Use variables with !, &&, ||, and parentheses.",
273 });
274
275 expect(parseWhenExpressionDraft("!(terminalFocus || modelPickerOpen)")).toEqual({
276 ok: true,
277 value: {
278 type: "not",
279 node: {
280 type: "or",
281 left: { type: "identifier", name: "terminalFocus" },
282 right: { type: "identifier", name: "modelPickerOpen" },
283 },
284 },
285 });
286 });
287
288 it("reports conflicting shortcuts that share an active when context", () => {
289 const rows = buildKeybindingRows([
290 {
291 command: "chat.new",
292 shortcut: {
293 key: "n",
294 modKey: true,
295 metaKey: false,
296 ctrlKey: false,
297 altKey: false,
298 shiftKey: false,
299 },
300 whenAst: {
301 type: "not",
302 node: { type: "identifier", name: "terminalFocus" },
303 },
304 },
305 {
306 command: "chat.newLocal",
307 shortcut: {
308 key: "n",
309 modKey: true,
310 metaKey: false,
311 ctrlKey: false,
312 altKey: false,
313 shiftKey: false,
314 },
315 whenAst: {
316 type: "not",
317 node: { type: "identifier", name: "terminalFocus" },
318 },
319 },
320 ] satisfies ResolvedKeybindingsConfig);
321
322 expect(rows[0]?.conflicts).toEqual(["Chat: New Local"]);
323 const disabledRows = buildKeybindingRows([
324 rows[0]!.binding,
325 { ...rows[1]!.binding, whenAst: { type: "identifier", name: "false" } },
326 ]);
327 expect(disabledRows.find((row) => row.when === "false")?.previewLabels).toEqual([]);
328 expect(disabledRows.map((row) => row.conflicts)).toEqual([[], []]);
329
330 expect(
331 keybindingConflictLabels(rows, {
332 rowId: rows[0]?.id ?? "",
333 key: "mod+n",
334 when: "",
335 }),
336 ).toEqual(["Chat: New Local"]);
337 });
338
339 it.each([
340 { platform: "Linux x86_64", ctrl: ["Chat: New"], meta: [] },
341 { platform: "Win32", ctrl: ["Chat: New"], meta: [] },
342 { platform: "MacIntel", ctrl: [], meta: ["Chat: New"] },
343 ])("resolves mod to the $platform modifier when reporting conflicts", (expected) => {
344 const rows = buildKeybindingRows(
345 compileResolvedKeybindingsConfig([
346 { command: "chat.new", key: "mod+k" },
347 { command: "chat.newLocal", key: "ctrl+k" },
348 { command: "thread.copyReference", key: "meta+k" },
349 ]),
350 expected.platform,
351 );
352 const conflictsFor = (key: string) => rows.find((row) => row.key === key)?.conflicts;
353
354 expect(conflictsFor("ctrl+k")).toEqual(expected.ctrl);
355 expect(conflictsFor("meta+k")).toEqual(expected.meta);
356 expect(
357 keybindingConflictLabels(rows, { rowId: "new", key: "ctrl+k", when: "" }, expected.platform),
358 ).toEqual(["Chat: New Local", ...expected.ctrl].toSorted());
359 });
360
361 it("keeps mod and ctrl shortcuts apart when their when contexts differ", () => {
362 const rows = buildKeybindingRows(
363 compileResolvedKeybindingsConfig([
364 { command: "chat.new", key: "mod+k", when: "!terminalFocus" },
365 { command: "chat.newLocal", key: "ctrl+k", when: "terminalFocus" },
366 ]),
367 "Linux x86_64",
368 );
369
370 expect(rows.map((row) => row.conflicts)).toEqual([[], []]);
371 });
372 });