fix(schedules): intercept local report links while repo data loads

This commit is contained in:
Chris Scott committed 2026-09-29 10:43:49 -04:00
1 parent e497dbfe34
commit eb40a04353
2 files changed
+153 -13

No files matched your search

@@ -0,0 +1,130 @@
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 } 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(),
}))
vi.mock('@/api/repos', () => ({
getRepo: mocks.getRepo,
}))
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() {
const queryClient = new QueryClient({
defaultOptions: {
queries: { retry: false },
mutations: { retry: false },
},
})
return render(
<MemoryRouter>
<QueryClientProvider client={queryClient}>
<RunDetailPanel
repoId={5}
activeRun={run}
selectedRunLoading={false}
onCancelRun={vi.fn()}
cancelRunPending={false}
/>
</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')
})
})
@@ -23,20 +23,19 @@ interface RunDetailPanelProps {
export function RunDetailPanel({ repoId, activeRun, selectedRunLoading, onCancelRun, cancelRunPending }: RunDetailPanelProps) {
const navigate = useNavigate()
const [selectedFilePath, setSelectedFilePath] = useState<string | null>(null)
const { data: repo } = useQuery({
const { data: repo, isPending: repoPending, isError: repoError, refetch: refetchRepo } = useQuery({
queryKey: ['repo', repoId],
queryFn: () => getRepo(repoId),
})
const handleOpenLocalPath = repo
? (linkPath: string) => {
setSelectedFilePath(getWorkspaceFilePath(linkPath, {
directory: activeRun?.worktreePath ?? repo.fullPath,
repoFullPath: repo.fullPath,
repoLocalPath: repo.localPath,
}))
}
: undefined
const handleOpenLocalPath = (linkPath: string) => {
if (!repo) return
setSelectedFilePath(getWorkspaceFilePath(linkPath, {
directory: activeRun?.worktreePath ?? repo.fullPath,
repoFullPath: repo.fullPath,
repoLocalPath: repo.localPath,
}))
}
if (selectedRunLoading && !activeRun) {
return (
@@ -90,9 +89,20 @@ export function RunDetailPanel({ repoId, activeRun, selectedRunLoading, onCancel
{selectedRunLoading && !activeRun ? (
<div className="flex items-center justify-center p-4"><Loader2 className="h-6 w-6 animate-spin text-muted-foreground" /></div>
) : activeRun.responseText ? (
<div className="min-h-0 flex-1 overflow-y-auto px-3 py-2 xl:[mask-image:linear-gradient(to_bottom,transparent,black_16px,black)]">
<ScheduleRunMarkdown content={activeRun.responseText} onOpenLocalPath={handleOpenLocalPath} />
</div>
<>
{repoPending && (
<div role="status" className="px-3 pt-2 text-xs text-muted-foreground">Loading repository details for local links.</div>
)}
{repoError && (
<div role="alert" className="flex items-center gap-2 px-3 pt-2 text-xs text-destructive">
<span>Could not load repository details for local links.</span>
<Button variant="outline" size="sm" onClick={() => { void refetchRepo() }}>Retry</Button>
</div>
)}
<div className="min-h-0 flex-1 overflow-y-auto px-3 py-2 xl:[mask-image:linear-gradient(to_bottom,transparent,black_16px,black)]">
<ScheduleRunMarkdown content={activeRun.responseText} onOpenLocalPath={handleOpenLocalPath} />
</div>
</>
) : (
<div className="p-3"><pre className="whitespace-pre-wrap break-words text-sm font-mono leading-6">No assistant output captured.</pre></div>
)}