|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act } from 'react' |
| 5 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
| 6 | +import { createRoot, type Root } from 'react-dom/client' |
| 7 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 8 | +import { ApiClientError } from '@/lib/api/client/errors' |
| 9 | +import type { PausePointWithQueue } from '@/hooks/queries/resume-execution' |
| 10 | + |
| 11 | +const mocks = vi.hoisted(() => ({ |
| 12 | + pauseContextDetail: vi.fn(), |
| 13 | + refetch: vi.fn(), |
| 14 | + replace: vi.fn(), |
| 15 | + resumeContext: vi.fn(), |
| 16 | + resumeExecutionDetail: vi.fn(), |
| 17 | +})) |
| 18 | + |
| 19 | +vi.mock('next/navigation', () => ({ |
| 20 | + useRouter: () => ({ replace: mocks.replace }), |
| 21 | +})) |
| 22 | + |
| 23 | +vi.mock('@/hooks/queries/resume-execution', () => ({ |
| 24 | + resumeKeys: { |
| 25 | + execution: (workflowId: string, executionId: string) => [ |
| 26 | + 'resume-execution', |
| 27 | + 'execution', |
| 28 | + workflowId, |
| 29 | + executionId, |
| 30 | + ], |
| 31 | + context: (workflowId: string, executionId: string, contextId: string) => [ |
| 32 | + 'resume-execution', |
| 33 | + 'context', |
| 34 | + workflowId, |
| 35 | + executionId, |
| 36 | + contextId, |
| 37 | + ], |
| 38 | + }, |
| 39 | + usePauseContextDetail: mocks.pauseContextDetail, |
| 40 | + useResumeContext: mocks.resumeContext, |
| 41 | + useResumeExecutionDetail: mocks.resumeExecutionDetail, |
| 42 | +})) |
| 43 | + |
| 44 | +import ResumeExecutionPage, { |
| 45 | + selectInitialResumeContextId, |
| 46 | +} from '@/app/(interfaces)/resume/[workflowId]/[executionId]/resume-page-client' |
| 47 | + |
| 48 | +const params = { workflowId: 'workflow-1', executionId: 'execution-1' } |
| 49 | + |
| 50 | +let container: HTMLDivElement |
| 51 | +let queryClient: QueryClient |
| 52 | +let root: Root |
| 53 | + |
| 54 | +function apiError(status: number): ApiClientError { |
| 55 | + return new ApiClientError({ |
| 56 | + status, |
| 57 | + message: status === 404 ? 'Workflow not found' : 'Request failed', |
| 58 | + body: { error: 'Request failed' }, |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +function renderPage(initialContextId?: string) { |
| 63 | + act(() => { |
| 64 | + root.render( |
| 65 | + <QueryClientProvider client={queryClient}> |
| 66 | + <ResumeExecutionPage params={params} initialContextId={initialContextId} /> |
| 67 | + </QueryClientProvider> |
| 68 | + ) |
| 69 | + }) |
| 70 | +} |
| 71 | + |
| 72 | +describe('ResumeExecutionPage', () => { |
| 73 | + beforeEach(() => { |
| 74 | + ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 75 | + container = document.createElement('div') |
| 76 | + document.body.appendChild(container) |
| 77 | + root = createRoot(container) |
| 78 | + queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }) |
| 79 | + mocks.pauseContextDetail.mockReturnValue({ data: undefined, isLoading: false }) |
| 80 | + mocks.resumeContext.mockReturnValue({ mutateAsync: vi.fn() }) |
| 81 | + mocks.resumeExecutionDetail.mockReturnValue({ |
| 82 | + data: undefined, |
| 83 | + error: null, |
| 84 | + isError: false, |
| 85 | + isFetching: true, |
| 86 | + isLoading: true, |
| 87 | + refetch: mocks.refetch, |
| 88 | + }) |
| 89 | + }) |
| 90 | + |
| 91 | + afterEach(() => { |
| 92 | + act(() => root.unmount()) |
| 93 | + queryClient.clear() |
| 94 | + container.remove() |
| 95 | + vi.clearAllMocks() |
| 96 | + }) |
| 97 | + |
| 98 | + it('renders a concealed state for an absent or newly inaccessible execution', () => { |
| 99 | + mocks.resumeExecutionDetail.mockReturnValue({ |
| 100 | + data: undefined, |
| 101 | + error: apiError(404), |
| 102 | + isError: true, |
| 103 | + isFetching: false, |
| 104 | + isLoading: false, |
| 105 | + refetch: mocks.refetch, |
| 106 | + }) |
| 107 | + |
| 108 | + renderPage('context-1') |
| 109 | + |
| 110 | + expect(container.textContent).toContain('Execution Not Found') |
| 111 | + expect(container.textContent).not.toContain('Could Not Load Execution') |
| 112 | + expect(mocks.pauseContextDetail).toHaveBeenLastCalledWith( |
| 113 | + params.workflowId, |
| 114 | + params.executionId, |
| 115 | + undefined |
| 116 | + ) |
| 117 | + }) |
| 118 | + |
| 119 | + it('redirects an expired session back through login', () => { |
| 120 | + mocks.resumeExecutionDetail.mockReturnValue({ |
| 121 | + data: undefined, |
| 122 | + error: apiError(401), |
| 123 | + isError: true, |
| 124 | + isFetching: false, |
| 125 | + isLoading: false, |
| 126 | + refetch: mocks.refetch, |
| 127 | + }) |
| 128 | + |
| 129 | + renderPage('context-1') |
| 130 | + |
| 131 | + const callbackPath = '/resume/workflow-1/execution-1?contextId=context-1' |
| 132 | + expect(mocks.replace).toHaveBeenCalledWith( |
| 133 | + `/login?callbackUrl=${encodeURIComponent(callbackPath)}` |
| 134 | + ) |
| 135 | + expect(container.textContent).toContain('Redirecting to sign in') |
| 136 | + }) |
| 137 | + |
| 138 | + it('shows a retryable error instead of mislabeling infrastructure failure', () => { |
| 139 | + mocks.resumeExecutionDetail.mockReturnValue({ |
| 140 | + data: undefined, |
| 141 | + error: apiError(500), |
| 142 | + isError: true, |
| 143 | + isFetching: false, |
| 144 | + isLoading: false, |
| 145 | + refetch: mocks.refetch, |
| 146 | + }) |
| 147 | + |
| 148 | + renderPage() |
| 149 | + |
| 150 | + expect(container.textContent).toContain('Could Not Load Execution') |
| 151 | + expect(container.textContent).not.toContain('Execution Not Found') |
| 152 | + const retryButton = Array.from(container.querySelectorAll('button')).find( |
| 153 | + (button) => button.textContent === 'Try again' |
| 154 | + ) |
| 155 | + expect(retryButton).toBeDefined() |
| 156 | + act(() => retryButton?.dispatchEvent(new MouseEvent('click', { bubbles: true }))) |
| 157 | + expect(mocks.refetch).toHaveBeenCalledOnce() |
| 158 | + }) |
| 159 | +}) |
| 160 | + |
| 161 | +describe('selectInitialResumeContextId', () => { |
| 162 | + const pausePoints = [ |
| 163 | + { contextId: 'resumed-context', resumeStatus: 'resumed' }, |
| 164 | + { contextId: 'paused-context', resumeStatus: 'paused' }, |
| 165 | + ] as PausePointWithQueue[] |
| 166 | + |
| 167 | + it('uses a requested context only when the authorized execution contains it', () => { |
| 168 | + expect(selectInitialResumeContextId(pausePoints, 'paused-context')).toBe('paused-context') |
| 169 | + expect(selectInitialResumeContextId(pausePoints, 'unknown-context')).toBe('paused-context') |
| 170 | + }) |
| 171 | + |
| 172 | + it('falls back to the first context when none is paused', () => { |
| 173 | + expect( |
| 174 | + selectInitialResumeContextId( |
| 175 | + [{ contextId: 'first-context', resumeStatus: 'resumed' }] as PausePointWithQueue[], |
| 176 | + null |
| 177 | + ) |
| 178 | + ).toBe('first-context') |
| 179 | + }) |
| 180 | +}) |
0 commit comments