|
1 | 1 | import type { DevframeCommandEntry, DevframeCommandKeybinding } from '@devframes/hub' |
2 | 2 | import type { WhenContext } from 'devframe/utils/when' |
3 | 3 | import { describe, expect, it } from 'vitest' |
4 | | -import { collectAllKeybindings, filterCommandsByWhen, findCommandDeep, getShortcutRows, walkCommands } from './keybindings' |
| 4 | +import { collectAllKeybindings, filterCommandsByWhen, findCommandDeep, findKeybindingConflict, getShortcutRows, walkCommands } from './keybindings' |
| 5 | + |
| 6 | +describe('findKeybindingConflict', () => { |
| 7 | + const commands: DevframeCommandEntry[] = [ |
| 8 | + { id: 'settings', title: 'Open Settings', source: 'client' }, |
| 9 | + { |
| 10 | + id: 'dock-mode', |
| 11 | + title: 'Dock Mode', |
| 12 | + source: 'client', |
| 13 | + when: '!popupOpen', |
| 14 | + children: [ |
| 15 | + { id: 'float', title: 'Float Mode', source: 'client', when: '!popupOpen' }, |
| 16 | + ], |
| 17 | + }, |
| 18 | + { id: 'hidden', title: 'Hide', source: 'client', when: 'clientType == embedded' }, |
| 19 | + { id: 'disabled', title: 'Disabled', source: 'client', allowShortcuts: false }, |
| 20 | + ] |
| 21 | + |
| 22 | + it.each<[string, Pick<WhenContext, 'popupOpen' | 'clientType'>]>([ |
| 23 | + ['float', { popupOpen: true, clientType: 'embedded' }], |
| 24 | + ['hidden', { popupOpen: false, clientType: 'standalone' }], |
| 25 | + ])('warns about %s even when the current mode hides it', (id, context) => { |
| 26 | + const visibleRows = getShortcutRows(filterCommandsByWhen(commands, { |
| 27 | + ...context, |
| 28 | + dockOpen: true, |
| 29 | + paletteOpen: false, |
| 30 | + dockSelectedId: '', |
| 31 | + })) |
| 32 | + expect(visibleRows.some(row => row.command.id === id)).toBe(false) |
| 33 | + const getKeybindings = (commandId: string) => commandId === id ? [{ key: 'Mod+Shift+J' }] : [] |
| 34 | + expect(findKeybindingConflict(commands, 'settings', 'Mod+Shift+J', getKeybindings)?.id).toBe(id) |
| 35 | + }) |
| 36 | + |
| 37 | + it('ignores the edited command and commands that disallow shortcuts', () => { |
| 38 | + const getKeybindings = (id: string) => ['settings', 'disabled'].includes(id) ? [{ key: 'Mod+J' }] : [] |
| 39 | + expect(findKeybindingConflict(commands, 'settings', 'Mod+J', getKeybindings)).toBeUndefined() |
| 40 | + }) |
| 41 | + |
| 42 | + it('uses effective bindings, including cleared defaults and multiple bindings', () => { |
| 43 | + const commands: DevframeCommandEntry[] = [ |
| 44 | + { id: 'cleared', title: 'Cleared', source: 'client', keybindings: [{ key: 'Mod+J' }] }, |
| 45 | + { id: 'overridden', title: 'Overridden', source: 'client', keybindings: [{ key: 'Mod+K' }] }, |
| 46 | + ] |
| 47 | + const getKeybindings = (id: string) => id === 'overridden' ? [{ key: 'Mod+L' }, { key: 'Mod+J' }] : [] |
| 48 | + expect(findKeybindingConflict(commands, 'settings', 'Mod+J', getKeybindings)?.id).toBe('overridden') |
| 49 | + expect(findKeybindingConflict(commands, 'settings', 'Mod+K', getKeybindings)).toBeUndefined() |
| 50 | + }) |
| 51 | +}) |
5 | 52 |
|
6 | 53 | describe('getShortcutRows', () => { |
7 | 54 | it('omits opted-out commands while retaining their bindable descendants and palette-hidden commands', () => { |
|
0 commit comments