-
Notifications
You must be signed in to change notification settings - Fork 140
feat(command-palette): jump to agents and sessions by name #358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kimbotai1337
wants to merge
3
commits into
next
Choose a base branch
from
feat/issue-180-command-palette-sessions
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,260 @@ | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
| import { render, screen, fireEvent, act } from '@testing-library/react'; | ||
| import { CommandPalette } from './CommandPalette'; | ||
| import type { Command } from './types'; | ||
|
|
||
| function staticCommand(id: string, label: string, action = vi.fn()): Command { | ||
| return { id, label, action, category: 'actions' }; | ||
| } | ||
|
|
||
| function sessionCommand( | ||
| sessionKey: string, | ||
| label: string, | ||
| opts: { isActive?: boolean; keywords?: string[]; action?: ReturnType<typeof vi.fn> } = {}, | ||
| ): Command { | ||
| return { | ||
| id: `session-${sessionKey}`, | ||
| label, | ||
| action: opts.action ?? vi.fn(), | ||
| category: 'sessions', | ||
| keywords: opts.keywords, | ||
| isActive: opts.isActive ?? false, | ||
| }; | ||
| } | ||
|
|
||
| describe('CommandPalette session entries', () => { | ||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| Object.defineProperty(HTMLElement.prototype, 'scrollIntoView', { | ||
| configurable: true, | ||
| value: vi.fn(), | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| function renderPalette(commands: Command[], onClose = vi.fn()) { | ||
| const result = render(<CommandPalette open commands={commands} onClose={onClose} />); | ||
| act(() => { | ||
| vi.advanceTimersByTime(100); | ||
| }); | ||
| return { ...result, onClose }; | ||
| } | ||
|
|
||
| function input() { | ||
| return screen.getByPlaceholderText(/search actions/i) as HTMLInputElement; | ||
| } | ||
|
|
||
| it('renders a Sessions section header when session commands are present', () => { | ||
| const commands = [ | ||
| staticCommand('search', 'Search messages'), | ||
| sessionCommand('agent:main:main', 'Main'), | ||
| ]; | ||
| renderPalette(commands); | ||
| expect(screen.getByText('Sessions')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('narrows results to matching session commands when typing', () => { | ||
| const commands = [ | ||
| staticCommand('search', 'Search messages'), | ||
| staticCommand('reset', 'Reset session'), | ||
| sessionCommand('agent:reviewer:main', 'Reviewer Bot', { | ||
| keywords: ['Reviewer Bot', 'reviewer', 'agent:reviewer:main'], | ||
| }), | ||
| sessionCommand('agent:planner:main', 'Planner Bot', { | ||
| keywords: ['Planner Bot', 'planner', 'agent:planner:main'], | ||
| }), | ||
| ]; | ||
| renderPalette(commands); | ||
| fireEvent.change(input(), { target: { value: 'reviewer' } }); | ||
| expect(screen.getByText('Reviewer Bot')).toBeInTheDocument(); | ||
| expect(screen.queryByText('Planner Bot')).not.toBeInTheDocument(); | ||
| expect(screen.queryByText('Search messages')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('fires the highlighted session action exactly once when Enter is pressed', () => { | ||
| const action = vi.fn(); | ||
| const commands = [ | ||
| sessionCommand('agent:reviewer:main', 'Reviewer Bot', { | ||
| keywords: ['reviewer'], | ||
| action, | ||
| }), | ||
| ]; | ||
| const { onClose } = renderPalette(commands); | ||
| fireEvent.change(input(), { target: { value: 'reviewer' } }); | ||
| fireEvent.keyDown(input(), { key: 'Enter' }); | ||
| expect(onClose).toHaveBeenCalledTimes(1); | ||
| act(() => { vi.advanceTimersByTime(100); }); | ||
| expect(action).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('Esc closes the palette without firing any action', () => { | ||
| const action = vi.fn(); | ||
| const commands = [ | ||
| sessionCommand('agent:reviewer:main', 'Reviewer Bot', { action }), | ||
| ]; | ||
| const { onClose } = renderPalette(commands); | ||
| fireEvent.keyDown(input(), { key: 'Escape' }); | ||
| expect(onClose).toHaveBeenCalledTimes(1); | ||
| expect(action).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('ArrowDown moves selection forward one step and Enter fires that item', () => { | ||
| const sessionAction = vi.fn(); | ||
| const staticAction = vi.fn(); | ||
| const commands = [ | ||
| staticCommand('toggle-log', 'Toggle Log Panel', staticAction), | ||
| sessionCommand('agent:reviewer:main', 'Reviewer Bot', { | ||
| keywords: ['Reviewer'], | ||
| action: sessionAction, | ||
| }), | ||
| ]; | ||
| renderPalette(commands); | ||
| // Sessions group sorts before actions per CATEGORY_ORDER, so index 0 is | ||
| // Reviewer Bot and ArrowDown advances to index 1, Toggle Log Panel. | ||
| fireEvent.keyDown(input(), { key: 'ArrowDown' }); | ||
| fireEvent.keyDown(input(), { key: 'Enter' }); | ||
| act(() => { vi.advanceTimersByTime(100); }); | ||
| expect(staticAction).toHaveBeenCalledTimes(1); | ||
| expect(sessionAction).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('ArrowDown then ArrowUp returns to the previous selection', () => { | ||
| const sessionAction = vi.fn(); | ||
| const staticAction = vi.fn(); | ||
| const commands = [ | ||
| staticCommand('toggle-log', 'Toggle Log Panel', staticAction), | ||
| sessionCommand('agent:reviewer:main', 'Reviewer Bot', { | ||
| keywords: ['Reviewer'], | ||
| action: sessionAction, | ||
| }), | ||
| ]; | ||
| renderPalette(commands); | ||
| fireEvent.keyDown(input(), { key: 'ArrowDown' }); | ||
| fireEvent.keyDown(input(), { key: 'ArrowUp' }); | ||
| fireEvent.keyDown(input(), { key: 'Enter' }); | ||
| act(() => { vi.advanceTimersByTime(100); }); | ||
| expect(sessionAction).toHaveBeenCalledTimes(1); | ||
| expect(staticAction).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('marks the active session with aria-current and a "current" badge', () => { | ||
| const commands = [ | ||
| sessionCommand('agent:main:main', 'Main', { isActive: true }), | ||
| sessionCommand('agent:reviewer:main', 'Reviewer'), | ||
| ]; | ||
| renderPalette(commands); | ||
| const mainButton = screen.getByText('Main').closest('button')!; | ||
| const reviewerButton = screen.getByText('Reviewer').closest('button')!; | ||
| expect(mainButton).toHaveAttribute('aria-current', 'true'); | ||
| expect(reviewerButton).not.toHaveAttribute('aria-current'); | ||
| const badges = screen.getAllByText('current'); | ||
| expect(badges).toHaveLength(1); | ||
| expect(mainButton).toContainElement(badges[0]); | ||
| }); | ||
|
|
||
| it('does not render the active affordance for a non-session command with isActive', () => { | ||
| const cmd: Command = { | ||
| id: 'rogue', | ||
| label: 'Rogue Command', | ||
| action: vi.fn(), | ||
| category: 'actions', | ||
| isActive: true, | ||
| }; | ||
| renderPalette([cmd]); | ||
| const button = screen.getByText('Rogue Command').closest('button')!; | ||
| expect(button).not.toHaveAttribute('aria-current'); | ||
| expect(screen.queryByText('current')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('clamps selectedIndex when filtered commands shrink below the cursor', () => { | ||
| const first = vi.fn(); | ||
| const second = vi.fn(); | ||
| const onClose = vi.fn(); | ||
| const commands: Command[] = [ | ||
| sessionCommand('agent:alpha:main', 'Alpha', { action: first }), | ||
| sessionCommand('agent:beta:main', 'Beta', { action: second }), | ||
| sessionCommand('agent:gamma:main', 'Gamma', { action: vi.fn() }), | ||
| ]; | ||
| const { rerender } = render(<CommandPalette open commands={commands} onClose={onClose} />); | ||
| act(() => { vi.advanceTimersByTime(100); }); | ||
| // Move cursor to the third entry. | ||
| fireEvent.keyDown(input(), { key: 'ArrowDown' }); | ||
| fireEvent.keyDown(input(), { key: 'ArrowDown' }); | ||
| // Shrink filtered list without typing so the clamp effect is the only | ||
| // path that can bring selectedIndex back into range. | ||
| rerender(<CommandPalette open commands={[commands[0]]} onClose={onClose} />); | ||
| fireEvent.keyDown(input(), { key: 'Enter' }); | ||
| act(() => { vi.advanceTimersByTime(100); }); | ||
| expect(first).toHaveBeenCalledTimes(1); | ||
| expect(second).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('ArrowDown on an empty filtered list does not produce a negative selectedIndex', () => { | ||
| const fallback = vi.fn(); | ||
| const onClose = vi.fn(); | ||
| const { rerender } = render( | ||
| <CommandPalette open commands={[] as Command[]} onClose={onClose} />, | ||
| ); | ||
| act(() => { vi.advanceTimersByTime(100); }); | ||
| fireEvent.keyDown(input(), { key: 'ArrowDown' }); | ||
| fireEvent.keyDown(input(), { key: 'Enter' }); | ||
| // Enter while filtered is empty must be a no-op (no action fires, palette | ||
| // stays open). | ||
| expect(onClose).not.toHaveBeenCalled(); | ||
| // When commands arrive, the first item must be reachable on Enter without | ||
| // an extra ArrowDown (proving selectedIndex was clamped, not stuck at -1). | ||
| const next = [sessionCommand('agent:alpha:main', 'Alpha', { action: fallback })]; | ||
| rerender(<CommandPalette open commands={next} onClose={onClose} />); | ||
| fireEvent.keyDown(input(), { key: 'Enter' }); | ||
| act(() => { vi.advanceTimersByTime(100); }); | ||
| expect(fallback).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('rapid double-Enter only fires the most-recent action once', () => { | ||
| const first = vi.fn(); | ||
| const second = vi.fn(); | ||
| const commands = [ | ||
| sessionCommand('agent:alpha:main', 'Alpha', { keywords: ['Alpha'], action: first }), | ||
| sessionCommand('agent:beta:main', 'Beta', { keywords: ['Beta'], action: second }), | ||
| ]; | ||
| const { onClose } = renderPalette(commands); | ||
| fireEvent.keyDown(input(), { key: 'Enter' }); | ||
| fireEvent.keyDown(input(), { key: 'ArrowDown' }); | ||
| fireEvent.keyDown(input(), { key: 'Enter' }); | ||
| act(() => { vi.advanceTimersByTime(100); }); | ||
| expect(first).not.toHaveBeenCalled(); | ||
| expect(second).toHaveBeenCalledTimes(1); | ||
| expect(onClose).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('a new command surfaced via parent re-render is searchable without remount', () => { | ||
| const initial = [sessionCommand('agent:main:main', 'Main')]; | ||
| const { rerender } = render( | ||
| <CommandPalette open commands={initial} onClose={vi.fn()} />, | ||
| ); | ||
| act(() => { vi.advanceTimersByTime(100); }); | ||
|
|
||
| const next = [ | ||
| sessionCommand('agent:main:main', 'Main'), | ||
| sessionCommand('agent:newcomer:main', 'Newcomer', { | ||
| keywords: ['Newcomer', 'newcomer', 'agent:newcomer:main'], | ||
| }), | ||
| ]; | ||
| rerender(<CommandPalette open commands={next} onClose={vi.fn()} />); | ||
| fireEvent.change(input(), { target: { value: 'newcomer' } }); | ||
| expect(screen.getByText('Newcomer')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('shows the empty-state copy when no command matches', () => { | ||
| const commands = [ | ||
| staticCommand('search', 'Search messages'), | ||
| sessionCommand('agent:main:main', 'Main'), | ||
| ]; | ||
| renderPalette(commands); | ||
| fireEvent.change(input(), { target: { value: 'zzzzzzz-no-match' } }); | ||
| expect(screen.getByText(/no matching command/i)).toBeInTheDocument(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.