|
1 | 1 | import { beforeEach, describe, expect, it, vi } from 'vitest' |
2 | | -import { fireEvent, render, screen, waitFor } from '@testing-library/react' |
| 2 | +import { act, fireEvent, render, screen, waitFor } from '@testing-library/react' |
3 | 3 | import FileTreePanel from './FileTreePanel' |
4 | 4 | import { useUIStore } from '@/stores/useUIStore' |
5 | 5 | import { useWorkspaceStore } from '@/stores/useWorkspaceStore' |
@@ -27,7 +27,10 @@ describe('FileTreePanel', () => { |
27 | 27 | } as never) |
28 | 28 | useWorkspaceStore.setState({ |
29 | 29 | currentWorkspaceHash: 'ws-1', |
30 | | - workspaces: [{ hash: 'ws-1', name: 'demo', path: 'D:/demo', createdAt: '', updatedAt: '' }], |
| 30 | + workspaces: [ |
| 31 | + { hash: 'ws-1', name: 'demo-1', path: 'D:/demo-1', createdAt: '', updatedAt: '' }, |
| 32 | + { hash: 'ws-2', name: 'demo-2', path: 'D:/demo-2', createdAt: '', updatedAt: '' }, |
| 33 | + ], |
31 | 34 | } as never) |
32 | 35 | }) |
33 | 36 |
|
@@ -121,4 +124,140 @@ describe('FileTreePanel', () => { |
121 | 124 | const scrollArea = screen.getByTestId('file-tree-scroll-area') |
122 | 125 | expect(scrollArea).toHaveStyle({ overflowY: 'auto', minHeight: '0px', flex: '1 1 0%' }) |
123 | 126 | }) |
| 127 | + |
| 128 | + it('reloads the root tree when the workspace changes', async () => { |
| 129 | + mockGatewayAPI = { |
| 130 | + listFiles: vi.fn().mockImplementation(async ({ path = '' }: { path?: string }) => { |
| 131 | + const workspaceHash = useWorkspaceStore.getState().currentWorkspaceHash |
| 132 | + if (path) { |
| 133 | + return { payload: { files: [] } } |
| 134 | + } |
| 135 | + if (workspaceHash === 'ws-1') { |
| 136 | + return { |
| 137 | + payload: { |
| 138 | + files: [{ name: 'old.go', path: 'old.go', is_dir: false }], |
| 139 | + }, |
| 140 | + } |
| 141 | + } |
| 142 | + return { |
| 143 | + payload: { |
| 144 | + files: [{ name: 'new.ts', path: 'new.ts', is_dir: false }], |
| 145 | + }, |
| 146 | + } |
| 147 | + }), |
| 148 | + readFile: vi.fn(), |
| 149 | + } |
| 150 | + |
| 151 | + render(<FileTreePanel />) |
| 152 | + |
| 153 | + await waitFor(() => { |
| 154 | + expect(screen.getByText('old.go')).toBeTruthy() |
| 155 | + }) |
| 156 | + |
| 157 | + act(() => { |
| 158 | + useWorkspaceStore.setState({ currentWorkspaceHash: 'ws-2' } as never) |
| 159 | + }) |
| 160 | + |
| 161 | + await waitFor(() => { |
| 162 | + expect(screen.getByText('new.ts')).toBeTruthy() |
| 163 | + }) |
| 164 | + expect(screen.queryByText('old.go')).toBeNull() |
| 165 | + }) |
| 166 | + |
| 167 | + it('does not keep expanded directory state across workspace switches', async () => { |
| 168 | + mockGatewayAPI = { |
| 169 | + listFiles: vi.fn().mockImplementation(async ({ path = '' }: { path?: string }) => { |
| 170 | + const workspaceHash = useWorkspaceStore.getState().currentWorkspaceHash |
| 171 | + if (!path) { |
| 172 | + return { |
| 173 | + payload: { |
| 174 | + files: [{ name: 'src', path: 'src', is_dir: true }], |
| 175 | + }, |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + if (workspaceHash === 'ws-1') { |
| 180 | + return { |
| 181 | + payload: { |
| 182 | + files: [{ name: 'old.txt', path: 'src/old.txt', is_dir: false }], |
| 183 | + }, |
| 184 | + } |
| 185 | + } |
| 186 | + return { |
| 187 | + payload: { |
| 188 | + files: [{ name: 'new.txt', path: 'src/new.txt', is_dir: false }], |
| 189 | + }, |
| 190 | + } |
| 191 | + }), |
| 192 | + readFile: vi.fn(), |
| 193 | + } |
| 194 | + |
| 195 | + render(<FileTreePanel />) |
| 196 | + |
| 197 | + await waitFor(() => { |
| 198 | + expect(screen.getByText('src')).toBeTruthy() |
| 199 | + }) |
| 200 | + |
| 201 | + fireEvent.click(screen.getByText('src')) |
| 202 | + |
| 203 | + await waitFor(() => { |
| 204 | + expect(screen.getByText('old.txt')).toBeTruthy() |
| 205 | + }) |
| 206 | + |
| 207 | + act(() => { |
| 208 | + useWorkspaceStore.setState({ currentWorkspaceHash: 'ws-2' } as never) |
| 209 | + }) |
| 210 | + |
| 211 | + await waitFor(() => { |
| 212 | + expect(screen.getByText('src')).toBeTruthy() |
| 213 | + }) |
| 214 | + expect(screen.queryByText('old.txt')).toBeNull() |
| 215 | + expect(screen.queryByText('new.txt')).toBeNull() |
| 216 | + }) |
| 217 | + |
| 218 | + it('ignores late root responses from the previous workspace', async () => { |
| 219 | + let resolveOldRoot: ((value: { payload: { files: Array<{ name: string; path: string; is_dir: boolean }> } }) => void) | null = null |
| 220 | + |
| 221 | + mockGatewayAPI = { |
| 222 | + listFiles: vi.fn().mockImplementation(({ path = '' }: { path?: string }) => { |
| 223 | + const workspaceHash = useWorkspaceStore.getState().currentWorkspaceHash |
| 224 | + if (path) { |
| 225 | + return Promise.resolve({ payload: { files: [] } }) |
| 226 | + } |
| 227 | + if (workspaceHash === 'ws-1') { |
| 228 | + return new Promise((resolve) => { |
| 229 | + resolveOldRoot = resolve |
| 230 | + }) |
| 231 | + } |
| 232 | + return Promise.resolve({ |
| 233 | + payload: { |
| 234 | + files: [{ name: 'new.ts', path: 'new.ts', is_dir: false }], |
| 235 | + }, |
| 236 | + }) |
| 237 | + }), |
| 238 | + readFile: vi.fn(), |
| 239 | + } |
| 240 | + |
| 241 | + render(<FileTreePanel />) |
| 242 | + |
| 243 | + act(() => { |
| 244 | + useWorkspaceStore.setState({ currentWorkspaceHash: 'ws-2' } as never) |
| 245 | + }) |
| 246 | + |
| 247 | + await waitFor(() => { |
| 248 | + expect(screen.getByText('new.ts')).toBeTruthy() |
| 249 | + }) |
| 250 | + |
| 251 | + await act(async () => { |
| 252 | + resolveOldRoot?.({ |
| 253 | + payload: { |
| 254 | + files: [{ name: 'old.go', path: 'old.go', is_dir: false }], |
| 255 | + }, |
| 256 | + }) |
| 257 | + await Promise.resolve() |
| 258 | + }) |
| 259 | + |
| 260 | + expect(screen.getByText('new.ts')).toBeTruthy() |
| 261 | + expect(screen.queryByText('old.go')).toBeNull() |
| 262 | + }) |
124 | 263 | }) |
0 commit comments