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