Files
orca/src/shared/keybindings.test.ts
T
NeilandOrca d855c69fe3 feat(shortcuts): make Select Tab/Workspace 1–9 remappable (#5789)
* feat(shortcuts): make Select Tab/Workspace 1-9 remappable (#5741)

Expose Select Tab 1-9 and Select Workspace 1-9 as single remappable rows in
Settings -> Shortcuts. Previously both chords were hardcoded in
window-shortcut-policy.ts and absent from the Shortcuts UI.

Each is modeled as one ranged action whose stored chord is a representative
(digit canonicalized to 1) that fires for any of 1-9, mirroring the cmux
single-row UX. Defaults reproduce existing behavior (mac: tab Ctrl+1-9,
workspace Cmd+1-9; win/linux: tab Alt+1-9, workspace Ctrl+1-9). Swapping the
two modifiers - the headline use case - works without a false conflict.

Co-authored-by: Orca <help@stably.ai>

* refactor(shortcuts): tighten digit-index review nits

- Canonicalize digit-index overrides in getEffectiveKeybindingsForAction so
  display/conflict detection stay consistent regardless of stored digit.
- Use a shared DIGIT_INDEX_KEY_PATTERN regex instead of string comparison.
- Document that extra modifiers (e.g. Shift) are allowed on digit-index chords
  and that the matcher honors terminal-context gating.
- Clarify the workspace-first precedence comment.
- Add tests: Shift modifier, physical-code fallback matching, both ranges disabled.

Co-authored-by: Orca <help@stably.ai>

---------

Co-authored-by: Orca <help@stably.ai>
2026-06-19 02:29:07 -07:00

1204 lines
39 KiB
TypeScript

/* eslint-disable max-lines -- Why: shared keybinding tests cover the central
* registry, parser, matcher, and conflict detector together so shortcut
* semantics cannot drift across app surfaces. */
import { describe, expect, it } from 'vitest'
import {
agentTabActionId,
getKeybindingDefinition,
findKeybindingConflicts,
formatKeybinding,
formatKeybindingList,
getEffectiveKeybindingsForAction,
isDigitIndexActionId,
isDoubleTapBinding,
keybindingFromInput,
keybindingFromInputForAction,
keybindingMatchesAction,
keybindingMatchesInput,
matchKeybindingDigitIndex,
normalizeKeybinding,
normalizeKeybindingArrayForAction,
normalizeKeybindingListForAction,
normalizeKeybindingList
} from './keybindings'
import { ALL_TUI_AGENTS } from './tui-agent-display-names'
describe('keybindings', () => {
it('normalizes editable shortcut input and rejects unsafe bindings', () => {
expect(normalizeKeybinding(' ctrl + shift + p ')).toEqual({
ok: true,
value: 'Ctrl+Shift+P'
})
expect(normalizeKeybinding('shift+insert')).toEqual({ ok: true, value: 'Shift+Insert' })
expect(normalizeKeybinding('cmdorctrl+p')).toEqual({ ok: true, value: 'Mod+P' })
expect(normalizeKeybindingList('Ctrl+Shift+P, ctrl+shift+p, ⌘+k')).toEqual([
'Ctrl+Shift+P',
'Cmd+K'
])
expect(normalizeKeybinding('Shift+P')).toMatchObject({ ok: false })
expect(normalizeKeybinding('Mod+Ctrl+P')).toMatchObject({ ok: false })
expect(normalizeKeybinding('Ctrl+Nope')).toMatchObject({ ok: false })
})
it('parses, normalizes, and rejects double-tap modifier bindings', () => {
expect(normalizeKeybinding('DoubleTap+Shift')).toEqual({ ok: true, value: 'DoubleTap+Shift' })
expect(normalizeKeybinding(' doubletap + shift ')).toEqual({
ok: true,
value: 'DoubleTap+Shift'
})
expect(normalizeKeybinding('DoubleTap+Mod')).toEqual({ ok: true, value: 'DoubleTap+Mod' })
expect(normalizeKeybinding('DoubleTap+Cmd')).toEqual({ ok: true, value: 'DoubleTap+Cmd' })
expect(normalizeKeybinding('DoubleTap+Alt')).toEqual({ ok: true, value: 'DoubleTap+Alt' })
expect(normalizeKeybinding('DoubleTap+Ctrl')).toEqual({ ok: true, value: 'DoubleTap+Ctrl' })
// A key after DoubleTap is invalid.
expect(normalizeKeybinding('DoubleTap+Shift+P')).toMatchObject({ ok: false })
// Two modifiers is invalid.
expect(normalizeKeybinding('DoubleTap+Shift+Alt')).toMatchObject({ ok: false })
// Mod + platform-specific reuses the shared error.
expect(normalizeKeybinding('DoubleTap+Mod+Cmd')).toEqual({
ok: false,
error: 'Use either Mod or a platform-specific modifier, not both.'
})
// Bare DoubleTap is invalid.
expect(normalizeKeybinding('DoubleTap')).toMatchObject({ ok: false })
expect(isDoubleTapBinding('DoubleTap+Shift')).toBe(true)
expect(isDoubleTapBinding('Mod+P')).toBe(false)
expect(isDoubleTapBinding('not-a-binding')).toBe(false)
})
it('allows safe bare keys only for scoped actions that opt in', () => {
expect(normalizeKeybinding('Delete')).toMatchObject({ ok: false })
expect(normalizeKeybindingListForAction('fileExplorer.delete', 'Delete')).toEqual(['Delete'])
expect(normalizeKeybindingListForAction('fileExplorer.delete', 'x')).toMatchObject({
ok: false
})
})
it('captures key events into canonical editable shortcuts', () => {
expect(
keybindingFromInput(
{ key: 'j', code: 'KeyJ', meta: true, control: false, alt: false, shift: false },
'darwin'
)
).toEqual({ ok: true, value: 'Mod+J' })
expect(
keybindingFromInput(
{ key: 'J', code: 'KeyJ', control: true, meta: false, alt: true, shift: true },
'linux'
)
).toEqual({ ok: true, value: 'Mod+Alt+Shift+J' })
expect(
keybindingFromInput({ key: 'Control', code: 'ControlLeft', control: true }, 'linux')
).toEqual({ ok: false, error: 'Press a key, not only a modifier.' })
})
it('captures macOS Option-composed key events via the physical code', () => {
expect(
keybindingFromInput(
{ key: 'ç', code: 'KeyC', meta: true, control: false, alt: true, shift: false },
'darwin'
)
).toEqual({ ok: true, value: 'Mod+Alt+C' })
expect(
keybindingFromInput(
{ key: '“', code: 'BracketLeft', meta: true, control: false, alt: true, shift: false },
'darwin'
)
).toEqual({ ok: true, value: 'Mod+Alt+BracketLeft' })
expect(
keybindingFromInput(
{ key: 'Alt', code: 'AltLeft', meta: false, control: false, alt: true, shift: false },
'darwin'
)
).toEqual({ ok: false, error: 'Press a key, not only a modifier.' })
expect(
keybindingFromInput(
{ key: '¡', code: 'Digit1', meta: true, control: false, alt: true, shift: false },
'darwin'
)
).toEqual({ ok: false, error: 'Press a key, not only a modifier.' })
})
it('applies per-action bare-key rules while capturing shortcuts', () => {
const deleteEvent = {
key: 'Delete',
code: 'Delete',
control: false,
meta: false,
alt: false,
shift: false
}
expect(keybindingFromInput(deleteEvent, 'linux')).toMatchObject({ ok: false })
expect(keybindingFromInputForAction('fileExplorer.delete', deleteEvent, 'linux')).toEqual({
ok: true,
value: 'Delete'
})
})
it('formats keybindings with platform labels', () => {
expect(formatKeybindingList(['Mod+Shift+J'], 'darwin')).toBe('⌘⇧J')
expect(formatKeybindingList(['Mod+Shift+J'], 'linux')).toBe('Ctrl+Shift+J')
expect(formatKeybindingList([], 'win32')).toBe('Unassigned')
})
it('preserves explicit numpad shortcut tokens', () => {
const numpadAdd = {
key: '+',
code: 'NumpadAdd',
control: false,
meta: true,
alt: false,
shift: false
}
expect(keybindingFromInput(numpadAdd, 'darwin')).toEqual({
ok: true,
value: 'Mod+NumpadAdd'
})
expect(keybindingMatchesAction('zoom.in', numpadAdd, 'darwin')).toBe(true)
expect(
keybindingMatchesAction(
'zoom.out',
{
...numpadAdd,
key: '-',
code: 'NumpadSubtract'
},
'darwin'
)
).toBe(true)
})
it('defines a default shortcut for opening markdown notes', () => {
expect(getEffectiveKeybindingsForAction('tab.openMarkdown', 'darwin')).toEqual(['Mod+Shift+O'])
expect(formatKeybindingList(['Mod+Shift+O'], 'darwin')).toBe('⌘⇧O')
})
it('uses overrides as the complete effective binding list for an action', () => {
const overrides = {
'worktree.quickOpen': ['Ctrl+Alt+O', 'not-a-shortcut']
}
expect(getEffectiveKeybindingsForAction('worktree.quickOpen', 'linux', overrides)).toEqual([
'Ctrl+Alt+O'
])
expect(
keybindingMatchesAction(
'worktree.quickOpen',
{ key: 'o', code: 'KeyO', control: true, meta: false, alt: true, shift: false },
'linux',
overrides
)
).toBe(true)
expect(
keybindingMatchesAction(
'worktree.quickOpen',
{ key: 'p', code: 'KeyP', control: true, meta: false, alt: false, shift: false },
'linux',
overrides
)
).toBe(false)
})
it('reports conflicts across default and customized actions', () => {
expect(findKeybindingConflicts('linux')).toEqual([])
const conflicts = findKeybindingConflicts('linux', { 'view.tasks': ['Mod+P'] })
expect(conflicts).toContainEqual({
binding: 'Mod+P',
actionIds: expect.arrayContaining(['worktree.quickOpen', 'view.tasks'])
})
})
it('defines macOS-only rename shortcuts that stay conflict-free', () => {
expect(getEffectiveKeybindingsForAction('tab.rename', 'darwin')).toEqual(['Mod+R'])
expect(getEffectiveKeybindingsForAction('tab.rename', 'linux')).toEqual([])
expect(getEffectiveKeybindingsForAction('tab.rename', 'win32')).toEqual([])
expect(getEffectiveKeybindingsForAction('workspace.rename', 'darwin')).toEqual(['Mod+Alt+R'])
expect(getEffectiveKeybindingsForAction('workspace.rename', 'linux')).toEqual([])
expect(formatKeybindingList(['Mod+Alt+R'], 'darwin')).toBe('⌘⌥R')
expect(
keybindingMatchesAction(
'tab.rename',
{
key: 'r',
code: 'KeyR',
meta: true,
control: false,
alt: false,
shift: false
},
'darwin'
)
).toBe(true)
expect(
keybindingMatchesAction(
'tab.rename',
{
key: 'r',
code: 'KeyR',
meta: false,
control: true,
alt: false,
shift: false
},
'linux'
)
).toBe(false)
// Why: tab.rename (Mod+R) intentionally shares its binding with
// browser.reload, but the two live in different scopes (tabs vs browser),
// so customizing tab.rename to its default must not flag a conflict.
expect(findKeybindingConflicts('darwin', { 'tab.rename': ['Mod+R'] })).toEqual([])
// Why: tab/workspace rename share the same active workspace keydown path,
// so Settings must reject user overrides that make one shadow the other.
expect(findKeybindingConflicts('darwin', { 'workspace.rename': ['Mod+R'] })).toEqual([
{
binding: 'Mod+R',
actionIds: ['workspace.rename', 'tab.rename']
}
])
expect(findKeybindingConflicts('darwin', { 'tab.rename': ['Mod+Alt+R'] })).toEqual([
{
binding: 'Mod+Alt+R',
actionIds: ['workspace.rename', 'tab.rename']
}
])
})
it('defines browser history shortcuts for Logitech side-button remaps', () => {
expect(getEffectiveKeybindingsForAction('browser.back', 'darwin')).toEqual(['Mod+BracketLeft'])
expect(getEffectiveKeybindingsForAction('browser.forward', 'darwin')).toEqual([
'Mod+BracketRight'
])
expect(getEffectiveKeybindingsForAction('browser.back', 'linux')).toEqual(['Alt+ArrowLeft'])
expect(getEffectiveKeybindingsForAction('browser.forward', 'win32')).toEqual(['Alt+ArrowRight'])
expect(
keybindingMatchesAction(
'browser.back',
{
key: '[',
code: 'BracketLeft',
meta: true,
control: false,
alt: false,
shift: false
},
'darwin'
)
).toBe(true)
expect(
keybindingMatchesAction(
'browser.forward',
{
key: 'ArrowRight',
code: 'ArrowRight',
meta: false,
control: false,
alt: true,
shift: false
},
'linux'
)
).toBe(true)
})
it('binds close-all editor tabs to Mod+Alt+W beside tab.close', () => {
expect(getEffectiveKeybindingsForAction('tab.closeAll', 'darwin')).toEqual(['Mod+Alt+W'])
expect(getEffectiveKeybindingsForAction('tab.closeAll', 'linux')).toEqual(['Mod+Alt+W'])
expect(getEffectiveKeybindingsForAction('tab.closeAll', 'win32')).toEqual(['Mod+Alt+W'])
expect(formatKeybindingList(['Mod+Alt+W'], 'darwin')).toBe('⌘⌥W')
expect(formatKeybindingList(['Mod+Alt+W'], 'linux')).toBe('Ctrl+Alt+W')
// Why: macOS Option+W composes to a glyph (∑), so the chord must resolve
// through the physical-code fallback rather than the logical key.
const macComposedCloseAll = {
key: '∑',
code: 'KeyW',
meta: true,
control: false,
alt: true,
shift: false
}
expect(keybindingMatchesAction('tab.closeAll', macComposedCloseAll, 'darwin')).toBe(true)
const linuxCloseAll = {
key: 'w',
code: 'KeyW',
meta: false,
control: true,
alt: true,
shift: false
}
expect(keybindingMatchesAction('tab.closeAll', linuxCloseAll, 'linux')).toBe(true)
expect(
keybindingMatchesAction('tab.closeAll', linuxCloseAll, 'linux', undefined, {
context: 'terminal',
terminalShortcutPolicy: 'orca-first'
})
).toBe(true)
// Why: close-all is a workspace tab command, so terminal-first mode should
// keep passing the chord through to shells and TUIs.
expect(
keybindingMatchesAction('tab.closeAll', linuxCloseAll, 'linux', undefined, {
context: 'terminal',
terminalShortcutPolicy: 'terminal-first'
})
).toBe(false)
// Why: Mod+Alt+W and Mod+W are neighbors; the extra Alt must keep the two
// actions from firing on each other's chord.
const macCloseActive = {
key: 'w',
code: 'KeyW',
meta: true,
control: false,
alt: false,
shift: false
}
expect(keybindingMatchesAction('tab.close', macComposedCloseAll, 'darwin')).toBe(false)
expect(keybindingMatchesAction('tab.closeAll', macCloseActive, 'darwin')).toBe(false)
// Stays in the Tabs group/scope so Settings → Shortcuts lists it for rebinding.
const definition = getKeybindingDefinition('tab.closeAll')
expect(definition?.group).toBe('Tabs')
expect(definition?.scope).toBe('tabs')
// Why: both live in the Tabs scope, so rebinding closeAll onto Mod+W must
// surface as a conflict with tab.close in Settings.
expect(findKeybindingConflicts('darwin', { 'tab.closeAll': ['Mod+W'] })).toContainEqual({
binding: 'Mod+W',
actionIds: expect.arrayContaining(['tab.close', 'tab.closeAll'])
})
})
it('keeps equalize pane sizes unassigned until users customize it', () => {
expect(getEffectiveKeybindingsForAction('terminal.equalizePaneSizes', 'darwin')).toEqual([])
expect(
keybindingMatchesAction(
'terminal.equalizePaneSizes',
{ key: '=', code: 'Equal', control: false, meta: true, alt: false, shift: false },
'darwin'
)
).toBe(false)
expect(
keybindingMatchesAction(
'terminal.equalizePaneSizes',
{ key: '=', code: 'Equal', control: false, meta: true, alt: false, shift: false },
'darwin',
{ 'terminal.equalizePaneSizes': ['Mod+Equal'] }
)
).toBe(true)
})
it('keeps workspace delete unassigned until users customize it', () => {
const binding = {
key: 'Backspace',
code: 'Backspace',
control: true,
meta: false,
alt: false,
shift: true
}
expect(getEffectiveKeybindingsForAction('workspace.delete', 'linux')).toEqual([])
expect(keybindingMatchesAction('workspace.delete', binding, 'linux')).toBe(false)
expect(
keybindingMatchesAction('workspace.delete', binding, 'linux', {
'workspace.delete': ['Mod+Shift+Backspace']
})
).toBe(true)
})
it('keeps workspace board unassigned until users customize it', () => {
const binding = {
key: 'k',
code: 'KeyK',
control: true,
meta: false,
alt: true,
shift: false
}
expect(getEffectiveKeybindingsForAction('workspace.openBoard', 'linux')).toEqual([])
expect(keybindingMatchesAction('workspace.openBoard', binding, 'linux')).toBe(false)
expect(
keybindingMatchesAction('workspace.openBoard', binding, 'linux', {
'workspace.openBoard': ['Mod+Alt+K']
})
).toBe(true)
const definition = getKeybindingDefinition('workspace.openBoard')
expect(definition?.title).toBe('Open Workspace Board')
expect(definition?.searchKeywords).toEqual(
expect.arrayContaining(['workspace', 'board', 'kanban'])
)
})
it('defines a macOS-only default for the new agent tab shortcut', () => {
expect(getEffectiveKeybindingsForAction('tab.newAgent', 'darwin')).toEqual(['Mod+Alt+T'])
expect(getEffectiveKeybindingsForAction('tab.newAgent', 'linux')).toEqual([])
expect(getEffectiveKeybindingsForAction('tab.newAgent', 'win32')).toEqual([])
expect(
keybindingMatchesAction(
'tab.newAgent',
{ key: 't', code: 'KeyT', meta: true, control: false, alt: true, shift: false },
'darwin'
)
).toBe(true)
})
it('defines an unassigned per-agent tab action for every TUI agent', () => {
for (const agent of ALL_TUI_AGENTS) {
const actionId = agentTabActionId(agent)
const definition = getKeybindingDefinition(actionId)
expect(definition, actionId).toBeDefined()
expect(definition?.group).toBe('Agents')
expect(definition?.scope).toBe('tabs')
expect(getEffectiveKeybindingsForAction(actionId, 'darwin')).toEqual([])
}
})
it('matches per-agent tab actions only through user overrides', () => {
const binding = { key: 'k', code: 'KeyK', meta: true, control: false, alt: true, shift: true }
expect(keybindingMatchesAction(agentTabActionId('claude'), binding, 'darwin')).toBe(false)
expect(
keybindingMatchesAction(agentTabActionId('claude'), binding, 'darwin', {
'tab.newAgent.claude': ['Mod+Alt+Shift+K']
})
).toBe(true)
})
it('ignores selected actions when checking shortcut conflicts', () => {
expect(
findKeybindingConflicts(
'darwin',
{
'tab.newAgent.claude': ['Mod+Alt+Shift+K'],
'tab.newAgent.codex': ['Mod+Alt+Shift+K']
},
{ ignoredActionIds: [agentTabActionId('claude')] }
)
).toEqual([])
})
it('reports customized renderer conflicts with native menu accelerators', () => {
expect(findKeybindingConflicts('darwin')).toEqual([])
const conflicts = findKeybindingConflicts('darwin', {
'worktree.palette': ['Mod+Shift+E']
})
expect(conflicts).toContainEqual({
binding: 'Mod+Shift+E',
actionIds: expect.arrayContaining(['sidebar.explorer.toggle', 'worktree.palette'])
})
})
it('keeps Orca-first terminal context backward compatible', () => {
const ctrlP = {
key: 'p',
code: 'KeyP',
control: true,
meta: false,
alt: false,
shift: false
}
expect(keybindingMatchesAction('worktree.quickOpen', ctrlP, 'linux')).toBe(true)
expect(
keybindingMatchesAction('worktree.quickOpen', ctrlP, 'linux', undefined, {
context: 'terminal',
terminalShortcutPolicy: 'orca-first'
})
).toBe(true)
expect(
keybindingMatchesAction('worktree.quickOpen', ctrlP, 'linux', undefined, {
context: 'terminal',
terminalShortcutPolicy: 'terminal-first'
})
).toBe(false)
expect(
keybindingMatchesAction(
'terminal.search',
{ key: 'f', code: 'KeyF', control: true, meta: false, alt: false, shift: false },
'linux',
undefined,
{ context: 'terminal', terminalShortcutPolicy: 'terminal-first' }
)
).toBe(true)
})
it('keeps terminal-allowed app shortcuts active in terminal-first mode', () => {
const deleteBinding = {
key: 'Backspace',
code: 'Backspace',
control: true,
meta: false,
alt: false,
shift: true
}
expect(
keybindingMatchesAction(
'floatingTerminal.toggle',
{ key: 'a', code: 'KeyA', control: true, meta: false, alt: true, shift: false },
'linux',
undefined,
{ context: 'terminal', terminalShortcutPolicy: 'terminal-first' }
)
).toBe(true)
expect(
keybindingMatchesAction(
'tab.previousRecent',
{ key: 'Tab', code: 'Tab', control: true, meta: false, alt: false, shift: false },
'linux',
undefined,
{ context: 'terminal', terminalShortcutPolicy: 'terminal-first' }
)
).toBe(true)
expect(
keybindingMatchesAction(
'workspace.delete',
deleteBinding,
'linux',
{ 'workspace.delete': ['Mod+Shift+Backspace'] },
{ context: 'terminal', terminalShortcutPolicy: 'terminal-first' }
)
).toBe(true)
expect(
keybindingMatchesAction(
'worktree.palette',
{ key: 'j', code: 'KeyJ', control: false, meta: true, alt: false, shift: false },
'darwin',
undefined,
{ context: 'app', terminalShortcutPolicy: 'terminal-first' }
)
).toBe(true)
})
it('keeps the existing terminal paste defaults on Windows and Linux', () => {
expect(getEffectiveKeybindingsForAction('terminal.paste', 'darwin')).toEqual(['Mod+V'])
expect(getEffectiveKeybindingsForAction('terminal.paste', 'linux')).toEqual([
'Ctrl+V',
'Ctrl+Shift+V',
'Shift+Insert'
])
expect(
keybindingMatchesAction(
'terminal.paste',
{ key: 'v', code: 'KeyV', control: true, meta: false, alt: false, shift: false },
'linux'
)
).toBe(true)
expect(
keybindingMatchesAction(
'terminal.paste',
{ key: 'Insert', code: 'Insert', control: false, meta: false, alt: false, shift: true },
'linux'
)
).toBe(true)
})
it('matches the default file explorer delete shortcut', () => {
expect(getEffectiveKeybindingsForAction('fileExplorer.delete', 'darwin')).toEqual([
'Mod+Backspace',
'Delete'
])
expect(
keybindingMatchesAction(
'fileExplorer.delete',
{ key: 'Delete', code: 'Delete', control: false, meta: false, alt: false, shift: false },
'linux'
)
).toBe(true)
})
it('matches file explorer undo and redo by produced logical key', () => {
expect(getEffectiveKeybindingsForAction('fileExplorer.undo', 'darwin')).toEqual(['Mod+Z'])
expect(getEffectiveKeybindingsForAction('fileExplorer.redo', 'darwin')).toEqual(['Mod+Shift+Z'])
expect(getEffectiveKeybindingsForAction('fileExplorer.redo', 'linux')).toEqual([
'Mod+Shift+Z',
'Ctrl+Y'
])
expect(
keybindingMatchesAction(
'fileExplorer.undo',
{ key: 'z', code: 'Semicolon', control: false, meta: true, alt: false, shift: false },
'darwin'
)
).toBe(true)
expect(
keybindingMatchesAction(
'fileExplorer.undo',
{ key: ';', code: 'KeyZ', control: false, meta: true, alt: false, shift: false },
'darwin'
)
).toBe(false)
expect(
keybindingMatchesAction(
'fileExplorer.redo',
{ key: 'Z', code: 'Semicolon', control: false, meta: true, alt: false, shift: true },
'darwin'
)
).toBe(true)
expect(
keybindingMatchesAction(
'fileExplorer.redo',
{ key: 'y', code: 'KeyF', control: true, meta: false, alt: false, shift: false },
'linux'
)
).toBe(true)
expect(
keybindingMatchesAction(
'fileExplorer.redo',
{ key: 'f', code: 'KeyY', control: true, meta: false, alt: false, shift: false },
'linux'
)
).toBe(false)
})
it('matches non-QWERTY shortcuts by the produced logical key', () => {
const dvorakPhysicalW = {
key: ',',
code: 'KeyW',
control: false,
meta: true,
alt: false,
shift: false
}
const dvorakPhysicalComma = {
key: 'w',
code: 'Comma',
control: false,
meta: true,
alt: false,
shift: false
}
expect(keybindingMatchesAction('app.settings', dvorakPhysicalW, 'darwin')).toBe(true)
expect(keybindingMatchesAction('tab.close', dvorakPhysicalW, 'darwin')).toBe(false)
expect(keybindingMatchesAction('tab.close', dvorakPhysicalComma, 'darwin')).toBe(true)
expect(keybindingMatchesAction('app.settings', dvorakPhysicalComma, 'darwin')).toBe(false)
expect(keybindingFromInput(dvorakPhysicalW, 'darwin')).toEqual({
ok: true,
value: 'Mod+Comma'
})
expect(keybindingFromInput(dvorakPhysicalComma, 'darwin')).toEqual({
ok: true,
value: 'Mod+W'
})
})
it('uses shifted punctuation aliases only while Shift is pressed', () => {
const shiftedComma = {
key: '<',
code: 'Comma',
control: false,
meta: true,
alt: false,
shift: true
}
expect(keybindingMatchesInput('Mod+Shift+Comma', shiftedComma, 'darwin')).toBe(true)
expect(keybindingFromInput(shiftedComma, 'darwin')).toEqual({
ok: true,
value: 'Mod+Shift+Comma'
})
expect(
keybindingMatchesInput(
'Mod+Comma',
{ ...shiftedComma, code: 'IntlBackslash', shift: false },
'darwin'
)
).toBe(false)
})
it('matches logical bracket shortcuts on JIS keyboards without changing code fallback', () => {
const jisLeftBracket = {
key: '[',
code: 'BracketRight',
control: false,
meta: true,
alt: false,
shift: false
}
const jisRightBracket = {
key: ']',
code: 'Backslash',
control: false,
meta: true,
alt: false,
shift: false
}
const jisLeftBracketShifted = { ...jisLeftBracket, key: '{', shift: true }
const jisRightBracketShifted = { ...jisRightBracket, key: '}', shift: true }
expect(
keybindingMatchesAction('tab.previousSameType', jisLeftBracketShifted, 'darwin', {
'tab.previousSameType': ['Mod+Shift+BracketLeft']
})
).toBe(true)
expect(
keybindingMatchesAction('tab.previousSameType', jisRightBracketShifted, 'darwin', {
'tab.previousSameType': ['Mod+Shift+BracketLeft']
})
).toBe(false)
expect(
keybindingMatchesAction('tab.nextSameType', jisRightBracketShifted, 'darwin', {
'tab.nextSameType': ['Mod+Shift+BracketRight']
})
).toBe(true)
expect(
keybindingMatchesAction('tab.nextSameType', jisLeftBracketShifted, 'darwin', {
'tab.nextSameType': ['Mod+Shift+BracketRight']
})
).toBe(false)
expect(keybindingMatchesAction('terminal.focusPreviousPane', jisLeftBracket, 'darwin')).toBe(
true
)
expect(keybindingMatchesAction('terminal.focusNextPane', jisLeftBracket, 'darwin')).toBe(false)
expect(keybindingMatchesAction('terminal.focusNextPane', jisRightBracket, 'darwin')).toBe(true)
expect(
keybindingMatchesAction('tab.previousAllTypes', { ...jisLeftBracket, alt: true }, 'darwin')
).toBe(true)
expect(
keybindingMatchesAction('tab.nextAllTypes', { ...jisRightBracket, alt: true }, 'darwin')
).toBe(true)
expect(
keybindingMatchesAction(
'tab.previousAllTypes',
{ ...jisLeftBracket, control: true, meta: false, alt: true },
'linux'
)
).toBe(true)
expect(
keybindingMatchesAction(
'tab.nextAllTypes',
{ ...jisLeftBracket, control: true, meta: false, alt: true },
'linux'
)
).toBe(false)
expect(
keybindingMatchesAction(
'tab.nextAllTypes',
{ ...jisRightBracket, control: true, meta: false, alt: true },
'linux'
)
).toBe(true)
expect(
keybindingMatchesAction('terminal.splitRight', jisRightBracketShifted, 'darwin', {
'terminal.splitRight': ['Mod+Shift+Backslash']
})
).toBe(false)
expect(
keybindingMatchesAction(
'tab.nextSameType',
{
key: 'Dead',
code: 'BracketRight',
control: false,
meta: true,
alt: false,
shift: true
},
'darwin',
{ 'tab.nextSameType': ['Mod+Shift+BracketRight'] }
)
).toBe(true)
expect(
keybindingMatchesAction(
'tab.previousAllTypes',
{
key: '[',
code: 'Digit8',
control: true,
meta: false,
alt: true,
shift: false
},
'linux'
)
).toBe(false)
expect(
keybindingMatchesAction(
'tab.previousAllTypes',
{
key: 'Dead',
code: 'BracketLeft',
control: true,
meta: false,
alt: true,
shift: false
},
'linux'
)
).toBe(true)
})
it('matches double-tap bindings only against synthetic double-tap input', () => {
expect(
keybindingMatchesInput('DoubleTap+Shift', { doubleTapModifier: 'Shift' }, 'darwin')
).toBe(true)
// Mod resolves per platform: meta on macOS, control elsewhere.
expect(keybindingMatchesInput('DoubleTap+Mod', { doubleTapModifier: 'Cmd' }, 'darwin')).toBe(
true
)
expect(keybindingMatchesInput('DoubleTap+Mod', { doubleTapModifier: 'Ctrl' }, 'win32')).toBe(
true
)
expect(keybindingMatchesInput('DoubleTap+Mod', { doubleTapModifier: 'Cmd' }, 'win32')).toBe(
false
)
expect(keybindingMatchesInput('DoubleTap+Mod', { doubleTapModifier: 'Ctrl' }, 'darwin')).toBe(
false
)
expect(keybindingMatchesInput('DoubleTap+Shift', { doubleTapModifier: 'Alt' }, 'darwin')).toBe(
false
)
// Cross-type negatives: a double-tap binding never matches a normal keydown,
// and a normal binding never matches a synthetic double-tap input.
expect(
keybindingMatchesInput('DoubleTap+Shift', { key: 'A', code: 'KeyA', shift: true }, 'darwin')
).toBe(false)
expect(keybindingMatchesInput('Mod+P', { doubleTapModifier: 'Cmd' }, 'darwin')).toBe(false)
// Action-level matching works through user overrides, for free.
expect(
keybindingMatchesAction('worktree.quickOpen', { doubleTapModifier: 'Shift' }, 'darwin', {
'worktree.quickOpen': ['DoubleTap+Shift']
})
).toBe(true)
expect(
keybindingMatchesAction('worktree.quickOpen', { doubleTapModifier: 'Alt' }, 'darwin', {
'worktree.quickOpen': ['DoubleTap+Shift']
})
).toBe(false)
})
it('captures double-tap gestures into canonical bindings', () => {
expect(keybindingFromInput({ doubleTapModifier: 'Shift' }, 'darwin')).toEqual({
ok: true,
value: 'DoubleTap+Shift'
})
// The platform primary modifier canonicalizes to Mod, matching normal capture.
expect(keybindingFromInput({ doubleTapModifier: 'Cmd' }, 'darwin')).toEqual({
ok: true,
value: 'DoubleTap+Mod'
})
expect(keybindingFromInput({ doubleTapModifier: 'Ctrl' }, 'win32')).toEqual({
ok: true,
value: 'DoubleTap+Mod'
})
// A non-primary modifier keeps its explicit token.
expect(keybindingFromInput({ doubleTapModifier: 'Ctrl' }, 'darwin')).toEqual({
ok: true,
value: 'DoubleTap+Ctrl'
})
expect(keybindingFromInput({ doubleTapModifier: 'Alt' }, 'linux')).toEqual({
ok: true,
value: 'DoubleTap+Alt'
})
// Ctrl is the primary modifier on Linux too, so it canonicalizes to Mod.
expect(keybindingFromInput({ doubleTapModifier: 'Ctrl' }, 'linux')).toEqual({
ok: true,
value: 'DoubleTap+Mod'
})
// Cmd is not the primary modifier off-mac, so it stays explicit.
expect(keybindingFromInput({ doubleTapModifier: 'Cmd' }, 'linux')).toEqual({
ok: true,
value: 'DoubleTap+Cmd'
})
})
it('formats double-tap bindings as the modifier glyph twice', () => {
expect(formatKeybinding('DoubleTap+Shift', 'darwin')).toEqual(['⇧', '⇧'])
expect(formatKeybinding('DoubleTap+Shift', 'linux')).toEqual(['Shift', 'Shift'])
expect(formatKeybinding('DoubleTap+Mod', 'darwin')).toEqual(['⌘', '⌘'])
expect(formatKeybinding('DoubleTap+Mod', 'win32')).toEqual(['Ctrl', 'Ctrl'])
expect(formatKeybinding('DoubleTap+Cmd', 'win32')).toEqual(['Cmd', 'Cmd'])
expect(formatKeybinding('DoubleTap+Alt', 'darwin')).toEqual(['⌥', '⌥'])
// Ctrl's glyph ⌃ diverges from Mod's ⌘ on Mac, so cover it explicitly.
expect(formatKeybinding('DoubleTap+Ctrl', 'darwin')).toEqual(['⌃', '⌃'])
expect(formatKeybindingList(['DoubleTap+Shift'], 'darwin')).toBe('⇧ ⇧')
expect(formatKeybindingList(['DoubleTap+Shift'], 'linux')).toBe('Shift Shift')
})
it('matches macOS Option-composed bracket shortcuts for all-type tab switching', () => {
const macOptionLeftBracket = {
key: '\u201c',
code: 'BracketLeft',
control: false,
meta: true,
alt: true,
shift: false
}
const macOptionRightBracket = {
key: '\u2018',
code: 'BracketRight',
control: false,
meta: true,
alt: true,
shift: false
}
expect(keybindingMatchesAction('tab.previousAllTypes', macOptionLeftBracket, 'darwin')).toBe(
true
)
expect(keybindingMatchesAction('tab.nextAllTypes', macOptionLeftBracket, 'darwin')).toBe(false)
expect(keybindingMatchesAction('tab.nextAllTypes', macOptionRightBracket, 'darwin')).toBe(true)
expect(keybindingMatchesAction('tab.previousAllTypes', macOptionRightBracket, 'darwin')).toBe(
false
)
})
it('reports conflicts across two double-tap bindings', () => {
// Both actions share the same DoubleTap+Shift binding via overrides, so both
// are in customizedActions and the conflict detector must flag them.
const conflicts = findKeybindingConflicts('darwin', {
'worktree.quickOpen': ['DoubleTap+Shift'],
'view.tasks': ['DoubleTap+Shift']
})
expect(conflicts).toContainEqual({
binding: 'DoubleTap+Shift',
actionIds: expect.arrayContaining(['worktree.quickOpen', 'view.tasks'])
})
})
it('reports conflicts across platform-primary double-tap aliases', () => {
expect(
findKeybindingConflicts('darwin', {
'worktree.quickOpen': ['DoubleTap+Mod'],
'view.tasks': ['DoubleTap+Cmd']
})
).toContainEqual({
binding: 'DoubleTap+Mod',
actionIds: expect.arrayContaining(['worktree.quickOpen', 'view.tasks'])
})
expect(
findKeybindingConflicts('linux', {
'worktree.quickOpen': ['DoubleTap+Mod'],
'view.tasks': ['DoubleTap+Ctrl']
})
).toContainEqual({
binding: 'DoubleTap+Mod',
actionIds: expect.arrayContaining(['worktree.quickOpen', 'view.tasks'])
})
})
it('does not report a conflict when one action lists double-tap aliases for itself', () => {
expect(
findKeybindingConflicts('darwin', {
'worktree.quickOpen': ['DoubleTap+Mod', 'DoubleTap+Cmd']
})
).toEqual([])
expect(
findKeybindingConflicts('linux', {
'worktree.quickOpen': ['DoubleTap+Mod', 'DoubleTap+Ctrl']
})
).toEqual([])
})
})
describe('digit-index shortcuts', () => {
const digitInput = (
digit: string,
modifiers: { meta?: boolean; control?: boolean; alt?: boolean; shift?: boolean }
): Parameters<typeof matchKeybindingDigitIndex>[1] => ({
key: digit,
code: `Digit${digit}`,
meta: Boolean(modifiers.meta),
control: Boolean(modifiers.control),
alt: Boolean(modifiers.alt),
shift: Boolean(modifiers.shift)
})
it('flags the two ranged actions as digit-index rows', () => {
expect(isDigitIndexActionId('tab.selectByIndex')).toBe(true)
expect(isDigitIndexActionId('workspace.selectByIndex')).toBe(true)
expect(isDigitIndexActionId('tab.newTerminal')).toBe(false)
})
it('resolves the default ranges per platform', () => {
// macOS: workspace = Cmd+1-9, tab = Ctrl+1-9.
expect(
matchKeybindingDigitIndex(
'workspace.selectByIndex',
digitInput('3', { meta: true }),
'darwin'
)
).toBe(2)
expect(
matchKeybindingDigitIndex('tab.selectByIndex', digitInput('3', { meta: true }), 'darwin')
).toBeNull()
expect(
matchKeybindingDigitIndex('tab.selectByIndex', digitInput('3', { control: true }), 'darwin')
).toBe(2)
// Windows/Linux: workspace = Ctrl+1-9, tab = Alt+1-9.
expect(
matchKeybindingDigitIndex(
'workspace.selectByIndex',
digitInput('4', { control: true }),
'linux'
)
).toBe(3)
expect(
matchKeybindingDigitIndex('tab.selectByIndex', digitInput('4', { alt: true }), 'linux')
).toBe(3)
})
it('ignores non-range presses and extra modifiers', () => {
expect(
matchKeybindingDigitIndex(
'workspace.selectByIndex',
digitInput('3', { meta: true, shift: true }),
'darwin'
)
).toBeNull()
expect(
matchKeybindingDigitIndex(
'tab.selectByIndex',
{ key: 'p', code: 'KeyP', meta: false, control: true, alt: false, shift: false },
'darwin'
)
).toBeNull()
})
it('honors custom bindings, including swapping tab and workspace modifiers', () => {
const swapped = {
'tab.selectByIndex': ['Mod+1'],
'workspace.selectByIndex': ['Ctrl+1']
}
expect(
matchKeybindingDigitIndex(
'tab.selectByIndex',
digitInput('5', { meta: true }),
'darwin',
swapped
)
).toBe(4)
expect(
matchKeybindingDigitIndex(
'workspace.selectByIndex',
digitInput('5', { control: true }),
'darwin',
swapped
)
).toBe(4)
// A disabled (empty) override never fires.
expect(
matchKeybindingDigitIndex('tab.selectByIndex', digitInput('5', { control: true }), 'darwin', {
'tab.selectByIndex': []
})
).toBeNull()
})
it('respects the terminal-first context gate', () => {
expect(
matchKeybindingDigitIndex(
'tab.selectByIndex',
digitInput('2', { control: true }),
'darwin',
undefined,
{
context: 'terminal',
terminalShortcutPolicy: 'terminal-first'
}
)
).toBeNull()
expect(
matchKeybindingDigitIndex(
'tab.selectByIndex',
digitInput('2', { control: true }),
'darwin',
undefined,
{
context: 'terminal',
terminalShortcutPolicy: 'orca-first'
}
)
).toBe(1)
})
it('canonicalizes a captured chord to the digit-1 representative', () => {
expect(
keybindingFromInputForAction(
'workspace.selectByIndex',
digitInput('7', { meta: true }),
'darwin'
)
).toEqual({ ok: true, value: 'Mod+1' })
expect(
keybindingFromInputForAction(
'tab.selectByIndex',
digitInput('9', { control: true }),
'darwin'
)
).toEqual({ ok: true, value: 'Ctrl+1' })
// A non-number chord is rejected with guidance.
expect(
keybindingFromInputForAction(
'tab.selectByIndex',
{ key: 'p', code: 'KeyP', meta: true, control: false, alt: false, shift: false },
'darwin'
)
).toMatchObject({ ok: false })
})
it('allows extra modifiers (e.g. Shift) on a digit-index chord', () => {
expect(
keybindingFromInputForAction(
'tab.selectByIndex',
digitInput('5', { control: true, shift: true }),
'darwin'
)
).toEqual({ ok: true, value: 'Ctrl+Shift+1' })
expect(normalizeKeybindingListForAction('workspace.selectByIndex', 'Mod+Shift+5')).toEqual([
'Mod+Shift+1'
])
})
it('matches via the physical-code fallback when the key value is unavailable', () => {
// macOS/IME edge cases can leave key empty while code carries the digit.
expect(
matchKeybindingDigitIndex(
'tab.selectByIndex',
{ key: '', code: 'Digit5', meta: false, control: true, alt: false, shift: false },
'darwin'
)
).toBe(4)
})
it('canonicalizes stored bindings and rejects non-number chords', () => {
expect(normalizeKeybindingListForAction('workspace.selectByIndex', 'Mod+5')).toEqual(['Mod+1'])
expect(normalizeKeybindingArrayForAction('tab.selectByIndex', ['Ctrl+9'])).toEqual(['Ctrl+1'])
expect(normalizeKeybindingListForAction('tab.selectByIndex', 'Mod+P')).toMatchObject({
ok: false
})
})
it('lets the two ranges swap modifiers without a false conflict', () => {
// The headline use case: tab → Cmd, workspace → Ctrl. They live in
// different scopes, so neither edit is blocked as a conflict.
expect(
findKeybindingConflicts('darwin', {
'tab.selectByIndex': ['Mod+1'],
'workspace.selectByIndex': ['Ctrl+1']
})
).toEqual([])
})
})