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