mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Per-session permission modes: each root session runs in ask or auto. Auto answers each permission request once from the backend, child sessions inherit their root's mode, forks copy the source's mode, and scheduled runs and agent-created sessions stay on ask. A lock reason reports why a session's mode cannot change. Session goals: arm a goal in the composer and a backend audit loop keeps the session working until an auditor model returns done or blocked. The loop is bounded by continuation and token limits, survives restarts, and pushes an outcome notification. Goals cannot start on scheduled-run or child sessions. ocm session tool: allow-listed internal routes to list, create, follow up on, read replies from, and fork sessions. Created and forked sessions are pinned to ask, listing covers a repo's workspaces, and replies wait for the session to settle with a capped response. Multi-run: launch one prompt on up to five models, optionally each in its own OpenCode workspace, with per-entry status and discard. Also add the shared owners this group needed (session launcher, session reply, workspace create/remove, multi-run storage) and update the feature docs.
145 lines
4.6 KiB
TypeScript
145 lines
4.6 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 { SessionGoalBar } from './SessionGoalBar'
|
|
import {
|
|
useCancelSessionGoal,
|
|
usePauseSessionGoal,
|
|
useResumeSessionGoal,
|
|
useSessionGoal,
|
|
} from '@/hooks/useSessionGoals'
|
|
import type { SessionGoal } from '@opencode-manager/shared/schemas'
|
|
|
|
vi.mock('@/hooks/useSessionGoals')
|
|
|
|
const baseGoal: SessionGoal = {
|
|
id: 1,
|
|
sessionId: 'ses_1',
|
|
directory: '/repo',
|
|
objective: 'Ship the feature',
|
|
status: 'active',
|
|
stopReason: null,
|
|
turnState: 'running',
|
|
continuationCount: 2,
|
|
maxContinuations: 20,
|
|
tokenBudget: null,
|
|
tokensUsed: 0,
|
|
consecutiveBlocked: 0,
|
|
lastVerdict: null,
|
|
lastReason: null,
|
|
createdAt: 1,
|
|
updatedAt: 1,
|
|
finishedAt: null,
|
|
}
|
|
|
|
function mockGoals(
|
|
goal: SessionGoal | null,
|
|
overrides: {
|
|
pause?: ReturnType<typeof vi.fn>
|
|
resume?: ReturnType<typeof vi.fn>
|
|
cancel?: ReturnType<typeof vi.fn>
|
|
} = {},
|
|
) {
|
|
vi.mocked(useSessionGoal).mockReturnValue({ data: goal } as ReturnType<typeof useSessionGoal>)
|
|
vi.mocked(usePauseSessionGoal).mockReturnValue({
|
|
mutate: overrides.pause ?? vi.fn(),
|
|
isPending: false,
|
|
} as unknown as ReturnType<typeof usePauseSessionGoal>)
|
|
vi.mocked(useResumeSessionGoal).mockReturnValue({
|
|
mutate: overrides.resume ?? vi.fn(),
|
|
isPending: false,
|
|
} as unknown as ReturnType<typeof useResumeSessionGoal>)
|
|
vi.mocked(useCancelSessionGoal).mockReturnValue({
|
|
mutate: overrides.cancel ?? vi.fn(),
|
|
isPending: false,
|
|
} as unknown as ReturnType<typeof useCancelSessionGoal>)
|
|
}
|
|
|
|
describe('SessionGoalBar', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it('renders nothing when there is no goal', () => {
|
|
mockGoals(null)
|
|
const { container } = render(<SessionGoalBar sessionID="ses_1" />)
|
|
expect(container).toBeEmptyDOMElement()
|
|
})
|
|
|
|
it('shows the turn counter and pauses the active goal', async () => {
|
|
const user = userEvent.setup()
|
|
const pause = vi.fn()
|
|
mockGoals({ ...baseGoal }, { pause })
|
|
render(<SessionGoalBar sessionID="ses_1" />)
|
|
|
|
expect(screen.getByText('Goal active')).toBeInTheDocument()
|
|
expect(screen.getByText('Turn 2/20')).toBeInTheDocument()
|
|
expect(screen.getByText('Ship the feature')).toBeInTheDocument()
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Pause' }))
|
|
|
|
expect(pause).toHaveBeenCalledWith(1)
|
|
})
|
|
|
|
it('shows token usage when a budget is set', () => {
|
|
mockGoals({ ...baseGoal, tokenBudget: 1000, tokensUsed: 250 })
|
|
render(<SessionGoalBar sessionID="ses_1" />)
|
|
|
|
expect(screen.getByText('250/1,000 tokens')).toBeInTheDocument()
|
|
})
|
|
|
|
it('resumes a paused goal', async () => {
|
|
const user = userEvent.setup()
|
|
const resume = vi.fn()
|
|
mockGoals({ ...baseGoal, status: 'paused' }, { resume })
|
|
render(<SessionGoalBar sessionID="ses_1" />)
|
|
|
|
expect(screen.getByText('Goal paused')).toBeInTheDocument()
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Resume' }))
|
|
|
|
expect(resume).toHaveBeenCalledWith(1)
|
|
})
|
|
|
|
it('shows a terminal summary and can be dismissed', async () => {
|
|
const user = userEvent.setup()
|
|
mockGoals({ ...baseGoal, status: 'completed', stopReason: 'continuation_limit' })
|
|
render(<SessionGoalBar sessionID="ses_1" />)
|
|
|
|
expect(screen.getByText('Goal completed')).toBeInTheDocument()
|
|
expect(screen.getByText('Continuation limit reached')).toBeInTheDocument()
|
|
expect(screen.queryByRole('button', { name: 'Pause' })).not.toBeInTheDocument()
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Dismiss' }))
|
|
|
|
expect(screen.queryByText('Goal completed')).not.toBeInTheDocument()
|
|
})
|
|
|
|
it('shows the shared stop-reason wording for a user-paused goal', () => {
|
|
mockGoals({ ...baseGoal, status: 'stopped', stopReason: 'user_paused' })
|
|
render(<SessionGoalBar sessionID="ses_1" />)
|
|
|
|
expect(screen.getByText('Goal stopped')).toBeInTheDocument()
|
|
expect(screen.getByText('Paused by user')).toBeInTheDocument()
|
|
})
|
|
|
|
it('shows the shared stop-reason wording for an audit failure', () => {
|
|
mockGoals({ ...baseGoal, status: 'blocked', stopReason: 'audit_failed' })
|
|
render(<SessionGoalBar sessionID="ses_1" />)
|
|
|
|
expect(screen.getByText('Goal blocked')).toBeInTheDocument()
|
|
expect(screen.getByText('Audit failed')).toBeInTheDocument()
|
|
})
|
|
|
|
it('cancels the goal through the cancel API', async () => {
|
|
const user = userEvent.setup()
|
|
const cancel = vi.fn()
|
|
mockGoals({ ...baseGoal }, { cancel })
|
|
render(<SessionGoalBar sessionID="ses_1" />)
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Cancel' }))
|
|
|
|
expect(cancel).toHaveBeenCalledWith(1)
|
|
})
|
|
})
|