Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

KeybindingsSettings.logic.test.ts

361 lines10.2 KB

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