Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion apps/penpal/frontend/src/pages/FilePage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ describe('FilePage', () => {
expect(api.startAgent).not.toHaveBeenCalled();
});

it('refreshes agent status and threads on SSE reconnect', async () => {
it('refreshes content, agent status, and threads on SSE reconnect', async () => {
vi.mocked(api.getAgentStatus).mockResolvedValue(agentNotRunning);

renderFilePage();
Expand All @@ -159,6 +159,7 @@ describe('FilePage', () => {
await waitFor(() => {
expect(api.getAgentStatus).toHaveBeenCalledTimes(1);
expect(api.getThreads).toHaveBeenCalledTimes(1);
expect(api.getRawFile).toHaveBeenCalledTimes(1);
});

// Get the onReconnect callback (2nd argument to useSSE)
Expand All @@ -169,13 +170,41 @@ describe('FilePage', () => {
// Simulate SSE reconnect
vi.mocked(api.getAgentStatus).mockClear();
vi.mocked(api.getThreads).mockClear();
vi.mocked(api.getRawFile).mockClear();
act(() => {
onReconnect!();
});

await waitFor(() => {
expect(api.getRawFile).toHaveBeenCalledTimes(1);
expect(api.getAgentStatus).toHaveBeenCalledTimes(1);
expect(api.getThreads).toHaveBeenCalledTimes(1);
});
});

it('refreshes content on SSE files event', async () => {
vi.mocked(api.getAgentStatus).mockResolvedValue(agentNotRunning);

renderFilePage();

// Wait for initial load
await waitFor(() => {
expect(api.getRawFile).toHaveBeenCalledTimes(1);
});

// Get the onEvent callback (1st argument to useSSE)
const useSSEMock = vi.mocked(useSSE);
const onEvent = useSSEMock.mock.calls[0]?.[0];
expect(onEvent).toBeDefined();

// Simulate a 'files' SSE event for our project
vi.mocked(api.getRawFile).mockClear();
act(() => {
onEvent!({ type: 'files', project: 'ws/proj' });
});

await waitFor(() => {
expect(api.getRawFile).toHaveBeenCalledTimes(1);
});
});
});
9 changes: 6 additions & 3 deletions apps/penpal/frontend/src/pages/FilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,16 @@ export default function FilePage() {
}, [location.pathname, projects]);

// Fetch raw file content
const fetchContent = useCallback(async () => {
const fetchContent = useCallback(async (opts?: { silent?: boolean }) => {
if (!project || !path) return;
try {
const content = await api.getRawFile(project, path);
setRawMarkdown(content);
setError(null);
} catch (err) {
setError('Failed to load file');
if (!opts?.silent) {
setError('Failed to load file');
}
console.error(err);
} finally {
setLoading(false);
Expand Down Expand Up @@ -262,9 +264,10 @@ export default function FilePage() {
[project, fetchThreads, fetchContent, fetchAgentStatus],
),
useCallback(() => {
fetchContent({ silent: true });

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Skip reconnect refresh on initial SSE open

useSSE invokes onReconnect from es.onopen for the very first EventSource connection (apps/penpal/frontend/src/hooks/useSSE.ts, lines 18–20), and FilePage already does an initial fetchContent() in its mount effect. Calling fetchContent({ silent: true }) here adds a second raw-file request on every page load, which doubles startup payload for large files and can race loading/content state (the silent call can clear loading before the original fetch completes).

Useful? React with 👍 / 👎.

fetchAgentStatus();
fetchThreads();
}, [fetchAgentStatus, fetchThreads]),
}, [fetchContent, fetchAgentStatus, fetchThreads]),
);

const handleComment = useCallback((anchor: Anchor, selectedText: string) => {
Expand Down