diff --git a/e2e/background-activity.spec.ts b/e2e/background-activity.spec.ts index 1e4f6be6..aa5814c9 100644 --- a/e2e/background-activity.spec.ts +++ b/e2e/background-activity.spec.ts @@ -23,7 +23,7 @@ async function requireOk(response: Awaited if (!response.ok()) { throw new Error(`${label} failed: ${response.status()} ${await response.text()}`) } - return response.json() + return response.status() === 204 ? null : response.json() } async function createSession(request: APIRequestContext, directory: string, parentID?: string): Promise { @@ -76,6 +76,12 @@ function createUnregisteredDirectory(name: string): string { return directory } +function waitForDirectorySubscription(page: Page, directory: string) { + return page.waitForResponse((response) => + response.url().includes('/api/sse/subscribe') && (response.request().postData() ?? '').includes(directory), + ) +} + async function navigateWithinApp(page: Page, url: string): Promise { await page.evaluate((target) => { window.history.pushState({}, '', target) @@ -196,9 +202,7 @@ test.describe('session background activity', () => { }) => { const directory = createUnregisteredDirectory('stale') const sessionId = await createSession(request, directory) - const subscribed = page.waitForResponse((response) => - response.url().includes('/api/sse/subscribe') && (response.request().postData() ?? '').includes(directory), - ) + const subscribed = waitForDirectorySubscription(page, directory) await page.goto(`/repos/${repo.id}/sessions/${sessionId}`) await expect(page.getByPlaceholder('Send a message...')).toBeVisible() @@ -218,4 +222,31 @@ test.describe('session background activity', () => { await expect(page.getByPlaceholder('Send a message...')).toBeVisible() await expect(backgroundIndicator(page)).toBeHidden({ timeout: 20_000 }) }) + + test('follows a session moved to another directory without a reload', async ({ page, request, repo }) => { + const source = createUnregisteredDirectory('move-source') + const target = createUnregisteredDirectory('move-target') + const sessionId = await createSession(request, source) + const subscribedToSource = waitForDirectorySubscription(page, source) + + await page.goto(`/repos/${repo.id}/sessions/${sessionId}`) + await expect(page.getByPlaceholder('Send a message...')).toBeVisible() + await subscribedToSource + + const subscribedToTarget = waitForDirectorySubscription(page, target) + await requireOk( + await request.post(`/api/opencode/api/session/${sessionId}/move`, { data: { directory: target } }), + 'move session', + ) + await subscribedToTarget + + const shellId = await startBackgroundShell(request, target, sessionId) + createdShells.push({ directory: target, id: shellId }) + + await expect(backgroundIndicator(page)).toBeVisible() + + await removeShell(request, target, shellId) + + await expect(backgroundIndicator(page)).toBeHidden() + }) }) diff --git a/frontend/src/hooks/useSSE.test.tsx b/frontend/src/hooks/useSSE.test.tsx index d014d512..a5997fa3 100644 --- a/frontend/src/hooks/useSSE.test.tsx +++ b/frontend/src/hooks/useSSE.test.tsx @@ -174,6 +174,40 @@ describe('useSSE', () => { unmount() }) + it('invalidates the cached session query on session.moved so the page follows the new directory', async () => { + const queryClient = new QueryClient({ + defaultOptions: { + queries: { retry: false }, + }, + }) + const invalidateQueries = vi.spyOn(queryClient, 'invalidateQueries') + queryClient.setQueryData(['opencode', 'session', 'session-1', '/repo'], { id: 'session-1' }) + + const { result, unmount } = renderHook( + () => useSSE('/repo', 'session-1'), + { wrapper: createWrapper(queryClient) } + ) + + await waitFor(() => expect(MockEventSource.instances).toHaveLength(1)) + await connect(0, 'client-1') + await waitFor(() => expect(result.current.isConnected).toBe(true)) + invalidateQueries.mockClear() + + act(() => { + MockEventSource.instances[0].emit('message', { + type: 'session.moved', + directory: '/repo', + data: { sessionID: 'session-1', projectID: 'proj-1', subpath: '', location: { directory: '/elsewhere' } }, + }) + }) + + await waitFor(() => { + expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: ['opencode', 'session', 'session-1'] }) + }) + + unmount() + }) + it('invalidates the cached session query on session.renamed', async () => { const queryClient = new QueryClient({ defaultOptions: { diff --git a/frontend/src/hooks/useSSE.ts b/frontend/src/hooks/useSSE.ts index 5d9f18d8..2448b463 100644 --- a/frontend/src/hooks/useSSE.ts +++ b/frontend/src/hooks/useSSE.ts @@ -80,6 +80,7 @@ export const useSSE = (directory?: string | string[], currentSessionId?: string) break case 'session.renamed': + case 'session.moved': invalidateSessionQueryIfCached(queryClient, event.data.sessionID) break