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