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