managedBrowser.test.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. import {afterEach,beforeEach,expect,it,vi} from 'vitest'
  2. import {flushPromises,mount} from '@vue/test-utils'
  3. import ComputerPanel from '@/components/workspace/ComputerPanel.vue'
  4. import Preview from '@/components/workspace/LocalVmChatPanel.vue'
  5. import Viewer from '@/views/ComputerViewerView.vue'
  6. import BrowserToolbar from '@/components/workspace/BrowserToolbar.vue'
  7. import {apiRequest} from '@/api/client'
  8. import type {ManagedBrowserState} from '@shared/managedBrowser'
  9. vi.mock('@/api/client',()=>({apiRequest:vi.fn()}))
  10. const route={params:{agentId:'bot'},query:{} as Record<string,unknown>}
  11. vi.mock('vue-router',()=>({useRoute:()=>route}))
  12. const request=vi.mocked(apiRequest)
  13. const agent={id:'bot',name:'竹儿',profile:'default',nodeId:'local'} as any
  14. const permission={supported:true,authorized:true,screen:true,accessibility:true,ready:true,fullAuthorized:true}
  15. const stubs={AppIcon:true,GrokAuthPanel:true}
  16. let enabled:boolean|undefined,state:ManagedBrowserState,controlled:boolean
  17. beforeEach(()=>{
  18. vi.spyOn(document,'hidden','get').mockReturnValue(false)
  19. route.query={};enabled=true;controlled=false
  20. state={enabled:true,available:true,open:false,generation:4,profile:'temporary',tabs:[],downloads:[]}
  21. request.mockReset()
  22. request.mockImplementation(async(path,options)=>{
  23. const url=new URL(path,'http://fixture'),backend=url.searchParams.get('backend')
  24. if(url.pathname==='/api/app/agents')return {agents:[agent]} as any
  25. if(url.pathname.endsWith('/host-tools'))return {scriptMachine:true,serverComputer:true,cloud:true,vm:true,managedBrowser:enabled} as any
  26. if(url.pathname.endsWith('/desktop-environment'))return {hosts:[{id:'local',name:'服务器',online:true,local:permission}]} as any
  27. if(url.pathname.endsWith('/cloud-computer'))return {configured:true,running:true,connected:true} as any
  28. if(url.pathname.endsWith('/local-vm'))return {enabled:true,image:true,container:'running',ready:true} as any
  29. if(url.pathname.endsWith('/managed-browser'))return structuredClone(state) as any
  30. if(url.pathname.endsWith('/managed-browser/action'))return {ok:true} as any
  31. if(url.pathname.endsWith('/frame'))return {id:'frame',data:backend,width:100,height:80,generation:4} as any
  32. if(url.pathname.includes('/computer')){
  33. if(options?.method==='POST'&&url.pathname.endsWith('/take')){
  34. controlled=true;state.open=true;state.tabs=[{id:'tab',title:'示例页面',url:'https://example.com',active:true}]
  35. }
  36. if(options?.method==='POST'&&url.pathname.endsWith('/giveback'))controlled=false
  37. return {mode:controlled?'human':backend==='managed-browser'&&!state.open?'off':'idle',backend:backend==='managed-browser'?'managed-browser':'local',hostName:'托管浏览器',generation:4,controlId:controlled?'control':undefined,token:'fixture-token'} as any
  38. }
  39. throw new Error(`Unexpected request ${path}`)
  40. })
  41. })
  42. afterEach(()=>{vi.useRealTimers();vi.restoreAllMocks();vi.clearAllMocks()})
  43. it.each([false,undefined])('keeps managed browsing absent with an off or older settings response (%s)',async(value)=>{
  44. enabled=value
  45. const wrapper=mount(ComputerPanel,{props:{agents:[agent],standalone:true},global:{stubs}})
  46. try{
  47. await flushPromises()
  48. expect(wrapper.find('option[value="managed-browser"]').exists()).toBe(false)
  49. expect(request.mock.calls.some(([path])=>path.includes('/managed-browser'))).toBe(false)
  50. expect(request.mock.calls.some(([,options])=>options?.method==='POST')).toBe(false)
  51. }finally{wrapper.unmount()}
  52. })
  53. it('shows the independent browser beside existing environments without opening it during preview',async()=>{
  54. const wrapper=mount(Preview,{props:{agents:[agent],isAdmin:true,active:true},global:{stubs}})
  55. try{
  56. await flushPromises()
  57. for(const key of ['desktop:local','cloud','vm','managed-browser'])expect(wrapper.find(`option[value="${key}"]`).exists()).toBe(true)
  58. await wrapper.get('[aria-label="显示的桌面"]').setValue('managed-browser');await flushPromises()
  59. expect(wrapper.get('.preview').text()).toContain('浏览器尚未打开')
  60. expect(wrapper.find('.preview img').exists()).toBe(false)
  61. expect(request.mock.calls.some(([path,options])=>path.includes('managed-browser')&&options?.method==='POST')).toBe(false)
  62. const section=wrapper.get('[aria-label="托管浏览器"]')
  63. await section.get('button').trigger('click')
  64. expect(wrapper.emitted('desktop')?.at(-1)).toEqual([agent,'managed-browser',undefined])
  65. expect(request.mock.calls.some(([path])=>path.includes('/computer/take'))).toBe(false)
  66. }finally{wrapper.unmount()}
  67. })
  68. it('requires takeover before navigation and routes browser actions and Linux shortcuts to the managed target',async()=>{
  69. const wrapper=mount(ComputerPanel,{props:{agents:[agent],backend:'managed-browser',standalone:true},global:{stubs}})
  70. try{
  71. await flushPromises()
  72. expect(wrapper.text()).toContain('浏览器尚未打开')
  73. expect(request.mock.calls.some(([,options])=>options?.method==='POST')).toBe(false)
  74. expect(wrapper.get('[aria-label="浏览器地址"]').attributes('disabled')).toBeDefined()
  75. await wrapper.findAll('button').find(button=>button.text()==='打开并控制')!.trigger('click');await flushPromises()
  76. expect(wrapper.get('.computer-screen img').attributes('src')).toContain('base64,managed-browser')
  77. expect(request.mock.calls.find(([path])=>path.includes('/computer/take'))?.[0]).toContain('backend=managed-browser')
  78. await wrapper.get('[aria-label="浏览器地址"]').setValue('https://example.org/')
  79. await wrapper.get('.browser-toolbar form').trigger('submit');await flushPromises()
  80. expect(request).toHaveBeenCalledWith('/api/app/agents/bot/managed-browser/action',{method:'POST',body:expect.objectContaining({controlId:'control',token:'fixture-token',requestId:expect.any(String),generation:4,action:{kind:'navigate',url:'https://example.org/'}})})
  81. expect(request.mock.calls.some(([path])=>path.includes('/computer/browser')||path.endsWith('/browser'))).toBe(false)
  82. const action=request.mock.calls.find(([path])=>path.endsWith('/managed-browser/action'))?.[1]?.body
  83. expect(action).not.toHaveProperty('frameId')
  84. await wrapper.findAll('button').find(button=>button.text()==='键盘与操作')!.trigger('click')
  85. await wrapper.findAll('button').find(button=>button.text()==='全选')!.trigger('click');await flushPromises()
  86. expect(request).toHaveBeenCalledWith('/api/app/agents/bot/computer/input?backend=managed-browser',{method:'POST',body:expect.objectContaining({generation:4,frameId:'frame',action:{kind:'key',key:'a',modifiers:['ctrl']}})})
  87. await wrapper.get('.computer-screen img').trigger('keydown',{key:'c',metaKey:true});await flushPromises()
  88. expect(request).toHaveBeenCalledWith('/api/app/agents/bot/computer/input?backend=managed-browser',{method:'POST',body:expect.objectContaining({action:{kind:'key',key:'c',modifiers:['ctrl']}})})
  89. await wrapper.findAll('button').find(button=>button.text()==='结束控制')!.trigger('click');await flushPromises()
  90. expect(request.mock.calls.find(([path])=>path.includes('/giveback'))?.[0]).toContain('backend=managed-browser')
  91. }finally{wrapper.unmount()}
  92. })
  93. it('never switches an unavailable requested browser to another computer',async()=>{
  94. state.available=false;state.reason='执行节点离线'
  95. const wrapper=mount(ComputerPanel,{props:{agents:[agent],backend:'managed-browser',standalone:true,autoTake:true},global:{stubs}})
  96. try{
  97. await flushPromises()
  98. expect(wrapper.get('.browser-availability [role="status"]').text()).toBe('执行节点离线')
  99. expect(wrapper.find('[role="alert"]').exists()).toBe(false)
  100. expect(request.mock.calls.some(([path])=>path.includes('/computer/take'))).toBe(false)
  101. expect(wrapper.find('.computer-screen img').exists()).toBe(false)
  102. }finally{wrapper.unmount()}
  103. })
  104. it('ignores browser metadata that arrives after a target switch',async()=>{
  105. state.open=true;state.tabs=[{id:'tab',title:'原页面',url:'https://example.com',active:true}]
  106. let release!:(value:unknown)=>void,count=0
  107. const original=request.getMockImplementation()!
  108. request.mockImplementation((path,options)=>path.endsWith('/managed-browser')&&++count===2?new Promise(resolve=>{release=resolve}):original(path,options))
  109. const wrapper=mount(ComputerPanel,{props:{agents:[agent],backend:'managed-browser',standalone:true},global:{stubs}})
  110. try{
  111. await flushPromises()
  112. await wrapper.get('[aria-label="切换电脑环境"]').setValue('desktop:local');await flushPromises()
  113. release({...state,tabs:[{id:'late',title:'延迟旧页面',url:'https://old.example',active:true}]});await flushPromises()
  114. expect(wrapper.findComponent(BrowserToolbar).exists()).toBe(false)
  115. expect(wrapper.text()).not.toContain('延迟旧页面')
  116. }finally{wrapper.unmount()}
  117. })
  118. it('accepts managed browser native viewer links but rejects a host mixed into them',async()=>{
  119. route.query={backend:'managed-browser'}
  120. const wrapper=mount(Viewer,{global:{stubs:{ComputerPanel:true}}})
  121. try{await flushPromises();expect(wrapper.getComponent(ComputerPanel).props()).toMatchObject({backend:'managed-browser',autoTake:true})}finally{wrapper.unmount()}
  122. route.query={backend:'managed-browser',host:'local'}
  123. const invalid=mount(Viewer,{global:{stubs:{ComputerPanel:true}}})
  124. try{await flushPromises();expect(invalid.text()).toContain('所选电脑无效');expect(invalid.findComponent(ComputerPanel).exists()).toBe(false)}finally{invalid.unmount()}
  125. })
  126. it('keeps a missing browser selectable and prepares it once before automatic takeover',async()=>{
  127. vi.useFakeTimers()
  128. state.available=false;state.installation={status:'missing',message:'首次使用会自动准备 Chromium',updatedAt:1}
  129. const original=request.getMockImplementation()!
  130. request.mockImplementation(async(path,options)=>{
  131. if(path.endsWith('/managed-browser/prepare')){state.installation={status:'installing',message:'正在下载 Chromium 45%',progress:45,updatedAt:2};return structuredClone(state) as any}
  132. return original(path,options)
  133. })
  134. const wrapper=mount(ComputerPanel,{props:{agents:[agent],backend:'managed-browser',standalone:true,autoTake:true},global:{stubs}})
  135. try{
  136. await flushPromises()
  137. expect(wrapper.get('option[value="managed-browser"]').attributes('disabled')).toBeUndefined()
  138. expect(wrapper.text()).toContain('正在下载 Chromium 45%')
  139. expect(wrapper.get('progress').attributes('value')).toBe('45')
  140. expect(request.mock.calls.filter(([path])=>path.endsWith('/prepare'))).toHaveLength(1)
  141. expect(request.mock.calls.some(([path])=>path.includes('/computer/take'))).toBe(false)
  142. state.available=true;state.installation={status:'ready',message:'浏览器已准备',updatedAt:3}
  143. await vi.advanceTimersByTimeAsync(1000);await flushPromises()
  144. expect(request.mock.calls.filter(([path])=>path.includes('/computer/take'))).toHaveLength(1)
  145. expect(wrapper.text()).toContain('人工控制中')
  146. expect(request.mock.calls.filter(([path])=>path.endsWith('/prepare'))).toHaveLength(1)
  147. }finally{wrapper.unmount()}
  148. })
  149. it('observes an existing installation without restarting it and cancels waiting when closed',async()=>{
  150. vi.useFakeTimers()
  151. state.available=false;state.installation={status:'installing',message:'正在下载 Chromium',updatedAt:1}
  152. const wrapper=mount(ComputerPanel,{props:{agents:[agent],backend:'managed-browser',standalone:true,autoTake:true},global:{stubs}})
  153. try{
  154. await flushPromises()
  155. expect(request.mock.calls.some(([path])=>path.endsWith('/prepare'))).toBe(false)
  156. expect(wrapper.get('.return-control').attributes('disabled')).toBeUndefined()
  157. await wrapper.get('.return-control').trigger('click');await flushPromises()
  158. expect(wrapper.emitted('close')).toHaveLength(1)
  159. const calls=request.mock.calls.length
  160. state.available=true;state.installation={status:'ready',message:'就绪',updatedAt:2}
  161. await vi.advanceTimersByTimeAsync(5000);await flushPromises()
  162. expect(request.mock.calls).toHaveLength(calls)
  163. expect(request.mock.calls.some(([path])=>path.includes('/computer/take'))).toBe(false)
  164. }finally{wrapper.unmount()}
  165. })
  166. it('shows installation failure with an explicit retry and resumes takeover after recovery',async()=>{
  167. vi.useFakeTimers()
  168. state.available=false;state.installation={status:'missing',message:'待准备',updatedAt:1}
  169. let attempts=0
  170. const original=request.getMockImplementation()!
  171. request.mockImplementation(async(path,options)=>{
  172. if(path.endsWith('/prepare')){
  173. attempts++
  174. state.installation=attempts===1?{status:'failed',message:'下载失败',error:'节点网络不可用',updatedAt:2}:{status:'ready',message:'已准备',updatedAt:3}
  175. state.available=attempts>1
  176. return structuredClone(state) as any
  177. }
  178. return original(path,options)
  179. })
  180. const wrapper=mount(ComputerPanel,{props:{agents:[agent],backend:'managed-browser',standalone:true,autoTake:true},global:{stubs}})
  181. try{
  182. await flushPromises()
  183. expect(wrapper.text()).toContain('节点网络不可用')
  184. expect(attempts).toBe(1)
  185. await wrapper.findAll('button').find(button=>button.text()==='重试并接管浏览器')!.trigger('click');await flushPromises()
  186. expect(request.mock.calls.filter(([path])=>path.endsWith('/prepare')).at(-1)?.[1]?.body).toEqual({retry:true})
  187. expect(attempts).toBe(2)
  188. expect(wrapper.text()).toContain('人工控制中')
  189. }finally{wrapper.unmount()}
  190. })
  191. it('returns a takeover receipt that arrives after its viewer unmounts to the original browser',async()=>{
  192. let resolve!:(value:unknown)=>void
  193. const original=request.getMockImplementation()!
  194. request.mockImplementation((path,options)=>path.includes('/computer/take')?new Promise(done=>{resolve=done}):original(path,options))
  195. const wrapper=mount(ComputerPanel,{props:{agents:[agent],backend:'managed-browser',standalone:true,autoTake:true},global:{stubs}})
  196. await flushPromises();wrapper.unmount()
  197. resolve({mode:'human',controlId:'late-control',token:'late-token',generation:4});await flushPromises()
  198. expect(request).toHaveBeenCalledWith('/api/app/agents/bot/computer/giveback?backend=managed-browser',{method:'POST',body:{controlId:'late-control',token:'late-token',notes:''}})
  199. })
  200. it('exposes preparation in the side panel while read-only viewing never installs the browser',async()=>{
  201. state.available=false;state.installation={status:'missing',message:'首次接管将准备浏览器',updatedAt:1}
  202. const wrapper=mount(Preview,{props:{agents:[agent],isAdmin:true,active:true},global:{stubs}})
  203. try{
  204. await flushPromises()
  205. const browser=wrapper.get('[aria-label="托管浏览器"]')
  206. expect(browser.text()).toContain('首次接管将准备浏览器')
  207. expect(browser.get('button.primary').attributes('disabled')).toBeUndefined()
  208. expect(request.mock.calls.some(([path])=>path.endsWith('/prepare'))).toBe(false)
  209. await browser.get('button.primary').trigger('click')
  210. expect(wrapper.emitted('desktop')?.at(-1)).toEqual([agent,'managed-browser',undefined])
  211. }finally{wrapper.unmount()}
  212. })
  213. it('rechecks an offline browser in the side panel and restores takeover with GET requests only',async()=>{
  214. state.available=false;state.reason='执行节点离线'
  215. const wrapper=mount(Preview,{props:{agents:[agent],isAdmin:true,active:true},global:{stubs}})
  216. try{
  217. await flushPromises()
  218. await wrapper.get('[aria-label="显示的桌面"]').setValue('managed-browser');await flushPromises()
  219. const browser=wrapper.get('[aria-label="托管浏览器"]')
  220. expect(browser.get('button.primary').attributes('disabled')).toBeDefined()
  221. expect(browser.get('[role="status"]').text()).toBe('执行节点离线')
  222. expect(browser.find('progress').exists()).toBe(false)
  223. const before=request.mock.calls.length
  224. state.available=true;state.reason=undefined
  225. await browser.findAll('button').find(button=>button.text()==='重新检测')!.trigger('click');await flushPromises()
  226. expect(browser.get('button.primary').attributes('disabled')).toBeUndefined()
  227. expect(wrapper.get<HTMLSelectElement>('[aria-label="显示的桌面"]').element.value).toBe('managed-browser')
  228. expect(request.mock.calls.slice(before).map(([path])=>path)).toEqual(['/api/app/agents/bot/managed-browser'])
  229. expect(request.mock.calls.some(([,options])=>options?.method==='POST')).toBe(false)
  230. await browser.get('button.primary').trigger('click')
  231. expect(wrapper.emitted('desktop')?.at(-1)).toEqual([agent,'managed-browser',undefined])
  232. }finally{wrapper.unmount()}
  233. })
  234. it('recovers an explicitly selected browser after polling without starting or switching computers',async()=>{
  235. vi.useFakeTimers()
  236. state.available=false;state.reason='执行节点离线'
  237. const wrapper=mount(ComputerPanel,{props:{agents:[agent],backend:'managed-browser',standalone:true,autoTake:true},global:{stubs}})
  238. try{
  239. await flushPromises()
  240. expect(wrapper.get('.browser-availability').text()).toContain('重新检测')
  241. expect(wrapper.find('progress').exists()).toBe(false)
  242. expect(wrapper.findAll('button').find(button=>button.text()==='打开并控制')!.attributes('disabled')).toBeDefined()
  243. state.available=true;state.reason=undefined
  244. await vi.advanceTimersByTimeAsync(1200);await flushPromises()
  245. expect(wrapper.findAll('button').find(button=>button.text()==='打开并控制')!.attributes('disabled')).toBeUndefined()
  246. expect(wrapper.get<HTMLSelectElement>('[aria-label="切换电脑环境"]').element.value).toBe('managed-browser')
  247. expect(wrapper.find('.browser-availability').exists()).toBe(false)
  248. expect(request.mock.calls.filter(([path])=>path.includes('/computer')).every(([path])=>path.includes('backend=managed-browser'))).toBe(true)
  249. expect(request.mock.calls.some(([,options])=>options?.method==='POST')).toBe(false)
  250. }finally{wrapper.unmount()}
  251. })
  252. it('refreshes viewer targets on explicit recheck without preparing the browser',async()=>{
  253. state.available=false;state.reason='执行节点离线'
  254. const wrapper=mount(ComputerPanel,{props:{agents:[agent],backend:'managed-browser',standalone:true},global:{stubs}})
  255. try{
  256. await flushPromises()
  257. const before=request.mock.calls.length
  258. state.reason=undefined;state.installation={status:'missing',message:'首次使用会自动准备 Chromium',updatedAt:1}
  259. await wrapper.get('.browser-availability button').trigger('click');await flushPromises()
  260. const calls=request.mock.calls.slice(before)
  261. expect(calls.some(([path])=>path.endsWith('/host-tools'))).toBe(true)
  262. expect(calls.some(([path])=>path.endsWith('/managed-browser'))).toBe(true)
  263. expect(calls.some(([,options])=>options?.method==='POST')).toBe(false)
  264. expect(wrapper.get('option[value="managed-browser"]').attributes('disabled')).toBeUndefined()
  265. expect(wrapper.text()).toContain('首次使用会自动准备 Chromium')
  266. expect(wrapper.find('progress').exists()).toBe(false)
  267. }finally{wrapper.unmount()}
  268. })
  269. it.each(['viewer','side panel'])('clears stale installation progress after a failed status read in the %s',async(view)=>{
  270. vi.useFakeTimers()
  271. state.available=false;state.installation={status:'installing',message:'正在下载 Chromium 40%',progress:40,updatedAt:1}
  272. const original=request.getMockImplementation()!
  273. let disconnected=false
  274. request.mockImplementation((path,options)=>disconnected&&path.endsWith('/managed-browser')?Promise.reject(new Error('执行节点连接已断开')):original(path,options))
  275. const wrapper=view==='viewer'
  276. ?mount(ComputerPanel,{props:{agents:[agent],backend:'managed-browser',standalone:true},global:{stubs}})
  277. :mount(Preview,{props:{agents:[agent],isAdmin:true,active:true},global:{stubs}})
  278. try{
  279. await flushPromises()
  280. expect(wrapper.find('progress').exists()).toBe(true)
  281. disconnected=true
  282. await vi.advanceTimersByTimeAsync(3000);await flushPromises()
  283. expect(wrapper.find('progress').exists()).toBe(false)
  284. expect(wrapper.text()).not.toContain('正在下载 Chromium 40%')
  285. expect(wrapper.text()).toContain('执行节点连接已断开')
  286. expect(wrapper.text()).toContain('重新检测')
  287. expect(request.mock.calls.some(([,options])=>options?.method==='POST')).toBe(false)
  288. }finally{wrapper.unmount()}
  289. })
  290. it('keeps a newer explicit browser detection result when an older periodic response arrives',async()=>{
  291. state.available=false;state.reason='执行节点离线'
  292. let release!:(value:unknown)=>void,count=0
  293. const original=request.getMockImplementation()!
  294. request.mockImplementation((path,options)=>path.endsWith('/managed-browser')&&++count===1?new Promise(resolve=>{release=resolve}):original(path,options))
  295. const wrapper=mount(Preview,{props:{agents:[agent],isAdmin:true,active:true},global:{stubs}})
  296. try{
  297. await flushPromises()
  298. const browser=wrapper.get('[aria-label="托管浏览器"]')
  299. state.available=true;state.reason=undefined
  300. await browser.findAll('button').find(button=>button.text()==='重新检测')!.trigger('click');await flushPromises()
  301. expect(browser.get('button.primary').attributes('disabled')).toBeUndefined()
  302. release({...state,available:false,reason:'过期的离线状态'});await flushPromises()
  303. expect(browser.get('button.primary').attributes('disabled')).toBeUndefined()
  304. expect(browser.text()).not.toContain('过期的离线状态')
  305. expect(request.mock.calls.some(([,options])=>options?.method==='POST')).toBe(false)
  306. }finally{wrapper.unmount()}
  307. })