- drop `key` and `when` from resolved keybinding rule schema/payload - update server/web/contracts tests and keybindings docs for `whenAst`-only resolution - delete unused web `LruCache` implementation and tests
413 lines
11 KiB
TypeScript
413 lines
11 KiB
TypeScript
import { assert, describe, it } from "vitest";
|
|
|
|
import {
|
|
type KeybindingCommand,
|
|
type KeybindingShortcut,
|
|
type KeybindingWhenNode,
|
|
type ResolvedKeybindingsConfig,
|
|
} from "@t3tools/contracts";
|
|
import {
|
|
formatShortcutLabel,
|
|
isChatNewShortcut,
|
|
isOpenFavoriteEditorShortcut,
|
|
isTerminalClearShortcut,
|
|
isTerminalNewShortcut,
|
|
isTerminalSplitShortcut,
|
|
isTerminalToggleShortcut,
|
|
shortcutLabelForCommand,
|
|
type ShortcutEventLike,
|
|
} from "./keybindings";
|
|
|
|
function event(overrides: Partial<ShortcutEventLike> = {}): ShortcutEventLike {
|
|
return {
|
|
key: "j",
|
|
metaKey: false,
|
|
ctrlKey: false,
|
|
shiftKey: false,
|
|
altKey: false,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function parseShortcut(value: string): KeybindingShortcut | null {
|
|
const rawTokens = value.toLowerCase().split("+").map((token) => token.trim());
|
|
const tokens = [...rawTokens];
|
|
let trailingEmptyCount = 0;
|
|
while (tokens[tokens.length - 1] === "") {
|
|
trailingEmptyCount += 1;
|
|
tokens.pop();
|
|
}
|
|
if (trailingEmptyCount > 0) {
|
|
tokens.push("+");
|
|
}
|
|
if (tokens.some((token) => token.length === 0)) {
|
|
return null;
|
|
}
|
|
if (tokens.length === 0) return null;
|
|
|
|
let key: string | null = null;
|
|
let metaKey = false;
|
|
let ctrlKey = false;
|
|
let shiftKey = false;
|
|
let altKey = false;
|
|
let modKey = false;
|
|
|
|
for (const token of tokens) {
|
|
switch (token) {
|
|
case "cmd":
|
|
case "meta":
|
|
metaKey = true;
|
|
break;
|
|
case "ctrl":
|
|
case "control":
|
|
ctrlKey = true;
|
|
break;
|
|
case "shift":
|
|
shiftKey = true;
|
|
break;
|
|
case "alt":
|
|
case "option":
|
|
altKey = true;
|
|
break;
|
|
case "mod":
|
|
modKey = true;
|
|
break;
|
|
default: {
|
|
if (key !== null) return null;
|
|
key = token;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (key === null) return null;
|
|
return {
|
|
key,
|
|
metaKey,
|
|
ctrlKey,
|
|
shiftKey,
|
|
altKey,
|
|
modKey,
|
|
};
|
|
}
|
|
|
|
function shortcut(value: string): KeybindingShortcut {
|
|
const parsed = parseShortcut(value);
|
|
if (!parsed) {
|
|
throw new Error(`invalid shortcut in test fixture: ${value}`);
|
|
}
|
|
return parsed;
|
|
}
|
|
|
|
function whenIdentifier(name: string): KeybindingWhenNode {
|
|
return { type: "identifier", name };
|
|
}
|
|
|
|
function whenNot(node: KeybindingWhenNode): KeybindingWhenNode {
|
|
return { type: "not", node };
|
|
}
|
|
|
|
function whenAnd(left: KeybindingWhenNode, right: KeybindingWhenNode): KeybindingWhenNode {
|
|
return { type: "and", left, right };
|
|
}
|
|
|
|
interface TestBinding {
|
|
key: string;
|
|
command: KeybindingCommand;
|
|
whenAst?: KeybindingWhenNode;
|
|
}
|
|
|
|
function compile(bindings: TestBinding[]): ResolvedKeybindingsConfig {
|
|
const resolved: ResolvedKeybindingsConfig = [];
|
|
for (const binding of bindings) {
|
|
const parsedShortcut = parseShortcut(binding.key);
|
|
if (!parsedShortcut) {
|
|
throw new Error(`invalid shortcut in test fixture: ${binding.key}`);
|
|
}
|
|
if (binding.whenAst) {
|
|
resolved.push({
|
|
command: binding.command,
|
|
shortcut: parsedShortcut,
|
|
whenAst: binding.whenAst,
|
|
});
|
|
continue;
|
|
}
|
|
resolved.push({
|
|
command: binding.command,
|
|
shortcut: parsedShortcut,
|
|
});
|
|
}
|
|
return resolved;
|
|
}
|
|
|
|
const DEFAULT_BINDINGS = compile([
|
|
{ key: "mod+j", command: "terminal.toggle" },
|
|
{
|
|
key: "mod+d",
|
|
command: "terminal.split",
|
|
whenAst: whenIdentifier("terminalFocus"),
|
|
},
|
|
{
|
|
key: "mod+shift+d",
|
|
command: "terminal.new",
|
|
whenAst: whenIdentifier("terminalFocus"),
|
|
},
|
|
{ key: "mod+shift+o", command: "chat.new" },
|
|
{ key: "mod+o", command: "editor.openFavorite" },
|
|
]);
|
|
|
|
describe("isTerminalToggleShortcut", () => {
|
|
it("matches Cmd+J on macOS", () => {
|
|
assert.isTrue(
|
|
isTerminalToggleShortcut(event({ metaKey: true }), DEFAULT_BINDINGS, { platform: "MacIntel" }),
|
|
);
|
|
});
|
|
|
|
it("matches Ctrl+J on non-macOS", () => {
|
|
assert.isTrue(
|
|
isTerminalToggleShortcut(event({ ctrlKey: true }), DEFAULT_BINDINGS, { platform: "Win32" }),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("split/new terminal shortcuts", () => {
|
|
it("requires terminalFocus for default split/new bindings", () => {
|
|
assert.isFalse(
|
|
isTerminalSplitShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
|
|
platform: "MacIntel",
|
|
context: { terminalFocus: false },
|
|
}),
|
|
);
|
|
assert.isFalse(
|
|
isTerminalNewShortcut(event({ key: "d", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
|
|
platform: "Linux",
|
|
context: { terminalFocus: false },
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("matches split/new when terminalFocus is true", () => {
|
|
assert.isTrue(
|
|
isTerminalSplitShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
|
|
platform: "MacIntel",
|
|
context: { terminalFocus: true },
|
|
}),
|
|
);
|
|
assert.isTrue(
|
|
isTerminalNewShortcut(event({ key: "d", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
|
|
platform: "Linux",
|
|
context: { terminalFocus: true },
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("supports when expressions", () => {
|
|
const keybindings = compile([
|
|
{
|
|
key: "mod+\\",
|
|
command: "terminal.split",
|
|
whenAst: whenAnd(whenIdentifier("terminalOpen"), whenNot(whenIdentifier("terminalFocus"))),
|
|
},
|
|
{
|
|
key: "mod+shift+n",
|
|
command: "terminal.new",
|
|
whenAst: whenAnd(whenIdentifier("terminalOpen"), whenNot(whenIdentifier("terminalFocus"))),
|
|
},
|
|
{ key: "mod+j", command: "terminal.toggle" },
|
|
]);
|
|
assert.isTrue(
|
|
isTerminalSplitShortcut(event({ key: "\\", ctrlKey: true }), keybindings, {
|
|
platform: "Win32",
|
|
context: { terminalOpen: true, terminalFocus: false },
|
|
}),
|
|
);
|
|
assert.isFalse(
|
|
isTerminalSplitShortcut(event({ key: "\\", ctrlKey: true }), keybindings, {
|
|
platform: "Win32",
|
|
context: { terminalOpen: false, terminalFocus: false },
|
|
}),
|
|
);
|
|
assert.isTrue(
|
|
isTerminalNewShortcut(event({ key: "n", ctrlKey: true, shiftKey: true }), keybindings, {
|
|
platform: "Win32",
|
|
context: { terminalOpen: true, terminalFocus: false },
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("supports when boolean literals", () => {
|
|
const keybindings = compile([
|
|
{ key: "mod+n", command: "terminal.new", whenAst: whenIdentifier("true") },
|
|
{ key: "mod+m", command: "terminal.new", whenAst: whenIdentifier("false") },
|
|
]);
|
|
|
|
assert.isTrue(
|
|
isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
|
|
platform: "Linux",
|
|
}),
|
|
);
|
|
assert.isFalse(
|
|
isTerminalNewShortcut(event({ key: "m", ctrlKey: true }), keybindings, {
|
|
platform: "Linux",
|
|
}),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("shortcutLabelForCommand", () => {
|
|
it("returns the most recent binding label", () => {
|
|
const bindings = compile([
|
|
{
|
|
key: "mod+\\",
|
|
command: "terminal.split",
|
|
whenAst: whenIdentifier("terminalFocus"),
|
|
},
|
|
{
|
|
key: "mod+shift+\\",
|
|
command: "terminal.split",
|
|
whenAst: whenNot(whenIdentifier("terminalFocus")),
|
|
},
|
|
]);
|
|
assert.strictEqual(shortcutLabelForCommand(bindings, "terminal.split", "Linux"), "Ctrl+Shift+\\");
|
|
});
|
|
|
|
it("returns labels for non-terminal commands", () => {
|
|
assert.strictEqual(shortcutLabelForCommand(DEFAULT_BINDINGS, "chat.new", "MacIntel"), "⇧⌘O");
|
|
assert.strictEqual(
|
|
shortcutLabelForCommand(DEFAULT_BINDINGS, "editor.openFavorite", "Linux"),
|
|
"Ctrl+O",
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("chat/editor shortcuts", () => {
|
|
it("matches chat.new shortcut", () => {
|
|
assert.isTrue(
|
|
isChatNewShortcut(event({ key: "o", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
|
|
platform: "MacIntel",
|
|
}),
|
|
);
|
|
assert.isTrue(
|
|
isChatNewShortcut(event({ key: "o", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
|
|
platform: "Linux",
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("matches editor.openFavorite shortcut", () => {
|
|
assert.isTrue(
|
|
isOpenFavoriteEditorShortcut(event({ key: "o", metaKey: true }), DEFAULT_BINDINGS, {
|
|
platform: "MacIntel",
|
|
}),
|
|
);
|
|
assert.isTrue(
|
|
isOpenFavoriteEditorShortcut(event({ key: "o", ctrlKey: true }), DEFAULT_BINDINGS, {
|
|
platform: "Linux",
|
|
}),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("cross-command precedence", () => {
|
|
it("uses when + order so a later focused rule overrides a global rule", () => {
|
|
const keybindings = compile([
|
|
{ key: "mod+n", command: "chat.new" },
|
|
{
|
|
key: "mod+n",
|
|
command: "terminal.new",
|
|
whenAst: whenIdentifier("terminalFocus"),
|
|
},
|
|
]);
|
|
|
|
assert.isTrue(
|
|
isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
|
|
platform: "MacIntel",
|
|
context: { terminalFocus: true },
|
|
}),
|
|
);
|
|
assert.isFalse(
|
|
isChatNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
|
|
platform: "MacIntel",
|
|
context: { terminalFocus: true },
|
|
}),
|
|
);
|
|
assert.isFalse(
|
|
isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
|
|
platform: "MacIntel",
|
|
context: { terminalFocus: false },
|
|
}),
|
|
);
|
|
assert.isTrue(
|
|
isChatNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
|
|
platform: "MacIntel",
|
|
context: { terminalFocus: false },
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("still lets a later global rule win when both rules match", () => {
|
|
const keybindings = compile([
|
|
{
|
|
key: "mod+n",
|
|
command: "terminal.new",
|
|
whenAst: whenIdentifier("terminalFocus"),
|
|
},
|
|
{ key: "mod+n", command: "chat.new" },
|
|
]);
|
|
|
|
assert.isFalse(
|
|
isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
|
|
platform: "Linux",
|
|
context: { terminalFocus: true },
|
|
}),
|
|
);
|
|
assert.isTrue(
|
|
isChatNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
|
|
platform: "Linux",
|
|
context: { terminalFocus: true },
|
|
}),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("formatShortcutLabel", () => {
|
|
it("formats labels for macOS", () => {
|
|
assert.strictEqual(formatShortcutLabel(shortcut("mod+shift+d"), "MacIntel"), "⇧⌘D");
|
|
});
|
|
|
|
it("formats labels for non-macOS", () => {
|
|
assert.strictEqual(formatShortcutLabel(shortcut("mod+shift+d"), "Linux"), "Ctrl+Shift+D");
|
|
});
|
|
|
|
it("formats labels for plus key", () => {
|
|
assert.strictEqual(formatShortcutLabel(shortcut("mod++"), "MacIntel"), "⌘+");
|
|
assert.strictEqual(formatShortcutLabel(shortcut("mod++"), "Linux"), "Ctrl++");
|
|
});
|
|
});
|
|
|
|
describe("isTerminalClearShortcut", () => {
|
|
it("matches Ctrl+L on all platforms", () => {
|
|
assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "Linux"));
|
|
assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "MacIntel"));
|
|
});
|
|
|
|
it("matches Cmd+K on macOS", () => {
|
|
assert.isTrue(isTerminalClearShortcut(event({ key: "k", metaKey: true }), "MacIntel"));
|
|
});
|
|
});
|
|
|
|
describe("plus key parsing", () => {
|
|
it("matches the plus key shortcut", () => {
|
|
const plusBindings = compile([{ key: "mod++", command: "terminal.toggle" }]);
|
|
assert.isTrue(
|
|
isTerminalToggleShortcut(event({ key: "+", metaKey: true }), plusBindings, {
|
|
platform: "MacIntel",
|
|
}),
|
|
);
|
|
assert.isTrue(
|
|
isTerminalToggleShortcut(event({ key: "+", ctrlKey: true }), plusBindings, {
|
|
platform: "Linux",
|
|
}),
|
|
);
|
|
});
|
|
});
|