import { describe, it, expect, vi, afterEach } from 'vitest' import { renderHook } from '@testing-library/react' import { useAppKeyboard } from './useAppKeyboard' function fireKey(key: string, mods: { altKey?: boolean; metaKey?: boolean; ctrlKey?: boolean; shiftKey?: boolean } = {}) { const event = new KeyboardEvent('keydown', { key, altKey: mods.altKey ?? false, metaKey: mods.metaKey ?? false, ctrlKey: mods.ctrlKey ?? false, shiftKey: mods.shiftKey ?? false, bubbles: true, cancelable: true, }) window.dispatchEvent(event) } function makeActions() { return { onQuickOpen: vi.fn(), onCommandPalette: vi.fn(), onSearch: vi.fn(), onCreateNote: vi.fn(), onOpenDailyNote: vi.fn(), onSave: vi.fn(), onOpenSettings: vi.fn(), onDeleteNote: vi.fn(), onArchiveNote: vi.fn(), onSetViewMode: vi.fn(), onZoomIn: vi.fn(), onZoomOut: vi.fn(), onZoomReset: vi.fn(), activeTabPathRef: { current: '/vault/test.md' } as React.MutableRefObject, } } describe('useAppKeyboard', () => { afterEach(() => vi.restoreAllMocks()) it('Cmd+1 sets view mode to editor-only', () => { const actions = makeActions() renderHook(() => useAppKeyboard(actions)) fireKey('1', { metaKey: true }) expect(actions.onSetViewMode).toHaveBeenCalledWith('editor-only') }) it('Cmd+2 sets view mode to editor-list', () => { const actions = makeActions() renderHook(() => useAppKeyboard(actions)) fireKey('2', { metaKey: true }) expect(actions.onSetViewMode).toHaveBeenCalledWith('editor-list') }) it('Cmd+3 sets view mode to all', () => { const actions = makeActions() renderHook(() => useAppKeyboard(actions)) fireKey('3', { metaKey: true }) expect(actions.onSetViewMode).toHaveBeenCalledWith('all') }) it('does not fire view mode when Cmd+Alt pressed', () => { const actions = makeActions() renderHook(() => useAppKeyboard(actions)) fireKey('1', { metaKey: true, altKey: true }) expect(actions.onSetViewMode).not.toHaveBeenCalled() }) it('Cmd+P triggers quick open', () => { const actions = makeActions() renderHook(() => useAppKeyboard(actions)) fireKey('p', { metaKey: true }) expect(actions.onQuickOpen).toHaveBeenCalled() }) it('Cmd+N triggers create note', () => { const actions = makeActions() renderHook(() => useAppKeyboard(actions)) fireKey('n', { metaKey: true }) expect(actions.onCreateNote).toHaveBeenCalled() }) it('Cmd+D triggers toggle favorite on active note', () => { const actions = makeActions() actions.onToggleFavorite = vi.fn() renderHook(() => useAppKeyboard(actions)) fireKey('d', { metaKey: true }) expect(actions.onToggleFavorite).toHaveBeenCalledWith('/vault/test.md') }) it('Cmd+J triggers open daily note', () => { const actions = makeActions() renderHook(() => useAppKeyboard(actions)) fireKey('j', { metaKey: true }) expect(actions.onOpenDailyNote).toHaveBeenCalled() }) it('Alt+4 does not trigger any view mode', () => { const actions = makeActions() renderHook(() => useAppKeyboard(actions)) fireKey('4', { altKey: true }) expect(actions.onSetViewMode).not.toHaveBeenCalled() }) it('Cmd+K triggers command palette', () => { const actions = makeActions() renderHook(() => useAppKeyboard(actions)) fireKey('k', { metaKey: true }) expect(actions.onCommandPalette).toHaveBeenCalled() }) it('Cmd+Shift+F triggers search', () => { const actions = makeActions() renderHook(() => useAppKeyboard(actions)) fireKey('f', { metaKey: true, shiftKey: true }) expect(actions.onSearch).toHaveBeenCalled() }) it('Cmd+Shift+F does not trigger other shortcuts', () => { const actions = makeActions() renderHook(() => useAppKeyboard(actions)) fireKey('f', { metaKey: true, shiftKey: true }) expect(actions.onQuickOpen).not.toHaveBeenCalled() expect(actions.onCreateNote).not.toHaveBeenCalled() }) function withFocusedInput(fn: () => void) { const input = document.createElement('input') document.body.appendChild(input) input.focus() try { fn() } finally { document.body.removeChild(input) } } it('Cmd+Backspace does not delete note when text input is focused', () => { const actions = makeActions() renderHook(() => useAppKeyboard(actions)) withFocusedInput(() => { fireKey('Backspace', { metaKey: true }) expect(actions.onDeleteNote).not.toHaveBeenCalled() }) }) it('Cmd+Backspace deletes note when no text input is focused', () => { const actions = makeActions() renderHook(() => useAppKeyboard(actions)) fireKey('Backspace', { metaKey: true }) expect(actions.onDeleteNote).toHaveBeenCalledWith('/vault/test.md') }) it('Cmd+K still works when text input is focused', () => { const actions = makeActions() renderHook(() => useAppKeyboard(actions)) withFocusedInput(() => { fireKey('k', { metaKey: true }) expect(actions.onCommandPalette).toHaveBeenCalled() }) }) it('Cmd+= triggers zoom in', () => { const actions = makeActions() renderHook(() => useAppKeyboard(actions)) fireKey('=', { metaKey: true }) expect(actions.onZoomIn).toHaveBeenCalled() }) it('Cmd++ triggers zoom in', () => { const actions = makeActions() renderHook(() => useAppKeyboard(actions)) fireKey('+', { metaKey: true }) expect(actions.onZoomIn).toHaveBeenCalled() }) it('Cmd+- triggers zoom out', () => { const actions = makeActions() renderHook(() => useAppKeyboard(actions)) fireKey('-', { metaKey: true }) expect(actions.onZoomOut).toHaveBeenCalled() }) it('Cmd+0 triggers zoom reset', () => { const actions = makeActions() renderHook(() => useAppKeyboard(actions)) fireKey('0', { metaKey: true }) expect(actions.onZoomReset).toHaveBeenCalled() }) it('Cmd+Option+I triggers toggle AI chat', () => { const actions = makeActions() const onToggleAIChat = vi.fn() renderHook(() => useAppKeyboard({ ...actions, onToggleAIChat })) fireKey('i', { metaKey: true, altKey: true }) expect(onToggleAIChat).toHaveBeenCalled() }) it('Cmd+Option+I works when text input is focused', () => { const actions = makeActions() const onToggleAIChat = vi.fn() renderHook(() => useAppKeyboard({ ...actions, onToggleAIChat })) withFocusedInput(() => { fireKey('i', { metaKey: true, altKey: true }) expect(onToggleAIChat).toHaveBeenCalled() }) }) it('Cmd+I does not trigger AI chat (reserved for italic)', () => { const actions = makeActions() const onToggleAIChat = vi.fn() renderHook(() => useAppKeyboard({ ...actions, onToggleAIChat })) fireKey('i', { metaKey: true }) expect(onToggleAIChat).not.toHaveBeenCalled() }) it('Cmd+Shift+O triggers open in new window', () => { const actions = makeActions() const onOpenInNewWindow = vi.fn() renderHook(() => useAppKeyboard({ ...actions, onOpenInNewWindow })) fireKey('o', { metaKey: true, shiftKey: true }) expect(onOpenInNewWindow).toHaveBeenCalled() }) it('Cmd+Shift+I triggers toggle inspector', () => { const actions = makeActions() const onToggleInspector = vi.fn() renderHook(() => useAppKeyboard({ ...actions, onToggleInspector })) fireKey('i', { metaKey: true, shiftKey: true }) expect(onToggleInspector).toHaveBeenCalled() }) it('Cmd+Shift+I does not trigger AI chat toggle', () => { const actions = makeActions() const onToggleAIChat = vi.fn() const onToggleInspector = vi.fn() renderHook(() => useAppKeyboard({ ...actions, onToggleAIChat, onToggleInspector })) fireKey('i', { metaKey: true, shiftKey: true }) expect(onToggleInspector).toHaveBeenCalled() expect(onToggleAIChat).not.toHaveBeenCalled() }) it('Cmd+Option+I does not trigger inspector toggle', () => { const actions = makeActions() const onToggleAIChat = vi.fn() const onToggleInspector = vi.fn() renderHook(() => useAppKeyboard({ ...actions, onToggleAIChat, onToggleInspector })) fireKey('i', { metaKey: true, altKey: true }) expect(onToggleAIChat).toHaveBeenCalled() expect(onToggleInspector).not.toHaveBeenCalled() }) })