Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import type { ResolvedKeybindingsConfig } from "@t3tools/contracts";
3import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings";
4
5import {
6 buildKeybindingRows,
7 buildKeybindingCommandOptions,
8 buildWhenVariableOptions,
9 commandLabel,
10 keybindingConflictLabels,
11 keybindingFromKeyboardEvent,
12 parseWhenExpressionDraft,
13 shortcutToKeybindingInput,
14 unknownWhenVariables,
15 whenAstToExpression,
16 whenNodeRemoveLabel,
17} from "./KeybindingsSettings.logic";
18
19describe("KeybindingsSettings.logic", () => {
20 it("lists composer, provider, and pull request commands with editable defaults", () => {
21 const rows = buildKeybindingRows(DEFAULT_RESOLVED_KEYBINDINGS, "");
22 for (const command of [
23 "composer.host",
24 "composer.effort",
25 "composer.mode",
26 "composer.workspace",
27 "composer.branch",
28 "composer.previousWorktree",
29 "modelPicker.previousProvider",
30 "modelPicker.nextProvider",
31 "thread.copyReference",
32 "pullRequest.copyNumber",
33 ]) {
34 expect(rows.find((row) => row.command === command)).toMatchObject({
35 source: "Default",
36 conflicts: [],
37 });
38 }
39 });
40 it.each(["pu", "pull request", "copy link", "thread id"])(
41 "finds the copy link shortcut with %s",
42 (query) => {
43 const rows = buildKeybindingRows(DEFAULT_RESOLVED_KEYBINDINGS, query);
44 expect(rows).toContainEqual(
45 expect.objectContaining({ command: "thread.copyReference", key: "mod+shift+c" }),
46 );
47 },
48 );
49 it("builds searchable rows with readable key and when values", () => {
50 const rows = buildKeybindingRows(
51 [
52 {
53 command: "terminal.toggle",
54 shortcut: {
55 key: "j",
56 modKey: true,
57 metaKey: false,
58 ctrlKey: false,
59 altKey: false,
60 shiftKey: false,
61 },
62 whenAst: {
63 type: "not",
64 node: { type: "identifier", name: "terminalFocus" },
65 },
66 },
67 ] satisfies ResolvedKeybindingsConfig,
68 "terminal",
69 );
70
71 expect(rows).toEqual([
72 expect.objectContaining({
73 command: "terminal.toggle",
74 key: "mod+j",
75 when: "!terminalFocus",
76 defaultKey: "mod+j",
77 defaultWhen: "",
78 source: "Custom",
79 }),
80 ]);
81 });
82
83 it("captures platform-specific mod shortcuts", () => {
84 expect(
85 keybindingFromKeyboardEvent(
86 { key: "K", code: "KeyK", metaKey: true, ctrlKey: false, altKey: false, shiftKey: true },
87 "MacIntel",
88 ),
89 ).toBe("mod+shift+k");
90 expect(
91 keybindingFromKeyboardEvent(
92 { key: "K", code: "KeyK", metaKey: false, ctrlKey: true, altKey: false, shiftKey: true },
93 "Win32",
94 ),
95 ).toBe("mod+shift+k");
96 });
97
98 it.each([
99 ["@", "Digit2", "mod+shift+2"],
100 ['"', "Digit2", "mod+shift+2"],
101 ["@", "Quote", "mod+shift+'"],
102 ])("captures %s at %s by physical key", (key, code, expected) => {
103 expect(
104 keybindingFromKeyboardEvent(
105 {
106 key,
107 code,
108 metaKey: true,
109 ctrlKey: false,
110 altKey: false,
111 shiftKey: true,
112 },
113 "MacIntel",
114 ),
115 ).toBe(expected);
116 });
117
118 it("captures Latin layout keys instead of their punctuation position", () => {
119 expect(
120 keybindingFromKeyboardEvent(
121 {
122 key: "m",
123 code: "Semicolon",
124 metaKey: true,
125 ctrlKey: false,
126 altKey: false,
127 shiftKey: false,
128 },
129 "MacIntel",
130 ),
131 ).toBe("mod+m");
132 });
133
134 it("serializes shortcuts and when expressions for upserts", () => {
135 expect(
136 shortcutToKeybindingInput({
137 key: " ",
138 modKey: true,
139 metaKey: false,
140 ctrlKey: false,
141 altKey: true,
142 shiftKey: false,
143 }),
144 ).toBe("mod+alt+space");
145
146 expect(
147 whenAstToExpression({
148 type: "and",
149 left: { type: "identifier", name: "editorFocus" },
150 right: {
151 type: "not",
152 node: { type: "identifier", name: "terminalFocus" },
153 },
154 }),
155 ).toBe("editorFocus && !terminalFocus");
156
157 expect(parseWhenExpressionDraft("editorFocus && (!terminalFocus || modelPickerOpen)")).toEqual({
158 ok: true,
159 value: {
160 type: "and",
161 left: { type: "identifier", name: "editorFocus" },
162 right: {
163 type: "or",
164 left: {
165 type: "not",
166 node: { type: "identifier", name: "terminalFocus" },
167 },
168 right: { type: "identifier", name: "modelPickerOpen" },
169 },
170 },
171 });
172 expect(parseWhenExpressionDraft("editorFocus &&")).toEqual({
173 ok: false,
174 message: "Use variables with !, &&, ||, and parentheses.",
175 });
176
177 expect(parseWhenExpressionDraft("!(terminalFocus || modelPickerOpen)")).toEqual({
178 ok: true,
179 value: {
180 type: "not",
181 node: {
182 type: "or",
183 left: { type: "identifier", name: "terminalFocus" },
184 right: { type: "identifier", name: "modelPickerOpen" },
185 },
186 },
187 });
188 });
189
190 it("describes the scope of each visual expression removal", () => {
191 const condition = { type: "identifier", name: "terminalFocus" } as const;
192 const negatedCondition = { type: "not", node: condition } as const;
193 const group = { type: "and", left: condition, right: negatedCondition } as const;
194 const negatedGroup = { type: "not", node: group } as const;
195
196 expect(whenNodeRemoveLabel(group, 0)).toBe("Clear all conditions");
197 expect(whenNodeRemoveLabel(condition, 1)).toBe("Remove condition");
198 expect(whenNodeRemoveLabel(negatedCondition, 1)).toBe("Remove condition");
199 expect(whenNodeRemoveLabel(group, 1)).toBe("Remove group and its conditions");
200 expect(whenNodeRemoveLabel(negatedGroup, 1)).toBe("Remove group and its conditions");
201 });
202
203 it("formats static and project script command labels", () => {
204 expect(commandLabel("commandPalette.toggle")).toBe("Command Palette: Toggle");
205 expect(commandLabel("themeEditor.toggle")).toBe("Theme Editor: Toggle");
206 expect(commandLabel("script.setup-db.run")).toBe("Run Script: Setup Db");
207 });
208
209 it("builds known when variable options from defaults without frontend labels", () => {
210 const options = buildWhenVariableOptions();
211
212 expect(options).toEqual(
213 expect.arrayContaining([
214 "terminalFocus",
215 "terminalOpen",
216 "isWeb",
217 "isDesktop",
218 "modelPickerOpen",
219 "true",
220 "false",
221 ]),
222 );
223 expect(options).not.toContain("customModeActive");
224 });
225
226 it("builds command options from all static commands and resolved project bindings", () => {
227 const options = buildKeybindingCommandOptions([
228 {
229 command: "script.setup-db.run",
230 shortcut: {
231 key: "r",
232 modKey: true,
233 metaKey: false,
234 ctrlKey: false,
235 altKey: false,
236 shiftKey: false,
237 },
238 },
239 ] satisfies ResolvedKeybindingsConfig);
240
241 expect(options).toEqual(
242 expect.arrayContaining([
243 "chat.new",
244 "rightPanel.toggleMaximized",
245 "thread.stop",
246 "script.setup-db.run",
247 ]),
248 );
249 expect(DEFAULT_RESOLVED_KEYBINDINGS.some((binding) => binding.command === "thread.stop")).toBe(
250 false,
251 );
252 });
253
254 it("reports unknown when variables without rejecting parseable expressions", () => {
255 const parsed = parseWhenExpressionDraft("!terminalFocus && terminalFoc");
256
257 expect(parsed.ok).toBe(true);
258 expect(unknownWhenVariables(parsed.ok ? parsed.value : undefined)).toEqual(["terminalFoc"]);
259 });
260
261 it("marks each default shortcut for multi-binding commands as default", () => {
262 const rows = buildKeybindingRows(
263 [
264 {
265 command: "chat.new",
266 shortcut: {
267 key: "n",
268 modKey: true,
269 metaKey: false,
270 ctrlKey: false,
271 altKey: false,
272 shiftKey: false,
273 },
274 whenAst: {
275 type: "not",
276 node: { type: "identifier", name: "terminalFocus" },
277 },
278 },
279 {
280 command: "chat.new",
281 shortcut: {
282 key: "o",
283 modKey: true,
284 metaKey: false,
285 ctrlKey: false,
286 altKey: false,
287 shiftKey: true,
288 },
289 whenAst: {
290 type: "not",
291 node: { type: "identifier", name: "terminalFocus" },
292 },
293 },
294 ] satisfies ResolvedKeybindingsConfig,
295 "",
296 );
297
298 expect(rows.map((row) => row.source)).toEqual(["Default", "Default"]);
299 });
300
301 it("reports conflicting shortcuts that share an active when context", () => {
302 const rows = buildKeybindingRows(
303 [
304 {
305 command: "chat.new",
306 shortcut: {
307 key: "n",
308 modKey: true,
309 metaKey: false,
310 ctrlKey: false,
311 altKey: false,
312 shiftKey: false,
313 },
314 whenAst: {
315 type: "not",
316 node: { type: "identifier", name: "terminalFocus" },
317 },
318 },
319 {
320 command: "chat.newLocal",
321 shortcut: {
322 key: "n",
323 modKey: true,
324 metaKey: false,
325 ctrlKey: false,
326 altKey: false,
327 shiftKey: false,
328 },
329 whenAst: {
330 type: "not",
331 node: { type: "identifier", name: "terminalFocus" },
332 },
333 },
334 ] satisfies ResolvedKeybindingsConfig,
335 "",
336 );
337
338 expect(rows[0]?.conflicts).toEqual(["Chat: New Local"]);
339 expect(
340 keybindingConflictLabels(rows, {
341 rowId: rows[0]?.id ?? "",
342 key: "mod+n",
343 when: "",
344 }),
345 ).toEqual(["Chat: New Local"]);
346 });
347});