The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
import { describe, expect, it } from 'vitest'
// Host-native: jsdom never reports a Mac platform, so `IS_MAC` is false here.
// Mac-only Control/Cmd branches are not faked (tests/AGENTS.md "Don't fake the host OS").
import { actionAllowedInInput, canonicalizeCombo, comboFromEvent } from './combo'
function keydown(init: KeyboardEventInit): KeyboardEvent {
return new KeyboardEvent('keydown', init)
}
describe('comboFromEvent', () => {
it('uses layout-aware letters for Cmd shortcuts on non-QWERTY layouts', () => {
expect(comboFromEvent(keydown({ code: 'KeyI', key: 'c', metaKey: true }))).toBe('mod+c')
expect(comboFromEvent(keydown({ code: 'KeyI', key: 'C', metaKey: true, shiftKey: true }))).toBe('mod+shift+c')
})
it('keeps shifted punctuation anchored to the physical key token', () => {
expect(comboFromEvent(keydown({ code: 'Slash', key: '?', metaKey: true, shiftKey: true }))).toBe('mod+shift+/')
})
it('uses layout-aware punctuation for Cmd shortcuts on non-QWERTY layouts', () => {
// Dvorak puts "." on the physical QWERTY V key — ⌘. must still reach the
// command center rather than resolving to the physical token.
expect(comboFromEvent(keydown({ code: 'KeyV', key: '.', metaKey: true }))).toBe('mod+.')
expect(comboFromEvent(keydown({ code: 'KeyW', key: ',', metaKey: true }))).toBe('mod+,')
expect(comboFromEvent(keydown({ code: 'BracketLeft', key: '/', metaKey: true }))).toBe('mod+/')
// AZERTY reaches "," from the physical QWERTY M key.
expect(comboFromEvent(keydown({ code: 'KeyM', key: ',', metaKey: true }))).toBe('mod+,')
})
it("keeps digits physical so AZERTY's shifted number row still binds", () => {
// On AZERTY the unshifted "1" key types "&", and "1" only with Shift held.
// Both must resolve to the same `mod+1` the QWERTY user gets.
expect(comboFromEvent(keydown({ code: 'Digit1', key: '&', metaKey: true }))).toBe('mod+1')
expect(comboFromEvent(keydown({ code: 'Digit1', key: '1', metaKey: true }))).toBe('mod+1')
})
it('falls back to the physical key for glyphs we do not ship as tokens', () => {
// Option-modified glyphs, dead keys, and non-Latin scripts are not combo
// tokens, so the physical code keeps the binding reachable.
expect(comboFromEvent(keydown({ code: 'KeyK', key: '˚', metaKey: true, altKey: true }))).toBe('mod+alt+k')
expect(comboFromEvent(keydown({ code: 'KeyN', key: 'Dead', metaKey: true, altKey: true }))).toBe('mod+alt+n')
expect(comboFromEvent(keydown({ code: 'KeyK', key: 'л', metaKey: true }))).toBe('mod+k')
})
it('treats Control as the "mod" accelerator off macOS', () => {
expect(comboFromEvent(keydown({ code: 'Tab', ctrlKey: true }))).toBe('mod+tab')
expect(comboFromEvent(keydown({ code: 'Tab', ctrlKey: true, shiftKey: true }))).toBe('mod+shift+tab')
})
it('keeps function and special keys available for custom bindings', () => {
expect(comboFromEvent(keydown({ code: 'F1', key: 'F1' }))).toBe('f1')
expect(comboFromEvent(keydown({ code: 'F12', key: 'F12' }))).toBe('f12')
expect(comboFromEvent(keydown({ code: 'F19', key: 'F19' }))).toBe('f19')
expect(comboFromEvent(keydown({ code: 'F18', key: 'F18' }))).toBe('f18')
expect(comboFromEvent(keydown({ code: 'CapsLock', key: 'CapsLock' }))).toBe('capslock')
expect(comboFromEvent(keydown({ code: 'Space', key: ' ', altKey: true }))).toBe('alt+space')
expect(comboFromEvent(keydown({ code: 'KeyV', key: 'v', metaKey: true, shiftKey: true }))).toBe('mod+shift+v')
expect(comboFromEvent(keydown({ code: 'F18', key: 'F18', metaKey: true, shiftKey: true }))).toBe('mod+shift+f18')
expect(comboFromEvent(keydown({ code: 'F13', key: 'F13', altKey: true }))).toBe('alt+f13')
})
})
describe('canonicalizeCombo', () => {
it('folds "ctrl+…" to "mod+…" off macOS so a real Control press resolves', () => {
expect(canonicalizeCombo('ctrl+tab')).toBe('mod+tab')
expect(canonicalizeCombo('ctrl+shift+tab')).toBe('mod+shift+tab')
// Non-ctrl combos are unchanged.
expect(canonicalizeCombo('mod+k')).toBe('mod+k')
})
})
describe('actionAllowedInInput', () => {
it('keeps primary-modifier chords global while typing, gating bare/Shift combos to the allowlist', () => {
// Mod/Ctrl chords are deliberate two-key gestures — they fire even with
// focus in the composer (⌘N, ⌘T, ⌘⇧N, ⌘K, ⌃Tab, …), matching every browser
// and chat app and the pre-#86586 behavior.
expect(actionAllowedInInput('session.new', 'mod+n')).toBe(true)
expect(actionAllowedInInput('session.newTab', 'mod+t')).toBe(true)
expect(actionAllowedInInput('session.newWindow', 'mod+shift+n')).toBe(true)
expect(actionAllowedInInput('session.next', 'ctrl+tab')).toBe(true)
expect(actionAllowedInInput('session.prev', 'ctrl+shift+tab')).toBe(true)
expect(actionAllowedInInput('nav.commandPalette', 'mod+k')).toBe(true)
expect(actionAllowedInInput('view.findInPage', 'mod+f')).toBe(true)
expect(actionAllowedInInput('nav.capabilities', 'mod+k')).toBe(true)
expect(actionAllowedInInput('view.showTerminal', 'ctrl+`')).toBe(true)
expect(actionAllowedInInput('profile.next', 'mod+shift+]')).toBe(true)
// A global action rebound onto a text-editing chord (Ctrl+A select-all,
// Ctrl+E line-end, …) fires deliberately while typing — mod-chords are
// two-key gestures, the pre-#86586 semantic. Unbound chords (the shipped
// default for a/e/u/backspace) never reach this function: the dispatcher
// returns before the gate when no action is bound, so the input keeps its
// native editing behavior out of the box. An editing-chord exclusion set
// would have to dodge shipped defaults (⌘K, ⌘W, ⌘D, ⌘F, ⌘B) or re-break them.
expect(actionAllowedInInput('session.new', 'ctrl+a')).toBe(true)
// Bare modifiers are not real chords — `comboFromEvent` never yields
// them, so a malformed stored binding must not pass the shape-only check.
expect(actionAllowedInInput('session.new', 'mod')).toBe(false)
expect(actionAllowedInInput('session.new', 'ctrl')).toBe(false)
// Bare/Shift-only combos must never hijack typing: a rebound ⌘N → 'n'
// (or the pre-#76185 'shift+n') must not fire while the user types N.
expect(actionAllowedInInput('session.new', 'n')).toBe(false)
expect(actionAllowedInInput('session.new', 'shift+n')).toBe(false)
// Dictation is intentionally bindable without a shipped chord. A user who
// assigns a bare/Shift chord expects it to remain reachable from the draft.
expect(actionAllowedInInput('composer.dictate', 'shift+d')).toBe(true)
})
it('leaves text navigation chords with the focused input even when rebound to an allowed action', () => {
expect(actionAllowedInInput('session.next', 'mod+right')).toBe(false)
expect(actionAllowedInInput('session.prev', 'mod+left')).toBe(false)
expect(actionAllowedInInput('nav.commandPalette', 'mod+pageup')).toBe(false)
expect(actionAllowedInInput('view.findInPage', 'mod+end')).toBe(false)
})
it('lets an explicitly rebound two-modifier navigation chord fire while typing (#115980)', () => {
// ⌘⌥←/→ and Ctrl+Alt+←/→ carry Alt on top of a primary modifier — that
// shape has no native text-editing meaning (it is not ⌥← word-jump or
// ⌘← line-start), so a deliberate rebind of `session.next`/`session.prev`
// keeps firing with focus in the composer, matching `mod+alt+t`.
expect(actionAllowedInInput('session.next', 'mod+alt+right')).toBe(true)
expect(actionAllowedInInput('session.prev', 'mod+alt+left')).toBe(true)
expect(actionAllowedInInput('session.next', 'ctrl+alt+right')).toBe(true)
expect(actionAllowedInInput('session.prev', 'ctrl+alt+left')).toBe(true)
// The accidental-trap class stays input-local: single primary modifier
// (± Shift) and bare Alt remain native editing gestures.
expect(actionAllowedInInput('session.next', 'mod+right')).toBe(false)
expect(actionAllowedInInput('session.next', 'mod+shift+right')).toBe(false)
expect(actionAllowedInInput('session.next', 'ctrl+pageup')).toBe(false)
expect(actionAllowedInInput('session.next', 'alt+right')).toBe(false)
// The dispatcher consumes real keydowns: ⌘⌥→ canonicalizes to the combo
// the gate now lets through.
expect(comboFromEvent(keydown({ code: 'ArrowRight', metaKey: true, altKey: true }))).toBe('mod+alt+right')
expect(comboFromEvent(keydown({ code: 'ArrowLeft', metaKey: true, altKey: true }))).toBe('mod+alt+left')
})
it('fires reasoning level actions from an editable target on modified chords, never on bare keys (#71627)', () => {
// Alt/Numpad-style chords without a primary modifier — the shapes users
// actually pick for runtime dials — reach the action while typing.
expect(actionAllowedInInput('composer.reasoningUp', 'alt+.')).toBe(true)
expect(actionAllowedInInput('composer.reasoningDown', 'alt+,')).toBe(true)
expect(actionAllowedInInput('composer.reasoningUp', 'mod+alt+down')).toBe(true)
// Primary-modifier chords were already global; the opt-in adds nothing new.
expect(actionAllowedInInput('composer.reasoningUp', 'mod+shift+m')).toBe(true)
// A bare or shift-only rebind stays with the input: typing '.' or 'U'
// in the composer must never change the reasoning level.
expect(actionAllowedInInput('composer.reasoningUp', '.')).toBe(false)
expect(actionAllowedInInput('composer.reasoningUp', 'shift+.')).toBe(false)
expect(actionAllowedInInput('composer.reasoningDown', ',')).toBe(false)
})
})
describe('comboFromEvent — IME composition keydowns never resolve to combos (#84957)', () => {
it('returns null while a composition is in progress (isComposing)', () => {
// Typing 你 with a Chinese IME: the preedit keydowns carry isComposing.
// Before the guard, these canonicalized to combos and fired keybinds
// (e.g. dispatched `session.new` mid-composition).
expect(comboFromEvent(keydown({ code: 'KeyN', isComposing: true, key: 'n' }))).toBeNull()
expect(comboFromEvent(keydown({ code: 'Enter', isComposing: true, key: 'Enter' }))).toBeNull()
expect(comboFromEvent(keydown({ code: 'Space', isComposing: true, key: ' ' }))).toBeNull()
})
it('returns null for the legacy key="Process" (VK_PROCESSKEY) keydown', () => {
expect(comboFromEvent(keydown({ code: 'KeyW', key: 'Process' }))).toBeNull()
})
it('ignores IME-synthesized modifier-name keys on non-modifier codes', () => {
// Q9 2002-style legacy IMEs synthesize key="Control" with code="KeyW",
// which would otherwise canonicalize to a phantom ctrl+w (close tab).
expect(comboFromEvent(keydown({ code: 'KeyW', key: 'Control' }))).toBeNull()
expect(comboFromEvent(keydown({ code: 'KeyA', key: 'Shift' }))).toBeNull()
})
it('still resolves real combos after composition ends', () => {
expect(comboFromEvent(keydown({ code: 'KeyN', isComposing: false, key: 'n', metaKey: true }))).toBe('mod+n')
})
})
describe('comboFromEvent — malformed keyboard events (#91611)', () => {
// Built as plain objects rather than via `new KeyboardEvent`, because the
// constructor coerces `code` to a string ("undefined", "42") and would hide
// the very shapes under test (packaged-renderer TypeError logs in the issue).
const malformed = (init: Record<string, unknown>): KeyboardEvent => init as unknown as KeyboardEvent
it.each([
['both key and code are absent', { code: undefined, key: undefined }],
['code is null', { code: null, key: undefined }],
['code is a number', { code: 42, key: undefined }],
['code is an empty string', { code: '', key: undefined }]
])('returns null when %s', (_label, init) => {
expect(comboFromEvent(malformed(init as Record<string, unknown>))).toBeNull()
})
// A junk `code` must make the physical fallback inert without discarding an
// otherwise legitimate event: some IME and synthetic keydowns carry an empty
// `code` alongside a real `key`, which still has to resolve via the key path.
it.each([
['null', null],
['a number', 42],
['an empty string', '']
])('resolves via event.key when code is %s but key is valid', (_label, code) => {
expect(comboFromEvent(malformed({ code, key: 'a' }))).toBe('a')
expect(comboFromEvent(malformed({ code, ctrlKey: true, key: 'k' }))).toBe('mod+k')
})
})