|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act, type ReactNode } from 'react' |
| 5 | +import { createRoot, type Root } from 'react-dom/client' |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | + |
| 8 | +const { mockToastError } = vi.hoisted(() => ({ |
| 9 | + mockToastError: vi.fn(), |
| 10 | +})) |
| 11 | + |
| 12 | +vi.mock('@sim/emcn', () => ({ |
| 13 | + Loader: () => <span aria-hidden='true' />, |
| 14 | + Modal: ({ |
| 15 | + children, |
| 16 | + open, |
| 17 | + onOpenChange, |
| 18 | + }: { |
| 19 | + children: ReactNode |
| 20 | + open: boolean |
| 21 | + onOpenChange: (open: boolean) => void |
| 22 | + }) => |
| 23 | + open ? ( |
| 24 | + <div> |
| 25 | + {children} |
| 26 | + <button type='button' onClick={() => onOpenChange(false)}> |
| 27 | + Close |
| 28 | + </button> |
| 29 | + </div> |
| 30 | + ) : null, |
| 31 | + ModalBody: ({ children }: { children: ReactNode }) => <div>{children}</div>, |
| 32 | + ModalContent: ({ children }: { children: ReactNode }) => <div>{children}</div>, |
| 33 | + ModalDescription: ({ children }: { children: ReactNode }) => <p>{children}</p>, |
| 34 | + ModalHeader: ({ children }: { children: ReactNode }) => <h2>{children}</h2>, |
| 35 | + toast: { error: mockToastError }, |
| 36 | +})) |
| 37 | + |
| 38 | +import { |
| 39 | + SnapshotBoundary, |
| 40 | + SnapshotModalFallback, |
| 41 | +} from '@/app/workspace/[workspaceId]/logs/components/log-details/components/execution-snapshot/snapshot-boundary' |
| 42 | + |
| 43 | +const LOAD_ERROR = new Error('snapshot chunk failed') |
| 44 | + |
| 45 | +function ThrowingSnapshot() { |
| 46 | + throw LOAD_ERROR |
| 47 | +} |
| 48 | + |
| 49 | +describe('SnapshotBoundary', () => { |
| 50 | + let container: HTMLDivElement |
| 51 | + let root: Root |
| 52 | + |
| 53 | + beforeEach(() => { |
| 54 | + vi.clearAllMocks() |
| 55 | + container = document.createElement('div') |
| 56 | + document.body.appendChild(container) |
| 57 | + act(() => { |
| 58 | + root = createRoot(container) |
| 59 | + }) |
| 60 | + }) |
| 61 | + |
| 62 | + afterEach(() => { |
| 63 | + act(() => root.unmount()) |
| 64 | + container.remove() |
| 65 | + }) |
| 66 | + |
| 67 | + it('contains a background pre-warm failure without notifying or closing', () => { |
| 68 | + const onLoadError = vi.fn() |
| 69 | + |
| 70 | + act(() => { |
| 71 | + root.render( |
| 72 | + <SnapshotBoundary isOpen={false} onLoadError={onLoadError}> |
| 73 | + <ThrowingSnapshot /> |
| 74 | + </SnapshotBoundary> |
| 75 | + ) |
| 76 | + }) |
| 77 | + |
| 78 | + expect(container.childNodes).toHaveLength(0) |
| 79 | + expect(mockToastError).not.toHaveBeenCalled() |
| 80 | + expect(onLoadError).not.toHaveBeenCalled() |
| 81 | + }) |
| 82 | + |
| 83 | + it('notifies and closes an explicitly opened snapshot after a load failure', () => { |
| 84 | + const onLoadError = vi.fn() |
| 85 | + |
| 86 | + act(() => { |
| 87 | + root.render( |
| 88 | + <SnapshotBoundary isOpen onLoadError={onLoadError}> |
| 89 | + <ThrowingSnapshot /> |
| 90 | + </SnapshotBoundary> |
| 91 | + ) |
| 92 | + }) |
| 93 | + |
| 94 | + expect(container.childNodes).toHaveLength(0) |
| 95 | + expect(mockToastError).toHaveBeenCalledWith( |
| 96 | + 'Could not load the workflow snapshot. Refresh and try again.' |
| 97 | + ) |
| 98 | + expect(onLoadError).toHaveBeenCalledOnce() |
| 99 | + }) |
| 100 | + |
| 101 | + it('keeps the modal shell visible while the snapshot bundle loads', () => { |
| 102 | + const onClose = vi.fn() |
| 103 | + |
| 104 | + act(() => { |
| 105 | + root.render(<SnapshotModalFallback isOpen onClose={onClose} />) |
| 106 | + }) |
| 107 | + |
| 108 | + expect(container.textContent).toContain('Workflow State') |
| 109 | + expect(container.textContent).toContain('Loading run snapshot…') |
| 110 | + |
| 111 | + const closeButton = container.querySelector('button') |
| 112 | + expect(closeButton).not.toBeNull() |
| 113 | + act(() => closeButton?.click()) |
| 114 | + expect(onClose).toHaveBeenCalledOnce() |
| 115 | + }) |
| 116 | +}) |
0 commit comments