732 lines · 21.9 KB · TypeScript
RawDownload
import { assert, describe, it } from "vitest"; import {  type KeybindingCommand,  type KeybindingShortcut,  type KeybindingWhenNode,  type ResolvedKeybindingsConfig,} from "@t3tools/contracts";import {  formatShortcutLabel,  isChatNewShortcut,  isChatNewLocalShortcut,  isDiffToggleShortcut,  modelPickerJumpCommandForIndex,  modelPickerJumpIndexFromCommand,  isOpenFavoriteEditorShortcut,  isTerminalClearShortcut,  isTerminalCloseShortcut,  isTerminalNewShortcut,  isTerminalSplitShortcut,  isTerminalToggleShortcut,  resolveShortcutCommand,  shouldShowModelPickerJumpHints,  shouldShowThreadJumpHints,  shortcutLabelForCommand,  terminalDeleteShortcutData,  terminalNavigationShortcutData,  threadJumpCommandForIndex,  threadJumpIndexFromCommand,  threadTraversalDirectionFromCommand,  type ShortcutEventLike,} from "./keybindings"; function event(overrides: Partial<ShortcutEventLike> = {}): ShortcutEventLike {  return {    key: "j",    metaKey: false,    ctrlKey: false,    shiftKey: false,    altKey: false,    ...overrides,  };} function modShortcut(  key: string,  overrides: Partial<Omit<KeybindingShortcut, "key">> = {},): KeybindingShortcut {  return {    key,    metaKey: false,    ctrlKey: false,    shiftKey: false,    altKey: false,    modKey: true,    ...overrides,  };} 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 {  shortcut: KeybindingShortcut;  command: KeybindingCommand;  whenAst?: KeybindingWhenNode;} function compile(bindings: TestBinding[]): ResolvedKeybindingsConfig {  return bindings.map((binding) => ({    command: binding.command,    shortcut: binding.shortcut,    ...(binding.whenAst ? { whenAst: binding.whenAst } : {}),  }));} const DEFAULT_BINDINGS = compile([  { shortcut: modShortcut("j"), command: "terminal.toggle" },  {    shortcut: modShortcut("d"),    command: "terminal.split",    whenAst: whenIdentifier("terminalFocus"),  },  {    shortcut: modShortcut("d", { shiftKey: true }),    command: "terminal.new",    whenAst: whenIdentifier("terminalFocus"),  },  {    shortcut: modShortcut("w"),    command: "terminal.close",    whenAst: whenIdentifier("terminalFocus"),  },  {    shortcut: modShortcut("d"),    command: "diff.toggle",    whenAst: whenNot(whenIdentifier("terminalFocus")),  },  {    shortcut: modShortcut("k"),    command: "commandPalette.toggle",    whenAst: whenNot(whenIdentifier("terminalFocus")),  },  {    shortcut: modShortcut("m", { shiftKey: true }),    command: "modelPicker.toggle",    whenAst: whenNot(whenIdentifier("terminalFocus")),  },  { shortcut: modShortcut("o", { shiftKey: true }), command: "chat.new" },  { shortcut: modShortcut("n", { shiftKey: true }), command: "chat.newLocal" },  { shortcut: modShortcut("o"), command: "editor.openFavorite" },  { shortcut: modShortcut("[", { shiftKey: true }), command: "thread.previous" },  { shortcut: modShortcut("]", { shiftKey: true }), command: "thread.next" },  { shortcut: modShortcut("1"), command: "thread.jump.1" },  { shortcut: modShortcut("2"), command: "thread.jump.2" },  { shortcut: modShortcut("3"), command: "thread.jump.3" },  {    shortcut: modShortcut("1"),    command: "modelPicker.jump.1",    whenAst: whenIdentifier("modelPickerOpen"),  },  {    shortcut: modShortcut("2"),    command: "modelPicker.jump.2",    whenAst: whenIdentifier("modelPickerOpen"),  },  {    shortcut: modShortcut("3"),    command: "modelPicker.jump.3",    whenAst: whenIdentifier("modelPickerOpen"),  },]); 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" }),    );  });   it("matches Ctrl+J on non-macOS while terminalFocus is true", () => {    assert.isTrue(      isTerminalToggleShortcut(event({ ctrlKey: true }), DEFAULT_BINDINGS, {        platform: "Win32",        context: { terminalFocus: true },      }),    );  });}); describe("split/new/close terminal shortcuts", () => {  it("requires terminalFocus for default split/new/close 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 },      }),    );    assert.isFalse(      isTerminalCloseShortcut(event({ key: "w", ctrlKey: 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 },      }),    );    assert.isTrue(      isTerminalCloseShortcut(event({ key: "w", ctrlKey: true }), DEFAULT_BINDINGS, {        platform: "Linux",        context: { terminalFocus: true },      }),    );  });   it("supports when expressions", () => {    const keybindings = compile([      {        shortcut: modShortcut("\\"),        command: "terminal.split",        whenAst: whenAnd(whenIdentifier("terminalOpen"), whenNot(whenIdentifier("terminalFocus"))),      },      {        shortcut: modShortcut("n", { shiftKey: true }),        command: "terminal.new",        whenAst: whenAnd(whenIdentifier("terminalOpen"), whenNot(whenIdentifier("terminalFocus"))),      },      { shortcut: modShortcut("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([      { shortcut: modShortcut("n"), command: "terminal.new", whenAst: whenIdentifier("true") },      { shortcut: modShortcut("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 effective binding label", () => {    const bindings = compile([      {        shortcut: modShortcut("\\"),        command: "terminal.split",        whenAst: whenIdentifier("terminalFocus"),      },      {        shortcut: modShortcut("\\", { shiftKey: true }),        command: "terminal.split",        whenAst: whenNot(whenIdentifier("terminalFocus")),      },    ]);    assert.strictEqual(      shortcutLabelForCommand(bindings, "terminal.split", {        platform: "Linux",        context: { terminalFocus: false },      }),      "Ctrl+Shift+\\",    );  });   it("returns effective labels for non-terminal commands", () => {    assert.strictEqual(shortcutLabelForCommand(DEFAULT_BINDINGS, "chat.new", "MacIntel"), "⇧⌘O");    assert.strictEqual(shortcutLabelForCommand(DEFAULT_BINDINGS, "diff.toggle", "Linux"), "Ctrl+D");    assert.strictEqual(      shortcutLabelForCommand(DEFAULT_BINDINGS, "commandPalette.toggle", "MacIntel"),      "⌘K",    );    assert.strictEqual(      shortcutLabelForCommand(DEFAULT_BINDINGS, "modelPicker.toggle", "Linux"),      "Ctrl+Shift+M",    );    assert.strictEqual(      shortcutLabelForCommand(DEFAULT_BINDINGS, "editor.openFavorite", "Linux"),      "Ctrl+O",    );    assert.strictEqual(      shortcutLabelForCommand(DEFAULT_BINDINGS, "thread.jump.3", "MacIntel"),      "⌘3",    );    assert.strictEqual(      shortcutLabelForCommand(DEFAULT_BINDINGS, "thread.previous", "Linux"),      "Ctrl+Shift+[",    );    assert.strictEqual(      shortcutLabelForCommand(DEFAULT_BINDINGS, "modelPicker.jump.3", {        platform: "MacIntel",        context: { modelPickerOpen: true },      }),      "⌘3",    );  });   it("returns null for commands shadowed by a later conflicting shortcut", () => {    const bindings = compile([      { shortcut: modShortcut("1", { shiftKey: true }), command: "thread.jump.1" },      { shortcut: modShortcut("1", { shiftKey: true }), command: "thread.jump.7" },    ]);     assert.isNull(shortcutLabelForCommand(bindings, "thread.jump.1", "MacIntel"));    assert.strictEqual(shortcutLabelForCommand(bindings, "thread.jump.7", "MacIntel"), "⇧⌘1");  });   it("respects when-context while resolving labels", () => {    const bindings = compile([      { shortcut: modShortcut("d"), command: "diff.toggle" },      {        shortcut: modShortcut("d"),        command: "terminal.split",        whenAst: whenIdentifier("terminalFocus"),      },    ]);     assert.strictEqual(      shortcutLabelForCommand(bindings, "diff.toggle", {        platform: "Linux",        context: { terminalFocus: false },      }),      "Ctrl+D",    );    assert.isNull(      shortcutLabelForCommand(bindings, "diff.toggle", {        platform: "Linux",        context: { terminalFocus: true },      }),    );    assert.strictEqual(      shortcutLabelForCommand(bindings, "terminal.split", {        platform: "Linux",        context: { terminalFocus: true },      }),      "Ctrl+D",    );  });}); describe("thread navigation helpers", () => {  it("maps jump commands to visible thread indices", () => {    assert.strictEqual(threadJumpCommandForIndex(0), "thread.jump.1");    assert.strictEqual(threadJumpCommandForIndex(2), "thread.jump.3");    assert.isNull(threadJumpCommandForIndex(9));    assert.strictEqual(threadJumpIndexFromCommand("thread.jump.1"), 0);    assert.strictEqual(threadJumpIndexFromCommand("thread.jump.3"), 2);    assert.isNull(threadJumpIndexFromCommand("thread.next"));  });   it("maps traversal commands to directions", () => {    assert.strictEqual(threadTraversalDirectionFromCommand("thread.previous"), "previous");    assert.strictEqual(threadTraversalDirectionFromCommand("thread.next"), "next");    assert.isNull(threadTraversalDirectionFromCommand("thread.jump.1"));    assert.isNull(threadTraversalDirectionFromCommand(null));  });   it("shows jump hints only when configured modifiers match", () => {    assert.isTrue(      shouldShowThreadJumpHints(event({ metaKey: true }), DEFAULT_BINDINGS, {        platform: "MacIntel",      }),    );    assert.isFalse(      shouldShowThreadJumpHints(event({ metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {        platform: "MacIntel",      }),    );    assert.isTrue(      shouldShowThreadJumpHints(event({ ctrlKey: true }), DEFAULT_BINDINGS, {        platform: "Linux",      }),    );  });}); describe("model picker navigation helpers", () => {  it("maps jump commands to visible model indices", () => {    assert.strictEqual(modelPickerJumpCommandForIndex(0), "modelPicker.jump.1");    assert.strictEqual(modelPickerJumpCommandForIndex(2), "modelPicker.jump.3");    assert.isNull(modelPickerJumpCommandForIndex(9));    assert.strictEqual(modelPickerJumpIndexFromCommand("modelPicker.jump.1"), 0);    assert.strictEqual(modelPickerJumpIndexFromCommand("modelPicker.jump.3"), 2);    assert.isNull(modelPickerJumpIndexFromCommand("thread.jump.1"));  });   it("shows jump hints only while the model picker context is active", () => {    assert.isFalse(      shouldShowModelPickerJumpHints(event({ metaKey: true }), DEFAULT_BINDINGS, {        platform: "MacIntel",        context: { modelPickerOpen: false },      }),    );    assert.isTrue(      shouldShowModelPickerJumpHints(event({ metaKey: true }), DEFAULT_BINDINGS, {        platform: "MacIntel",        context: { modelPickerOpen: true },      }),    );  });}); 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 chat.newLocal shortcut", () => {    assert.isTrue(      isChatNewLocalShortcut(event({ key: "n", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {        platform: "MacIntel",      }),    );    assert.isTrue(      isChatNewLocalShortcut(event({ key: "n", 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",      }),    );  });   it("matches commandPalette.toggle shortcut outside terminal focus", () => {    assert.strictEqual(      resolveShortcutCommand(event({ key: "k", metaKey: true }), DEFAULT_BINDINGS, {        platform: "MacIntel",        context: { terminalFocus: false },      }),      "commandPalette.toggle",    );    assert.notStrictEqual(      resolveShortcutCommand(event({ key: "k", metaKey: true }), DEFAULT_BINDINGS, {        platform: "MacIntel",        context: { terminalFocus: true },      }),      "commandPalette.toggle",    );  });   it("matches diff.toggle shortcut outside terminal focus", () => {    assert.isTrue(      isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {        platform: "MacIntel",        context: { terminalFocus: false },      }),    );    assert.isFalse(      isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {        platform: "MacIntel",        context: { terminalFocus: true },      }),    );  });}); describe("cross-command precedence", () => {  it("uses when + order so a later focused rule overrides a global rule", () => {    const keybindings = compile([      { shortcut: modShortcut("n"), command: "chat.new" },

Lines 1–500 of 732.