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

keybindings.test.ts

272 lines7.5 KB

1import { assert, it } from "@effect/vitest";
2import * as Schema from "effect/Schema";
3import * as Effect from "effect/Effect";
4​
5import {
6 KeybindingsConfig,
7 KeybindingRule,
8 ResolvedKeybindingRule,
9 ResolvedKeybindingsConfig,
10} from "./keybindings.ts";
11​
12const decode = <S extends Schema.Top>(
13 schema: S,
14 input: unknown,
15): Effect.Effect<Schema.Schema.Type<S>, Schema.SchemaError, never> =>
16 Schema.decodeUnknownEffect(schema as never)(input) as Effect.Effect<
17 Schema.Schema.Type<S>,
18 Schema.SchemaError,
19 never
20 >;
21​
22const decodeResolvedRule = Schema.decodeUnknownEffect(ResolvedKeybindingRule as never);
23const encodeResolvedKeybindings = Schema.encodeEffect(ResolvedKeybindingsConfig);
24​
25it.effect("parses keybinding rules", () =>
26 Effect.gen(function* () {
27 const parsed = yield* decode(KeybindingRule, {
28 key: "mod+j",
29 command: "terminal.toggle",
30 });
31 assert.strictEqual(parsed.command, "terminal.toggle");
32​
33 const parsedSidebarToggle = yield* decode(KeybindingRule, {
34 key: "mod+b",
35 command: "sidebar.toggle",
36 });
37 assert.strictEqual(parsedSidebarToggle.command, "sidebar.toggle");
38​
39 const parsedRightPanelToggle = yield* decode(KeybindingRule, {
40 key: "mod+alt+b",
41 command: "rightPanel.toggle",
42 });
43 assert.strictEqual(parsedRightPanelToggle.command, "rightPanel.toggle");
44​
45 const parsedClose = yield* decode(KeybindingRule, {
46 key: "mod+w",
47 command: "terminal.close",
48 });
49 assert.strictEqual(parsedClose.command, "terminal.close");
50​
51 const parsedDiffToggle = yield* decode(KeybindingRule, {
52 key: "mod+d",
53 command: "diff.toggle",
54 });
55 assert.strictEqual(parsedDiffToggle.command, "diff.toggle");
56​
57 const parsedCommandPalette = yield* decode(KeybindingRule, {
58 key: "mod+k",
59 command: "commandPalette.toggle",
60 });
61 assert.strictEqual(parsedCommandPalette.command, "commandPalette.toggle");
62​
63 const parsedFilePicker = yield* decode(KeybindingRule, {
64 key: "mod+p",
65 command: "filePicker.toggle",
66 });
67 assert.strictEqual(parsedFilePicker.command, "filePicker.toggle");
68​
69 const parsedProjectSearch = yield* decode(KeybindingRule, {
70 key: "mod+shift+f",
71 command: "projectSearch.toggle",
72 });
73 assert.strictEqual(parsedProjectSearch.command, "projectSearch.toggle");
74​
75 const parsedLocal = yield* decode(KeybindingRule, {
76 key: "mod+shift+n",
77 command: "chat.newLocal",
78 });
79 assert.strictEqual(parsedLocal.command, "chat.newLocal");
80​
81 const parsedModelPickerToggle = yield* decode(KeybindingRule, {
82 key: "mod+shift+m",
83 command: "modelPicker.toggle",
84 });
85 assert.strictEqual(parsedModelPickerToggle.command, "modelPicker.toggle");
86​
87 const parsedModelPickerJump = yield* decode(KeybindingRule, {
88 key: "mod+1",
89 command: "modelPicker.jump.1",
90 });
91 assert.strictEqual(parsedModelPickerJump.command, "modelPicker.jump.1");
92​
93 const parsedThreadPrevious = yield* decode(KeybindingRule, {
94 key: "mod+shift+[",
95 command: "thread.previous",
96 });
97 assert.strictEqual(parsedThreadPrevious.command, "thread.previous");
98 }),
99);
100​
101it.effect("rejects invalid command values", () =>
102 Effect.gen(function* () {
103 const result = yield* Effect.exit(
104 decode(KeybindingRule, {
105 key: "mod+j",
106 command: "script.Test.run",
107 }),
108 );
109 assert.strictEqual(result._tag, "Failure");
110 }),
111);
112​
113it.effect("accepts dynamic script run commands", () =>
114 Effect.gen(function* () {
115 const parsed = yield* decode(KeybindingRule, {
116 key: "mod+r",
117 command: "script.setup.run",
118 });
119 assert.strictEqual(parsed.command, "script.setup.run");
120 }),
121);
122​
123it.effect("parses keybindings array payload", () =>
124 Effect.gen(function* () {
125 const parsed = yield* decode(KeybindingsConfig, [
126 { key: "mod+j", command: "terminal.toggle" },
127 { key: "mod+d", command: "terminal.split", when: "terminalFocus" },
128 { key: "mod+shift+d", command: "terminal.splitVertical", when: "terminalFocus" },
129 ]);
130 assert.lengthOf(parsed, 3);
131 }),
132);
133​
134it.effect("parses resolved keybinding rules", () =>
135 Effect.gen(function* () {
136 const parsed = yield* decode(ResolvedKeybindingRule, {
137 command: "terminal.split",
138 shortcut: {
139 key: "d",
140 metaKey: false,
141 ctrlKey: false,
142 shiftKey: false,
143 altKey: false,
144 modKey: true,
145 },
146 whenAst: {
147 type: "and",
148 left: { type: "identifier", name: "terminalOpen" },
149 right: {
150 type: "not",
151 node: { type: "identifier", name: "terminalFocus" },
152 },
153 },
154 });
155 assert.strictEqual(parsed.shortcut.key, "d");
156 }),
157);
158​
159it.effect("parses resolved keybindings arrays", () =>
160 Effect.gen(function* () {
161 const parsed = yield* decode(ResolvedKeybindingsConfig, [
162 {
163 command: "terminal.toggle",
164 shortcut: {
165 key: "j",
166 metaKey: false,
167 ctrlKey: false,
168 shiftKey: false,
169 altKey: false,
170 modKey: true,
171 },
172 },
173 {
174 command: "thread.jump.3",
175 shortcut: {
176 key: "3",
177 metaKey: false,
178 ctrlKey: false,
179 shiftKey: false,
180 altKey: false,
181 modKey: true,
182 },
183 },
184 ]);
185 assert.lengthOf(parsed, 2);
186 }),
187);
188​
189const shortcut = {
190 key: "p",
191 metaKey: false,
192 ctrlKey: false,
193 shiftKey: false,
194 altKey: false,
195 modKey: true,
196};
197​
198it.effect("drops resolved rules with commands this build does not know", () =>
199 Effect.gen(function* () {
200 const parsed = yield* decode(ResolvedKeybindingsConfig, [
201 { command: "terminal.toggle", shortcut },
202 { command: "someFuture.toggle", shortcut },
203 { command: "filePicker.toggle", shortcut },
204 ]);
205 assert.deepEqual(
206 parsed.map((rule) => rule.command),
207 ["terminal.toggle", "filePicker.toggle"],
208 );
209 }),
210);
211​
212it.effect("drops resolved rules with unknown when-node types", () =>
213 Effect.gen(function* () {
214 const parsed = yield* decode(ResolvedKeybindingsConfig, [
215 {
216 command: "terminal.toggle",
217 shortcut,
218 whenAst: { type: "xor", left: 1, right: 2 },
219 },
220 { command: "terminal.split", shortcut },
221 ]);
222 assert.deepEqual(
223 parsed.map((rule) => rule.command),
224 ["terminal.split"],
225 );
226 }),
227);
228​
229it.effect("drops malformed resolved rule entries", () =>
230 Effect.gen(function* () {
231 const parsed = yield* decode(ResolvedKeybindingsConfig, [
232 "garbage",
233 { command: "terminal.toggle", shortcut },
234 null,
235 ]);
236 assert.deepEqual(
237 parsed.map((rule) => rule.command),
238 ["terminal.toggle"],
239 );
240 }),
241);
242​
243it.effect("encodes resolved keybindings to the plain wire shape", () =>
244 Effect.gen(function* () {
245 const rules = [{ command: "terminal.toggle" as const, shortcut }];
246 const encoded = yield* encodeResolvedKeybindings(rules);
247 assert.deepEqual(encoded, rules);
248 const roundTripped = yield* decode(ResolvedKeybindingsConfig, encoded);
249 assert.deepEqual(roundTripped, rules);
250 }),
251);
252​
253it.effect("drops unknown fields in resolved keybinding rules", () =>
254 decodeResolvedRule({
255 command: "terminal.toggle",
256 shortcut: {
257 key: "j",
258 metaKey: false,
259 ctrlKey: false,
260 shiftKey: false,
261 altKey: false,
262 modKey: true,
263 },
264 key: "mod+j",
265 }).pipe(
266 Effect.map((parsed) => {
267 const view = parsed as Record<string, unknown>;
268 assert.strictEqual("key" in view, false);
269 assert.strictEqual(view.command, "terminal.toggle");
270 }),
271 ),
272);