mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
fix(git): refresh branch labels after branch changes
Invalidate the repo siblings cache with repo metadata so the session header branch updates after a switch, refresh the repo list git status and worktree siblings on vcs.branch.updated, and refresh repo metadata after continuing or aborting an operation.
This commit is contained in:
1 parent
9075ab1101
commit
f5199931d4
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' },
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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')
|
||||
})
|
||||
|
||||
|
||||
@@ -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', () => {
|
||||
|
||||
@@ -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),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user