|
1 | 1 | import type { DevToolsCommandEntry, DevToolsCommandKeybinding } from '@vitejs/devtools-kit' |
| 2 | +import type { WhenContext } from '../keybindings' |
2 | 3 | import { describe, expect, it } from 'vitest' |
3 | | -import { areKeybindingsEqual, collectAllKeybindings, formatKeybinding, isKeybindingOverrideDifferentFromDefault, KNOWN_BROWSER_SHORTCUTS, normalizeKeyEvent } from '../keybindings' |
| 4 | +import { areKeybindingsEqual, collectAllKeybindings, filterCommandsByWhen, formatKeybinding, isKeybindingOverrideDifferentFromDefault, KNOWN_BROWSER_SHORTCUTS, normalizeKeyEvent } from '../keybindings' |
4 | 5 |
|
5 | 6 | describe('formatKeybinding', () => { |
6 | 7 | it('splits key string into parts', () => { |
@@ -105,6 +106,78 @@ describe('collectAllKeybindings', () => { |
105 | 106 | }) |
106 | 107 | }) |
107 | 108 |
|
| 109 | +describe('filterCommandsByWhen', () => { |
| 110 | + function makeContext(overrides: Partial<WhenContext> = {}): WhenContext { |
| 111 | + return { |
| 112 | + clientType: 'embedded', |
| 113 | + dockOpen: false, |
| 114 | + paletteOpen: false, |
| 115 | + dockSelectedId: '', |
| 116 | + popupOpen: false, |
| 117 | + ...overrides, |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + function makeDockMode(): DevToolsCommandEntry[] { |
| 122 | + return [ |
| 123 | + { |
| 124 | + id: 'devtools:dock-mode', |
| 125 | + source: 'client' as const, |
| 126 | + title: 'Dock Mode', |
| 127 | + when: 'clientType == embedded && !popupOpen', |
| 128 | + children: [ |
| 129 | + { id: 'devtools:dock-mode:float', source: 'client' as const, title: 'Float Mode', when: '!popupOpen' }, |
| 130 | + { id: 'devtools:dock-mode:edge', source: 'client' as const, title: 'Edge Mode', when: '!popupOpen' }, |
| 131 | + ], |
| 132 | + }, |
| 133 | + ] as DevToolsCommandEntry[] |
| 134 | + } |
| 135 | + |
| 136 | + it('passes through commands without a when clause', () => { |
| 137 | + const commands = [ |
| 138 | + { id: 'cmd1', source: 'client' as const, title: 'Cmd 1' }, |
| 139 | + { id: 'cmd2', source: 'client' as const, title: 'Cmd 2' }, |
| 140 | + ] as DevToolsCommandEntry[] |
| 141 | + |
| 142 | + expect(filterCommandsByWhen(commands, makeContext())).toEqual(commands) |
| 143 | + }) |
| 144 | + |
| 145 | + it('drops a parent whose when clause fails, children included', () => { |
| 146 | + const result = filterCommandsByWhen(makeDockMode(), makeContext({ popupOpen: true })) |
| 147 | + expect(result).toHaveLength(0) |
| 148 | + }) |
| 149 | + |
| 150 | + it('keeps a passing parent but removes children whose own when clause fails', () => { |
| 151 | + const commands = [ |
| 152 | + { |
| 153 | + id: 'parent', |
| 154 | + source: 'client' as const, |
| 155 | + title: 'Parent', |
| 156 | + children: [ |
| 157 | + { id: 'parent:always', source: 'client' as const, title: 'Always' }, |
| 158 | + { id: 'parent:embedded', source: 'client' as const, title: 'Embedded only', when: 'clientType == embedded' }, |
| 159 | + ], |
| 160 | + }, |
| 161 | + ] as DevToolsCommandEntry[] |
| 162 | + |
| 163 | + const result = filterCommandsByWhen(commands, makeContext({ clientType: 'standalone' })) |
| 164 | + expect(result).toHaveLength(1) |
| 165 | + expect(result[0]!.children?.map(c => c.id)).toEqual(['parent:always']) |
| 166 | + }) |
| 167 | + |
| 168 | + it('keeps everything when the context satisfies every clause', () => { |
| 169 | + const result = filterCommandsByWhen(makeDockMode(), makeContext()) |
| 170 | + expect(result).toHaveLength(1) |
| 171 | + expect(result[0]!.children).toHaveLength(2) |
| 172 | + }) |
| 173 | + |
| 174 | + it('does not mutate the input commands', () => { |
| 175 | + const commands = makeDockMode() |
| 176 | + filterCommandsByWhen(commands, makeContext({ popupOpen: true })) |
| 177 | + expect(commands[0]!.children).toHaveLength(2) |
| 178 | + }) |
| 179 | +}) |
| 180 | + |
108 | 181 | describe('areKeybindingsEqual', () => { |
109 | 182 | it('treats undefined and empty arrays as equal', () => { |
110 | 183 | expect(areKeybindingsEqual(undefined, [])).toBe(true) |
|
0 commit comments