|
| 1 | +import { describe, it, expect, beforeEach, vi } from 'vitest' |
| 2 | + |
| 3 | +// Shared test state — the mock factory closures capture these references |
| 4 | +const listeners = new Set<(commandId: string) => void>() |
| 5 | +const customOverrides = new Map<string, string[]>() |
| 6 | + |
| 7 | +// Mock the shortcuts store before importing the module under test |
| 8 | +vi.mock('./shortcuts-store', () => ({ |
| 9 | + getEffectiveShortcuts: vi.fn(), |
| 10 | + onShortcutChange: vi.fn((listener: (commandId: string) => void) => { |
| 11 | + listeners.add(listener) |
| 12 | + return () => listeners.delete(listener) |
| 13 | + }), |
| 14 | +})) |
| 15 | + |
| 16 | +// Mock the command registry with a controlled set of commands |
| 17 | +vi.mock('$lib/commands/command-registry', () => ({ |
| 18 | + commands: [ |
| 19 | + // Tier 1: showInPalette true, has shortcut |
| 20 | + { id: 'app.quit', name: 'Quit', scope: 'App', showInPalette: true, shortcuts: ['⌘Q'] }, |
| 21 | + { |
| 22 | + id: 'file.rename', |
| 23 | + name: 'Rename', |
| 24 | + scope: 'Main window/File list', |
| 25 | + showInPalette: true, |
| 26 | + shortcuts: ['F2', '⇧F6'], |
| 27 | + }, |
| 28 | + { id: 'file.copy', name: 'Copy', scope: 'Main window/File list', showInPalette: true, shortcuts: ['F5'] }, |
| 29 | + { id: 'view.showHidden', name: 'Toggle hidden', scope: 'Main window', showInPalette: true, shortcuts: ['⌘⇧.'] }, |
| 30 | + // Tier 1: showInPalette false but in ALWAYS_DISPATCH_IDS |
| 31 | + { |
| 32 | + id: 'app.commandPalette', |
| 33 | + name: 'Open command palette', |
| 34 | + scope: 'App', |
| 35 | + showInPalette: false, |
| 36 | + shortcuts: ['⌘⇧P'], |
| 37 | + }, |
| 38 | + // Tier 2: showInPalette false, basic nav — should NOT be in dispatch |
| 39 | + { |
| 40 | + id: 'nav.up', |
| 41 | + name: 'Select previous', |
| 42 | + scope: 'Main window/File list', |
| 43 | + showInPalette: false, |
| 44 | + shortcuts: ['↑'], |
| 45 | + }, |
| 46 | + { id: 'nav.down', name: 'Select next', scope: 'Main window/File list', showInPalette: false, shortcuts: ['↓'] }, |
| 47 | + // Tier 2: palette-internal |
| 48 | + { |
| 49 | + id: 'palette.close', |
| 50 | + name: 'Close palette', |
| 51 | + scope: 'Command palette', |
| 52 | + showInPalette: false, |
| 53 | + shortcuts: ['Escape'], |
| 54 | + }, |
| 55 | + ], |
| 56 | +})) |
| 57 | + |
| 58 | +import { getEffectiveShortcuts, onShortcutChange } from './shortcuts-store' |
| 59 | +import { commands } from '$lib/commands/command-registry' |
| 60 | +import { lookupCommand, initShortcutDispatch, destroyShortcutDispatch } from './shortcut-dispatch' |
| 61 | + |
| 62 | +/** |
| 63 | + * Wire up getEffectiveShortcuts to return registry defaults |
| 64 | + * unless a custom override exists. |
| 65 | + */ |
| 66 | +function setupEffectiveShortcuts() { |
| 67 | + vi.mocked(getEffectiveShortcuts).mockImplementation((commandId: string) => { |
| 68 | + const override = customOverrides.get(commandId) |
| 69 | + if (override) { |
| 70 | + return [...override] |
| 71 | + } |
| 72 | + const cmd = commands.find((c) => c.id === commandId) |
| 73 | + return [...(cmd?.shortcuts ?? [])] |
| 74 | + }) |
| 75 | +} |
| 76 | + |
| 77 | +describe('shortcut-dispatch', () => { |
| 78 | + beforeEach(() => { |
| 79 | + destroyShortcutDispatch() |
| 80 | + customOverrides.clear() |
| 81 | + listeners.clear() |
| 82 | + vi.clearAllMocks() |
| 83 | + setupEffectiveShortcuts() |
| 84 | + }) |
| 85 | + |
| 86 | + describe('lookupCommand', () => { |
| 87 | + it('returns the correct command ID for a Tier 1 shortcut', () => { |
| 88 | + initShortcutDispatch() |
| 89 | + expect(lookupCommand('⌘Q')).toBe('app.quit') |
| 90 | + }) |
| 91 | + |
| 92 | + it('handles commands with multiple shortcuts', () => { |
| 93 | + initShortcutDispatch() |
| 94 | + expect(lookupCommand('F2')).toBe('file.rename') |
| 95 | + expect(lookupCommand('⇧F6')).toBe('file.rename') |
| 96 | + }) |
| 97 | + |
| 98 | + it('returns undefined for unregistered key combos', () => { |
| 99 | + initShortcutDispatch() |
| 100 | + expect(lookupCommand('⌘Z')).toBeUndefined() |
| 101 | + expect(lookupCommand('F12')).toBeUndefined() |
| 102 | + }) |
| 103 | + |
| 104 | + it('returns undefined for Tier 2 (non-palette) command shortcuts', () => { |
| 105 | + initShortcutDispatch() |
| 106 | + // nav.up (↑) and nav.down (↓) have showInPalette: false |
| 107 | + expect(lookupCommand('↑')).toBeUndefined() |
| 108 | + expect(lookupCommand('↓')).toBeUndefined() |
| 109 | + }) |
| 110 | + |
| 111 | + it('includes app.commandPalette despite showInPalette: false', () => { |
| 112 | + initShortcutDispatch() |
| 113 | + expect(lookupCommand('⌘⇧P')).toBe('app.commandPalette') |
| 114 | + }) |
| 115 | + |
| 116 | + it('returns undefined before init is called', () => { |
| 117 | + // Map is empty before init |
| 118 | + expect(lookupCommand('⌘Q')).toBeUndefined() |
| 119 | + }) |
| 120 | + }) |
| 121 | + |
| 122 | + describe('custom shortcut overrides', () => { |
| 123 | + it('uses the new binding after a custom override', () => { |
| 124 | + initShortcutDispatch() |
| 125 | + |
| 126 | + // Override file.copy from F5 to F9 |
| 127 | + customOverrides.set('file.copy', ['F9']) |
| 128 | + |
| 129 | + // Trigger the change listener |
| 130 | + for (const listener of listeners) { |
| 131 | + listener('file.copy') |
| 132 | + } |
| 133 | + |
| 134 | + expect(lookupCommand('F9')).toBe('file.copy') |
| 135 | + expect(lookupCommand('F5')).toBeUndefined() |
| 136 | + }) |
| 137 | + |
| 138 | + it('handles adding a shortcut to a command that had none', () => { |
| 139 | + initShortcutDispatch() |
| 140 | + |
| 141 | + customOverrides.set('view.showHidden', ['⌘⇧.', '⌘⇧H']) |
| 142 | + |
| 143 | + for (const listener of listeners) { |
| 144 | + listener('view.showHidden') |
| 145 | + } |
| 146 | + |
| 147 | + expect(lookupCommand('⌘⇧H')).toBe('view.showHidden') |
| 148 | + expect(lookupCommand('⌘⇧.')).toBe('view.showHidden') |
| 149 | + }) |
| 150 | + }) |
| 151 | + |
| 152 | + describe('initShortcutDispatch', () => { |
| 153 | + it('subscribes to shortcut changes', () => { |
| 154 | + initShortcutDispatch() |
| 155 | + expect(onShortcutChange).toHaveBeenCalledOnce() |
| 156 | + }) |
| 157 | + }) |
| 158 | + |
| 159 | + describe('destroyShortcutDispatch', () => { |
| 160 | + it('clears the map after destroy', () => { |
| 161 | + initShortcutDispatch() |
| 162 | + expect(lookupCommand('⌘Q')).toBe('app.quit') |
| 163 | + |
| 164 | + destroyShortcutDispatch() |
| 165 | + expect(lookupCommand('⌘Q')).toBeUndefined() |
| 166 | + }) |
| 167 | + }) |
| 168 | +}) |
0 commit comments