|
| 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 { MessageContent } from '@/app/workspace/[workspaceId]/home/components/message-content/message-content' |
| 9 | +import type { ContentBlock, ToolCallStatus } from '@/app/workspace/[workspaceId]/home/types' |
| 10 | + |
| 11 | +vi.mock('@/lib/auth/auth-client', () => ({ |
| 12 | + useSession: vi.fn(() => ({ data: null, isPending: false })), |
| 13 | +})) |
| 14 | + |
| 15 | +function start(name: string, parentSpanId = 'main'): ContentBlock { |
| 16 | + return { type: 'subagent', content: name, spanId: name, parentSpanId, timestamp: 1 } |
| 17 | +} |
| 18 | + |
| 19 | +function tool(spanId: string, status: ToolCallStatus = 'executing'): ContentBlock { |
| 20 | + return { |
| 21 | + type: 'tool_call', |
| 22 | + spanId, |
| 23 | + toolCall: { |
| 24 | + id: `${spanId}-read`, |
| 25 | + name: 'read', |
| 26 | + calledBy: spanId, |
| 27 | + status, |
| 28 | + activityDescription: `Reading ${spanId} notes`, |
| 29 | + }, |
| 30 | + timestamp: 2, |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +describe('MessageContent shared thinking indicator', () => { |
| 35 | + let queryClient: QueryClient |
| 36 | + let root: Root |
| 37 | + let container: HTMLDivElement |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + queryClient = new QueryClient() |
| 41 | + vi.useFakeTimers() |
| 42 | + vi.stubGlobal('matchMedia', vi.fn().mockReturnValue({ matches: false })) |
| 43 | + ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 44 | + container = document.createElement('div') |
| 45 | + document.body.appendChild(container) |
| 46 | + root = createRoot(container) |
| 47 | + }) |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + act(() => root.unmount()) |
| 51 | + container.remove() |
| 52 | + queryClient.clear() |
| 53 | + vi.useRealTimers() |
| 54 | + vi.unstubAllGlobals() |
| 55 | + }) |
| 56 | + |
| 57 | + const render = (blocks: ContentBlock[], isStreaming = true) => |
| 58 | + act(() => { |
| 59 | + root.render( |
| 60 | + <QueryClientProvider client={queryClient}> |
| 61 | + <MessageContent blocks={blocks} fallbackContent='' isStreaming={isStreaming} isLast /> |
| 62 | + </QueryClientProvider> |
| 63 | + ) |
| 64 | + }) |
| 65 | + const thinking = () => container.querySelectorAll('[aria-hidden="false"] svg') |
| 66 | + const groups = () => container.querySelectorAll('[data-agent-group]') |
| 67 | + |
| 68 | + it('shares one indicator across parallel and nested empty agents', () => { |
| 69 | + render([start('workflow'), start('browser'), start('deploy', 'workflow')]) |
| 70 | + expect(thinking()).toHaveLength(1) |
| 71 | + expect(groups()).toHaveLength(0) |
| 72 | + expect(container.querySelector('[role="status"]')).toBeNull() |
| 73 | + }) |
| 74 | + |
| 75 | + it('hands off immediately to meaningful activity without remounting existing rows', () => { |
| 76 | + const blocks = [start('workflow'), start('browser')] |
| 77 | + render(blocks) |
| 78 | + expect(thinking()).toHaveLength(1) |
| 79 | + render([...blocks, tool('workflow')]) |
| 80 | + expect(thinking()).toHaveLength(0) |
| 81 | + expect(groups()).toHaveLength(1) |
| 82 | + const firstRow = container.querySelector('[role="status"]') |
| 83 | + expect(firstRow?.textContent).toBe('Reading workflow notes') |
| 84 | + render([...blocks, tool('workflow'), tool('browser')]) |
| 85 | + expect(groups()).toHaveLength(2) |
| 86 | + expect(container.querySelector('[role="status"]')).toBe(firstRow) |
| 87 | + expect(thinking()).toHaveLength(0) |
| 88 | + }) |
| 89 | + |
| 90 | + it('shows the pending indicator after the only visible agent finishes', () => { |
| 91 | + const blocks = [start('workflow'), start('browser'), tool('workflow', 'success')] |
| 92 | + render(blocks) |
| 93 | + expect(thinking()).toHaveLength(0) |
| 94 | + render([...blocks, { type: 'subagent_end', spanId: 'workflow', timestamp: 3 }]) |
| 95 | + expect(thinking()).toHaveLength(1) |
| 96 | + expect(groups()).toHaveLength(1) |
| 97 | + }) |
| 98 | + |
| 99 | + it('keeps a singleton action flat when its nested agent has no output', () => { |
| 100 | + render([start('workflow'), tool('workflow'), start('deploy', 'workflow')]) |
| 101 | + expect(groups()).toHaveLength(1) |
| 102 | + expect(container.querySelectorAll('[role="status"]')).toHaveLength(1) |
| 103 | + expect(container.querySelector('[role="button"]')).toBeNull() |
| 104 | + expect(thinking()).toHaveLength(0) |
| 105 | + }) |
| 106 | + |
| 107 | + it('retains nested activity while another child is empty', () => { |
| 108 | + render([ |
| 109 | + start('workflow'), |
| 110 | + start('deploy', 'workflow'), |
| 111 | + start('browser', 'workflow'), |
| 112 | + tool('deploy'), |
| 113 | + ]) |
| 114 | + const trigger = container.querySelector<HTMLElement>('[role="button"]')! |
| 115 | + act(() => trigger.click()) |
| 116 | + expect(container.querySelector('[data-state="open"]')?.textContent).toContain( |
| 117 | + 'Reading deploy notes' |
| 118 | + ) |
| 119 | + expect(container.querySelectorAll('[role="status"]')).toHaveLength(2) |
| 120 | + expect(thinking()).toHaveLength(0) |
| 121 | + }) |
| 122 | + |
| 123 | + it('preserves narration and skips whitespace-only output', () => { |
| 124 | + const blocks: ContentBlock[] = [ |
| 125 | + start('workflow'), |
| 126 | + { type: 'subagent_text', spanId: 'workflow', content: ' ', timestamp: 2 }, |
| 127 | + ] |
| 128 | + render(blocks) |
| 129 | + expect(groups()).toHaveLength(0) |
| 130 | + expect(thinking()).toHaveLength(1) |
| 131 | + render( |
| 132 | + [ |
| 133 | + ...blocks, |
| 134 | + { type: 'subagent_text', spanId: 'workflow', content: 'Checking the setup.', timestamp: 3 }, |
| 135 | + ], |
| 136 | + false |
| 137 | + ) |
| 138 | + expect(groups()).toHaveLength(1) |
| 139 | + act(() => container.querySelector<HTMLElement>('[role="button"]')!.click()) |
| 140 | + expect(container.querySelector('[data-state="open"]')?.textContent).toContain( |
| 141 | + 'Checking the setup.' |
| 142 | + ) |
| 143 | + }) |
| 144 | + |
| 145 | + it.each(['awaiting_approval', 'error', 'cancelled', 'rejected'] as const)( |
| 146 | + 'keeps %s tool rows visible while another agent is pending', |
| 147 | + (status) => { |
| 148 | + render([start('workflow'), start('browser'), tool('workflow', status)]) |
| 149 | + expect(groups()).toHaveLength(1) |
| 150 | + expect(container.querySelector('[role="status"]')?.textContent).toContain('workflow notes') |
| 151 | + expect(thinking()).toHaveLength(1) |
| 152 | + } |
| 153 | + ) |
| 154 | + |
| 155 | + it('keeps thinking hidden while prose streams and finishes revealing', () => { |
| 156 | + const blocks: ContentBlock[] = [ |
| 157 | + start('browser'), |
| 158 | + { |
| 159 | + type: 'text', |
| 160 | + content: 'Here is the result of reviewing the project and checking its configuration.', |
| 161 | + timestamp: 3, |
| 162 | + }, |
| 163 | + ] |
| 164 | + render([start('browser'), { type: 'text', content: 'Here is', timestamp: 3 }]) |
| 165 | + render(blocks) |
| 166 | + act(() => vi.advanceTimersByTime(100)) |
| 167 | + expect(thinking()).toHaveLength(0) |
| 168 | + render(blocks, false) |
| 169 | + expect(thinking()).toHaveLength(0) |
| 170 | + }) |
| 171 | + |
| 172 | + it('waits for the normal quiet period between prose chunks while an agent is pending', () => { |
| 173 | + const prose = 'Here is the result of reviewing the project and checking its configuration.' |
| 174 | + const blocks: ContentBlock[] = [ |
| 175 | + start('browser'), |
| 176 | + { type: 'text', content: prose, timestamp: 3 }, |
| 177 | + ] |
| 178 | + render(blocks) |
| 179 | + expect(thinking()).toHaveLength(0) |
| 180 | + act(() => vi.advanceTimersByTime(1_499)) |
| 181 | + expect(thinking()).toHaveLength(0) |
| 182 | + act(() => vi.advanceTimersByTime(1)) |
| 183 | + expect(thinking()).toHaveLength(1) |
| 184 | + render([...blocks, { type: 'text', content: ' The configuration is valid.', timestamp: 4 }]) |
| 185 | + expect(thinking()).toHaveLength(0) |
| 186 | + }) |
| 187 | + |
| 188 | + it('removes thinking when a turn stops or finishes without agent output', () => { |
| 189 | + const blocks = [start('workflow'), start('browser')] |
| 190 | + render(blocks) |
| 191 | + expect(thinking()).toHaveLength(1) |
| 192 | + render([...blocks, { type: 'stopped', timestamp: 3 }], false) |
| 193 | + expect(thinking()).toHaveLength(0) |
| 194 | + expect(groups()).toHaveLength(0) |
| 195 | + expect(container.textContent).toContain('Stopped') |
| 196 | + render(blocks, false) |
| 197 | + expect(thinking()).toHaveLength(0) |
| 198 | + expect(groups()).toHaveLength(0) |
| 199 | + }) |
| 200 | +}) |
0 commit comments