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(["terminalFocus", "terminalOpen", "modelPickerOpen", "true", "false"]),
214 );
215 expect(options).not.toContain("customModeActive");
216 });
217
218 it("builds command options from all static commands and resolved project bindings", () => {
219 const options = buildKeybindingCommandOptions([
220 {
221 command: "script.setup-db.run",
222 shortcut: {
223 key: "r",
224 modKey: true,
225 metaKey: false,
226 ctrlKey: false,
227 altKey: false,
228 shiftKey: false,
229 },
230 },
231 ] satisfies ResolvedKeybindingsConfig);
232
233 expect(options).toEqual(
234 expect.arrayContaining([
235 "chat.new",
236 "rightPanel.toggleMaximized",
237 "thread.stop",
238 "script.setup-db.run",
239 ]),
240 );
241 expect(DEFAULT_RESOLVED_KEYBINDINGS.some((binding) => binding.command === "thread.stop")).toBe(
242 false,
243 );
244 });
245
246 it("reports unknown when variables without rejecting parseable expressions", () => {
247 const parsed = parseWhenExpressionDraft("!terminalFocus && terminalFoc");
248
249 expect(parsed.ok).toBe(true);
250 expect(unknownWhenVariables(parsed.ok ? parsed.value : undefined)).toEqual(["terminalFoc"]);
251 });
252
253 it("marks each default shortcut for multi-binding commands as default", () => {
254 const rows = buildKeybindingRows(
255 [
256 {
257 command: "chat.new",
258 shortcut: {
259 key: "n",
260 modKey: true,
261 metaKey: false,
262 ctrlKey: false,
263 altKey: false,
264 shiftKey: false,
265 },
266 whenAst: {
267 type: "not",
268 node: { type: "identifier", name: "terminalFocus" },
269 },
270 },
271 {
272 command: "chat.new",
273 shortcut: {
274 key: "o",
275 modKey: true,
276 metaKey: false,
277 ctrlKey: false,
278 altKey: false,
279 shiftKey: true,
280 },
281 whenAst: {
282 type: "not",
283 node: { type: "identifier", name: "terminalFocus" },
284 },
285 },
286 ] satisfies ResolvedKeybindingsConfig,
287 "",
288 );
289
290 expect(rows.map((row) => row.source)).toEqual(["Default", "Default"]);
291 });
292
293 it("reports conflicting shortcuts that share an active when context", () => {
294 const rows = buildKeybindingRows(
295 [
296 {
297 command: "chat.new",
298 shortcut: {
299 key: "n",
300 modKey: true,
301 metaKey: false,
302 ctrlKey: false,
303 altKey: false,
304 shiftKey: false,
305 },
306 whenAst: {
307 type: "not",
308 node: { type: "identifier", name: "terminalFocus" },
309 },
310 },
311 {
312 command: "chat.newLocal",
313 shortcut: {
314 key: "n",
315 modKey: true,
316 metaKey: false,
317 ctrlKey: false,
318 altKey: false,
319 shiftKey: false,
320 },
321 whenAst: {
322 type: "not",
323 node: { type: "identifier", name: "terminalFocus" },
324 },
325 },
326 ] satisfies ResolvedKeybindingsConfig,
327 "",
328 );
329
330 expect(rows[0]?.conflicts).toEqual(["Chat: New Local"]);
331 expect(
332 keybindingConflictLabels(rows, {
333 rowId: rows[0]?.id ?? "",
334 key: "mod+n",
335 when: "",
336 }),
337 ).toEqual(["Chat: New Local"]);
338 });
339});