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

KeybindingsSettings.logic.test.ts

348 lines9.7 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.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 "usage.open",
247 "script.setup-db.run",
248 ]),
249 );
250 expect(DEFAULT_RESOLVED_KEYBINDINGS.some((binding) => binding.command === "thread.stop")).toBe(
251 false,
252 );
253 });
254​
255 it("reports unknown when variables without rejecting parseable expressions", () => {
256 const parsed = parseWhenExpressionDraft("!terminalFocus && terminalFoc");
257​
258 expect(parsed.ok).toBe(true);
259 expect(unknownWhenVariables(parsed.ok ? parsed.value : undefined)).toEqual(["terminalFoc"]);
260 });
261​
262 it("marks each default shortcut for multi-binding commands as default", () => {
263 const rows = buildKeybindingRows(
264 [
265 {
266 command: "chat.new",
267 shortcut: {
268 key: "n",
269 modKey: true,
270 metaKey: false,
271 ctrlKey: false,
272 altKey: false,
273 shiftKey: false,
274 },
275 whenAst: {
276 type: "not",
277 node: { type: "identifier", name: "terminalFocus" },
278 },
279 },
280 {
281 command: "chat.new",
282 shortcut: {
283 key: "o",
284 modKey: true,
285 metaKey: false,
286 ctrlKey: false,
287 altKey: false,
288 shiftKey: true,
289 },
290 whenAst: {
291 type: "not",
292 node: { type: "identifier", name: "terminalFocus" },
293 },
294 },
295 ] satisfies ResolvedKeybindingsConfig,
296 "",
297 );
298​
299 expect(rows.map((row) => row.source)).toEqual(["Default", "Default"]);
300 });
301​
302 it("reports conflicting shortcuts that share an active when context", () => {
303 const rows = buildKeybindingRows(
304 [
305 {
306 command: "chat.new",
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 {
321 command: "chat.newLocal",
322 shortcut: {
323 key: "n",
324 modKey: true,
325 metaKey: false,
326 ctrlKey: false,
327 altKey: false,
328 shiftKey: false,
329 },
330 whenAst: {
331 type: "not",
332 node: { type: "identifier", name: "terminalFocus" },
333 },
334 },
335 ] satisfies ResolvedKeybindingsConfig,
336 "",
337 );
338​
339 expect(rows[0]?.conflicts).toEqual(["Chat: New Local"]);
340 expect(
341 keybindingConflictLabels(rows, {
342 rowId: rows[0]?.id ?? "",
343 key: "mod+n",
344 when: "",
345 }),
346 ).toEqual(["Chat: New Local"]);
347 });
348});