localVmImages.test.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import {it,expect,vi,afterEach} from 'vitest'
  2. import {mount,flushPromises} from '@vue/test-utils'
  3. import Settings from '../../src/client/components/app/LocalVmSettingsPanel.vue'
  4. import Panel from '../../src/client/components/workspace/LocalVmChatPanel.vue'
  5. import {apiRequest} from '../../src/client/api/client'
  6. vi.mock('../../src/client/api/client',()=>({apiRequest:vi.fn()}))
  7. const images=[{key:'standard',name:'标准桌面',description:'XFCE',ready:true,imageId:'sha256:a'},{key:'cursor',name:'Cursor Universal',description:'Debian',ready:true,imageId:'sha256:b'}]
  8. const agent={id:'image-fixture',name:'镜像测试',computer:'vm',execution:'computer',nodeId:'local',profile:'default'} as any
  9. const vm={configured:true,runtime:'docker',daemonUp:true,image:true,mode:'per-bot',maxInstances:2,busy:false,images}
  10. it('selects Profile collaboration without changing the VM and locks the mode while it is in use',async()=>{
  11. vi.spyOn(document,'hidden','get').mockReturnValue(false)
  12. let inUse=false
  13. vi.mocked(apiRequest).mockImplementation(async path=>{
  14. if(path.endsWith('/desktop-environment'))return {selected:null,local:{supported:false},browser:{available:false}} as any
  15. if(path.endsWith('/cloud-computer'))return {configured:false} as any
  16. return {...vm,enabled:true,container:'missing',ready:false,inUse} as any
  17. })
  18. const wrapper=mount(Panel,{props:{agents:[agent],isAdmin:true,active:true},global:{stubs:{AppIcon:true,GrokAuthPanel:true}}})
  19. try{
  20. await flushPromises();await wrapper.get('[aria-label="本地虚拟机执行方式"]').setValue('profile');await flushPromises()
  21. expect(apiRequest).toHaveBeenCalledWith('/api/app/agents/image-fixture/computer-selection',{method:'PUT',body:{computer:'vm',vmExecution:'profile',allowHostEnvironment:true}})
  22. await wrapper.setProps({agents:[{...agent,vmExecution:'profile',allowHostEnvironment:true}]});await flushPromises()
  23. expect(wrapper.text()).toContain('使用基础机器人的完整本机环境')
  24. expect(wrapper.find('.host-environment-option').exists()).toBe(false)
  25. inUse=true;await wrapper.setProps({active:false});await wrapper.setProps({active:true});await flushPromises()
  26. expect((wrapper.get('[aria-label="本地虚拟机执行方式"]').element as HTMLSelectElement).disabled).toBe(true)
  27. }finally{wrapper.unmount()}
  28. })
  29. afterEach(()=>{vi.clearAllMocks();vi.restoreAllMocks()})
  30. it('saves never-stop and restores the selected value after refresh',async()=>{
  31. let idleStopMinutes=5
  32. vi.mocked(apiRequest).mockImplementation(async(path,options:any)=>{
  33. if(path.endsWith('/idle-policy'))idleStopMinutes=options.body.idleStopMinutes
  34. return path==='/api/app/computers'?{computers:[]}:{...vm,idleStopMinutes} as any
  35. })
  36. const wrapper=mount(Settings,{global:{stubs:{AppIcon:true,RunnerSettingsPanel:true}}})
  37. try{
  38. await flushPromises();await wrapper.get('[aria-label="虚拟机空闲停止时间"]').setValue('0');await flushPromises()
  39. expect(apiRequest).toHaveBeenCalledWith('/api/app/admin/local-vm/idle-policy',expect.objectContaining({body:expect.objectContaining({idleStopMinutes:0})}))
  40. expect((wrapper.get('select[aria-label="虚拟机空闲停止时间"]').element as HTMLSelectElement).value).toBe('0')
  41. expect(wrapper.text()).toContain('空闲时保持运行')
  42. }finally{wrapper.unmount()}
  43. })
  44. it('offers a host environment checkbox without a combined environment tab',async()=>{
  45. vi.spyOn(document,'hidden','get').mockReturnValue(false)
  46. vi.mocked(apiRequest).mockImplementation(async path=>{
  47. if(path.endsWith('/desktop-environment'))return {selected:null,local:{supported:false},browser:{available:false}} as any
  48. if(path.endsWith('/cloud-computer'))return {configured:false} as any
  49. return {...vm,enabled:true,container:'missing',ready:false,inUse:false} as any
  50. })
  51. const wrapper=mount(Panel,{props:{agents:[agent],isAdmin:true,active:true},global:{stubs:{AppIcon:true,GrokAuthPanel:true}}})
  52. try{
  53. await flushPromises();expect(wrapper.find('[role="tab"][aria-label="本机 + 虚拟机"]').exists()).toBe(false);await wrapper.get('input[type="checkbox"]').setValue(true);await flushPromises()
  54. expect(apiRequest).toHaveBeenCalledWith('/api/app/agents/image-fixture/computer-selection',{method:'PUT',body:{computer:'vm',allowHostEnvironment:true}})
  55. }finally{wrapper.unmount()}
  56. })
  57. it('offers the checkbox for cloud selection and hides it for other environments',async()=>{
  58. vi.spyOn(document,'hidden','get').mockReturnValue(false)
  59. vi.mocked(apiRequest).mockImplementation(async path=>{
  60. if(path.endsWith('/desktop-environment'))return {selected:null,local:{supported:false},browser:{available:false}} as any
  61. if(path.endsWith('/cloud-computer'))return {configured:true,running:false} as any
  62. return {...vm,enabled:false,container:'missing',ready:false,inUse:false} as any
  63. })
  64. const cloudAgent={...agent,computer:'cloud',execution:'profile'}
  65. const wrapper=mount(Panel,{props:{agents:[cloudAgent],isAdmin:true,active:true},global:{stubs:{AppIcon:true,GrokAuthPanel:true}}})
  66. try{
  67. await flushPromises();await wrapper.get('input[type="checkbox"]').setValue(true);await flushPromises()
  68. expect(apiRequest).toHaveBeenCalledWith('/api/app/agents/image-fixture/computer-selection',{method:'PUT',body:{computer:'cloud',allowHostEnvironment:true}})
  69. for(const computer of ['auto','off','local','browser']){
  70. await wrapper.setProps({agents:[{...cloudAgent,computer}]});await flushPromises()
  71. expect(wrapper.find('.host-environment-option').exists()).toBe(false)
  72. }
  73. }finally{wrapper.unmount()}
  74. })
  75. it('keeps Compose lifecycle controls hidden while enabling host tools for an already assigned desktop',async()=>{
  76. vi.spyOn(document,'hidden','get').mockReturnValue(false)
  77. vi.mocked(apiRequest).mockImplementation(async path=>{
  78. if(path==='/api/app/computers')return {computers:[]} as any
  79. if(path.endsWith('/desktop-environment'))return {selected:null,local:{supported:false},browser:{available:false}} as any
  80. if(path.endsWith('/cloud-computer'))return {configured:false} as any
  81. return {...vm,fixedCapacity:true,desktopId:'desktop',desktops:[{id:'desktop',name:'已有桌面',ready:true}],enabled:true,container:'running',ready:false,inUse:false} as any
  82. })
  83. const settings=mount(Settings,{global:{stubs:{AppIcon:true,RunnerSettingsPanel:true}}})
  84. const wrapper=mount(Panel,{props:{agents:[agent],isAdmin:true,active:true},global:{stubs:{AppIcon:true,GrokAuthPanel:true}}})
  85. try{
  86. await flushPromises();expect(settings.find('[aria-label="虚拟机空闲停止时间"]').exists()).toBe(false)
  87. expect(wrapper.findAll('button').some(b=>b.text()==='停止虚拟机')).toBe(false)
  88. await wrapper.get('input[type="checkbox"]').setValue(true);await flushPromises()
  89. expect(apiRequest).toHaveBeenCalledWith('/api/app/agents/image-fixture/computer-selection',{method:'PUT',body:{computer:'vm',allowHostEnvironment:true}})
  90. }finally{settings.unmount();wrapper.unmount()}
  91. })
  92. it('prepares the selected image in Settings and explains the shared image constraint',async()=>{
  93. vi.mocked(apiRequest).mockImplementation(async path=>path==='/api/app/computers'?{computers:[]}:vm as any)
  94. const wrapper=mount(Settings,{global:{stubs:{AppIcon:true,RunnerSettingsPanel:true}}})
  95. try{
  96. await flushPromises()
  97. const select=wrapper.findAll('select').find(select=>select.text().includes('Cursor Universal'))!
  98. await select.setValue('cursor')
  99. await wrapper.findAll('button').find(b=>b.text()==='重新检查所选镜像')!.trigger('click')
  100. await flushPromises()
  101. expect(apiRequest).toHaveBeenCalledWith('/api/app/admin/local-vm/prepare',expect.objectContaining({body:expect.objectContaining({imageKey:'cursor'})}))
  102. expect(wrapper.text()).toContain('同一共享桌面的所有机器人使用一个镜像')
  103. }finally{wrapper.unmount()}
  104. })
  105. it('selects a prepared desktop image and locks the selection for an existing shared instance',async()=>{
  106. vi.spyOn(document,'hidden','get').mockReturnValue(false)
  107. let detail={...vm,enabled:true,container:'missing',ready:false,inUse:false,imageKey:'standard'}
  108. vi.mocked(apiRequest).mockImplementation(async path=>{
  109. if(path.endsWith('/desktop-environment'))return {selected:null,local:{supported:false},browser:{available:false}} as any
  110. if(path.endsWith('/cloud-computer'))return {configured:false} as any
  111. return detail as any
  112. })
  113. const wrapper=mount(Panel,{props:{agents:[agent],isAdmin:true,active:true},global:{stubs:{AppIcon:true,GrokAuthPanel:true}}})
  114. try{
  115. await flushPromises()
  116. const select=wrapper.findAll('select').find(s=>s.text().includes('Cursor Universal'))!
  117. await select.setValue('cursor');await flushPromises()
  118. expect(apiRequest).toHaveBeenCalledWith('/api/app/agents/image-fixture/local-vm/image',{method:'PUT',body:{imageKey:'cursor'}})
  119. detail={...detail,mode:'shared',container:'stopped'}
  120. await wrapper.setProps({active:false});await wrapper.setProps({active:true});await flushPromises()
  121. expect(select.attributes('disabled')).toBeDefined()
  122. expect(wrapper.text()).toContain('同一共享桌面只能使用一个镜像')
  123. }finally{wrapper.unmount()}
  124. })