Files
opencode-manager/frontend/src/hooks/useGlobalShortcutActions.test.tsx
T
ioplex 4ec62cd4e7 fix: address findings from the full Docker evaluation of main
Critical and high:
- repos: unlinking a local repo removes only the Manager reference and alias,
  never the repository files; confirm button reads "Unlink"
- auth: rate-limit by the socket address; trust proxy headers only when
  AUTH_TRUSTED_PROXY_HEADERS is set (documented and forwarded in compose)
- composer: per-session in-memory drafts (text, bash mode, attachments);
  Escape no longer clears the prompt
- url params: compose same-tick updates so schedule dialogs stay closed after
  create/delete; delete dialog names the job

Medium:
- tool rows show failures (non-zero shell exit, failed exploration members)
- voice conversation shows the specific error and a starting state
- settings: Escape in dropdowns and the shortcut recorder no longer closes
  the dialog; mobile deep links open the requested section; one content tree
  per platform
- closing pushed sheets, settings, tool panels and schedule dialogs goes back
  in history
- login returns to the requested page; signed-out pages make no
  authenticated requests
- action terminals keep their output (Manager-side output capture and replay)
- unpushed-commit detection, commit message kept on failure
- binary files with unknown extensions are detected and not editable
- tool panel resizes when docked and floats as a dismissible sheet when the
  page is too narrow
- Docker image runs tini as PID 1

Low: not-found states, add-repo and branch validation errors (4xx),
search-aware empty states, mobile repo sort/filter/select cues, sidebar
resize affordance and collapse shortcut, mobile unread badge, preview port
filtering, terminal close handling, walkthrough empty state and helper
session filtering, placeholder contrast, permission labels and focus,
TTS task-list sanitizing, git identity email validation, quieter git
logging, hermetic worktree tests, and accessible names for icon-only
controls.
2026-10-23 11:42:37 -04:00

220 lines
7.1 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { act, render, screen, waitFor } from '@testing-library/react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { MemoryRouter, useLocation } from 'react-router-dom'
import { KeyboardShortcutsProvider } from '@/contexts/KeyboardShortcutsContext'
import { useGlobalShortcutActions } from './useGlobalShortcutActions'
const mocks = vi.hoisted(() => ({
getRepo: vi.fn(),
getSession: vi.fn(),
createSession: vi.fn(),
useMobile: vi.fn(),
}))
vi.mock('@/hooks/useSettings', () => ({
useSettings: () => ({
preferences: { leaderKey: 'Ctrl+O', keyboardShortcuts: {}, directShortcuts: undefined },
}),
}))
vi.mock('@/hooks/useMobile', () => ({
useMobile: mocks.useMobile,
}))
vi.mock('@/api/repos', () => ({
getRepo: mocks.getRepo,
}))
vi.mock('@/api/opencode', () => ({
createSession: mocks.createSession,
getSession: mocks.getSession,
}))
function Harness() {
useGlobalShortcutActions()
return null
}
function LocationDisplay() {
const location = useLocation()
return <div data-testid="location">{`${location.pathname}${location.search}`}</div>
}
function pressKey(init: KeyboardEventInit) {
const event = new KeyboardEvent('keydown', { bubbles: true, cancelable: true, ...init })
act(() => {
document.body.dispatchEvent(event)
})
return event
}
function pressLeaderFollowUp(key: string) {
pressKey({ key: 'o', ctrlKey: true })
pressKey({ key })
}
describe('useGlobalShortcutActions', () => {
let queryClient: QueryClient
beforeEach(() => {
vi.clearAllMocks()
localStorage.clear()
queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
mocks.useMobile.mockReturnValue(false)
mocks.getRepo.mockResolvedValue({ id: 5, fullPath: '/repo/five' })
mocks.getSession.mockResolvedValue({ id: 'ses_1', location: { directory: '/srv/kitchen' } })
mocks.createSession.mockResolvedValue({ id: 'session-new' })
})
afterEach(() => {
queryClient.clear()
})
function renderHarness(initialEntry: string) {
render(
<QueryClientProvider client={queryClient}>
<KeyboardShortcutsProvider>
<MemoryRouter initialEntries={[initialEntry]}>
<Harness />
<LocationDisplay />
</MemoryRouter>
</KeyboardShortcutsProvider>
</QueryClientProvider>,
)
return {
location: () => screen.getByTestId('location').textContent,
}
}
it('opens the terminal dialog on the assistant host from the root route', async () => {
const { location } = renderHarness('/')
pressLeaderFollowUp('t')
await waitFor(() => expect(location()).toBe('/assistant?dialog=terminal'))
})
it('navigates to the nearest repo host from a repo sub-route without the tool', async () => {
const { location } = renderHarness('/repos/5/schedules')
pressLeaderFollowUp('t')
await waitFor(() => expect(location()).toBe('/repos/5?dialog=terminal'))
})
it('toggles the docked panel on and off when the route hosts the tool', async () => {
const { location } = renderHarness('/repos/5')
pressLeaderFollowUp('t')
await waitFor(() => expect(location()).toBe('/repos/5?panel=terminal'))
pressLeaderFollowUp('t')
await waitFor(() => expect(location()).toBe('/repos/5'))
})
it('toggles the dialog instead of the panel on mobile', async () => {
mocks.useMobile.mockReturnValue(true)
const { location } = renderHarness('/repos/5')
pressLeaderFollowUp('t')
await waitFor(() => expect(location()).toBe('/repos/5?dialog=terminal'))
})
it('creates a session in the repo directory and navigates to it from the leader shortcut', async () => {
queryClient.setQueryData(['repo', 5], { id: 5, fullPath: '/repo/five' })
const { location } = renderHarness('/repos/5')
pressKey({ key: 'o', ctrlKey: true })
pressKey({ key: 'n' })
await waitFor(() => expect(mocks.createSession).toHaveBeenCalledWith({ directory: '/repo/five' }, 'local'))
await waitFor(() => expect(location()).toBe('/repos/5/sessions/session-new'))
})
it('toggles a linked-server route tool without falling back to a Manager host', async () => {
const { location } = renderHarness('/servers/srv_kitchen/sessions/ses_1')
pressLeaderFollowUp('t')
await waitFor(() => expect(location()).toBe('/servers/srv_kitchen/sessions/ses_1?panel=terminal'))
pressLeaderFollowUp('t')
await waitFor(() => expect(location()).toBe('/servers/srv_kitchen/sessions/ses_1'))
expect(mocks.getRepo).not.toHaveBeenCalled()
})
it('toggles the navigation sidebar instead of the Files panel from the sidebar shortcut', async () => {
const { location } = renderHarness('/repos/5')
pressLeaderFollowUp('b')
await waitFor(() => expect(localStorage.getItem('oc:sidebar:collapsed')).toBe('true'))
expect(location()).toBe('/repos/5')
pressLeaderFollowUp('b')
await waitFor(() => expect(localStorage.getItem('oc:sidebar:collapsed')).toBe('false'))
expect(location()).toBe('/repos/5')
})
it('does not change the persisted sidebar state from the sidebar shortcut on mobile', async () => {
mocks.useMobile.mockReturnValue(true)
const { location } = renderHarness('/repos/5')
pressLeaderFollowUp('b')
await act(async () => {
await Promise.resolve()
})
expect(localStorage.getItem('oc:sidebar:collapsed')).toBeNull()
expect(location()).toBe('/repos/5')
})
it('does not fall back to a Manager host for a tool the linked-server route lacks', async () => {
const { location } = renderHarness('/servers/srv_kitchen/sessions/ses_1')
pressLeaderFollowUp('b')
await waitFor(() => expect(localStorage.getItem('oc:sidebar:collapsed')).toBe('true'))
expect(location()).toBe('/servers/srv_kitchen/sessions/ses_1')
expect(mocks.getRepo).not.toHaveBeenCalled()
})
it('toggles the terminal dialog on a linked-server route on mobile', async () => {
mocks.useMobile.mockReturnValue(true)
const { location } = renderHarness('/servers/srv_kitchen/sessions/ses_1')
pressLeaderFollowUp('t')
await waitFor(() => expect(location()).toBe('/servers/srv_kitchen/sessions/ses_1?dialog=terminal'))
})
it('creates a session in the linked server directory and navigates to the server session path', async () => {
const { location } = renderHarness('/servers/srv_kitchen/sessions/ses_1')
await waitFor(() => expect(mocks.getSession).toHaveBeenCalledWith('ses_1', 'srv_kitchen'))
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 0))
})
pressKey({ key: 'o', ctrlKey: true })
pressKey({ key: 'n' })
await waitFor(() => expect(mocks.createSession).toHaveBeenCalledWith({ directory: '/srv/kitchen' }, 'srv_kitchen'))
await waitFor(() => expect(location()).toBe('/servers/srv_kitchen/sessions/session-new'))
})
it('does not swallow the follow-up key when the route has no new-session host', async () => {
renderHarness('/')
pressKey({ key: 'o', ctrlKey: true })
const followUp = pressKey({ key: 'n' })
expect(followUp.defaultPrevented).toBe(false)
expect(mocks.createSession).not.toHaveBeenCalled()
})
})