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

KeybindingsSettings.logic.test.ts

250 lines6.7 KB

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 built-ins 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(
153 expect.arrayContaining(["chat.new", "threadPanel.toggle", "script.setup-db.run"]),
154 );
155 });
156​
157 it("reports unknown when variables without rejecting parseable expressions", () => {
158 const parsed = parseWhenExpressionDraft("!terminalFocus && terminalFoc");
159​
160 expect(parsed.ok).toBe(true);
161 expect(unknownWhenVariables(parsed.ok ? parsed.value : undefined)).toEqual(["terminalFoc"]);
162 });
163​
164 it("marks each default shortcut for multi-binding commands as default", () => {
165 const rows = buildKeybindingRows(
166 [
167 {
168 command: "chat.new",
169 shortcut: {
170 key: "n",
171 modKey: true,
172 metaKey: false,
173 ctrlKey: false,
174 altKey: false,
175 shiftKey: false,
176 },
177 whenAst: {
178 type: "not",
179 node: { type: "identifier", name: "terminalFocus" },
180 },
181 },
182 {
183 command: "chat.new",
184 shortcut: {
185 key: "o",
186 modKey: true,
187 metaKey: false,
188 ctrlKey: false,
189 altKey: false,
190 shiftKey: true,
191 },
192 whenAst: {
193 type: "not",
194 node: { type: "identifier", name: "terminalFocus" },
195 },
196 },
197 ] satisfies ResolvedKeybindingsConfig,
198 "",
199 );
200​
201 expect(rows.map((row) => row.source)).toEqual(["Default", "Default"]);
202 });
203​
204 it("reports conflicting shortcuts that share an active when context", () => {
205 const rows = buildKeybindingRows(
206 [
207 {
208 command: "chat.new",
209 shortcut: {
210 key: "n",
211 modKey: true,
212 metaKey: false,
213 ctrlKey: false,
214 altKey: false,
215 shiftKey: false,
216 },
217 whenAst: {
218 type: "not",
219 node: { type: "identifier", name: "terminalFocus" },
220 },
221 },
222 {
223 command: "chat.newLocal",
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 ] satisfies ResolvedKeybindingsConfig,
238 "",
239 );
240​
241 expect(rows[0]?.conflicts).toEqual(["Chat: New Local"]);
242 expect(
243 keybindingConflictLabels(rows, {
244 rowId: rows[0]?.id ?? "",
245 key: "mod+n",
246 when: "",
247 }),
248 ).toEqual(["Chat: New Local"]);
249 });
250});