Open History to see the last commit
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 * as HostProcess 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 { key: "mod+shift+b", command: "script.custom-panel.run" },
262 {
263 key: "mod+shift+b",
264 command: "script.custom-composer.run",
265 when: "composerFocus",
266 },
267 ]);
268
269 const keybindings = yield* Keybindings.Keybindings;
270 yield* keybindings.syncDefaultKeybindingsOnStartup;
271
272 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
273 const byCommand = new Map(persisted.map((entry) => [entry.command, entry]));
274
275 const persistedToggle = byCommand.get("terminal.toggle");
276 assert.isNotNull(persistedToggle);
277 assert.equal(persistedToggle?.key, "mod+shift+y");
278 assert.isFalse(
279 persisted.some((entry) => entry.command === "terminal.toggle" && entry.key === "mod+j"),
280 );
281
282 for (const defaultRule of Keybindings.DEFAULT_KEYBINDINGS) {
283 assert.isTrue(byCommand.has(defaultRule.command), `expected ${defaultRule.command}`);
284 }
285 assert.isTrue(byCommand.has("script.run-tests.run"));
286
287 const configState = yield* keybindings.loadConfigState;
288 assert.deepEqual(
289 configState.keybindings
290 .filter((entry) => entry.shortcut.key === "b" && entry.shortcut.shiftKey)
291 .map((entry) => entry.command),
292 ["threadPanel.toggle", "script.custom-panel.run", "script.custom-composer.run"],
293 );
294 }).pipe(Effect.provide(layerKeybindings())),
295 );
296
297 it.effect("adds a late default to an existing command once", () =>
298 Effect.gen(function* () {
299 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
300 const keybindings = yield* Keybindings.Keybindings;
301 const when = "composerFocus && draftThreadRoute";
302 const existing = { key: "mod+alt+enter", command: "composer.sendBackground", when } as const;
303 const backgroundRules = Effect.map(readKeybindingsConfig(keybindingsConfigPath), (rules) =>
304 rules.filter((entry) => entry.command === "composer.sendBackground"),
305 );
306 yield* writeKeybindingsConfig(keybindingsConfigPath, [existing]);
307
308 yield* keybindings.syncDefaultKeybindingsOnStartup;
309 assert.deepStrictEqual(yield* backgroundRules, [
310 { key: "mod+enter", command: "composer.sendBackground", when },
311 existing,
312 ]);
313
314 yield* writeKeybindingsConfig(keybindingsConfigPath, [existing]);
315 yield* keybindings.syncDefaultKeybindingsOnStartup;
316 assert.deepStrictEqual(yield* backgroundRules, [existing]);
317 }).pipe(Effect.provide(layerKeybindings())),
318 );
319
320 it.effect("leaves a customized command without the late default", () =>
321 Effect.gen(function* () {
322 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
323 const keybindings = yield* Keybindings.Keybindings;
324 const custom = {
325 key: "alt+b",
326 command: "composer.sendBackground",
327 when: "composerFocus && draftThreadRoute",
328 } as const;
329 yield* writeKeybindingsConfig(keybindingsConfigPath, [custom]);
330
331 yield* keybindings.syncDefaultKeybindingsOnStartup;
332 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
333 assert.deepStrictEqual(
334 persisted.filter((entry) => entry.command === "composer.sendBackground"),
335 [custom],
336 );
337 }).pipe(Effect.provide(layerKeybindings())),
338 );
339
340 it.effect("keeps a late default pending while the config is full", () =>
341 Effect.gen(function* () {
342 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
343 const keybindings = yield* Keybindings.Keybindings;
344 const when = "composerFocus && draftThreadRoute";
345 const existing = { key: "mod+alt+enter", command: "composer.sendBackground", when } as const;
346 const fillers = Array.from({ length: MAX_KEYBINDINGS_COUNT - 1 }, (_, index) => ({
347 key: "mod+alt+f1",
348 command: `script.filler-${index}.run` as const,
349 }));
350 const hasModEnter = Effect.map(readKeybindingsConfig(keybindingsConfigPath), (rules) =>
351 rules.some(
352 (entry) => entry.command === "composer.sendBackground" && entry.key === "mod+enter",
353 ),
354 );
355
356 yield* writeKeybindingsConfig(keybindingsConfigPath, [existing, ...fillers]);
357 yield* keybindings.syncDefaultKeybindingsOnStartup;
358 assert.isFalse(yield* hasModEnter);
359
360 yield* writeKeybindingsConfig(keybindingsConfigPath, [existing]);
361 yield* keybindings.syncDefaultKeybindingsOnStartup;
362 assert.isTrue(yield* hasModEnter);
363 }).pipe(Effect.provide(layerKeybindings())),
364 );
365
366 it.effect("skips conflicting default keybindings on startup and logs a detailed warning", () => {
367 const messages: string[] = [];
368 const logger = Logger.make(({ message }) => {
369 messages.push(String(message));
370 });
371
372 return Effect.gen(function* () {
373 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
374 yield* writeKeybindingsConfig(keybindingsConfigPath, [
375 { key: "mod+j", command: "script.custom-action.run" },
376 ]);
377
378 yield* Effect.gen(function* () {
379 const keybindings = yield* Keybindings.Keybindings;
380 yield* keybindings.syncDefaultKeybindingsOnStartup;
381 });
382
383 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
384 assert.isFalse(persisted.some((entry) => entry.command === "terminal.toggle"));
385 assert.isTrue(persisted.some((entry) => entry.command === "script.custom-action.run"));
386
387 assert.isTrue(
388 messages.some((message) =>
389 message.includes("skipping default keybinding due to shortcut conflict"),
390 ),
391 );
392 }).pipe(
393 Effect.provide(
394 Layer.mergeAll(layerKeybindings(), Logger.layer([logger], { mergeWithExisting: false })),
395 ),
396 );
397 });
398
399 it.effect("upserts custom keybindings to configured path", () =>
400 Effect.gen(function* () {
401 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
402 yield* writeKeybindingsConfig(keybindingsConfigPath, [
403 { key: "mod+j", command: "terminal.toggle" },
404 ]);
405
406 const resolved = yield* Effect.gen(function* () {
407 const keybindings = yield* Keybindings.Keybindings;
408 return yield* keybindings.upsertKeybindingRule({
409 key: "mod+shift+r",
410 command: "script.run-tests.run",
411 });
412 });
413
414 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
415 const persistedView = persisted.map(({ key, command }) => ({ key, command }));
416
417 assert.deepEqual(persistedView, [
418 { key: "mod+j", command: "terminal.toggle" },
419 { key: "mod+shift+r", command: "script.run-tests.run" },
420 ]);
421 assert.isTrue(resolved.some((entry) => entry.command === "script.run-tests.run"));
422 }).pipe(Effect.provide(layerKeybindings())),
423 );
424
425 it.effect("appends additional custom keybindings for the same command", () =>
426 Effect.gen(function* () {
427 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
428 yield* writeKeybindingsConfig(keybindingsConfigPath, [
429 { key: "mod+r", command: "script.run-tests.run" },
430 ]);
431 yield* Effect.gen(function* () {
432 const keybindings = yield* Keybindings.Keybindings;
433 return yield* keybindings.upsertKeybindingRule({
434 key: "mod+shift+r",
435 command: "script.run-tests.run",
436 });
437 });
438
439 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
440 const persistedView = persisted.map(({ key, command }) => ({ key, command }));
441 assert.deepEqual(persistedView, [
442 { key: "mod+r", command: "script.run-tests.run" },
443 { key: "mod+shift+r", command: "script.run-tests.run" },
444 ]);
445 }).pipe(Effect.provide(layerKeybindings())),
446 );
447
448 it.effect("replaces only the targeted custom keybinding", () =>
449 Effect.gen(function* () {
450 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
451 yield* writeKeybindingsConfig(keybindingsConfigPath, [
452 { key: "mod+r", command: "script.run-tests.run" },
453 { key: "mod+shift+r", command: "script.run-tests.run" },
454 ]);
455 yield* Effect.gen(function* () {
456 const keybindings = yield* Keybindings.Keybindings;
457 return yield* keybindings.upsertKeybindingRule({
458 key: "mod+alt+r",
459 command: "script.run-tests.run",
460 replace: { key: "mod+r", command: "script.run-tests.run" },
461 });
462 });
463
464 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
465 const persistedView = persisted.map(({ key, command }) => ({ key, command }));
466 assert.deepEqual(persistedView, [
467 { key: "mod+shift+r", command: "script.run-tests.run" },
468 { key: "mod+alt+r", command: "script.run-tests.run" },
469 ]);
470 }).pipe(Effect.provide(layerKeybindings())),
471 );
472
473 it.effect("replacing with a rule that already exists elsewhere does not duplicate it", () =>
474 Effect.gen(function* () {
475 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
476 yield* writeKeybindingsConfig(keybindingsConfigPath, [
477 { key: "mod+r", command: "script.run-tests.run" },
478 { key: "mod+alt+r", command: "script.run-tests.run" },
479 ]);
480 yield* Effect.gen(function* () {
481 const keybindings = yield* Keybindings.Keybindings;
482 return yield* keybindings.upsertKeybindingRule({
483 key: "mod+alt+r",
484 command: "script.run-tests.run",
485 replace: { key: "mod+r", command: "script.run-tests.run" },
486 });
487 });
488
489 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
490 const persistedView = persisted.map(({ key, command }) => ({ key, command }));
491 assert.deepEqual(persistedView, [{ key: "mod+alt+r", command: "script.run-tests.run" }]);
492 }).pipe(Effect.provide(layerKeybindings())),
493 );
494
495 it.effect("removes only the targeted custom keybinding", () =>
496 Effect.gen(function* () {
497 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
498 yield* writeKeybindingsConfig(keybindingsConfigPath, [
499 { key: "mod+r", command: "script.run-tests.run" },
500 { key: "mod+shift+r", command: "script.run-tests.run" },
501 ]);
502 yield* Effect.gen(function* () {
503 const keybindings = yield* Keybindings.Keybindings;
504 return yield* keybindings.removeKeybindingRule({
505 key: "mod+r",
506 command: "script.run-tests.run",
507 });
508 });
509
510 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
511 const persistedView = persisted.map(({ key, command }) => ({ key, command }));
512 assert.deepEqual(persistedView, [{ key: "mod+shift+r", command: "script.run-tests.run" }]);
513 }).pipe(Effect.provide(layerKeybindings())),
514 );
515
516 it.effect("refuses to overwrite malformed keybindings config", () =>
517 Effect.gen(function* () {
518 const fs = yield* FileSystem.FileSystem;
519 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
520 yield* fs.writeFileString(keybindingsConfigPath, "{ not-json");
521
522 const result = yield* Effect.gen(function* () {
523 const keybindings = yield* Keybindings.Keybindings;
524 return yield* keybindings.upsertKeybindingRule({
525 key: "mod+shift+r",
526 command: "script.run-tests.run",
527 });
528 }).pipe(toDetailResult);
529 assertFailure(result, "expected JSON array");
530
531 const persistedRaw = yield* fs.readFileString(keybindingsConfigPath);
532 assert.equal(persistedRaw, "{ not-json");
533 }).pipe(Effect.provide(layerKeybindings())),
534 );
535
536 it.effect("reports non-array config parse errors without duplicate prefix", () =>
537 Effect.gen(function* () {
538 const fs = yield* FileSystem.FileSystem;
539 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
540 yield* fs.writeFileString(
541 keybindingsConfigPath,
542 '{"key":"mod+j","command":"terminal.toggle"}',
543 );
544
545 const firstResult = yield* Effect.gen(function* () {
546 const keybindings = yield* Keybindings.Keybindings;
547 return yield* keybindings.upsertKeybindingRule({
548 key: "mod+shift+r",
549 command: "script.run-tests.run",
550 });
551 }).pipe(toDetailResult);
552 assertFailure(firstResult, "expected JSON array");
553
554 const secondResult = yield* Effect.gen(function* () {
555 const keybindings = yield* Keybindings.Keybindings;
556 return yield* keybindings.upsertKeybindingRule({
557 key: "mod+shift+r",
558 command: "script.run-tests.run",
559 });
560 }).pipe(toDetailResult);
561 assertFailure(secondResult, "expected JSON array");
562 }).pipe(Effect.provide(layerKeybindings())),
563 );
564
565 // chmod cannot make a directory unwritable on Windows, so the write succeeds.
566 it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
567 "fails when config directory is not writable",
568 () =>
569 Effect.gen(function* () {
570 const fs = yield* FileSystem.FileSystem;
571 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
572 const { dirname } = yield* Path.Path;
573 yield* writeKeybindingsConfig(keybindingsConfigPath, [
574 { key: "mod+j", command: "terminal.toggle" },
575 ]);
576 yield* fs.chmod(dirname(keybindingsConfigPath), 0o500);
577
578 const result = yield* Effect.gen(function* () {
579 const keybindings = yield* Keybindings.Keybindings;
580 return yield* keybindings.upsertKeybindingRule({
581 key: "mod+shift+r",
582 command: "script.run-tests.run",
583 });
584 }).pipe(toDetailResult);
585 assertFailure(result, "failed to write keybindings config");
586
587 yield* fs.chmod(dirname(keybindingsConfigPath), 0o700);
588
589 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
590 const persistedView = persisted.map(({ key, command }) => ({ key, command }));
591 assert.deepEqual(persistedView, [{ key: "mod+j", command: "terminal.toggle" }]);
592 }).pipe(Effect.provide(layerKeybindings())),
593 );
594
595 it.effect("caches loaded resolved config across repeated reads", () =>
596 Effect.gen(function* () {
597 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
598 yield* writeKeybindingsConfig(keybindingsConfigPath, [
599 { key: "mod+j", command: "terminal.toggle" },
600 ]);
601
602 const [first, second] = yield* Effect.gen(function* () {
603 const keybindings = yield* Keybindings.Keybindings;
604 const firstLoad = (yield* keybindings.loadConfigState).keybindings;
605 const secondLoad = (yield* keybindings.loadConfigState).keybindings;
606 return [firstLoad, secondLoad] as const;
607 });
608
609 assert.deepEqual(first, second);
610 assert.isTrue(second.some((entry) => entry.command === "terminal.toggle"));
611 }).pipe(Effect.provide(layerKeybindings())),
612 );
613
614 it.effect("retries a failed config read instead of keeping the failure", () =>
615 Effect.gen(function* () {
616 const fileSystem = yield* FileSystem.FileSystem;
617 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
618 yield* fileSystem.makeDirectory(keybindingsConfigPath, { recursive: true });
619
620 const keybindings = yield* Keybindings.Keybindings;
621 const failed = yield* toDetailResult(keybindings.loadConfigState);
622 assertFailure(failed, "failed to read keybindings config");
623
624 yield* fileSystem.remove(keybindingsConfigPath, { recursive: true });
625 yield* writeKeybindingsConfig(keybindingsConfigPath, [
626 { key: "mod+j", command: "terminal.toggle" },
627 ]);
628
629 const configState = yield* keybindings.loadConfigState;
630 assert.deepEqual(configState.issues, []);
631 assert.isTrue(configState.keybindings.some((entry) => entry.command === "terminal.toggle"));
632 }).pipe(Effect.provide(layerKeybindings())),
633 );
634
635 it.effect("updates cached resolved config after upsert", () =>
636 Effect.gen(function* () {
637 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
638 yield* writeKeybindingsConfig(keybindingsConfigPath, [
639 { key: "mod+j", command: "terminal.toggle" },
640 ]);
641
642 const loadedAfterUpsert = yield* Effect.gen(function* () {
643 const keybindings = yield* Keybindings.Keybindings;
644 yield* keybindings.loadConfigState;
645 yield* keybindings.upsertKeybindingRule({
646 key: "mod+shift+r",
647 command: "script.run-tests.run",
648 });
649 return (yield* keybindings.loadConfigState).keybindings;
650 });
651
652 assert.isTrue(loadedAfterUpsert.some((entry) => entry.command === "script.run-tests.run"));
653 assert.isTrue(loadedAfterUpsert.some((entry) => entry.command === "terminal.toggle"));
654 }).pipe(Effect.provide(layerKeybindings())),
655 );
656
657 it.effect("serializes concurrent upserts to avoid lost updates", () =>
658 Effect.gen(function* () {
659 const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
660 yield* writeKeybindingsConfig(keybindingsConfigPath, []);
661
662 const commands = Array.from(
663 { length: 20 },
664 (_, index): KeybindingCommand => `script.concurrent-${index}.run`,
665 );
666 yield* Effect.gen(function* () {
667 const keybindings = yield* Keybindings.Keybindings;
668 yield* Effect.forEach(
669 commands,
670 (command, index) =>
671 keybindings.upsertKeybindingRule({
672 key: `mod+${String.fromCharCode(97 + index)}`,
673 command,
674 }),
675 { concurrency: "unbounded", discard: true },
676 );
677 });
678
679 const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
680 const persistedCommands = new Set(persisted.map((entry) => entry.command));
681 for (const command of commands) {
682 assert.isTrue(persistedCommands.has(command), `expected persisted command ${command}`);
683 }
684 }).pipe(Effect.provide(layerKeybindings())),
685 );
686});