mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
Replace the plaintext pairing exchange with a nonce and HMAC proof plus AES-256-GCM encrypted credentials, bumping links to v2. Track session ownership per linked server so activity, session picker, transcript, MCP and skills views work against remote servers. Share encrypted secret access, event routing and URL helpers across backend, shared and CLI.
195 lines
7.1 KiB
TypeScript
195 lines
7.1 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'
|
|
import { OpenCodeServerProvider } from '@/contexts/OpenCodeServerContext'
|
|
import { useRecentCommandsStore } from '@/stores/recentCommandsStore'
|
|
|
|
vi.mock('../api/opencode', () => ({
|
|
listCommands: vi.fn(),
|
|
}))
|
|
|
|
const createWrapper = (serverId?: string) => {
|
|
const queryClient = new QueryClient({
|
|
defaultOptions: {
|
|
queries: { retry: false },
|
|
},
|
|
})
|
|
return ({ children }: { children: React.ReactNode }) => (
|
|
<QueryClientProvider client={queryClient}>
|
|
{serverId ? <OpenCodeServerProvider serverId={serverId}>{children}</OpenCodeServerProvider> : children}
|
|
</QueryClientProvider>
|
|
)
|
|
}
|
|
|
|
describe('useCommands', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
useRecentCommandsStore.setState({ names: [] })
|
|
})
|
|
|
|
it('returns the built-in commands sorted by name when disabled', () => {
|
|
const { result } = renderHook(() => useCommands({ enabled: false }), { wrapper: createWrapper() })
|
|
|
|
const names = result.current.searchCommands('').map(({ item }) => item.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.searchCommands('co').map(({ item }) => item.name)).toEqual([
|
|
'compact',
|
|
'connect',
|
|
'continue',
|
|
'copy',
|
|
])
|
|
expect(result.current.searchCommands('do').map(({ item }) => item.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.searchCommands('').map(({ item }) => item.name).slice(0, 3)).toEqual([
|
|
'agent',
|
|
'alpha',
|
|
'btw',
|
|
])
|
|
})
|
|
|
|
expect(listCommands).toHaveBeenCalledWith('/repo', 'local')
|
|
})
|
|
|
|
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.searchCommands('copy').map(({ item }) => item)).toEqual([
|
|
{ name: 'copy', description: 'server copy' },
|
|
])
|
|
})
|
|
|
|
const copy = result.current.findCommand('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.commands.some(command => command.name === 'xundo')).toBe(true)
|
|
})
|
|
|
|
expect(result.current.searchCommands('undo').map(({ item }) => item.name)).toEqual([
|
|
'undo',
|
|
'xundo',
|
|
'redo',
|
|
])
|
|
})
|
|
|
|
it('lists recent commands first for an empty query and uses recency to break ties', () => {
|
|
useRecentCommandsStore.setState({ names: ['copy', 'undo'] })
|
|
const { result } = renderHook(() => useCommands({ enabled: false }), { wrapper: createWrapper() })
|
|
|
|
expect(result.current.searchCommands('').map(({ item }) => item.name).slice(0, 2)).toEqual(['copy', 'undo'])
|
|
expect(result.current.searchCommands('co').map(({ item }) => item.name)).toEqual([
|
|
'copy',
|
|
'compact',
|
|
'connect',
|
|
'continue',
|
|
])
|
|
})
|
|
|
|
it('matches command descriptions after name matches', async () => {
|
|
vi.mocked(listCommands).mockResolvedValue([{ name: 'ship', description: 'Deploy a preview build' }])
|
|
const { result } = renderHook(() => useCommands({ directory: '/repo' }), { wrapper: createWrapper() })
|
|
|
|
await waitFor(() => {
|
|
expect(result.current.searchCommands('preview').map(({ item }) => item.name)).toEqual(['ship'])
|
|
})
|
|
})
|
|
|
|
it('loads commands for a newly selected directory even when queries have a stale time', async () => {
|
|
vi.mocked(listCommands).mockImplementation(async (directory) => [
|
|
{ name: directory === '/repo-b' ? 'from-b' : 'from-a', description: '' },
|
|
])
|
|
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false, staleTime: 60_000 } } })
|
|
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
|
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
|
)
|
|
|
|
const { result, rerender } = renderHook(({ directory }) => useCommands({ directory }), {
|
|
wrapper,
|
|
initialProps: { directory: '/repo-a' },
|
|
})
|
|
await waitFor(() => expect(result.current.findCommand('from-a')).toBeDefined())
|
|
|
|
rerender({ directory: '/repo-b' })
|
|
|
|
await waitFor(() => expect(result.current.findCommand('from-b')).toBeDefined())
|
|
expect(listCommands).toHaveBeenCalledWith('/repo-b', 'local')
|
|
})
|
|
|
|
it('finds only exact command names, ignoring case', () => {
|
|
const { result } = renderHook(() => useCommands({ enabled: false }), { wrapper: createWrapper() })
|
|
|
|
expect(result.current.findCommand('UNDO')?.name).toBe('undo')
|
|
expect(result.current.findCommand('und')).toBeUndefined()
|
|
})
|
|
|
|
it('keeps remote-capable built-ins and omits Manager-only ones for a linked server', () => {
|
|
const { result } = renderHook(() => useCommands({ enabled: false }), {
|
|
wrapper: createWrapper('srv_kitchen'),
|
|
})
|
|
|
|
const names = result.current.searchCommands('').map(({ item }) => item.name)
|
|
|
|
expect(names).not.toContain('sessions')
|
|
expect(names).not.toContain('resume')
|
|
expect(names).not.toContain('continue')
|
|
expect(names).not.toContain('walkthrough')
|
|
expect(names).toContain('mcp')
|
|
expect(names).toContain('skills')
|
|
expect(names).toContain('models')
|
|
expect(names).toContain('compact')
|
|
expect(result.current.findCommand('mcp')).toBeDefined()
|
|
})
|
|
|
|
it('keeps Manager-only built-ins for the local server', () => {
|
|
const { result } = renderHook(() => useCommands({ enabled: false }), {
|
|
wrapper: createWrapper('local'),
|
|
})
|
|
|
|
const names = result.current.searchCommands('').map(({ item }) => item.name)
|
|
|
|
expect(names).toContain('sessions')
|
|
expect(names).toContain('mcp')
|
|
expect(names).toContain('skills')
|
|
expect(names).toContain('walkthrough')
|
|
})
|
|
})
|