|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act } from 'react' |
| 5 | +import { createRoot, type Root } from 'react-dom/client' |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | +import type { BackgroundWorkItem } from '@/lib/api/contracts/workspace-fork' |
| 8 | + |
| 9 | +const { mockUseWorkspaceBackgroundWork } = vi.hoisted(() => ({ |
| 10 | + mockUseWorkspaceBackgroundWork: vi.fn(), |
| 11 | +})) |
| 12 | + |
| 13 | +vi.mock('@/ee/workspace-forking/hooks/background-work', () => ({ |
| 14 | + useWorkspaceBackgroundWork: mockUseWorkspaceBackgroundWork, |
| 15 | +})) |
| 16 | + |
| 17 | +vi.mock('@/app/workspace/[workspaceId]/settings/components/settings-empty-state', () => ({ |
| 18 | + SettingsEmptyState: ({ children }: { children: React.ReactNode }) => <div>{children}</div>, |
| 19 | +})) |
| 20 | + |
| 21 | +vi.mock('@/app/workspace/[workspaceId]/components', () => ({ |
| 22 | + FloatingOverflowText: ({ label, className }: { label: string; className?: string }) => ( |
| 23 | + <span className={className}>{label}</span> |
| 24 | + ), |
| 25 | +})) |
| 26 | + |
| 27 | +import { ForkActivityPanel } from '@/ee/workspace-forking/components/fork-activity-panel/fork-activity-panel' |
| 28 | + |
| 29 | +const WORKSPACE_ID = 'ws-1' |
| 30 | +const PARTNER_ID = 'ws-parent' |
| 31 | +const WORKSPACE_NAMES = new Map([[PARTNER_ID, 'another workspace']]) |
| 32 | + |
| 33 | +function makeJob(overrides: Partial<BackgroundWorkItem> = {}): BackgroundWorkItem { |
| 34 | + return { |
| 35 | + id: 'job-1', |
| 36 | + workspaceId: PARTNER_ID, |
| 37 | + workflowId: null, |
| 38 | + kind: 'fork_content_copy', |
| 39 | + status: 'completed', |
| 40 | + message: null, |
| 41 | + error: null, |
| 42 | + metadata: { childWorkspaceId: WORKSPACE_ID, actorName: 'Brandon Tarr' }, |
| 43 | + startedAt: '2026-07-28T15:58:00.000Z', |
| 44 | + completedAt: null, |
| 45 | + ...overrides, |
| 46 | + } as BackgroundWorkItem |
| 47 | +} |
| 48 | + |
| 49 | +let container: HTMLDivElement |
| 50 | +let root: Root |
| 51 | + |
| 52 | +function renderJobs(jobs: BackgroundWorkItem[]) { |
| 53 | + mockUseWorkspaceBackgroundWork.mockReturnValue({ |
| 54 | + data: { pages: [{ items: jobs, nextCursor: null }] }, |
| 55 | + isPending: false, |
| 56 | + isError: false, |
| 57 | + hasNextPage: false, |
| 58 | + fetchNextPage: vi.fn(), |
| 59 | + isFetchingNextPage: false, |
| 60 | + }) |
| 61 | + act(() => { |
| 62 | + root.render(<ForkActivityPanel workspaceId={WORKSPACE_ID} workspaceNames={WORKSPACE_NAMES} />) |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +/** The Event-column badge for the single rendered row (`Badge`'s base class). */ |
| 67 | +function badgeElement(): HTMLElement { |
| 68 | + const badge = container.querySelector<HTMLElement>('.inline-flex') |
| 69 | + if (!badge) throw new Error('badge not found') |
| 70 | + return badge |
| 71 | +} |
| 72 | + |
| 73 | +/** Hover the badge the way React sees it — `onPointerEnter` is delegated from `pointerover`. */ |
| 74 | +function hover(element: HTMLElement) { |
| 75 | + act(() => { |
| 76 | + element.dispatchEvent( |
| 77 | + new MouseEvent('pointerover', { bubbles: true, clientX: 100, clientY: 100 }) |
| 78 | + ) |
| 79 | + }) |
| 80 | +} |
| 81 | + |
| 82 | +function tooltipText(): string | null { |
| 83 | + return document.body.querySelector('[role="tooltip"]')?.textContent ?? null |
| 84 | +} |
| 85 | + |
| 86 | +describe('ForkActivityPanel event badge tooltip', () => { |
| 87 | + beforeEach(() => { |
| 88 | + vi.clearAllMocks() |
| 89 | + container = document.createElement('div') |
| 90 | + document.body.appendChild(container) |
| 91 | + root = createRoot(container) |
| 92 | + }) |
| 93 | + |
| 94 | + afterEach(() => { |
| 95 | + act(() => root.unmount()) |
| 96 | + container.remove() |
| 97 | + }) |
| 98 | + |
| 99 | + it('shows the failure reason when hovering a failed (red) badge', () => { |
| 100 | + renderJobs([makeJob({ status: 'failed', error: 'Storage quota exceeded while copying files' })]) |
| 101 | + |
| 102 | + expect(tooltipText()).toBeNull() |
| 103 | + hover(badgeElement()) |
| 104 | + expect(tooltipText()).toBe('Storage quota exceeded while copying files') |
| 105 | + }) |
| 106 | + |
| 107 | + it('falls back to a generic label when a failed row carries no error text', () => { |
| 108 | + renderJobs([makeJob({ status: 'failed', error: null })]) |
| 109 | + |
| 110 | + hover(badgeElement()) |
| 111 | + expect(tooltipText()).toBe('Failed') |
| 112 | + }) |
| 113 | + |
| 114 | + it('truncates a very long failure reason', () => { |
| 115 | + renderJobs([makeJob({ status: 'failed', error: 'x'.repeat(500) })]) |
| 116 | + |
| 117 | + hover(badgeElement()) |
| 118 | + const text = tooltipText() ?? '' |
| 119 | + expect(text.endsWith('...')).toBe(true) |
| 120 | + expect(text.length).toBeLessThan(260) |
| 121 | + }) |
| 122 | + |
| 123 | + it('says "In progress" when hovering a processing (grey) badge', () => { |
| 124 | + renderJobs([makeJob({ status: 'processing' })]) |
| 125 | + |
| 126 | + hover(badgeElement()) |
| 127 | + expect(tooltipText()).toBe('In progress') |
| 128 | + }) |
| 129 | + |
| 130 | + it('says "Queued" when hovering a pending (grey) badge', () => { |
| 131 | + renderJobs([makeJob({ status: 'pending' })]) |
| 132 | + |
| 133 | + hover(badgeElement()) |
| 134 | + expect(tooltipText()).toBe('Queued') |
| 135 | + }) |
| 136 | + |
| 137 | + it('shows nothing when hovering a completed (blue) badge', () => { |
| 138 | + renderJobs([makeJob({ status: 'completed' })]) |
| 139 | + |
| 140 | + hover(badgeElement()) |
| 141 | + expect(tooltipText()).toBeNull() |
| 142 | + }) |
| 143 | + |
| 144 | + it('surfaces the partial-copy summary on a completed_with_warnings (amber) badge', () => { |
| 145 | + renderJobs([ |
| 146 | + makeJob({ |
| 147 | + status: 'completed_with_warnings', |
| 148 | + message: 'Copied 12 items; 3 could not be copied', |
| 149 | + }), |
| 150 | + ]) |
| 151 | + |
| 152 | + hover(badgeElement()) |
| 153 | + expect(tooltipText()).toBe('Copied 12 items; 3 could not be copied') |
| 154 | + }) |
| 155 | + |
| 156 | + it('keeps a still-running row hoverable by not rendering it as a disabled button', () => { |
| 157 | + renderJobs([makeJob({ status: 'processing' })]) |
| 158 | + |
| 159 | + // A processing row has no report yet, so it is not expandable. It must still |
| 160 | + // be a plain row — a disabled button would swallow the badge's hover events. |
| 161 | + expect(container.querySelector('button[disabled]')).toBeNull() |
| 162 | + }) |
| 163 | + |
| 164 | + it('still renders an expandable row as a button', () => { |
| 165 | + renderJobs([makeJob({ status: 'failed', error: 'boom' })]) |
| 166 | + |
| 167 | + const row = container.querySelector('button') |
| 168 | + expect(row).not.toBeNull() |
| 169 | + expect(row?.getAttribute('aria-expanded')).toBe('false') |
| 170 | + }) |
| 171 | +}) |
0 commit comments