Files
opencode-manager/frontend/src/components/session/PermissionModeToggle.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

122 lines
4.5 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { PermissionModeToggle } from './PermissionModeToggle'
import { useSessionPermissionMode, useSetSessionPermissionMode } from '@/hooks/useSessionPermissionMode'
import type { SessionPermissionModeState } from '@opencode-manager/shared/schemas'
vi.mock('@/hooks/useSessionPermissionMode')
const baseState: SessionPermissionModeState = {
sessionId: 'ses_1',
rootSessionId: 'ses_1',
mode: 'ask',
lockedReason: null,
}
function mockHooks(overrides: {
data?: SessionPermissionModeState | undefined
state?: Partial<SessionPermissionModeState>
mutate?: ReturnType<typeof vi.fn>
isPending?: boolean
isError?: boolean
} = {}) {
const data = 'data' in overrides ? overrides.data : { ...baseState, ...overrides.state }
vi.mocked(useSessionPermissionMode).mockReturnValue({
data,
isError: overrides.isError ?? false,
} as ReturnType<typeof useSessionPermissionMode>)
vi.mocked(useSetSessionPermissionMode).mockReturnValue({
mutate: overrides.mutate ?? vi.fn(),
isPending: overrides.isPending ?? false,
} as unknown as ReturnType<typeof useSetSessionPermissionMode>)
}
describe('PermissionModeToggle', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('renders the ask state', () => {
mockHooks()
render(<PermissionModeToggle sessionID="ses_1" directory="/repo" />)
expect(screen.getByRole('button', { name: 'Permissions: follow configured rules' })).toBeEnabled()
})
it('switches to accept-everything with the directory when clicked', async () => {
const user = userEvent.setup()
const mutate = vi.fn()
mockHooks({ mutate })
render(<PermissionModeToggle sessionID="ses_1" directory="/repo" />)
await user.click(screen.getByRole('button', { name: 'Permissions: follow configured rules' }))
expect(mutate).toHaveBeenCalledWith({ directory: '/repo', mode: 'auto' })
})
it('switches back to ask when already accepting everything', async () => {
const user = userEvent.setup()
const mutate = vi.fn()
mockHooks({ state: { mode: 'auto' }, mutate })
render(<PermissionModeToggle sessionID="ses_1" directory="/repo" />)
await user.click(screen.getByRole('button', { name: 'Permissions: auto-accept all' }))
expect(mutate).toHaveBeenCalledWith({ directory: '/repo', mode: 'ask' })
})
it('is disabled and labelled as locked for child sessions', () => {
mockHooks({ state: { lockedReason: 'child', mode: 'auto' } })
render(<PermissionModeToggle sessionID="ses_child" directory="/repo" />)
const button = screen.getByRole('button', { name: 'Inherited from parent session' })
expect(button).toBeDisabled()
})
it('is disabled and labelled for schedule-run sessions', () => {
mockHooks({ state: { lockedReason: 'schedule', mode: 'ask' } })
render(<PermissionModeToggle sessionID="ses_sched" directory="/repo" />)
const button = screen.getByRole('button', { name: "Scheduled runs use the schedule's permission configuration" })
expect(button).toBeDisabled()
})
it('cannot mutate while the mode read is still loading', async () => {
const user = userEvent.setup()
const mutate = vi.fn()
mockHooks({ data: undefined, mutate })
render(<PermissionModeToggle sessionID="ses_1" directory="/repo" />)
const button = screen.getByRole('button', { name: 'Permissions: loading' })
expect(button).toBeDisabled()
await user.click(button)
expect(mutate).not.toHaveBeenCalled()
})
it('cannot mutate and reports unavailable when the mode read failed', async () => {
const user = userEvent.setup()
const mutate = vi.fn()
mockHooks({ data: undefined, isError: true, mutate })
render(<PermissionModeToggle sessionID="ses_1" directory="/repo" />)
const button = screen.getByRole('button', { name: 'Permissions: unavailable' })
expect(button).toBeDisabled()
await user.click(button)
expect(mutate).not.toHaveBeenCalled()
})
it('switches a loaded auto mode back to ask when clicked', async () => {
const user = userEvent.setup()
const mutate = vi.fn()
mockHooks({ data: { ...baseState, mode: 'auto' }, mutate })
render(<PermissionModeToggle sessionID="ses_1" directory="/repo" />)
await user.click(screen.getByRole('button', { name: 'Permissions: auto-accept all' }))
expect(mutate).toHaveBeenCalledWith({ directory: '/repo', mode: 'ask' })
})
})