Merge pull request #439 from chriswritescode-dev/fix/branch-label-invalidation

fix(git): refresh branch labels after branch changes
This commit is contained in:
Chris Scott authored and GitHub committed 2026-10-17 13:01:01 -04:00
commit 851a63fb32
6 files changed
+107 -4

No files matched your search

@@ -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(<Harness />, { 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(<Harness />, { 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' },
+4 -1
View File
@@ -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) {
+4
View File
@@ -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')
})
+2 -2
View File
@@ -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',
},
)
@@ -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', () => {
+2 -1
View File
@@ -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),
)
}