computerTargetSelection.test.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 cloud 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,cloud')
  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');await flushPromises()
  44. expect(wrapper.emitted('desktop')?.at(-1)).toEqual([agent,'cloud',undefined])
  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==='cloud'?new Promise(resolve=>{resolveClient=resolve}):screenshot(key)
  59. await wrapper.get('[aria-label="显示的桌面"]').setValue('cloud')
  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('vm');await flushPromises()
  64. expect(wrapper.get('.preview img').attributes('src')).toContain('base64,vm')
  65. resolveClient(screenshot('stale-cloud'));await flushPromises()
  66. expect(wrapper.get('.preview img').attributes('src')).toContain('base64,vm')
  67. await wrapper.get('.preview').trigger('click');await flushPromises()
  68. expect(wrapper.emitted('desktop')?.at(-1)).toEqual([agent,'vm',undefined])
  69. }finally{wrapper.unmount()}
  70. })
  71. it('never exposes physical desktops or authorization in the computer panel',async()=>{
  72. const wrapper=mount(Preview,{props:{agents:[agent],isAdmin:true,active:true},global:{stubs}})
  73. try{
  74. await flushPromises()
  75. expect(wrapper.findAll('option').some(option=>option.attributes('value')?.startsWith('desktop:'))).toBe(false)
  76. expect(wrapper.find('.host-permission').exists()).toBe(false)
  77. expect(wrapper.findAll('button').some(button=>button.text().includes('授权'))).toBe(false)
  78. expect(vi.mocked(apiRequest).mock.calls.some(([path])=>path.endsWith('/desktop-environment'))).toBe(false)
  79. }finally{wrapper.unmount()}
  80. })
  81. it.each([
  82. ['cloud',undefined],['vm',undefined],
  83. ] as const)('opens and controls exactly the requested %s/%s target',async(backend,host)=>{
  84. const wrapper=mount(ComputerPanel,{props:{agents:[agent],backend,host,standalone:true,autoTake:true},global:{stubs}})
  85. try{
  86. await flushPromises()
  87. const call=vi.mocked(apiRequest).mock.calls.find(([path])=>path.includes('/computer/take'))!
  88. const url=new URL(call[0],'http://fixture')
  89. expect(url.searchParams.get('backend')).toBe(backend)
  90. expect(url.searchParams.get('host')).toBe(host??null)
  91. expect(wrapper.get('.computer-screen img').attributes('src')).toContain('base64,'+(host||backend))
  92. }finally{wrapper.unmount()}
  93. })
  94. it.each(['local','client-mac','missing-host'])('rejects the physical desktop %s without falling back',async(host)=>{
  95. const wrapper=mount(ComputerPanel,{props:{agents:[agent],backend:'desktop',host,standalone:true,autoTake:true},global:{stubs}})
  96. try{
  97. await flushPromises()
  98. expect(wrapper.get('[role="alert"]').text()).toContain('所选电脑不可用')
  99. expect(vi.mocked(apiRequest).mock.calls.some(([path])=>path.includes('/computer/take'))).toBe(false)
  100. expect(wrapper.find('.computer-screen img').exists()).toBe(false)
  101. }finally{wrapper.unmount()}
  102. })
  103. it('passes the native window URL target into the viewer and rejects malformed targets',async()=>{
  104. route.query={backend:'cloud'}
  105. const wrapper=mount(Viewer,{global:{stubs:{ComputerPanel:true}}})
  106. try{
  107. await flushPromises()
  108. expect(wrapper.getComponent(ComputerPanel).props()).toMatchObject({backend:'cloud',host:undefined,autoTake:true})
  109. }finally{wrapper.unmount()}
  110. route.query={backend:'vm',host:'client-mac'}
  111. const invalid=mount(Viewer,{global:{stubs:{ComputerPanel:true}}})
  112. try{
  113. await flushPromises();expect(invalid.text()).toContain('所选电脑无效')
  114. expect(invalid.findComponent(ComputerPanel).exists()).toBe(false)
  115. }finally{invalid.unmount()}
  116. })