@devries-cameron20 / t3code-1 / commits / 6667992753
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Harden keybinding schemas and align tests with server defaults

- make resolved keybinding/shortcut schemas strict to reject unknown fields - export server `DEFAULT_KEYBINDINGS` as source of truth and reuse it in ws tests - simplify keybinding test fixtures and update docs to reference server defaults

Julius Marminge committed Feb 15, 2026 at 14:20 UTC 6667992753c3995fb591356ce8784624c6d4bd05
6 files changed +82 -162
KEYBINDINGS.md
+3 -10
@@ -21,11 +21,8 @@ The file must be a JSON array of rules:
21
22 ```json
23 [
24 - { "key": "mod+j", "command": "terminal.toggle" },
25 - { "key": "mod+d", "command": "terminal.split", "when": "terminalFocus" },
26 - { "key": "mod+shift+d", "command": "terminal.new", "when": "terminalFocus" },
27 - { "key": "mod+shift+o", "command": "chat.new" },
28 - { "key": "mod+o", "command": "editor.openFavorite" }
24 + { "key": "mod+g", "command": "terminal.toggle" },
25 + { "key": "mod+shift+g", "command": "terminal.new", "when": "terminalFocus" }
26 ]
27 ```
28
@@ -90,11 +87,7 @@ Unknown condition keys evaluate to `false`.
87
88 Built-in defaults are resolved on the server and sent to the web client via `server.getConfig`:
89
93 -- `terminal.toggle`: `mod+j`
94 -- `terminal.split`: `mod+d` when `terminalFocus`
95 -- `terminal.new`: `mod+shift+d` when `terminalFocus`
96 -- `chat.new`: `mod+shift+o`
97 -- `editor.openFavorite`: `mod+o`
90 +- Source of truth: [`DEFAULT_KEYBINDINGS` in `apps/server/src/keybindings.ts`](apps/server/src/keybindings.ts)
91
92 If you define any rules for a command, the default rule for that command is removed on the server.
93
apps/server/src/keybindings.ts
+1 -1
@@ -24,7 +24,7 @@ type WhenToken =
24 | { type: "lparen" }
25 | { type: "rparen" };
26
27 -const DEFAULT_KEYBINDINGS: KeybindingsConfig = [
27 +export const DEFAULT_KEYBINDINGS: ReadonlyArray<KeybindingRule> = [
28 { key: "mod+j", command: "terminal.toggle" },
29 { key: "mod+d", command: "terminal.split", when: "terminalFocus" },
30 { key: "mod+shift+d", command: "terminal.new", when: "terminalFocus" },
apps/server/src/wsServer.test.ts
+22 -42
@@ -17,7 +17,7 @@ import {
17 type WsResponse,
18 } from "@t3tools/contracts";
19 import { ProjectRegistry } from "./projectRegistry";
20 -import { compileResolvedKeybindingRule } from "./keybindings";
20 +import { compileResolvedKeybindingRule, DEFAULT_KEYBINDINGS } from "./keybindings";
21 import type {
22 TerminalClearInput,
23 TerminalCloseInput,
@@ -207,6 +207,20 @@ function compileKeybindings(bindings: KeybindingsConfig): ResolvedKeybindingsCon
207 return resolved;
208 }
209
210 +const DEFAULT_RESOLVED_KEYBINDINGS = compileKeybindings([...DEFAULT_KEYBINDINGS]);
211 +
212 +function mergeWithDefaultsForTest(custom: KeybindingsConfig): ResolvedKeybindingsConfig {
213 + if (custom.length === 0) {
214 + return DEFAULT_RESOLVED_KEYBINDINGS;
215 + }
216 +
217 + const overriddenCommands = new Set(custom.map((binding) => binding.command));
218 + const retainedDefaults = DEFAULT_KEYBINDINGS.filter(
219 + (binding) => !overriddenCommands.has(binding.command),
220 + );
221 + return compileKeybindings([...retainedDefaults, ...custom].slice(-256));
222 +}
223 +
224 describe("WebSocket Server", () => {
225 let server: ReturnType<typeof createServer> | null = null;
226 const connections: WebSocket[] = [];
@@ -324,13 +338,7 @@ describe("WebSocket Server", () => {
338 expect(response.error).toBeUndefined();
339 expect(response.result).toEqual({
340 cwd: "/my/workspace",
327 - keybindings: compileKeybindings([
328 - { key: "mod+j", command: "terminal.toggle" },
329 - { key: "mod+d", command: "terminal.split", when: "terminalFocus" },
330 - { key: "mod+shift+d", command: "terminal.new", when: "terminalFocus" },
331 - { key: "mod+shift+o", command: "chat.new" },
332 - { key: "mod+o", command: "editor.openFavorite" },
333 - ]),
341 + keybindings: DEFAULT_RESOLVED_KEYBINDINGS,
342 });
343 });
344
@@ -363,9 +371,7 @@ describe("WebSocket Server", () => {
371 expect(response.error).toBeUndefined();
372 expect(response.result).toEqual({
373 cwd: "/my/workspace",
366 - keybindings: compileKeybindings([
367 - { key: "mod+shift+o", command: "chat.new" },
368 - { key: "mod+o", command: "editor.openFavorite" },
374 + keybindings: mergeWithDefaultsForTest([
375 { key: "cmd+j", command: "terminal.toggle" },
376 { key: "mod+d", command: "terminal.split", when: "terminalFocus" },
377 { key: "mod+shift+d", command: "terminal.new", when: "terminalFocus" },
@@ -402,11 +408,7 @@ describe("WebSocket Server", () => {
408 expect(response.error).toBeUndefined();
409 expect(response.result).toEqual({
410 cwd: "/my/workspace",
405 - keybindings: compileKeybindings([
406 - { key: "mod+d", command: "terminal.split", when: "terminalFocus" },
407 - { key: "mod+shift+d", command: "terminal.new", when: "terminalFocus" },
408 - { key: "mod+shift+o", command: "chat.new" },
409 - { key: "mod+o", command: "editor.openFavorite" },
411 + keybindings: mergeWithDefaultsForTest([
412 { key: "mod+j", command: "terminal.toggle" },
413 ]),
414 });
@@ -443,13 +445,7 @@ describe("WebSocket Server", () => {
445 expect(response.error).toBeUndefined();
446 expect(response.result).toEqual({
447 cwd: "/my/workspace",
446 - keybindings: compileKeybindings([
447 - { key: "mod+j", command: "terminal.toggle" },
448 - { key: "mod+d", command: "terminal.split", when: "terminalFocus" },
449 - { key: "mod+shift+d", command: "terminal.new", when: "terminalFocus" },
450 - { key: "mod+shift+o", command: "chat.new" },
451 - { key: "mod+o", command: "editor.openFavorite" },
452 - ]),
448 + keybindings: DEFAULT_RESOLVED_KEYBINDINGS,
449 });
450 expect(
451 warnSpy.mock.calls.some(([message]) =>
@@ -483,11 +479,7 @@ describe("WebSocket Server", () => {
479 expect(firstResponse.error).toBeUndefined();
480 expect(firstResponse.result).toEqual({
481 cwd: "/my/workspace",
486 - keybindings: compileKeybindings([
487 - { key: "mod+d", command: "terminal.split", when: "terminalFocus" },
488 - { key: "mod+shift+d", command: "terminal.new", when: "terminalFocus" },
489 - { key: "mod+shift+o", command: "chat.new" },
490 - { key: "mod+o", command: "editor.openFavorite" },
482 + keybindings: mergeWithDefaultsForTest([
483 { key: "cmd+j", command: "terminal.toggle" },
484 ]),
485 });
@@ -529,13 +521,7 @@ describe("WebSocket Server", () => {
521 expect(response.error).toBeUndefined();
522 expect(response.result).toEqual({
523 cwd: "/my/workspace",
532 - keybindings: compileKeybindings([
533 - { key: "mod+j", command: "terminal.toggle" },
534 - { key: "mod+d", command: "terminal.split", when: "terminalFocus" },
535 - { key: "mod+shift+d", command: "terminal.new", when: "terminalFocus" },
536 - { key: "mod+shift+o", command: "chat.new" },
537 - { key: "mod+o", command: "editor.openFavorite" },
538 - ]),
524 + keybindings: DEFAULT_RESOLVED_KEYBINDINGS,
525 });
526 expect(
527 warnSpy.mock.calls.some(([message]) =>
@@ -566,13 +552,7 @@ describe("WebSocket Server", () => {
552 expect(response.error).toBeUndefined();
553 expect(response.result).toEqual({
554 cwd: "/my/workspace",
569 - keybindings: compileKeybindings([
570 - { key: "mod+j", command: "terminal.toggle" },
571 - { key: "mod+d", command: "terminal.split", when: "terminalFocus" },
572 - { key: "mod+shift+d", command: "terminal.new", when: "terminalFocus" },
573 - { key: "mod+shift+o", command: "chat.new" },
574 - { key: "mod+o", command: "editor.openFavorite" },
575 - ]),
555 + keybindings: DEFAULT_RESOLVED_KEYBINDINGS,
556 });
557 expect(
558 warnSpy.mock.calls.some(([message]) =>
apps/web/src/keybindings.test.ts
+37 -106
@@ -29,75 +29,21 @@ function event(overrides: Partial<ShortcutEventLike> = {}): ShortcutEventLike {
29 };
30 }
31
32 -function parseShortcut(value: string): KeybindingShortcut | null {
33 - const rawTokens = value.toLowerCase().split("+").map((token) => token.trim());
34 - const tokens = [...rawTokens];
35 - let trailingEmptyCount = 0;
36 - while (tokens[tokens.length - 1] === "") {
37 - trailingEmptyCount += 1;
38 - tokens.pop();
39 - }
40 - if (trailingEmptyCount > 0) {
41 - tokens.push("+");
42 - }
43 - if (tokens.some((token) => token.length === 0)) {
44 - return null;
45 - }
46 - if (tokens.length === 0) return null;
47 -
48 - let key: string | null = null;
49 - let metaKey = false;
50 - let ctrlKey = false;
51 - let shiftKey = false;
52 - let altKey = false;
53 - let modKey = false;
54 -
55 - for (const token of tokens) {
56 - switch (token) {
57 - case "cmd":
58 - case "meta":
59 - metaKey = true;
60 - break;
61 - case "ctrl":
62 - case "control":
63 - ctrlKey = true;
64 - break;
65 - case "shift":
66 - shiftKey = true;
67 - break;
68 - case "alt":
69 - case "option":
70 - altKey = true;
71 - break;
72 - case "mod":
73 - modKey = true;
74 - break;
75 - default: {
76 - if (key !== null) return null;
77 - key = token;
78 - }
79 - }
80 - }
81 -
82 - if (key === null) return null;
32 +function modShortcut(
33 + key: string,
34 + overrides: Partial<Omit<KeybindingShortcut, "key">> = {},
35 +): KeybindingShortcut {
36 return {
37 key,
85 - metaKey,
86 - ctrlKey,
87 - shiftKey,
88 - altKey,
89 - modKey,
38 + metaKey: false,
39 + ctrlKey: false,
40 + shiftKey: false,
41 + altKey: false,
42 + modKey: true,
43 + ...overrides,
44 };
45 }
46
93 -function shortcut(value: string): KeybindingShortcut {
94 - const parsed = parseShortcut(value);
95 - if (!parsed) {
96 - throw new Error(`invalid shortcut in test fixture: ${value}`);
97 - }
98 - return parsed;
99 -}
100 -
47 function whenIdentifier(name: string): KeybindingWhenNode {
48 return { type: "identifier", name };
49 }
@@ -111,48 +57,33 @@ function whenAnd(left: KeybindingWhenNode, right: KeybindingWhenNode): Keybindin
57 }
58
59 interface TestBinding {
114 - key: string;
60 + shortcut: KeybindingShortcut;
61 command: KeybindingCommand;
62 whenAst?: KeybindingWhenNode;
63 }
64
65 function compile(bindings: TestBinding[]): ResolvedKeybindingsConfig {
120 - const resolved: ResolvedKeybindingsConfig = [];
121 - for (const binding of bindings) {
122 - const parsedShortcut = parseShortcut(binding.key);
123 - if (!parsedShortcut) {
124 - throw new Error(`invalid shortcut in test fixture: ${binding.key}`);
125 - }
126 - if (binding.whenAst) {
127 - resolved.push({
128 - command: binding.command,
129 - shortcut: parsedShortcut,
130 - whenAst: binding.whenAst,
131 - });
132 - continue;
133 - }
134 - resolved.push({
135 - command: binding.command,
136 - shortcut: parsedShortcut,
137 - });
138 - }
139 - return resolved;
66 + return bindings.map((binding) => ({
67 + command: binding.command,
68 + shortcut: binding.shortcut,
69 + ...(binding.whenAst ? { whenAst: binding.whenAst } : {}),
70 + }));
71 }
72
73 const DEFAULT_BINDINGS = compile([
143 - { key: "mod+j", command: "terminal.toggle" },
74 + { shortcut: modShortcut("j"), command: "terminal.toggle" },
75 {
145 - key: "mod+d",
76 + shortcut: modShortcut("d"),
77 command: "terminal.split",
78 whenAst: whenIdentifier("terminalFocus"),
79 },
80 {
150 - key: "mod+shift+d",
81 + shortcut: modShortcut("d", { shiftKey: true }),
82 command: "terminal.new",
83 whenAst: whenIdentifier("terminalFocus"),
84 },
154 - { key: "mod+shift+o", command: "chat.new" },
155 - { key: "mod+o", command: "editor.openFavorite" },
85 + { shortcut: modShortcut("o", { shiftKey: true }), command: "chat.new" },
86 + { shortcut: modShortcut("o"), command: "editor.openFavorite" },
87 ]);
88
89 describe("isTerminalToggleShortcut", () => {
@@ -203,16 +134,16 @@ describe("split/new terminal shortcuts", () => {
134 it("supports when expressions", () => {
135 const keybindings = compile([
136 {
206 - key: "mod+\\",
137 + shortcut: modShortcut("\\"),
138 command: "terminal.split",
139 whenAst: whenAnd(whenIdentifier("terminalOpen"), whenNot(whenIdentifier("terminalFocus"))),
140 },
141 {
211 - key: "mod+shift+n",
142 + shortcut: modShortcut("n", { shiftKey: true }),
143 command: "terminal.new",
144 whenAst: whenAnd(whenIdentifier("terminalOpen"), whenNot(whenIdentifier("terminalFocus"))),
145 },
215 - { key: "mod+j", command: "terminal.toggle" },
146 + { shortcut: modShortcut("j"), command: "terminal.toggle" },
147 ]);
148 assert.isTrue(
149 isTerminalSplitShortcut(event({ key: "\\", ctrlKey: true }), keybindings, {
@@ -236,8 +167,8 @@ describe("split/new terminal shortcuts", () => {
167
168 it("supports when boolean literals", () => {
169 const keybindings = compile([
239 - { key: "mod+n", command: "terminal.new", whenAst: whenIdentifier("true") },
240 - { key: "mod+m", command: "terminal.new", whenAst: whenIdentifier("false") },
170 + { shortcut: modShortcut("n"), command: "terminal.new", whenAst: whenIdentifier("true") },
171 + { shortcut: modShortcut("m"), command: "terminal.new", whenAst: whenIdentifier("false") },
172 ]);
173
174 assert.isTrue(
@@ -257,12 +188,12 @@ describe("shortcutLabelForCommand", () => {
188 it("returns the most recent binding label", () => {
189 const bindings = compile([
190 {
260 - key: "mod+\\",
191 + shortcut: modShortcut("\\"),
192 command: "terminal.split",
193 whenAst: whenIdentifier("terminalFocus"),
194 },
195 {
265 - key: "mod+shift+\\",
196 + shortcut: modShortcut("\\", { shiftKey: true }),
197 command: "terminal.split",
198 whenAst: whenNot(whenIdentifier("terminalFocus")),
199 },
@@ -310,9 +241,9 @@ describe("chat/editor shortcuts", () => {
241 describe("cross-command precedence", () => {
242 it("uses when + order so a later focused rule overrides a global rule", () => {
243 const keybindings = compile([
313 - { key: "mod+n", command: "chat.new" },
244 + { shortcut: modShortcut("n"), command: "chat.new" },
245 {
315 - key: "mod+n",
246 + shortcut: modShortcut("n"),
247 command: "terminal.new",
248 whenAst: whenIdentifier("terminalFocus"),
249 },
@@ -347,11 +278,11 @@ describe("cross-command precedence", () => {
278 it("still lets a later global rule win when both rules match", () => {
279 const keybindings = compile([
280 {
350 - key: "mod+n",
281 + shortcut: modShortcut("n"),
282 command: "terminal.new",
283 whenAst: whenIdentifier("terminalFocus"),
284 },
354 - { key: "mod+n", command: "chat.new" },
285 + { shortcut: modShortcut("n"), command: "chat.new" },
286 ]);
287
288 assert.isFalse(
@@ -371,16 +302,16 @@ describe("cross-command precedence", () => {
302
303 describe("formatShortcutLabel", () => {
304 it("formats labels for macOS", () => {
374 - assert.strictEqual(formatShortcutLabel(shortcut("mod+shift+d"), "MacIntel"), "⇧⌘D");
305 + assert.strictEqual(formatShortcutLabel(modShortcut("d", { shiftKey: true }), "MacIntel"), "⇧⌘D");
306 });
307
308 it("formats labels for non-macOS", () => {
378 - assert.strictEqual(formatShortcutLabel(shortcut("mod+shift+d"), "Linux"), "Ctrl+Shift+D");
309 + assert.strictEqual(formatShortcutLabel(modShortcut("d", { shiftKey: true }), "Linux"), "Ctrl+Shift+D");
310 });
311
312 it("formats labels for plus key", () => {
382 - assert.strictEqual(formatShortcutLabel(shortcut("mod++"), "MacIntel"), "⌘+");
383 - assert.strictEqual(formatShortcutLabel(shortcut("mod++"), "Linux"), "Ctrl++");
313 + assert.strictEqual(formatShortcutLabel(modShortcut("+"), "MacIntel"), "⌘+");
314 + assert.strictEqual(formatShortcutLabel(modShortcut("+"), "Linux"), "Ctrl++");
315 });
316 });
317
@@ -397,7 +328,7 @@ describe("isTerminalClearShortcut", () => {
328
329 describe("plus key parsing", () => {
330 it("matches the plus key shortcut", () => {
400 - const plusBindings = compile([{ key: "mod++", command: "terminal.toggle" }]);
331 + const plusBindings = compile([{ shortcut: modShortcut("+"), command: "terminal.toggle" }]);
332 assert.isTrue(
333 isTerminalToggleShortcut(event({ key: "+", metaKey: true }), plusBindings, {
334 platform: "MacIntel",
packages/contracts/src/keybindings.test.ts
+17 -1
@@ -61,7 +61,6 @@ describe("keybindings contracts", () => {
61 it("parses resolved keybindings arrays", () => {
62 const parsed = resolvedKeybindingsConfigSchema.parse([
63 {
64 - key: "mod+j",
64 command: "terminal.toggle",
65 shortcut: {
66 key: "j",
@@ -75,4 +74,21 @@ describe("keybindings contracts", () => {
74 ]);
75 assert.lengthOf(parsed, 1);
76 });
77 +
78 + it("rejects unknown fields in resolved keybinding rules", () => {
79 + assert.throws(() =>
80 + resolvedKeybindingRuleSchema.parse({
81 + command: "terminal.toggle",
82 + shortcut: {
83 + key: "j",
84 + metaKey: false,
85 + ctrlKey: false,
86 + shiftKey: false,
87 + altKey: false,
88 + modKey: true,
89 + },
90 + key: "mod+j",
91 + }),
92 + );
93 + });
94 });
packages/contracts/src/keybindings.ts
+2 -2
@@ -26,7 +26,7 @@ export const keybindingShortcutSchema = z.object({
26 shiftKey: z.boolean(),
27 altKey: z.boolean(),
28 modKey: z.boolean(),
29 -});
29 +}).strict();
30
31 export type KeybindingWhenNode =
32 | { type: "identifier"; name: string }
@@ -61,7 +61,7 @@ export const resolvedKeybindingRuleSchema = z.object({
61 command: keybindingCommandSchema,
62 shortcut: keybindingShortcutSchema,
63 whenAst: keybindingWhenNodeSchema.optional(),
64 -});
64 +}).strict();
65
66 export const resolvedKeybindingsConfigSchema = z.array(resolvedKeybindingRuleSchema).max(256);
67