mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
231 lines
7.6 KiB
TypeScript
231 lines
7.6 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import { render, screen, waitFor, fireEvent } from '@testing-library/react'
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
import { MemoryRouter, useLocation } from 'react-router-dom'
|
|
import { RunDetailPanel } from './RunDetailPanel'
|
|
import type { Repo } from '@/api/types'
|
|
import type { ScheduleRun } from '@opencode-manager/shared/types'
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
getRepo: vi.fn(),
|
|
}))
|
|
|
|
const apiMocks = vi.hoisted(() => ({
|
|
createSessionWithContext: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/api/repos', () => ({
|
|
getRepo: mocks.getRepo,
|
|
}))
|
|
|
|
vi.mock('@/api/opencode', () => ({
|
|
createSessionWithContext: apiMocks.createSessionWithContext,
|
|
}))
|
|
|
|
vi.mock('@/lib/toast', () => ({
|
|
showToast: {
|
|
success: vi.fn(),
|
|
error: vi.fn(),
|
|
info: vi.fn(),
|
|
warning: vi.fn(),
|
|
loading: vi.fn(),
|
|
promise: vi.fn(),
|
|
dismiss: vi.fn(),
|
|
},
|
|
}))
|
|
|
|
function LocationProbe() {
|
|
const location = useLocation()
|
|
return <div data-testid="location">{location.pathname}</div>
|
|
}
|
|
|
|
vi.mock('@/components/file-browser/FileBrowserSheet', () => ({
|
|
FileBrowserSheet: ({ isOpen, initialSelectedFile }: { isOpen: boolean; initialSelectedFile?: string }) =>
|
|
isOpen ? <div data-testid="file-browser-sheet" data-selected-file={initialSelectedFile} /> : null,
|
|
}))
|
|
|
|
const repo: Repo = {
|
|
id: 5,
|
|
name: 'my-repo',
|
|
localPath: 'repos/my-repo',
|
|
fullPath: '/abs/repos/my-repo',
|
|
defaultBranch: 'main',
|
|
cloneStatus: 'ready',
|
|
clonedAt: 0,
|
|
}
|
|
|
|
const run: ScheduleRun = {
|
|
id: 1,
|
|
jobId: 1,
|
|
repoId: 5,
|
|
triggerSource: 'manual',
|
|
status: 'completed',
|
|
startedAt: 0,
|
|
finishedAt: null,
|
|
createdAt: 0,
|
|
sessionId: null,
|
|
sessionTitle: null,
|
|
logText: null,
|
|
responseText: '[Report](recaps/daily.html)\n\n[Docs](https://example.com)',
|
|
errorText: null,
|
|
runBranch: null,
|
|
commitHash: null,
|
|
worktreePath: null,
|
|
}
|
|
|
|
function renderPanel(activeRun: ScheduleRun = run) {
|
|
const queryClient = new QueryClient({
|
|
defaultOptions: {
|
|
queries: { retry: false },
|
|
mutations: { retry: false },
|
|
},
|
|
})
|
|
return render(
|
|
<MemoryRouter>
|
|
<QueryClientProvider client={queryClient}>
|
|
<RunDetailPanel
|
|
repoId={5}
|
|
activeRun={activeRun}
|
|
selectedRunLoading={false}
|
|
onCancelRun={vi.fn()}
|
|
cancelRunPending={false}
|
|
/>
|
|
<LocationProbe />
|
|
</QueryClientProvider>
|
|
</MemoryRouter>,
|
|
)
|
|
}
|
|
|
|
function clickLink(name: string) {
|
|
const link = screen.getByRole('link', { name })
|
|
const event = new MouseEvent('click', { bubbles: true, cancelable: true })
|
|
fireEvent(link, event)
|
|
return event
|
|
}
|
|
|
|
describe('RunDetailPanel local link handling', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it('prevents navigation and opens no sheet while the repo query is pending', async () => {
|
|
mocks.getRepo.mockReturnValue(new Promise(() => {}))
|
|
renderPanel()
|
|
|
|
await screen.findByRole('status')
|
|
|
|
const event = clickLink('Report')
|
|
|
|
expect(event.defaultPrevented).toBe(true)
|
|
expect(screen.queryByTestId('file-browser-sheet')).not.toBeInTheDocument()
|
|
})
|
|
|
|
it('prevents navigation, shows a retryable error, and refetches when the repo query fails', async () => {
|
|
mocks.getRepo.mockRejectedValue(new Error('failed'))
|
|
renderPanel()
|
|
|
|
await screen.findByRole('alert')
|
|
expect(mocks.getRepo).toHaveBeenCalledTimes(1)
|
|
|
|
const event = clickLink('Report')
|
|
|
|
expect(event.defaultPrevented).toBe(true)
|
|
expect(screen.queryByTestId('file-browser-sheet')).not.toBeInTheDocument()
|
|
|
|
fireEvent.click(screen.getByRole('button', { name: 'Retry' }))
|
|
|
|
await waitFor(() => expect(mocks.getRepo).toHaveBeenCalledTimes(2))
|
|
})
|
|
|
|
it('opens the sheet with the resolved path once the repo is loaded and keeps external links external', async () => {
|
|
mocks.getRepo.mockResolvedValue(repo)
|
|
renderPanel()
|
|
|
|
await waitFor(() => expect(screen.queryByRole('status')).not.toBeInTheDocument())
|
|
|
|
const event = clickLink('Report')
|
|
|
|
expect(event.defaultPrevented).toBe(true)
|
|
const sheet = await screen.findByTestId('file-browser-sheet')
|
|
expect(sheet.getAttribute('data-selected-file')).toBe('repos/my-repo/recaps/daily.html')
|
|
|
|
const docs = screen.getByRole('link', { name: 'Docs' })
|
|
expect(docs.getAttribute('target')).toBe('_blank')
|
|
expect(docs.getAttribute('rel')).toBe('noopener noreferrer')
|
|
})
|
|
})
|
|
|
|
describe('RunDetailPanel open session', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
mocks.getRepo.mockResolvedValue(repo)
|
|
})
|
|
|
|
async function clickOpenSession() {
|
|
await waitFor(() => expect(screen.getByRole('button', { name: 'Open session' })).toBeEnabled())
|
|
fireEvent.click(screen.getByRole('button', { name: 'Open session' }))
|
|
}
|
|
|
|
it('opens the original session for a run that worked in the repository', async () => {
|
|
renderPanel({ ...run, sessionId: 'ses_run', worktreePath: null })
|
|
|
|
await clickOpenSession()
|
|
|
|
await waitFor(() => expect(screen.getByTestId('location').textContent).toBe('/repos/5/sessions/ses_run'))
|
|
expect(apiMocks.createSessionWithContext).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('opens the original session while a worktree run is still running', async () => {
|
|
renderPanel({ ...run, status: 'running', sessionId: 'ses_run', worktreePath: '/abs/worktrees/run-1' })
|
|
|
|
await clickOpenSession()
|
|
|
|
await waitFor(() => expect(screen.getByTestId('location').textContent).toBe('/repos/5/sessions/ses_run'))
|
|
expect(apiMocks.createSessionWithContext).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('opens a new repo session seeded with the full output and branch for a finished worktree run', async () => {
|
|
apiMocks.createSessionWithContext.mockResolvedValue({ id: 'ses_new' })
|
|
renderPanel({
|
|
...run,
|
|
sessionId: 'ses_run',
|
|
sessionTitle: 'Daily recap',
|
|
finishedAt: Date.UTC(2026, 9, 2),
|
|
worktreePath: '/abs/worktrees/run-1',
|
|
runBranch: 'schedule/run-1',
|
|
commitHash: 'abc123',
|
|
})
|
|
|
|
await clickOpenSession()
|
|
|
|
await waitFor(() => expect(screen.getByTestId('location').textContent).toBe('/repos/5/sessions/ses_new'))
|
|
const [input, context] = apiMocks.createSessionWithContext.mock.calls[0] ?? []
|
|
expect(input).toEqual({ directory: '/abs/repos/my-repo', title: 'Daily recap (continued)' })
|
|
expect(context).toContain('scheduled run #1 "Daily recap", which completed and finished at 2026-10-02T00:00:00.000Z')
|
|
expect(context).toContain('branch `schedule/run-1` at commit `abc123`')
|
|
expect(context).toContain(`Run output:\n\n${run.responseText}`)
|
|
})
|
|
|
|
it('includes the error and notes no committed changes for a failed worktree run', async () => {
|
|
apiMocks.createSessionWithContext.mockResolvedValue({ id: 'ses_new' })
|
|
renderPanel({ ...run, status: 'failed', responseText: null, errorText: 'Model timed out', worktreePath: '/abs/worktrees/run-1' })
|
|
|
|
await clickOpenSession()
|
|
|
|
await waitFor(() => expect(apiMocks.createSessionWithContext).toHaveBeenCalled())
|
|
const [input, context] = apiMocks.createSessionWithContext.mock.calls[0] ?? []
|
|
expect(input).toEqual({ directory: '/abs/repos/my-repo', title: 'Scheduled run (continued)' })
|
|
expect(context).toContain('ended as failed')
|
|
expect(context).toContain('committed no changes')
|
|
expect(context).toContain('The run produced no output.')
|
|
expect(context).toContain('Run error:\n\nModel timed out')
|
|
})
|
|
|
|
it('shows a single open session button', async () => {
|
|
renderPanel({ ...run, sessionId: 'ses_run', worktreePath: '/abs/worktrees/run-1' })
|
|
|
|
await waitFor(() => expect(screen.getByRole('button', { name: 'Open session' })).toBeEnabled())
|
|
expect(screen.getAllByRole('button', { name: /session|repo/i })).toHaveLength(1)
|
|
})
|
|
})
|