computerTargetSelection.test.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import {afterEach,beforeEach,expect,it,vi} from 'vitest'
  2. import {flushPromises,mount} from '@vue/test-utils'
  3. import Preview from '../../src/client/components/workspace/LocalVmChatPanel.vue'
  4. import ComputerPanel from '../../src/client/components/workspace/ComputerPanel.vue'
  5. import Viewer from '../../src/client/views/ComputerViewerView.vue'
  6. import {apiRequest} from '../../src/client/api/client'
  7. vi.mock('../../src/client/api/client',()=>({apiRequest:vi.fn()}))
  8. const route={params:{agentId:'bot'},query:{} as Record<string,unknown>}
  9. vi.mock('vue-router',()=>({useRoute:()=>route}))
  10. const agent={id:'bot',name:'竹儿',profile:'default',nodeId:'local'} as any
  11. const permission={supported:true,authorized:true,screen:true,accessibility:true,ready:true,fullAuthorized:true}
  12. let hosts:any[],readFrame:(key:string)=>unknown,vmProblem:string
  13. const screenshot=(key:string)=>({id:key,data:key,width:100,height:80,generation:1})
  14. const stubs={AppIcon:true,GrokAuthPanel:true,BrowserToolbar:true}
  15. beforeEach(()=>{
  16. vi.spyOn(document,'hidden','get').mockReturnValue(false)
  17. route.query={}
  18. hosts=[{id:'local',name:'服务器',online:true,local:permission},{id:'client-mac',name:'Mac Studio',online:true,local:permission}]
  19. readFrame=screenshot
  20. vmProblem=''
  21. vi.mocked(apiRequest).mockImplementation(async(path,options:any)=>{
  22. const url=new URL(path,'http://fixture'),key=url.searchParams.get('host')||url.searchParams.get('backend')||'unspecified'
  23. if(url.pathname==='/api/app/agents')return {agents:[agent]} as any
  24. if(url.pathname.endsWith('/host-tools'))return {serverComputer:true,scriptMachine:true,cloud:true,vm:true} as any
  25. if(url.pathname.endsWith('/desktop-environment'))return {hosts:hosts.map(h=>({...h}))} as any
  26. if(url.pathname.endsWith('/cloud-computer'))return {configured:true,running:true,connected:true} as any
  27. if(url.pathname.endsWith('/cloud-computer/open'))return {} as any
  28. if(url.pathname.endsWith('/local-vm'))return {enabled:true,image:!vmProblem,container:vmProblem?'missing':'running',ready:!vmProblem,problem:vmProblem} as any
  29. if(url.pathname.endsWith('/frame'))return await readFrame(key) as any
  30. return {mode:options?.method==='POST'?'human':'idle',backend:key==='cloud'?'grok':key==='vm'?'docker':'local',generation:1,hostName:key,controlId:'control',token:'fixture-token'} as any
  31. })
  32. })
  33. afterEach(()=>{vi.restoreAllMocks();vi.clearAllMocks()})
  34. it('scopes an unavailable VM to its own settings while the server preview remains usable',async()=>{
  35. vmProblem='本地虚拟机执行节点尚未启用,请在本地虚拟机设置中完成配置'
  36. const wrapper=mount(Preview,{props:{agents:[agent],isAdmin:true,active:true},global:{stubs}})
  37. try{
  38. await flushPromises()
  39. expect(wrapper.get('.preview img').attributes('src')).toContain('base64,local')
  40. expect(wrapper.find('.panel-body > .problem').exists()).toBe(false)
  41. expect(wrapper.get('.computer-options [aria-label="本地虚拟机状态"]').text()).toBe(vmProblem)
  42. await wrapper.get('.preview').trigger('click')
  43. expect(wrapper.emitted('desktop')?.at(-1)).toEqual([agent,'desktop','local'])
  44. await wrapper.get('[aria-label="显示的桌面"]').setValue('vm');await flushPromises()
  45. expect(wrapper.get('.preview').text()).toContain(vmProblem)
  46. expect(wrapper.find('.preview img').exists()).toBe(false)
  47. expect(wrapper.get('.preview').attributes('disabled')).toBeDefined()
  48. }finally{wrapper.unmount()}
  49. })
  50. it('refreshes the selected preview immediately and never replaces it with a late frame from another computer',async()=>{
  51. const wrapper=mount(Preview,{props:{agents:[agent],isAdmin:true,active:true},global:{stubs}})
  52. try{
  53. await flushPromises()
  54. expect(wrapper.get('.preview img').attributes('src')).toContain('base64,local')
  55. let resolveClient!:(value:unknown)=>void
  56. readFrame=key=>key==='client-mac'?new Promise(resolve=>{resolveClient=resolve}):screenshot(key)
  57. await wrapper.get('[aria-label="显示的桌面"]').setValue('desktop:client-mac')
  58. await flushPromises()
  59. expect(wrapper.find('.preview img').exists()).toBe(false)
  60. expect(wrapper.get('.preview').attributes('disabled')).toBeDefined()
  61. await wrapper.get('[aria-label="显示的桌面"]').setValue('cloud');await flushPromises()
  62. expect(wrapper.get('.preview img').attributes('src')).toContain('base64,cloud')
  63. resolveClient(screenshot('stale-client'));await flushPromises()
  64. expect(wrapper.get('.preview img').attributes('src')).toContain('base64,cloud')
  65. await wrapper.get('.preview').trigger('click');await flushPromises()
  66. expect(wrapper.emitted('desktop')?.at(-1)).toEqual([agent,'cloud',undefined])
  67. readFrame=screenshot
  68. for(const [key,backend,host] of [['desktop:local','desktop','local'],['desktop:client-mac','desktop','client-mac'],['vm','vm',undefined]]){
  69. await wrapper.get('[aria-label="显示的桌面"]').setValue(key);await flushPromises()
  70. expect(wrapper.get('.preview img').attributes('src')).toContain('base64,'+(host||backend))
  71. await wrapper.get('.preview').trigger('click');await flushPromises()
  72. expect(wrapper.emitted('desktop')?.at(-1)).toEqual([agent,backend,host])
  73. }
  74. }finally{wrapper.unmount()}
  75. })
  76. it('keeps an offline or removed explicit selection instead of opening the server',async()=>{
  77. const wrapper=mount(Preview,{props:{agents:[agent],isAdmin:true,active:true},global:{stubs}})
  78. try{
  79. await flushPromises();await wrapper.get('[aria-label="显示的桌面"]').setValue('desktop:client-mac');await flushPromises()
  80. hosts[1].online=false
  81. await wrapper.setProps({active:false});await wrapper.setProps({active:true});await flushPromises()
  82. expect(wrapper.text()).toContain('这台电脑已离线')
  83. expect(wrapper.find('.preview img').exists()).toBe(false)
  84. hosts=hosts.slice(0,1)
  85. await wrapper.setProps({active:false});await wrapper.setProps({active:true});await flushPromises()
  86. expect(wrapper.text()).toContain('所选电脑已不可用')
  87. expect(wrapper.get('.preview').attributes('disabled')).toBeDefined()
  88. expect(wrapper.emitted('desktop')).toBeUndefined()
  89. }finally{wrapper.unmount()}
  90. })
  91. it.each([
  92. ['desktop','local'],['desktop','client-mac'],['cloud',undefined],['vm',undefined],
  93. ] as const)('opens and controls exactly the requested %s/%s target',async(backend,host)=>{
  94. const wrapper=mount(ComputerPanel,{props:{agents:[agent],backend,host,standalone:true,autoTake:true},global:{stubs}})
  95. try{
  96. await flushPromises()
  97. const call=vi.mocked(apiRequest).mock.calls.find(([path])=>path.includes('/computer/take'))!
  98. const url=new URL(call[0],'http://fixture')
  99. expect(url.searchParams.get('backend')).toBe(backend)
  100. expect(url.searchParams.get('host')).toBe(host??null)
  101. expect(wrapper.get('.computer-screen img').attributes('src')).toContain('base64,'+(host||backend))
  102. }finally{wrapper.unmount()}
  103. })
  104. it('does not auto-take a different computer when the requested host is missing',async()=>{
  105. const wrapper=mount(ComputerPanel,{props:{agents:[agent],backend:'desktop',host:'missing-host',standalone:true,autoTake:true},global:{stubs}})
  106. try{
  107. await flushPromises()
  108. expect(wrapper.get('[role="alert"]').text()).toContain('所选电脑不可用')
  109. expect(vi.mocked(apiRequest).mock.calls.some(([path])=>path.includes('/computer/take'))).toBe(false)
  110. expect(wrapper.find('.computer-screen img').exists()).toBe(false)
  111. }finally{wrapper.unmount()}
  112. })
  113. it('passes the native window URL target into the viewer and rejects malformed targets',async()=>{
  114. route.query={backend:'desktop',host:'client-mac'}
  115. const wrapper=mount(Viewer,{global:{stubs:{ComputerPanel:true}}})
  116. try{
  117. await flushPromises()
  118. expect(wrapper.getComponent(ComputerPanel).props()).toMatchObject({backend:'desktop',host:'client-mac',autoTake:true})
  119. }finally{wrapper.unmount()}
  120. route.query={backend:'vm',host:'client-mac'}
  121. const invalid=mount(Viewer,{global:{stubs:{ComputerPanel:true}}})
  122. try{
  123. await flushPromises();expect(invalid.text()).toContain('所选电脑主机无效')
  124. expect(invalid.findComponent(ComputerPanel).exists()).toBe(false)
  125. }finally{invalid.unmount()}
  126. })