computerTargetSelection.test.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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('[aria-label="显示的桌面"] option:checked').text()).toBe('自动 · 服务器 · 服务器')
  40. expect(wrapper.get('.preview img').attributes('src')).toContain('base64,local')
  41. expect(wrapper.find('.panel-body > .problem').exists()).toBe(false)
  42. expect(wrapper.get('.computer-options [aria-label="本地虚拟机状态"]').text()).toBe(vmProblem)
  43. await wrapper.get('.preview').trigger('click')
  44. expect(wrapper.emitted('desktop')?.at(-1)).toEqual([agent,'desktop','local'])
  45. await wrapper.get('[aria-label="显示的桌面"]').setValue('vm');await flushPromises()
  46. expect(wrapper.get('.preview').text()).toContain(vmProblem)
  47. expect(wrapper.find('.preview img').exists()).toBe(false)
  48. expect(wrapper.get('.preview').attributes('disabled')).toBeDefined()
  49. }finally{wrapper.unmount()}
  50. })
  51. it('refreshes the selected preview immediately and never replaces it with a late frame from another computer',async()=>{
  52. const wrapper=mount(Preview,{props:{agents:[agent],isAdmin:true,active:true},global:{stubs}})
  53. try{
  54. await flushPromises()
  55. expect(wrapper.get('[aria-label="显示的桌面"] option:checked').text()).toBe('自动 · 虚拟环境')
  56. expect(wrapper.get('.preview img').attributes('src')).toContain('base64,vm')
  57. let resolveClient!:(value:unknown)=>void
  58. readFrame=key=>key==='client-mac'?new Promise(resolve=>{resolveClient=resolve}):screenshot(key)
  59. await wrapper.get('[aria-label="显示的桌面"]').setValue('desktop:client-mac')
  60. await flushPromises()
  61. expect(wrapper.find('.preview img').exists()).toBe(false)
  62. expect(wrapper.get('.preview').attributes('disabled')).toBeDefined()
  63. await wrapper.get('[aria-label="显示的桌面"]').setValue('cloud');await flushPromises()
  64. expect(wrapper.get('.preview img').attributes('src')).toContain('base64,cloud')
  65. resolveClient(screenshot('stale-client'));await flushPromises()
  66. expect(wrapper.get('.preview img').attributes('src')).toContain('base64,cloud')
  67. await wrapper.get('.preview').trigger('click');await flushPromises()
  68. expect(wrapper.emitted('desktop')?.at(-1)).toEqual([agent,'cloud',undefined])
  69. readFrame=screenshot
  70. for(const [key,backend,host] of [['desktop:local','desktop','local'],['desktop:client-mac','desktop','client-mac'],['vm','vm',undefined]]){
  71. await wrapper.get('[aria-label="显示的桌面"]').setValue(key);await flushPromises()
  72. expect(wrapper.get('.preview img').attributes('src')).toContain('base64,'+(host||backend))
  73. await wrapper.get('.preview').trigger('click');await flushPromises()
  74. expect(wrapper.emitted('desktop')?.at(-1)).toEqual([agent,backend,host])
  75. }
  76. }finally{wrapper.unmount()}
  77. })
  78. it('keeps an offline or removed explicit selection instead of opening the server',async()=>{
  79. const wrapper=mount(Preview,{props:{agents:[agent],isAdmin:true,active:true},global:{stubs}})
  80. try{
  81. await flushPromises();await wrapper.get('[aria-label="显示的桌面"]').setValue('desktop:client-mac');await flushPromises()
  82. hosts[1].online=false
  83. await wrapper.setProps({active:false});await wrapper.setProps({active:true});await flushPromises()
  84. expect(wrapper.text()).toContain('这台电脑已离线')
  85. expect(wrapper.find('.preview img').exists()).toBe(false)
  86. hosts=hosts.slice(0,1)
  87. await wrapper.setProps({active:false});await wrapper.setProps({active:true});await flushPromises()
  88. expect(wrapper.text()).toContain('所选电脑已不可用')
  89. expect(wrapper.get('.preview').attributes('disabled')).toBeDefined()
  90. expect(wrapper.emitted('desktop')).toBeUndefined()
  91. }finally{wrapper.unmount()}
  92. })
  93. it.each([
  94. ['desktop','local'],['desktop','client-mac'],['cloud',undefined],['vm',undefined],
  95. ] as const)('opens and controls exactly the requested %s/%s target',async(backend,host)=>{
  96. const wrapper=mount(ComputerPanel,{props:{agents:[agent],backend,host,standalone:true,autoTake:true},global:{stubs}})
  97. try{
  98. await flushPromises()
  99. const call=vi.mocked(apiRequest).mock.calls.find(([path])=>path.includes('/computer/take'))!
  100. const url=new URL(call[0],'http://fixture')
  101. expect(url.searchParams.get('backend')).toBe(backend)
  102. expect(url.searchParams.get('host')).toBe(host??null)
  103. expect(wrapper.get('.computer-screen img').attributes('src')).toContain('base64,'+(host||backend))
  104. }finally{wrapper.unmount()}
  105. })
  106. it('does not auto-take a different computer when the requested host is missing',async()=>{
  107. const wrapper=mount(ComputerPanel,{props:{agents:[agent],backend:'desktop',host:'missing-host',standalone:true,autoTake:true},global:{stubs}})
  108. try{
  109. await flushPromises()
  110. expect(wrapper.get('[role="alert"]').text()).toContain('所选电脑不可用')
  111. expect(vi.mocked(apiRequest).mock.calls.some(([path])=>path.includes('/computer/take'))).toBe(false)
  112. expect(wrapper.find('.computer-screen img').exists()).toBe(false)
  113. }finally{wrapper.unmount()}
  114. })
  115. it('passes the native window URL target into the viewer and rejects malformed targets',async()=>{
  116. route.query={backend:'desktop',host:'client-mac'}
  117. const wrapper=mount(Viewer,{global:{stubs:{ComputerPanel:true}}})
  118. try{
  119. await flushPromises()
  120. expect(wrapper.getComponent(ComputerPanel).props()).toMatchObject({backend:'desktop',host:'client-mac',autoTake:true})
  121. }finally{wrapper.unmount()}
  122. route.query={backend:'vm',host:'client-mac'}
  123. const invalid=mount(Viewer,{global:{stubs:{ComputerPanel:true}}})
  124. try{
  125. await flushPromises();expect(invalid.text()).toContain('所选电脑无效')
  126. expect(invalid.findComponent(ComputerPanel).exists()).toBe(false)
  127. }finally{invalid.unmount()}
  128. })