From eb40a04353b8c971dab6768d36e51b50725d749a Mon Sep 17 00:00:00 2001 From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> Date: Tue, 29 Sep 2026 10:43:49 -0400 Subject: [PATCH] fix(schedules): intercept local report links while repo data loads --- .../schedules/RunDetailPanel.test.tsx | 130 ++++++++++++++++++ .../components/schedules/RunDetailPanel.tsx | 36 +++-- 2 files changed, 153 insertions(+), 13 deletions(-) create mode 100644 frontend/src/components/schedules/RunDetailPanel.test.tsx diff --git a/frontend/src/components/schedules/RunDetailPanel.test.tsx b/frontend/src/components/schedules/RunDetailPanel.test.tsx new file mode 100644 index 00000000..aeeabd47 --- /dev/null +++ b/frontend/src/components/schedules/RunDetailPanel.test.tsx @@ -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 ?
: 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( + + + + + , + ) +} + +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') + }) +}) diff --git a/frontend/src/components/schedules/RunDetailPanel.tsx b/frontend/src/components/schedules/RunDetailPanel.tsx index 1dfe2dea..dd2ad238 100644 --- a/frontend/src/components/schedules/RunDetailPanel.tsx +++ b/frontend/src/components/schedules/RunDetailPanel.tsx @@ -23,20 +23,19 @@ interface RunDetailPanelProps { export function RunDetailPanel({ repoId, activeRun, selectedRunLoading, onCancelRun, cancelRunPending }: RunDetailPanelProps) { const navigate = useNavigate() const [selectedFilePath, setSelectedFilePath] = useState(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 ? (
) : activeRun.responseText ? ( -
- -
+ <> + {repoPending && ( +
Loading repository details for local links.
+ )} + {repoError && ( +
+ Could not load repository details for local links. + +
+ )} +
+ +
+ ) : (
No assistant output captured.
)}