diff --git a/frontend/src/contexts/EventContext.test.tsx b/frontend/src/contexts/EventContext.test.tsx index 61507633..28ba86d1 100644 --- a/frontend/src/contexts/EventContext.test.tsx +++ b/frontend/src/contexts/EventContext.test.tsx @@ -1083,6 +1083,61 @@ describe('EventProvider permissions and forms', () => { }) }) + it('invalidates the sibling caches when a branch changes outside a repo row', async () => { + mocks.listRepos.mockResolvedValue([]) + + const queryClient = createTestQueryClient() + const invalidateQueries = vi.spyOn(queryClient, 'invalidateQueries') + + render(, { wrapper: createWrapper(queryClient) }) + + await waitFor(() => expect(mocks.subscribeGlobalMonitor).toHaveBeenCalled()) + invalidateQueries.mockClear() + + const lastSubscribeCall = mocks.subscribeGlobalMonitor.mock.calls[mocks.subscribeGlobalMonitor.mock.calls.length - 1] + const onEvent = lastSubscribeCall[0].onEvent as (data: unknown) => void + + act(() => { + onEvent({ type: 'vcs.branch.updated', data: { branch: 'feature' }, directory: '/repos/a/.worktrees/feat' }) + }) + + expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: ['repo', 'siblings'] }) + }) + + it('updates the repo branch and refreshes the sibling caches on vcs.branch.updated', async () => { + mocks.listRepos.mockResolvedValue([{ id: 1, fullPath: '/repo-a' }]) + + const updateDirectories = vi.fn() + mocks.subscribeGlobalMonitor.mockReturnValue({ + dispose: vi.fn(), + updateDirectories, + reconnect: vi.fn(), + reportVisibility: vi.fn(), + }) + + const queryClient = createTestQueryClient() + const invalidateQueries = vi.spyOn(queryClient, 'invalidateQueries') + + render(, { wrapper: createWrapper(queryClient) }) + + await waitFor(() => expect(queryClient.getQueryData(['repos'])).toBeDefined()) + await waitFor(() => expect(updateDirectories).toHaveBeenCalledWith(expect.arrayContaining(['/repo-a']))) + invalidateQueries.mockClear() + + const lastSubscribeCall = mocks.subscribeGlobalMonitor.mock.calls[mocks.subscribeGlobalMonitor.mock.calls.length - 1] + const onEvent = lastSubscribeCall[0].onEvent as (data: unknown) => void + + act(() => { + onEvent({ type: 'vcs.branch.updated', data: { branch: 'feature' }, directory: '/repo-a' }) + }) + + expect(queryClient.getQueryData<{ currentBranch?: string; branch?: string }>(['repo', 1])) + .toMatchObject({ currentBranch: 'feature', branch: 'feature' }) + await waitFor(() => { + expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: ['repo', 'siblings'] }) + }) + }) + it('reconciles every tracked directory concurrently on an upstream resync', async () => { mocks.listRepos.mockResolvedValue([ { id: 1, fullPath: '/repo-a' }, diff --git a/frontend/src/contexts/EventContext.tsx b/frontend/src/contexts/EventContext.tsx index 2a964a80..deaf7782 100644 --- a/frontend/src/contexts/EventContext.tsx +++ b/frontend/src/contexts/EventContext.tsx @@ -814,7 +814,10 @@ export function EventProvider({ children }: { children: React.ReactNode }) { break case 'vcs.branch.updated': { const repo = reposRef.current?.find((candidate) => repoMatchesDirectory(candidate, event.directory)) - if (!repo) break + if (!repo) { + queryClient.invalidateQueries({ queryKey: ['repo', 'siblings'] }) + break + } const branch = event.data.branch if (branch) { diff --git a/frontend/src/hooks/useGit.test.tsx b/frontend/src/hooks/useGit.test.tsx index 1e0458bc..7725e183 100644 --- a/frontend/src/hooks/useGit.test.tsx +++ b/frontend/src/hooks/useGit.test.tsx @@ -530,6 +530,8 @@ describe('useGit', () => { expect(gitApi.gitContinueOperation).toHaveBeenCalledWith(1) expectTargetedStatusCacheUpdate() + expect(mockInvalidateQueries).toHaveBeenCalledWith({ queryKey: ['repo', 1] }) + expect(mockInvalidateQueries).toHaveBeenCalledWith({ queryKey: ['repo', 'siblings'] }) expect(toast.showToast.success).toHaveBeenCalledWith('Operation continued') }) @@ -585,6 +587,8 @@ describe('useGit', () => { expect(gitApi.gitAbortOperation).toHaveBeenCalledWith(1) expectTargetedStatusCacheUpdate() + expect(mockInvalidateQueries).toHaveBeenCalledWith({ queryKey: ['repo', 1] }) + expect(mockInvalidateQueries).toHaveBeenCalledWith({ queryKey: ['repo', 'siblings'] }) expect(toast.showToast.success).toHaveBeenCalledWith('Operation aborted') }) diff --git a/frontend/src/hooks/useGit.ts b/frontend/src/hooks/useGit.ts index c80ffee5..8f5e4987 100644 --- a/frontend/src/hooks/useGit.ts +++ b/frontend/src/hooks/useGit.ts @@ -278,7 +278,7 @@ export function useGit(repoId: number | undefined, onError?: (error: unknown) => { mutationFn: (id) => gitContinueOperation(id), statusFromData: (data) => data, - invalidate: (client, id) => invalidateRepoGitCaches(client, id, { invalidateStatus: false, invalidateRepoMeta: false }), + invalidate: (client, id) => invalidateRepoGitCaches(client, id, { invalidateStatus: false }), successMessage: 'Operation continued', onError: (error) => { if (repoId) invalidateRepoGitCaches(queryClient, repoId, { invalidateRepoListStatus: true }) @@ -294,7 +294,7 @@ export function useGit(repoId: number | undefined, onError?: (error: unknown) => { mutationFn: (id) => gitAbortOperation(id), statusFromData: (data) => data, - invalidate: (client, id) => invalidateRepoGitCaches(client, id, { invalidateStatus: false, invalidateRepoMeta: false }), + invalidate: (client, id) => invalidateRepoGitCaches(client, id, { invalidateStatus: false }), successMessage: 'Operation aborted', }, ) diff --git a/frontend/src/lib/queryInvalidation.test.ts b/frontend/src/lib/queryInvalidation.test.ts index dd386053..672d500a 100644 --- a/frontend/src/lib/queryInvalidation.test.ts +++ b/frontend/src/lib/queryInvalidation.test.ts @@ -6,6 +6,7 @@ import { invalidateProviderCachesDebounced, invalidateQueryKeysDebounced, invalidateRepoGitCaches, + invalidateRepoGitCachesDebounced, invalidateSessionListCachesDebounced, refreshOpenCodeServerCaches, } from './queryInvalidation' @@ -80,6 +81,45 @@ describe('invalidateRepoGitCaches', () => { expect.objectContaining({ queryKey: ['reposGitStatus'], predicate: expect.any(Function) }), ) }) + + it('invalidates every sibling cache so branches resolved from siblings refresh', () => { + const queryClient = new QueryClient() + queryClient.setQueryData(['repo', 'siblings', 1], []) + queryClient.setQueryData(['repo', 'siblings', 2], []) + + invalidateRepoGitCaches(queryClient, 1) + + expect(queryClient.getQueryState(['repo', 'siblings', 1])?.isInvalidated).toBe(true) + expect(queryClient.getQueryState(['repo', 'siblings', 2])?.isInvalidated).toBe(true) + }) + + it('keeps the sibling caches when repo metadata invalidation is skipped', () => { + const queryClient = new QueryClient() + queryClient.setQueryData(['repo', 'siblings', 1], []) + + invalidateRepoGitCaches(queryClient, 1, { invalidateRepoMeta: false }) + + expect(queryClient.getQueryState(['repo', 'siblings', 1])?.isInvalidated).toBe(false) + }) +}) + +describe('invalidateRepoGitCachesDebounced', () => { + afterEach(() => { + vi.useRealTimers() + }) + + it('refreshes the batched repo list status that holds the branch after the debounce window', () => { + vi.useFakeTimers() + const queryClient = new QueryClient() + queryClient.setQueryData(['reposGitStatus', [1, 2]], new Map()) + queryClient.setQueryData(['reposGitStatus', [3]], new Map()) + + invalidateRepoGitCachesDebounced(queryClient, 1) + vi.advanceTimersByTime(200) + + expect(queryClient.getQueryState(['reposGitStatus', [1, 2]])?.isInvalidated).toBe(true) + expect(queryClient.getQueryState(['reposGitStatus', [3]])?.isInvalidated).toBe(false) + }) }) describe('invalidateProviderCaches', () => { diff --git a/frontend/src/lib/queryInvalidation.ts b/frontend/src/lib/queryInvalidation.ts index 9da123c5..b3bec075 100644 --- a/frontend/src/lib/queryInvalidation.ts +++ b/frontend/src/lib/queryInvalidation.ts @@ -112,6 +112,7 @@ export function invalidateRepoGitCaches( if (options.invalidateRepoMeta ?? true) { queryClient.invalidateQueries({ queryKey: ['repos'] }) queryClient.invalidateQueries({ queryKey: ['repo', repoId] }) + queryClient.invalidateQueries({ queryKey: ['repo', 'siblings'] }) queryClient.invalidateQueries({ queryKey: ['branches', repoId] }) } if (options.invalidateStatus ?? true) { @@ -166,7 +167,7 @@ export function invalidateRepoGitCachesDebounced(queryClient: QueryClient, repoI repoId, setTimeout(() => { timers.delete(repoId) - invalidateRepoGitCaches(queryClient, repoId) + invalidateRepoGitCaches(queryClient, repoId, { invalidateRepoListStatus: true }) }, delayMs), ) }