|
| 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 } from 'vitest' |
| 7 | +import type { AgentGroupItem } from '@/app/workspace/[workspaceId]/home/components/message-content/components/agent-group/agent-group-view' |
| 8 | +import { |
| 9 | + BrowserAgentIcon, |
| 10 | + getBrowserAgentHostname, |
| 11 | +} from '@/app/workspace/[workspaceId]/home/components/message-content/components/agent-group/browser-agent-icon' |
| 12 | +import type { ToolCallData } from '@/app/workspace/[workspaceId]/home/types' |
| 13 | + |
| 14 | +function tool(overrides: Partial<ToolCallData> = {}): AgentGroupItem { |
| 15 | + return { |
| 16 | + type: 'tool', |
| 17 | + data: { |
| 18 | + id: 'browser-tool', |
| 19 | + toolName: 'browser_navigate', |
| 20 | + displayTitle: 'Opening page', |
| 21 | + status: 'success', |
| 22 | + result: { success: true, output: { url: 'https://example.com/document' } }, |
| 23 | + ...overrides, |
| 24 | + }, |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +describe('getBrowserAgentHostname', () => { |
| 29 | + it('uses the pending destination, then the observed redirect, and retains it during editing', () => { |
| 30 | + const navigating = tool({ |
| 31 | + status: 'executing', |
| 32 | + params: { url: 'https://example.org/start' }, |
| 33 | + result: undefined, |
| 34 | + }) |
| 35 | + expect(getBrowserAgentHostname([tool(), navigating])).toBe('example.org') |
| 36 | + const redirected = tool({ params: { url: 'https://example.org/start' } }) |
| 37 | + expect(getBrowserAgentHostname([redirected])).toBe('example.com') |
| 38 | + expect( |
| 39 | + getBrowserAgentHostname([ |
| 40 | + redirected, |
| 41 | + tool({ toolName: 'browser_type', status: 'executing', result: undefined }), |
| 42 | + ]) |
| 43 | + ).toBe('example.com') |
| 44 | + }) |
| 45 | + |
| 46 | + it.each(['error', 'cancelled', 'rejected', 'awaiting_approval'] as const)( |
| 47 | + 'does not adopt a destination from a %s tool', |
| 48 | + (status) => { |
| 49 | + expect( |
| 50 | + getBrowserAgentHostname([ |
| 51 | + tool(), |
| 52 | + tool({ status, params: { url: 'https://example.org' }, result: undefined }), |
| 53 | + ]) |
| 54 | + ).toBe('example.com') |
| 55 | + } |
| 56 | + ) |
| 57 | + |
| 58 | + it.each(['', 'about:blank', 'file:///document', 'data:text/html,hello', 'not a URL'])( |
| 59 | + 'clears the previous site for a page without an HTTP hostname: %s', |
| 60 | + (url) => { |
| 61 | + expect( |
| 62 | + getBrowserAgentHostname([ |
| 63 | + tool(), |
| 64 | + tool({ toolName: 'browser_switch_tab', result: { success: true, output: { url } } }), |
| 65 | + ]) |
| 66 | + ).toBeNull() |
| 67 | + } |
| 68 | + ) |
| 69 | + |
| 70 | + it.each(['browser_open_tab', 'browser_switch_tab', 'browser_close_tab', 'browser_go_back'])( |
| 71 | + 'clears an obsolete site while %s has no known destination', |
| 72 | + (toolName) => { |
| 73 | + expect( |
| 74 | + getBrowserAgentHostname([ |
| 75 | + tool(), |
| 76 | + tool({ toolName, status: 'executing', result: undefined }), |
| 77 | + ]) |
| 78 | + ).toBeNull() |
| 79 | + } |
| 80 | + ) |
| 81 | + |
| 82 | + it('uses the agent tab from a tab list, including legacy results', () => { |
| 83 | + const tabs = [ |
| 84 | + { tabId: 'visible', url: 'https://example.org' }, |
| 85 | + { tabId: 'agent', url: 'https://example.com' }, |
| 86 | + ] |
| 87 | + for (const output of [ |
| 88 | + { tabs, activeTabId: 'visible', automationTabId: 'agent' }, |
| 89 | + { tabs, activeTabId: 'agent' }, |
| 90 | + ]) { |
| 91 | + expect( |
| 92 | + getBrowserAgentHostname([ |
| 93 | + tool({ toolName: 'browser_list_tabs', result: { success: true, output } }), |
| 94 | + ]) |
| 95 | + ).toBe('example.com') |
| 96 | + } |
| 97 | + expect( |
| 98 | + getBrowserAgentHostname([ |
| 99 | + tool(), |
| 100 | + tool({ |
| 101 | + toolName: 'browser_list_tabs', |
| 102 | + result: { |
| 103 | + success: true, |
| 104 | + output: { tabs, activeTabId: 'visible', automationTabId: null }, |
| 105 | + }, |
| 106 | + }), |
| 107 | + ]) |
| 108 | + ).toBeNull() |
| 109 | + }) |
| 110 | + |
| 111 | + it('follows a click into a new tab and screenshot page metadata', () => { |
| 112 | + for (const [toolName, output] of [ |
| 113 | + [ |
| 114 | + 'browser_click', |
| 115 | + { activeTab: { url: 'https://example.org' }, effect: { tabChanged: true } }, |
| 116 | + ], |
| 117 | + ['browser_screenshot', { viewport: { url: 'https://example.org' } }], |
| 118 | + ['browser_extract', { page: { url: 'https://example.org' } }], |
| 119 | + ] as const) { |
| 120 | + expect( |
| 121 | + getBrowserAgentHostname([tool(), tool({ toolName, result: { success: true, output } })]) |
| 122 | + ).toBe('example.org') |
| 123 | + } |
| 124 | + }) |
| 125 | + |
| 126 | + it('clears a stale site when an action navigated without reporting its destination', () => { |
| 127 | + expect( |
| 128 | + getBrowserAgentHostname([ |
| 129 | + tool(), |
| 130 | + tool({ |
| 131 | + toolName: 'browser_click', |
| 132 | + result: { success: true, output: { effect: { urlChanged: true } } }, |
| 133 | + }), |
| 134 | + ]) |
| 135 | + ).toBeNull() |
| 136 | + }) |
| 137 | + |
| 138 | + it('ignores URLs from other tools and nested agent runs', () => { |
| 139 | + expect( |
| 140 | + getBrowserAgentHostname([ |
| 141 | + tool(), |
| 142 | + tool({ |
| 143 | + toolName: 'browser_read_text', |
| 144 | + params: { elementId: 4 }, |
| 145 | + result: { success: true, output: { url: 'https://example.org/embedded' } }, |
| 146 | + }), |
| 147 | + tool({ |
| 148 | + toolName: 'web_search', |
| 149 | + result: { success: true, output: { url: 'https://example.org' } }, |
| 150 | + }), |
| 151 | + { |
| 152 | + type: 'agent_group', |
| 153 | + group: { |
| 154 | + id: 'other-run', |
| 155 | + agentName: 'browser', |
| 156 | + agentLabel: 'Browser', |
| 157 | + isOpen: true, |
| 158 | + isDelegating: true, |
| 159 | + items: [tool({ result: { success: true, output: { url: 'https://example.org' } } })], |
| 160 | + }, |
| 161 | + }, |
| 162 | + ]) |
| 163 | + ).toBe('example.com') |
| 164 | + }) |
| 165 | +}) |
| 166 | + |
| 167 | +describe('BrowserAgentIcon', () => { |
| 168 | + let container: HTMLDivElement |
| 169 | + let root: Root |
| 170 | + |
| 171 | + beforeEach(() => { |
| 172 | + ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 173 | + container = document.createElement('div') |
| 174 | + root = createRoot(container) |
| 175 | + }) |
| 176 | + |
| 177 | + afterEach(() => act(() => root.unmount())) |
| 178 | + |
| 179 | + const render = (url: string) => { |
| 180 | + act(() => |
| 181 | + root.render( |
| 182 | + <BrowserAgentIcon items={[tool({ result: { success: true, output: { url } } })]} /> |
| 183 | + ) |
| 184 | + ) |
| 185 | + } |
| 186 | + |
| 187 | + it('keeps the globe until load and resets image state when the hostname changes', () => { |
| 188 | + render('https://example.com/document?token=private#section') |
| 189 | + const firstImage = container.querySelector('img')! |
| 190 | + expect(firstImage.src).toBe('https://www.google.com/s2/favicons?domain=example.com&sz=32') |
| 191 | + expect(container.querySelector('svg')).not.toBeNull() |
| 192 | + act(() => firstImage.dispatchEvent(new Event('load'))) |
| 193 | + expect(container.querySelector('svg')).toBeNull() |
| 194 | + |
| 195 | + render('https://example.com/another-document') |
| 196 | + expect(container.querySelector('img')).toBe(firstImage) |
| 197 | + expect(container.querySelector('svg')).toBeNull() |
| 198 | + |
| 199 | + render('https://example.org/document') |
| 200 | + expect(container.querySelector('img')).not.toBe(firstImage) |
| 201 | + expect(container.querySelector('svg')).not.toBeNull() |
| 202 | + act(() => firstImage.dispatchEvent(new Event('error'))) |
| 203 | + expect(container.querySelector('img')).not.toBeNull() |
| 204 | + act(() => container.querySelector('img')!.dispatchEvent(new Event('error'))) |
| 205 | + expect(container.querySelector('img')).toBeNull() |
| 206 | + expect(container.querySelector('svg')).not.toBeNull() |
| 207 | + |
| 208 | + render('https://example.com') |
| 209 | + expect(container.querySelector('img')).not.toBeNull() |
| 210 | + render('about:blank') |
| 211 | + expect(container.querySelector('img')).toBeNull() |
| 212 | + expect(container.querySelector('svg')).not.toBeNull() |
| 213 | + }) |
| 214 | +}) |
0 commit comments