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

KeybindingsSettings.logic.test.ts

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