Files
opencode-manager/frontend/src/hooks/useCommands.test.tsx
T
Chris Scott 7e93eb4c34 feat(commands): align built-in slash commands with OpenCode v2
Replace the split built-in command name list and switch with one typed
registry, so a registered command without a handler is a compile error.
Remove the broken themes/share/unshare/editor entries and the hardcoded
init; server and custom commands now win on a name clash and built-ins
dispatch by registry identity.

Add /copy, /btw, /rename, /fork (message picker), /timeline, /model,
/agent, /variants, /mcp, /skills, /settings and /connect. Open the model
picker through state so /model and the select-model shortcut work while a
response is running, load full session history in the fork picker, and
keep attachments when a built-in command is submitted.

Extract shared clipboard and primary-agent helpers, reuse CopyButton in
the manager token settings, and update the Quick Start and chat docs.
2026-10-02 09:53:57 -04:00

108 lines
3.5 KiB
TypeScript

import { renderHook, waitFor } from '@testing-library/react'
import { describe, expect, it, vi, beforeEach } from 'vitest'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { useCommands } from './useCommands'
import { listCommands } from '../api/opencode'
import { BUILTIN_COMMANDS } from '@/lib/builtinCommands'
vi.mock('../api/opencode', () => ({
listCommands: vi.fn(),
}))
const createWrapper = () => {
const queryClient = new QueryClient({
defaultOptions: {
queries: { retry: false },
},
})
return ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
)
}
describe('useCommands', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('returns the built-in commands sorted by name when disabled', () => {
const { result } = renderHook(() => useCommands({ enabled: false }), { wrapper: createWrapper() })
const names = result.current.filterCommands('').map(command => command.name)
expect(names).toEqual([...names].sort((a, b) => a.localeCompare(b)))
for (const command of BUILTIN_COMMANDS) {
expect(names).toContain(command.name)
}
expect(names).not.toContain('themes')
expect(names).not.toContain('share')
expect(names).not.toContain('unshare')
expect(names).not.toContain('editor')
expect(listCommands).not.toHaveBeenCalled()
})
it('prioritizes exact and prefix matches before other matches', () => {
const { result } = renderHook(() => useCommands({ enabled: false }), { wrapper: createWrapper() })
expect(result.current.filterCommands('co').map(command => command.name)).toEqual([
'compact',
'connect',
'continue',
'copy',
])
expect(result.current.filterCommands('do').map(command => command.name)).toEqual([
'redo',
'undo',
])
})
it('sorts loaded custom commands with built-in commands', async () => {
vi.mocked(listCommands).mockResolvedValue([
{ name: 'zebra', description: 'Zebra' },
{ name: 'alpha', description: 'Alpha' },
])
const { result } = renderHook(() => useCommands({ directory: '/repo' }), { wrapper: createWrapper() })
await waitFor(() => {
expect(result.current.filterCommands('').map(command => command.name).slice(0, 3)).toEqual([
'agent',
'alpha',
'btw',
])
})
expect(listCommands).toHaveBeenCalledWith('/repo')
})
it('lets a loaded command override a built-in command with the same name', async () => {
vi.mocked(listCommands).mockResolvedValue([{ name: 'copy', description: 'server copy' }])
const { result } = renderHook(() => useCommands({ directory: '/repo' }), { wrapper: createWrapper() })
await waitFor(() => {
expect(result.current.filterCommands('copy')).toEqual([
{ name: 'copy', description: 'server copy' },
])
})
const [copy] = result.current.filterCommands('copy')
expect('action' in copy).toBe(false)
})
it('ranks an exact built-in match ahead of a server command that contains it', async () => {
vi.mocked(listCommands).mockResolvedValue([{ name: 'xundo', description: 'X undo' }])
const { result } = renderHook(() => useCommands({ directory: '/repo' }), { wrapper: createWrapper() })
await waitFor(() => {
expect(result.current.filterCommands('').some(command => command.name === 'xundo')).toBe(true)
})
expect(result.current.filterCommands('undo').map(command => command.name)).toEqual([
'undo',
'xundo',
])
})
})