diff --git a/src/renderer/src/store/slices/worktrees.test.ts b/src/renderer/src/store/slices/worktrees.test.ts index 4bdccb291..0c37595f8 100644 --- a/src/renderer/src/store/slices/worktrees.test.ts +++ b/src/renderer/src/store/slices/worktrees.test.ts @@ -5,7 +5,12 @@ import type { Worktree } from '../../../../shared/types' const mockApi = { worktrees: { - list: vi.fn().mockResolvedValue([]) + list: vi.fn().mockResolvedValue([]), + remove: vi.fn().mockResolvedValue(undefined), + updateMeta: vi.fn().mockResolvedValue(undefined) + }, + pty: { + kill: vi.fn().mockResolvedValue(undefined) } } @@ -20,8 +25,36 @@ function createTestStore() { ({ // Why: this test isolates the worktree slice, so it only provides the // state surface that `createWorktreeSlice` reads and writes. - ...createWorktreeSlice(...a) - }) as AppState + ...createWorktreeSlice(...a), + shutdownWorktreeTerminals: vi.fn().mockResolvedValue(undefined), + tabsByWorktree: {}, + tabBarOrderByWorktree: {}, + pendingReconnectTabByWorktree: {}, + activeTabIdByWorktree: {}, + unifiedTabsByWorktree: {}, + groupsByWorktree: {}, + activeGroupIdByWorktree: {}, + layoutByWorktree: {}, + openFiles: [], + editorDrafts: {}, + markdownViewMode: {}, + expandedDirs: {}, + gitStatusByWorktree: {}, + gitConflictOperationByWorktree: {}, + trackedConflictPathsByWorktree: {}, + gitBranchChangesByWorktree: {}, + gitBranchCompareSummaryByWorktree: {}, + gitBranchCompareRequestKeyByWorktree: {}, + activeFileIdByWorktree: {}, + activeBrowserTabIdByWorktree: {}, + browserTabsByWorktree: {}, + activeTabTypeByWorktree: {}, + activeWorktreeId: null, + activeTabId: null, + activeFileId: null, + activeBrowserTabId: null, + activeTabType: 'terminal' as const + }) as unknown as AppState ) } @@ -93,3 +126,246 @@ describe('fetchWorktrees', () => { expect(store.getState().sortEpoch).toBe(8) }) }) + +describe('removeWorktree state cleanup', () => { + beforeEach(() => { + vi.clearAllMocks() + }) + + it('cleans up editorDrafts for files in the removed worktree', async () => { + const store = createTestStore() + const wt = makeWorktree({ id: 'repo1::/path/wt1', repoId: 'repo1', path: '/path/wt1' }) + + store.setState({ + worktreesByRepo: { repo1: [wt] }, + openFiles: [ + { + id: 'file-1', + worktreeId: 'repo1::/path/wt1', + filePath: '/path/wt1/file.ts', + relativePath: 'file.ts', + language: 'typescript', + isDirty: true, + isPreview: false, + mode: 'edit' as const + } + ], + editorDrafts: { + 'file-1': 'draft content for wt1', + 'file-2': 'draft content for another worktree' + } + } as unknown as Partial) + + const result = await store.getState().removeWorktree('repo1::/path/wt1') + + expect(result).toEqual({ ok: true }) + // Draft for file-1 should be removed, draft for file-2 should remain + expect(store.getState().editorDrafts).toEqual({ + 'file-2': 'draft content for another worktree' + }) + }) + + it('cleans up markdownViewMode for files in the removed worktree', async () => { + const store = createTestStore() + const wt = makeWorktree({ id: 'repo1::/path/wt1', repoId: 'repo1', path: '/path/wt1' }) + + store.setState({ + worktreesByRepo: { repo1: [wt] }, + openFiles: [ + { + id: 'file-1', + worktreeId: 'repo1::/path/wt1', + filePath: '/path/wt1/readme.md', + relativePath: 'readme.md', + language: 'markdown', + isDirty: false, + isPreview: false, + mode: 'edit' as const + } + ], + markdownViewMode: { + 'file-1': 'rich' as const, + 'file-2': 'source' as const + } + } as unknown as Partial) + + await store.getState().removeWorktree('repo1::/path/wt1') + + expect(store.getState().markdownViewMode).toEqual({ 'file-2': 'source' }) + }) + + it('cleans up expandedDirs for the removed worktree', async () => { + const store = createTestStore() + const wt = makeWorktree({ id: 'repo1::/path/wt1', repoId: 'repo1', path: '/path/wt1' }) + + store.setState({ + worktreesByRepo: { repo1: [wt] }, + expandedDirs: { + 'repo1::/path/wt1': new Set(['src', 'src/lib']), + 'repo1::/path/wt2': new Set(['test']) + } + } as Partial) + + await store.getState().removeWorktree('repo1::/path/wt1') + + expect(store.getState().expandedDirs).toEqual({ + 'repo1::/path/wt2': new Set(['test']) + }) + }) + + it('cleans up activeTabIdByWorktree for the removed worktree', async () => { + const store = createTestStore() + const wt = makeWorktree({ id: 'repo1::/path/wt1', repoId: 'repo1', path: '/path/wt1' }) + + store.setState({ + worktreesByRepo: { repo1: [wt] }, + activeTabIdByWorktree: { + 'repo1::/path/wt1': 'tab-1', + 'repo1::/path/wt2': 'tab-2' + } + } as Partial) + + await store.getState().removeWorktree('repo1::/path/wt1') + + expect(store.getState().activeTabIdByWorktree).toEqual({ + 'repo1::/path/wt2': 'tab-2' + }) + }) + + it('cleans up tabBarOrderByWorktree for the removed worktree', async () => { + const store = createTestStore() + const wt = makeWorktree({ id: 'repo1::/path/wt1', repoId: 'repo1', path: '/path/wt1' }) + + store.setState({ + worktreesByRepo: { repo1: [wt] }, + tabBarOrderByWorktree: { + 'repo1::/path/wt1': ['tab-1', 'file-1', 'browser-1'], + 'repo1::/path/wt2': ['tab-2'] + } + } as Partial) + + await store.getState().removeWorktree('repo1::/path/wt1') + + expect(store.getState().tabBarOrderByWorktree).toEqual({ + 'repo1::/path/wt2': ['tab-2'] + }) + }) + + it('cleans up split-tab model state for the removed worktree', async () => { + const store = createTestStore() + const wt = makeWorktree({ id: 'repo1::/path/wt1', repoId: 'repo1', path: '/path/wt1' }) + + store.setState({ + worktreesByRepo: { repo1: [wt] }, + pendingReconnectTabByWorktree: { + 'repo1::/path/wt1': ['tab-1'], + 'repo1::/path/wt2': ['tab-2'] + }, + unifiedTabsByWorktree: { + 'repo1::/path/wt1': [{ id: 'tab-1', worktreeId: 'repo1::/path/wt1' }], + 'repo1::/path/wt2': [{ id: 'tab-2', worktreeId: 'repo1::/path/wt2' }] + }, + groupsByWorktree: { + 'repo1::/path/wt1': [{ id: 'group-1', worktreeId: 'repo1::/path/wt1', activeTabId: 'tab-1' }], + 'repo1::/path/wt2': [{ id: 'group-2', worktreeId: 'repo1::/path/wt2', activeTabId: 'tab-2' }] + }, + activeGroupIdByWorktree: { + 'repo1::/path/wt1': 'group-1', + 'repo1::/path/wt2': 'group-2' + }, + layoutByWorktree: { + 'repo1::/path/wt1': { type: 'leaf', groupId: 'group-1' }, + 'repo1::/path/wt2': { type: 'leaf', groupId: 'group-2' } + } + } as unknown as Partial) + + await store.getState().removeWorktree('repo1::/path/wt1') + + expect(store.getState().pendingReconnectTabByWorktree).toEqual({ + 'repo1::/path/wt2': ['tab-2'] + }) + expect(store.getState().unifiedTabsByWorktree).toEqual({ + 'repo1::/path/wt2': [{ id: 'tab-2', worktreeId: 'repo1::/path/wt2' }] + }) + expect(store.getState().groupsByWorktree).toEqual({ + 'repo1::/path/wt2': [{ id: 'group-2', worktreeId: 'repo1::/path/wt2', activeTabId: 'tab-2' }] + }) + expect(store.getState().activeGroupIdByWorktree).toEqual({ + 'repo1::/path/wt2': 'group-2' + }) + expect(store.getState().layoutByWorktree).toEqual({ + 'repo1::/path/wt2': { type: 'leaf', groupId: 'group-2' } + }) + }) + + it('cleans up git caches for the removed worktree', async () => { + const store = createTestStore() + const wt = makeWorktree({ id: 'repo1::/path/wt1', repoId: 'repo1', path: '/path/wt1' }) + + store.setState({ + worktreesByRepo: { repo1: [wt] }, + gitStatusByWorktree: { + 'repo1::/path/wt1': [{ path: 'a.ts' }], + 'repo1::/path/wt2': [{ path: 'b.ts' }] + }, + gitConflictOperationByWorktree: { + 'repo1::/path/wt1': 'merge', + 'repo1::/path/wt2': 'unknown' + }, + trackedConflictPathsByWorktree: { + 'repo1::/path/wt1': { 'a.ts': 'both_modified' }, + 'repo1::/path/wt2': { 'b.ts': 'both_modified' } + }, + gitBranchChangesByWorktree: { + 'repo1::/path/wt1': [{ path: 'a.ts' }], + 'repo1::/path/wt2': [{ path: 'b.ts' }] + }, + gitBranchCompareSummaryByWorktree: { + 'repo1::/path/wt1': { status: 'ready' }, + 'repo1::/path/wt2': { status: 'loading' } + }, + gitBranchCompareRequestKeyByWorktree: { + 'repo1::/path/wt1': 'req-1', + 'repo1::/path/wt2': 'req-2' + } + } as unknown as Partial) + + await store.getState().removeWorktree('repo1::/path/wt1') + + expect(store.getState().gitStatusByWorktree).toEqual({ + 'repo1::/path/wt2': [{ path: 'b.ts' }] + }) + expect(store.getState().gitConflictOperationByWorktree).toEqual({ + 'repo1::/path/wt2': 'unknown' + }) + expect(store.getState().trackedConflictPathsByWorktree).toEqual({ + 'repo1::/path/wt2': { 'b.ts': 'both_modified' } + }) + expect(store.getState().gitBranchChangesByWorktree).toEqual({ + 'repo1::/path/wt2': [{ path: 'b.ts' }] + }) + expect(store.getState().gitBranchCompareSummaryByWorktree).toEqual({ + 'repo1::/path/wt2': { status: 'loading' } + }) + expect(store.getState().gitBranchCompareRequestKeyByWorktree).toEqual({ + 'repo1::/path/wt2': 'req-2' + }) + }) + + it('skips editorDrafts shallow copy when no files belong to the removed worktree', async () => { + const store = createTestStore() + const wt = makeWorktree({ id: 'repo1::/path/wt1', repoId: 'repo1', path: '/path/wt1' }) + + const drafts = { 'file-2': 'some content' } + store.setState({ + worktreesByRepo: { repo1: [wt] }, + openFiles: [], + editorDrafts: drafts + } as Partial) + + await store.getState().removeWorktree('repo1::/path/wt1') + + // The same reference should be returned (no unnecessary shallow copy) + expect(store.getState().editorDrafts).toBe(drafts) + }) +}) diff --git a/src/renderer/src/store/slices/worktrees.ts b/src/renderer/src/store/slices/worktrees.ts index a53dfb9f2..000948566 100644 --- a/src/renderer/src/store/slices/worktrees.ts +++ b/src/renderer/src/store/slices/worktrees.ts @@ -134,6 +134,61 @@ export const createWorktreeSlice: StateCreator delete nextActiveBrowserTabIdByWorktree[worktreeId] const nextActiveTabTypeByWorktree = { ...s.activeTabTypeByWorktree } delete nextActiveTabTypeByWorktree[worktreeId] + const nextActiveTabIdByWorktree = { ...s.activeTabIdByWorktree } + delete nextActiveTabIdByWorktree[worktreeId] + const nextTabBarOrderByWorktree = { ...s.tabBarOrderByWorktree } + // Why: the mixed terminal/editor/browser tab strip persists visual order + // per worktree. If a deleted worktree keeps its entry, stale tab IDs stay + // retained indefinitely even though reconcileTabOrder filters them later. + delete nextTabBarOrderByWorktree[worktreeId] + const nextPendingReconnectTabByWorktree = { ...s.pendingReconnectTabByWorktree } + delete nextPendingReconnectTabByWorktree[worktreeId] + // Why: split-tab layout/group state is owned by the worktree. Leaving it + // behind retains full tab chrome for terminals/editors/browser tabs that + // no longer exist and makes a deleted worktree look restorable in session + // state even though its backing entities were already removed. + const nextUnifiedTabsByWorktree = { ...s.unifiedTabsByWorktree } + delete nextUnifiedTabsByWorktree[worktreeId] + const nextGroupsByWorktree = { ...s.groupsByWorktree } + delete nextGroupsByWorktree[worktreeId] + const nextActiveGroupIdByWorktree = { ...s.activeGroupIdByWorktree } + delete nextActiveGroupIdByWorktree[worktreeId] + const nextLayoutByWorktree = { ...s.layoutByWorktree } + delete nextLayoutByWorktree[worktreeId] + // Why: git status / compare caches are keyed by worktree and stop being + // refreshed once the worktree is deleted. Remove them here so deleted + // worktrees cannot retain stale conflict badges, branch diffs, or compare + // request keys indefinitely in a long-lived renderer session. + const nextGitStatusByWorktree = { ...s.gitStatusByWorktree } + delete nextGitStatusByWorktree[worktreeId] + const nextGitConflictOperationByWorktree = { ...s.gitConflictOperationByWorktree } + delete nextGitConflictOperationByWorktree[worktreeId] + const nextTrackedConflictPathsByWorktree = { ...s.trackedConflictPathsByWorktree } + delete nextTrackedConflictPathsByWorktree[worktreeId] + const nextGitBranchChangesByWorktree = { ...s.gitBranchChangesByWorktree } + delete nextGitBranchChangesByWorktree[worktreeId] + const nextGitBranchCompareSummaryByWorktree = { ...s.gitBranchCompareSummaryByWorktree } + delete nextGitBranchCompareSummaryByWorktree[worktreeId] + const nextGitBranchCompareRequestKeyByWorktree = { + ...s.gitBranchCompareRequestKeyByWorktree + } + delete nextGitBranchCompareRequestKeyByWorktree[worktreeId] + // Why: clean up per-file editor state for files belonging to the removed + // worktree so stale drafts and view modes never accumulate in memory. + const removedFileIds = new Set( + s.openFiles.filter((f) => f.worktreeId === worktreeId).map((f) => f.id) + ) + const nextEditorDrafts = removedFileIds.size > 0 ? { ...s.editorDrafts } : s.editorDrafts + const nextMarkdownViewMode = + removedFileIds.size > 0 ? { ...s.markdownViewMode } : s.markdownViewMode + if (removedFileIds.size > 0) { + for (const fileId of removedFileIds) { + delete nextEditorDrafts[fileId] + delete nextMarkdownViewMode[fileId] + } + } + const nextExpandedDirs = { ...s.expandedDirs } + delete nextExpandedDirs[worktreeId] // If the active file belonged to the removed worktree, clear it const activeFileCleared = s.activeFileId ? s.openFiles.some((f) => f.id === s.activeFileId && f.worktreeId === worktreeId) @@ -161,6 +216,22 @@ export const createWorktreeSlice: StateCreator activeFileIdByWorktree: nextActiveFileIdByWorktree, activeBrowserTabIdByWorktree: nextActiveBrowserTabIdByWorktree, activeTabTypeByWorktree: nextActiveTabTypeByWorktree, + activeTabIdByWorktree: nextActiveTabIdByWorktree, + tabBarOrderByWorktree: nextTabBarOrderByWorktree, + pendingReconnectTabByWorktree: nextPendingReconnectTabByWorktree, + unifiedTabsByWorktree: nextUnifiedTabsByWorktree, + groupsByWorktree: nextGroupsByWorktree, + activeGroupIdByWorktree: nextActiveGroupIdByWorktree, + layoutByWorktree: nextLayoutByWorktree, + editorDrafts: nextEditorDrafts, + markdownViewMode: nextMarkdownViewMode, + expandedDirs: nextExpandedDirs, + gitStatusByWorktree: nextGitStatusByWorktree, + gitConflictOperationByWorktree: nextGitConflictOperationByWorktree, + trackedConflictPathsByWorktree: nextTrackedConflictPathsByWorktree, + gitBranchChangesByWorktree: nextGitBranchChangesByWorktree, + gitBranchCompareSummaryByWorktree: nextGitBranchCompareSummaryByWorktree, + gitBranchCompareRequestKeyByWorktree: nextGitBranchCompareRequestKeyByWorktree, activeFileId: activeFileCleared ? null : s.activeFileId, activeBrowserTabId: removedActiveWorktree ? null : s.activeBrowserTabId, activeTabType: removedActiveWorktree || activeFileCleared ? 'terminal' : s.activeTabType,