|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 2 | +import { renderHook, waitFor } from '@testing-library/react' |
| 3 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
| 4 | +import { useWorkspace } from '../useWorkspace' |
| 5 | +import type { AssistantModeStatus, Repo } from '@opencode-manager/shared/types' |
| 6 | + |
| 7 | +const mocks = vi.hoisted(() => ({ |
| 8 | + getRepo: vi.fn(), |
| 9 | + getAssistantModeStatus: vi.fn(), |
| 10 | +})) |
| 11 | + |
| 12 | +vi.mock('@/api/repos', () => ({ |
| 13 | + getRepo: mocks.getRepo, |
| 14 | + getAssistantModeStatus: mocks.getAssistantModeStatus, |
| 15 | +})) |
| 16 | + |
| 17 | +const createWrapper = () => { |
| 18 | + const queryClient = new QueryClient({ |
| 19 | + defaultOptions: { |
| 20 | + queries: { retry: false }, |
| 21 | + mutations: { retry: false }, |
| 22 | + }, |
| 23 | + }) |
| 24 | + return ({ children }: { children: React.ReactNode }) => |
| 25 | + <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> |
| 26 | +} |
| 27 | + |
| 28 | +describe('useWorkspace', () => { |
| 29 | + beforeEach(() => { |
| 30 | + vi.clearAllMocks() |
| 31 | + }) |
| 32 | + |
| 33 | + describe('repoId === 0 (assistant)', () => { |
| 34 | + it('returns assistant workspace with correct properties', async () => { |
| 35 | + const mockStatus: AssistantModeStatus = { |
| 36 | + repoId: 0, |
| 37 | + directory: '/abs/assistant', |
| 38 | + relativePath: 'repos/assistant', |
| 39 | + files: { |
| 40 | + agentsMd: { path: '', exists: false, created: false }, |
| 41 | + opencodeJson: { path: '', exists: false, created: false }, |
| 42 | + }, |
| 43 | + schedulesSkill: { path: '', exists: false, created: false }, |
| 44 | + } |
| 45 | + |
| 46 | + mocks.getAssistantModeStatus.mockResolvedValue(mockStatus) |
| 47 | + |
| 48 | + const { result } = renderHook(() => useWorkspace(0), { wrapper: createWrapper() }) |
| 49 | + |
| 50 | + await waitFor(() => { |
| 51 | + expect(result.current.workspace).toBeDefined() |
| 52 | + }) |
| 53 | + |
| 54 | + expect(result.current.workspace?.kind).toBe('assistant') |
| 55 | + expect(result.current.workspace?.fullPath).toBe('/abs/assistant') |
| 56 | + expect(result.current.workspace?.repoId).toBe(0) |
| 57 | + expect(result.current.isLoading).toBe(false) |
| 58 | + expect(result.current.isError).toBe(false) |
| 59 | + }) |
| 60 | + |
| 61 | + it('does not call getRepo for assistant', async () => { |
| 62 | + mocks.getAssistantModeStatus.mockResolvedValue({ |
| 63 | + directory: '/abs/assistant', |
| 64 | + relativePath: 'repos/assistant', |
| 65 | + files: { agentsMd: { path: '', exists: false, created: false }, opencodeJson: { path: '', exists: false, created: false } }, |
| 66 | + schedulesSkill: { path: '', exists: false, created: false }, |
| 67 | + repoId: 0, |
| 68 | + }) |
| 69 | + |
| 70 | + renderHook(() => useWorkspace(0), { wrapper: createWrapper() }) |
| 71 | + |
| 72 | + await vi.waitFor(() => { |
| 73 | + expect(mocks.getRepo).not.toHaveBeenCalled() |
| 74 | + }) |
| 75 | + }) |
| 76 | + }) |
| 77 | + |
| 78 | + describe('repoId === 5 (real repo)', () => { |
| 79 | + it('returns repo workspace with correct properties', async () => { |
| 80 | + const mockRepo: Repo = { |
| 81 | + id: 5, |
| 82 | + repoUrl: 'https://x/my-repo', |
| 83 | + localPath: 'repos/my-repo', |
| 84 | + fullPath: '/abs/repos/my-repo', |
| 85 | + sourcePath: undefined, |
| 86 | + defaultBranch: 'main', |
| 87 | + cloneStatus: 'ready', |
| 88 | + clonedAt: 0, |
| 89 | + } |
| 90 | + |
| 91 | + mocks.getRepo.mockResolvedValue(mockRepo) |
| 92 | + |
| 93 | + const { result } = renderHook(() => useWorkspace(5), { wrapper: createWrapper() }) |
| 94 | + |
| 95 | + await waitFor(() => { |
| 96 | + expect(result.current.workspace).toBeDefined() |
| 97 | + }) |
| 98 | + |
| 99 | + expect(result.current.workspace?.kind).toBe('repo') |
| 100 | + expect(result.current.workspace?.repoId).toBe(5) |
| 101 | + expect(result.current.workspace?.fullPath).toBe('/abs/repos/my-repo') |
| 102 | + expect(result.current.workspace?.backHref).toBe('/repos/5') |
| 103 | + }) |
| 104 | + |
| 105 | + it('does not call getAssistantModeStatus for repo', async () => { |
| 106 | + const mockRepo: Repo = { |
| 107 | + id: 5, |
| 108 | + repoUrl: 'https://x/my-repo', |
| 109 | + localPath: 'repos/my-repo', |
| 110 | + fullPath: '/abs/repos/my-repo', |
| 111 | + sourcePath: undefined, |
| 112 | + defaultBranch: 'main', |
| 113 | + cloneStatus: 'ready', |
| 114 | + clonedAt: 0, |
| 115 | + } |
| 116 | + |
| 117 | + mocks.getRepo.mockResolvedValue(mockRepo) |
| 118 | + |
| 119 | + renderHook(() => useWorkspace(5), { wrapper: createWrapper() }) |
| 120 | + |
| 121 | + await vi.waitFor(() => { |
| 122 | + expect(mocks.getAssistantModeStatus).not.toHaveBeenCalled() |
| 123 | + }) |
| 124 | + }) |
| 125 | + }) |
| 126 | + |
| 127 | + describe('repoId === undefined', () => { |
| 128 | + it('returns undefined workspace', () => { |
| 129 | + const { result } = renderHook(() => useWorkspace(undefined), { wrapper: createWrapper() }) |
| 130 | + |
| 131 | + expect(result.current.workspace).toBeUndefined() |
| 132 | + expect(result.current.isLoading).toBe(false) |
| 133 | + }) |
| 134 | + }) |
| 135 | +}) |
0 commit comments