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

keybindings.test.ts

676 lines25.8 KB

1import { KeybindingCommand, KeybindingRule, KeybindingsConfig } from "@t3tools/contracts";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { assert, it } from "@effect/vitest";
4import { assertFailure } from "@effect/vitest/utils";
5import * as Cause from "effect/Cause";
6import * as Effect from "effect/Effect";
7import * as FileSystem from "effect/FileSystem";
8import * as Layer from "effect/Layer";
9import * as Logger from "effect/Logger";
10import * as Path from "effect/Path";
11import * as Schema from "effect/Schema";
12import * as ServerConfig from "./config.ts";
13import * as Keybindings from "./keybindings.ts";
14import { KeybindingsConfigError, MAX_KEYBINDINGS_COUNT } from "@t3tools/contracts";
15import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
16​
17const KeybindingsConfigJson = Schema.fromJsonString(KeybindingsConfig);
18const encodeKeybindingsConfigJson = Schema.encodeEffect(KeybindingsConfigJson);
19const decodeKeybindingsConfigJson = Schema.decodeUnknownEffect(KeybindingsConfigJson);
20const encodeResolvedKeybindingFromConfig = Schema.encodeEffect(
21 Keybindings.ResolvedKeybindingFromConfig,
22);
23const decodeResolvedKeybindingFromConfigExit = Schema.decodeUnknownExit(
24 Keybindings.ResolvedKeybindingFromConfig,
25);
26const layerKeybindings = () => {
27 return Keybindings.layer.pipe(
28 Layer.provideMerge(
29 Layer.fresh(
30 ServerConfig.layerTest(process.cwd(), {
31 prefix: "t3code-keybindings-test-",
32 }),
33 ),
34 ),
35 );
36};
37​
38const toDetailResult = <A, R>(effect: Effect.Effect<A, KeybindingsConfigError, R>) =>
39 effect.pipe(
40 Effect.mapError((error) => error.detail),
41 Effect.result,
42 );
43​
44const writeKeybindingsConfig = (configPath: string, rules: readonly KeybindingRule[]) =>
45 Effect.gen(function* () {
46 const fileSystem = yield* FileSystem.FileSystem;
47 const path = yield* Path.Path;
48 const encoded = yield* encodeKeybindingsConfigJson(rules);
49 yield* fileSystem.makeDirectory(path.dirname(configPath), { recursive: true });
50 yield* fileSystem.writeFileString(configPath, encoded);
51 });
52​
53const readKeybindingsConfig = (configPath: string) =>
54 Effect.gen(function* () {
55 const fileSystem = yield* FileSystem.FileSystem;
56 const rawConfig = yield* fileSystem.readFileString(configPath);
57 return yield* decodeKeybindingsConfigJson(rawConfig);
58 });
59​
60it.layer(NodeServices.layer)("keybindings", (it) => {
61 it.effect("parses shortcuts including plus key", () =>
62 Effect.sync(() => {
63 assert.deepEqual(Keybindings.parseKeybindingShortcut("mod+j"), {
64 key: "j",
65 metaKey: false,
66 ctrlKey: false,
67 shiftKey: false,
68 altKey: false,
69 modKey: true,
70 });
71 assert.deepEqual(Keybindings.parseKeybindingShortcut("mod++"), {
72 key: "+",
73 metaKey: false,
74 ctrlKey: false,
75 shiftKey: false,
76 altKey: false,
77 modKey: true,
78 });
79 }),
80 );
81​
82 it.effect("compiles valid rule with parsed when AST", () =>
83 Effect.sync(() => {
84 const compiled = Keybindings.compileResolvedKeybindingRule({
85 key: "mod+d",
86 command: "terminal.split",
87 when: "terminalOpen && !terminalFocus",
88 });
89​
90 assert.deepEqual(compiled, {
91 command: "terminal.split",
92 shortcut: {
93 key: "d",
94 metaKey: false,
95 ctrlKey: false,
96 shiftKey: false,
97 altKey: false,
98 modKey: true,
99 },
100 whenAst: {
101 type: "and",
102 left: { type: "identifier", name: "terminalOpen" },
103 right: {
104 type: "not",
105 node: { type: "identifier", name: "terminalFocus" },
106 },
107 },
108 });
109 }),
110 );
111​
112 it.effect("encodes resolved plus-key shortcuts", () =>
113 Effect.gen(function* () {
114 const encoded = yield* encodeResolvedKeybindingFromConfig({
115 command: "terminal.toggle",
116 shortcut: {
117 key: "+",
118 metaKey: false,
119 ctrlKey: false,
120 shiftKey: false,
121 altKey: false,
122 modKey: true,
123 },
124 });
125​
126 assert.equal(encoded.key, "mod++");
127 assert.equal(encoded.command, "terminal.toggle");
128 }),
129 );
130​
131 it.effect("rejects invalid rules", () =>
132 Effect.sync(() => {
133 assert.isNull(
134 Keybindings.compileResolvedKeybindingRule({
135 key: "mod+shift+d+o",
136 command: "terminal.new",
137 }),
138 );
139​
140 assert.isNull(
141 Keybindings.compileResolvedKeybindingRule({
142 key: "mod+d",
143 command: "terminal.split",
144 when: "terminalFocus && (",
145 }),
146 );
147​
148 assert.isNull(
149 Keybindings.compileResolvedKeybindingRule({
150 key: "mod+d",
151 command: "terminal.split",
152 when: `${"!".repeat(300)}terminalFocus`,
153 }),
154 );
155 }),
156 );
157​
158 it.effect("formats invalid resolved keybinding rules with the custom message", () =>
159 Effect.sync(() => {
160 const result = decodeResolvedKeybindingFromConfigExit({
161 key: "mod+shift+d+o",
162 command: "terminal.new",
163 });
164​
165 if (result._tag !== "Failure") {
166 assert.fail("Expected invalid keybinding decode to fail");
167 }
168​
169 const detail = Cause.pretty(result.cause);
170 assert.isTrue(detail.includes("Invalid keybinding rule"));
171 assert.isFalse(detail.includes("Invalid data"));
172 }),
173 );
174​
175 it.effect("bootstraps default keybindings when config file is missing", () =>
176 Effect.gen(function* () {
177 const fs = yield* FileSystem.FileSystem;
178 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
179 assert.isFalse(yield* fs.exists(keybindingsConfigPath));
180​
181 yield* Effect.gen(function* () {
182 const keybindings = yield* Keybindings.Keybindings;
183 yield* keybindings.syncDefaultKeybindingsOnStartup;
184 });
185​
186 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
187 assert.deepEqual(persisted, Keybindings.DEFAULT_KEYBINDINGS);
188 }).pipe(Effect.provide(layerKeybindings())),
189 );
190​
191 it.effect("uses defaults in runtime when config is malformed without overriding file", () =>
192 Effect.gen(function* () {
193 const fs = yield* FileSystem.FileSystem;
194 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
195 yield* fs.writeFileString(keybindingsConfigPath, "{ not-json");
196​
197 const configState = yield* Effect.gen(function* () {
198 const keybindings = yield* Keybindings.Keybindings;
199 return yield* keybindings.loadConfigState;
200 });
201​
202 assert.deepEqual(
203 configState.keybindings,
204 Keybindings.compileResolvedKeybindingsConfig(Keybindings.DEFAULT_KEYBINDINGS),
205 );
206 assert.deepEqual(configState.issues, [
207 {
208 kind: "keybindings.malformed-config",
209 message: configState.issues[0]?.message ?? "",
210 },
211 ]);
212 assert.equal(yield* fs.readFileString(keybindingsConfigPath), "{ not-json");
213 }).pipe(Effect.provide(layerKeybindings())),
214 );
215​
216 it.effect("ignores invalid entries in runtime and reports them as issues", () =>
217 Effect.gen(function* () {
218 const fs = yield* FileSystem.FileSystem;
219 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
220 yield* fs.writeFileString(
221 keybindingsConfigPath,
222 JSON.stringify([
223 { key: "mod+j", command: "terminal.toggle" },
224 { key: "mod+shift+d+o", command: "terminal.new" },
225 { key: "mod+x", command: "invalid.command" },
226 ]),
227 );
228​
229 const configState = yield* Effect.gen(function* () {
230 const keybindings = yield* Keybindings.Keybindings;
231 return yield* keybindings.loadConfigState;
232 });
233​
234 assert.isTrue(configState.keybindings.some((entry) => entry.command === "terminal.toggle"));
235 assert.isFalse(
236 configState.keybindings.some((entry) => String(entry.command) === "invalid.command"),
237 );
238 assert.deepEqual(configState.issues, [
239 {
240 kind: "keybindings.invalid-entry",
241 index: 1,
242 message: configState.issues[0]?.message ?? "",
243 },
244 {
245 kind: "keybindings.invalid-entry",
246 index: 2,
247 message: configState.issues[1]?.message ?? "",
248 },
249 ]);
250 }).pipe(Effect.provide(layerKeybindings())),
251 );
252​
253 it.effect(
254 "upserts missing default keybindings on startup without overriding existing command rules",
255 () =>
256 Effect.gen(function* () {
257 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
258 yield* writeKeybindingsConfig(keybindingsConfigPath, [
259 { key: "mod+shift+y", command: "terminal.toggle" },
260 { key: "mod+shift+r", command: "script.run-tests.run" },
261 ]);
262​
263 yield* Effect.gen(function* () {
264 const keybindings = yield* Keybindings.Keybindings;
265 yield* keybindings.syncDefaultKeybindingsOnStartup;
266 });
267​
268 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
269 const byCommand = new Map(persisted.map((entry) => [entry.command, entry]));
270​
271 const persistedToggle = byCommand.get("terminal.toggle");
272 assert.isNotNull(persistedToggle);
273 assert.equal(persistedToggle?.key, "mod+shift+y");
274 assert.isFalse(
275 persisted.some((entry) => entry.command === "terminal.toggle" && entry.key === "mod+j"),
276 );
277​
278 for (const defaultRule of Keybindings.DEFAULT_KEYBINDINGS) {
279 assert.isTrue(byCommand.has(defaultRule.command), `expected ${defaultRule.command}`);
280 }
281 assert.isTrue(byCommand.has("script.run-tests.run"));
282 }).pipe(Effect.provide(layerKeybindings())),
283 );
284​
285 it.effect("adds a late default to an existing command once", () =>
286 Effect.gen(function* () {
287 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
288 const keybindings = yield* Keybindings.Keybindings;
289 const when = "composerFocus && draftThreadRoute";
290 const existing = { key: "mod+alt+enter", command: "composer.sendBackground", when } as const;
291 const backgroundRules = Effect.map(readKeybindingsConfig(keybindingsConfigPath), (rules) =>
292 rules.filter((entry) => entry.command === "composer.sendBackground"),
293 );
294 yield* writeKeybindingsConfig(keybindingsConfigPath, [existing]);
295​
296 yield* keybindings.syncDefaultKeybindingsOnStartup;
297 assert.deepStrictEqual(yield* backgroundRules, [
298 existing,
299 { key: "mod+enter", command: "composer.sendBackground", when },
300 ]);
301​
302 // Removing the added rule later must survive the next startup.
303 yield* writeKeybindingsConfig(keybindingsConfigPath, [existing]);
304 yield* keybindings.syncDefaultKeybindingsOnStartup;
305 assert.deepStrictEqual(yield* backgroundRules, [existing]);
306 }).pipe(Effect.provide(layerKeybindings())),
307 );
308​
309 it.effect("leaves a customized command without the late default", () =>
310 Effect.gen(function* () {
311 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
312 const keybindings = yield* Keybindings.Keybindings;
313 const custom = {
314 key: "alt+b",
315 command: "composer.sendBackground",
316 when: "composerFocus && draftThreadRoute",
317 } as const;
318 yield* writeKeybindingsConfig(keybindingsConfigPath, [custom]);
319​
320 yield* keybindings.syncDefaultKeybindingsOnStartup;
321 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
322 assert.deepStrictEqual(
323 persisted.filter((entry) => entry.command === "composer.sendBackground"),
324 [custom],
325 );
326 }).pipe(Effect.provide(layerKeybindings())),
327 );
328​
329 it.effect("keeps a late default pending while the config is full", () =>
330 Effect.gen(function* () {
331 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
332 const keybindings = yield* Keybindings.Keybindings;
333 const when = "composerFocus && draftThreadRoute";
334 const existing = { key: "mod+alt+enter", command: "composer.sendBackground", when } as const;
335 const fillers = Array.from({ length: MAX_KEYBINDINGS_COUNT - 1 }, (_, index) => ({
336 key: "mod+alt+f1",
337 command: `script.filler-${index}.run` as const,
338 }));
339 const hasModEnter = Effect.map(readKeybindingsConfig(keybindingsConfigPath), (rules) =>
340 rules.some(
341 (entry) => entry.command === "composer.sendBackground" && entry.key === "mod+enter",
342 ),
343 );
344​
345 yield* writeKeybindingsConfig(keybindingsConfigPath, [existing, ...fillers]);
346 yield* keybindings.syncDefaultKeybindingsOnStartup;
347 assert.isFalse(yield* hasModEnter);
348​
349 yield* writeKeybindingsConfig(keybindingsConfigPath, [existing]);
350 yield* keybindings.syncDefaultKeybindingsOnStartup;
351 assert.isTrue(yield* hasModEnter);
352 }).pipe(Effect.provide(layerKeybindings())),
353 );
354​
355 it.effect("skips conflicting default keybindings on startup and logs a detailed warning", () => {
356 const messages: string[] = [];
357 const logger = Logger.make(({ message }) => {
358 messages.push(String(message));
359 });
360​
361 return Effect.gen(function* () {
362 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
363 yield* writeKeybindingsConfig(keybindingsConfigPath, [
364 { key: "mod+j", command: "script.custom-action.run" },
365 ]);
366​
367 yield* Effect.gen(function* () {
368 const keybindings = yield* Keybindings.Keybindings;
369 yield* keybindings.syncDefaultKeybindingsOnStartup;
370 });
371​
372 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
373 assert.isFalse(persisted.some((entry) => entry.command === "terminal.toggle"));
374 assert.isTrue(persisted.some((entry) => entry.command === "script.custom-action.run"));
375​
376 assert.isTrue(
377 messages.some((message) =>
378 message.includes("skipping default keybinding due to shortcut conflict"),
379 ),
380 );
381 }).pipe(
382 Effect.provide(
383 Layer.mergeAll(layerKeybindings(), Logger.layer([logger], { mergeWithExisting: false })),
384 ),
385 );
386 });
387​
388 it.effect("upserts custom keybindings to configured path", () =>
389 Effect.gen(function* () {
390 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
391 yield* writeKeybindingsConfig(keybindingsConfigPath, [
392 { key: "mod+j", command: "terminal.toggle" },
393 ]);
394​
395 const resolved = yield* Effect.gen(function* () {
396 const keybindings = yield* Keybindings.Keybindings;
397 return yield* keybindings.upsertKeybindingRule({
398 key: "mod+shift+r",
399 command: "script.run-tests.run",
400 });
401 });
402​
403 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
404 const persistedView = persisted.map(({ key, command }) => ({ key, command }));
405​
406 assert.deepEqual(persistedView, [
407 { key: "mod+j", command: "terminal.toggle" },
408 { key: "mod+shift+r", command: "script.run-tests.run" },
409 ]);
410 assert.isTrue(resolved.some((entry) => entry.command === "script.run-tests.run"));
411 }).pipe(Effect.provide(layerKeybindings())),
412 );
413​
414 it.effect("appends additional custom keybindings for the same command", () =>
415 Effect.gen(function* () {
416 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
417 yield* writeKeybindingsConfig(keybindingsConfigPath, [
418 { key: "mod+r", command: "script.run-tests.run" },
419 ]);
420 yield* Effect.gen(function* () {
421 const keybindings = yield* Keybindings.Keybindings;
422 return yield* keybindings.upsertKeybindingRule({
423 key: "mod+shift+r",
424 command: "script.run-tests.run",
425 });
426 });
427​
428 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
429 const persistedView = persisted.map(({ key, command }) => ({ key, command }));
430 assert.deepEqual(persistedView, [
431 { key: "mod+r", command: "script.run-tests.run" },
432 { key: "mod+shift+r", command: "script.run-tests.run" },
433 ]);
434 }).pipe(Effect.provide(layerKeybindings())),
435 );
436​
437 it.effect("replaces only the targeted custom keybinding", () =>
438 Effect.gen(function* () {
439 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
440 yield* writeKeybindingsConfig(keybindingsConfigPath, [
441 { key: "mod+r", command: "script.run-tests.run" },
442 { key: "mod+shift+r", command: "script.run-tests.run" },
443 ]);
444 yield* Effect.gen(function* () {
445 const keybindings = yield* Keybindings.Keybindings;
446 return yield* keybindings.upsertKeybindingRule({
447 key: "mod+alt+r",
448 command: "script.run-tests.run",
449 replace: { key: "mod+r", command: "script.run-tests.run" },
450 });
451 });
452​
453 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
454 const persistedView = persisted.map(({ key, command }) => ({ key, command }));
455 assert.deepEqual(persistedView, [
456 { key: "mod+shift+r", command: "script.run-tests.run" },
457 { key: "mod+alt+r", command: "script.run-tests.run" },
458 ]);
459 }).pipe(Effect.provide(layerKeybindings())),
460 );
461​
462 it.effect("replacing with a rule that already exists elsewhere does not duplicate it", () =>
463 Effect.gen(function* () {
464 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
465 yield* writeKeybindingsConfig(keybindingsConfigPath, [
466 { key: "mod+r", command: "script.run-tests.run" },
467 { key: "mod+alt+r", command: "script.run-tests.run" },
468 ]);
469 yield* Effect.gen(function* () {
470 const keybindings = yield* Keybindings.Keybindings;
471 return yield* keybindings.upsertKeybindingRule({
472 key: "mod+alt+r",
473 command: "script.run-tests.run",
474 replace: { key: "mod+r", command: "script.run-tests.run" },
475 });
476 });
477​
478 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
479 const persistedView = persisted.map(({ key, command }) => ({ key, command }));
480 assert.deepEqual(persistedView, [{ key: "mod+alt+r", command: "script.run-tests.run" }]);
481 }).pipe(Effect.provide(layerKeybindings())),
482 );
483​
484 it.effect("removes only the targeted custom keybinding", () =>
485 Effect.gen(function* () {
486 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
487 yield* writeKeybindingsConfig(keybindingsConfigPath, [
488 { key: "mod+r", command: "script.run-tests.run" },
489 { key: "mod+shift+r", command: "script.run-tests.run" },
490 ]);
491 yield* Effect.gen(function* () {
492 const keybindings = yield* Keybindings.Keybindings;
493 return yield* keybindings.removeKeybindingRule({
494 key: "mod+r",
495 command: "script.run-tests.run",
496 });
497 });
498​
499 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
500 const persistedView = persisted.map(({ key, command }) => ({ key, command }));
501 assert.deepEqual(persistedView, [{ key: "mod+shift+r", command: "script.run-tests.run" }]);
502 }).pipe(Effect.provide(layerKeybindings())),
503 );
504​
505 it.effect("refuses to overwrite malformed keybindings config", () =>
506 Effect.gen(function* () {
507 const fs = yield* FileSystem.FileSystem;
508 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
509 yield* fs.writeFileString(keybindingsConfigPath, "{ not-json");
510​
511 const result = yield* Effect.gen(function* () {
512 const keybindings = yield* Keybindings.Keybindings;
513 return yield* keybindings.upsertKeybindingRule({
514 key: "mod+shift+r",
515 command: "script.run-tests.run",
516 });
517 }).pipe(toDetailResult);
518 assertFailure(result, "expected JSON array");
519​
520 const persistedRaw = yield* fs.readFileString(keybindingsConfigPath);
521 assert.equal(persistedRaw, "{ not-json");
522 }).pipe(Effect.provide(layerKeybindings())),
523 );
524​
525 it.effect("reports non-array config parse errors without duplicate prefix", () =>
526 Effect.gen(function* () {
527 const fs = yield* FileSystem.FileSystem;
528 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
529 yield* fs.writeFileString(
530 keybindingsConfigPath,
531 '{"key":"mod+j","command":"terminal.toggle"}',
532 );
533​
534 const firstResult = yield* Effect.gen(function* () {
535 const keybindings = yield* Keybindings.Keybindings;
536 return yield* keybindings.upsertKeybindingRule({
537 key: "mod+shift+r",
538 command: "script.run-tests.run",
539 });
540 }).pipe(toDetailResult);
541 assertFailure(firstResult, "expected JSON array");
542​
543 const secondResult = yield* Effect.gen(function* () {
544 const keybindings = yield* Keybindings.Keybindings;
545 return yield* keybindings.upsertKeybindingRule({
546 key: "mod+shift+r",
547 command: "script.run-tests.run",
548 });
549 }).pipe(toDetailResult);
550 assertFailure(secondResult, "expected JSON array");
551 }).pipe(Effect.provide(layerKeybindings())),
552 );
553​
554 // chmod cannot make a directory unwritable on Windows, so the write succeeds.
555 it.effect.skipIf(HostProcessPlatform.defaultValue() === "win32")(
556 "fails when config directory is not writable",
557 () =>
558 Effect.gen(function* () {
559 const fs = yield* FileSystem.FileSystem;
560 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
561 const { dirname } = yield* Path.Path;
562 yield* writeKeybindingsConfig(keybindingsConfigPath, [
563 { key: "mod+j", command: "terminal.toggle" },
564 ]);
565 yield* fs.chmod(dirname(keybindingsConfigPath), 0o500);
566​
567 const result = yield* Effect.gen(function* () {
568 const keybindings = yield* Keybindings.Keybindings;
569 return yield* keybindings.upsertKeybindingRule({
570 key: "mod+shift+r",
571 command: "script.run-tests.run",
572 });
573 }).pipe(toDetailResult);
574 assertFailure(result, "failed to write keybindings config");
575​
576 yield* fs.chmod(dirname(keybindingsConfigPath), 0o700);
577​
578 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
579 const persistedView = persisted.map(({ key, command }) => ({ key, command }));
580 assert.deepEqual(persistedView, [{ key: "mod+j", command: "terminal.toggle" }]);
581 }).pipe(Effect.provide(layerKeybindings())),
582 );
583​
584 it.effect("caches loaded resolved config across repeated reads", () =>
585 Effect.gen(function* () {
586 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
587 yield* writeKeybindingsConfig(keybindingsConfigPath, [
588 { key: "mod+j", command: "terminal.toggle" },
589 ]);
590​
591 const [first, second] = yield* Effect.gen(function* () {
592 const keybindings = yield* Keybindings.Keybindings;
593 const firstLoad = (yield* keybindings.loadConfigState).keybindings;
594 const secondLoad = (yield* keybindings.loadConfigState).keybindings;
595 return [firstLoad, secondLoad] as const;
596 });
597​
598 assert.deepEqual(first, second);
599 assert.isTrue(second.some((entry) => entry.command === "terminal.toggle"));
600 }).pipe(Effect.provide(layerKeybindings())),
601 );
602​
603 it.effect("retries a failed config read instead of keeping the failure", () =>
604 Effect.gen(function* () {
605 const fileSystem = yield* FileSystem.FileSystem;
606 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
607 // A directory where the file should be makes the read itself fail.
608 yield* fileSystem.makeDirectory(keybindingsConfigPath, { recursive: true });
609​
610 const keybindings = yield* Keybindings.Keybindings;
611 const failed = yield* toDetailResult(keybindings.loadConfigState);
612 assertFailure(failed, "failed to read keybindings config");
613​
614 yield* fileSystem.remove(keybindingsConfigPath, { recursive: true });
615 yield* writeKeybindingsConfig(keybindingsConfigPath, [
616 { key: "mod+j", command: "terminal.toggle" },
617 ]);
618​
619 const configState = yield* keybindings.loadConfigState;
620 assert.deepEqual(configState.issues, []);
621 assert.isTrue(configState.keybindings.some((entry) => entry.command === "terminal.toggle"));
622 }).pipe(Effect.provide(layerKeybindings())),
623 );
624​
625 it.effect("updates cached resolved config after upsert", () =>
626 Effect.gen(function* () {
627 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
628 yield* writeKeybindingsConfig(keybindingsConfigPath, [
629 { key: "mod+j", command: "terminal.toggle" },
630 ]);
631​
632 const loadedAfterUpsert = yield* Effect.gen(function* () {
633 const keybindings = yield* Keybindings.Keybindings;
634 yield* keybindings.loadConfigState;
635 yield* keybindings.upsertKeybindingRule({
636 key: "mod+shift+r",
637 command: "script.run-tests.run",
638 });
639 return (yield* keybindings.loadConfigState).keybindings;
640 });
641​
642 assert.isTrue(loadedAfterUpsert.some((entry) => entry.command === "script.run-tests.run"));
643 assert.isTrue(loadedAfterUpsert.some((entry) => entry.command === "terminal.toggle"));
644 }).pipe(Effect.provide(layerKeybindings())),
645 );
646​
647 it.effect("serializes concurrent upserts to avoid lost updates", () =>
648 Effect.gen(function* () {
649 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
650 yield* writeKeybindingsConfig(keybindingsConfigPath, []);
651​
652 const commands = Array.from(
653 { length: 20 },
654 (_, index): KeybindingCommand => `script.concurrent-${index}.run`,
655 );
656 yield* Effect.gen(function* () {
657 const keybindings = yield* Keybindings.Keybindings;
658 yield* Effect.forEach(
659 commands,
660 (command, index) =>
661 keybindings.upsertKeybindingRule({
662 key: `mod+${String.fromCharCode(97 + index)}`,
663 command,
664 }),
665 { concurrency: "unbounded", discard: true },
666 );
667 });
668​
669 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
670 const persistedCommands = new Set(persisted.map((entry) => entry.command));
671 for (const command of commands) {
672 assert.isTrue(persistedCommands.has(command), `expected persisted command ${command}`);
673 }
674 }).pipe(Effect.provide(layerKeybindings())),
675 );
676});