localVmImages.test.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. afterEach(()=>{vi.clearAllMocks();vi.restoreAllMocks()})
  11. it('saves never-stop and restores the selected value after refresh',async()=>{
  12. let idleStopMinutes=5
  13. vi.mocked(apiRequest).mockImplementation(async(path,options:any)=>{
  14. if(path.endsWith('/idle-policy'))idleStopMinutes=options.body.idleStopMinutes
  15. return path==='/api/app/computers'?{computers:[]}:{...vm,idleStopMinutes} as any
  16. })
  17. const wrapper=mount(Settings,{global:{stubs:{AppIcon:true,RunnerSettingsPanel:true}}})
  18. try{
  19. await flushPromises();await wrapper.get('[aria-label="虚拟机空闲停止时间"]').setValue('0');await flushPromises()
  20. expect(apiRequest).toHaveBeenCalledWith('/api/app/admin/local-vm/idle-policy',expect.objectContaining({body:expect.objectContaining({idleStopMinutes:0})}))
  21. expect((wrapper.get('select[aria-label="虚拟机空闲停止时间"]').element as HTMLSelectElement).value).toBe('0')
  22. expect(wrapper.text()).toContain('空闲时保持运行')
  23. }finally{wrapper.unmount()}
  24. })
  25. it('offers a host environment checkbox without a combined environment tab',async()=>{
  26. vi.spyOn(document,'hidden','get').mockReturnValue(false)
  27. vi.mocked(apiRequest).mockImplementation(async path=>{
  28. if(path.endsWith('/desktop-environment'))return {selected:null,local:{supported:false},browser:{available:false}} as any
  29. if(path.endsWith('/cloud-computer'))return {configured:false} as any
  30. return {...vm,enabled:true,container:'missing',ready:false,inUse:false} as any
  31. })
  32. const wrapper=mount(Panel,{props:{agents:[agent],isAdmin:true,active:true},global:{stubs:{AppIcon:true,GrokAuthPanel:true}}})
  33. try{
  34. await flushPromises();expect(wrapper.find('[role="tab"][aria-label="本机 + 虚拟机"]').exists()).toBe(false);await wrapper.get('input[type="checkbox"]').setValue(true);await flushPromises()
  35. expect(apiRequest).toHaveBeenCalledWith('/api/app/agents/image-fixture/computer-selection',{method:'PUT',body:{computer:'vm',allowHostEnvironment:true}})
  36. }finally{wrapper.unmount()}
  37. })
  38. it('offers the checkbox for cloud selection and hides it for other environments',async()=>{
  39. vi.spyOn(document,'hidden','get').mockReturnValue(false)
  40. vi.mocked(apiRequest).mockImplementation(async path=>{
  41. if(path.endsWith('/desktop-environment'))return {selected:null,local:{supported:false},browser:{available:false}} as any
  42. if(path.endsWith('/cloud-computer'))return {configured:true,running:false} as any
  43. return {...vm,enabled:false,container:'missing',ready:false,inUse:false} as any
  44. })
  45. const cloudAgent={...agent,computer:'cloud',execution:'profile'}
  46. const wrapper=mount(Panel,{props:{agents:[cloudAgent],isAdmin:true,active:true},global:{stubs:{AppIcon:true,GrokAuthPanel:true}}})
  47. try{
  48. await flushPromises();await wrapper.get('input[type="checkbox"]').setValue(true);await flushPromises()
  49. expect(apiRequest).toHaveBeenCalledWith('/api/app/agents/image-fixture/computer-selection',{method:'PUT',body:{computer:'cloud',allowHostEnvironment:true}})
  50. for(const computer of ['auto','off','local','browser']){
  51. await wrapper.setProps({agents:[{...cloudAgent,computer}]});await flushPromises()
  52. expect(wrapper.find('.host-environment-option').exists()).toBe(false)
  53. }
  54. }finally{wrapper.unmount()}
  55. })
  56. it('keeps Compose lifecycle controls hidden while enabling host tools for an already assigned desktop',async()=>{
  57. vi.spyOn(document,'hidden','get').mockReturnValue(false)
  58. vi.mocked(apiRequest).mockImplementation(async path=>{
  59. if(path==='/api/app/computers')return {computers:[]} as any
  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:false} as any
  62. return {...vm,fixedCapacity:true,desktopId:'desktop',desktops:[{id:'desktop',name:'已有桌面',ready:true}],enabled:true,container:'running',ready:false,inUse:false} as any
  63. })
  64. const settings=mount(Settings,{global:{stubs:{AppIcon:true,RunnerSettingsPanel:true}}})
  65. const wrapper=mount(Panel,{props:{agents:[agent],isAdmin:true,active:true},global:{stubs:{AppIcon:true,GrokAuthPanel:true}}})
  66. try{
  67. await flushPromises();expect(settings.find('[aria-label="虚拟机空闲停止时间"]').exists()).toBe(false)
  68. expect(wrapper.findAll('button').some(b=>b.text()==='停止虚拟机')).toBe(false)
  69. await wrapper.get('input[type="checkbox"]').setValue(true);await flushPromises()
  70. expect(apiRequest).toHaveBeenCalledWith('/api/app/agents/image-fixture/computer-selection',{method:'PUT',body:{computer:'vm',allowHostEnvironment:true}})
  71. }finally{settings.unmount();wrapper.unmount()}
  72. })
  73. it('prepares the selected image in Settings and explains the shared image constraint',async()=>{
  74. vi.mocked(apiRequest).mockImplementation(async path=>path==='/api/app/computers'?{computers:[]}:vm as any)
  75. const wrapper=mount(Settings,{global:{stubs:{AppIcon:true,RunnerSettingsPanel:true}}})
  76. try{
  77. await flushPromises()
  78. const select=wrapper.findAll('select').find(select=>select.text().includes('Cursor Universal'))!
  79. await select.setValue('cursor')
  80. await wrapper.findAll('button').find(b=>b.text()==='重新检查所选镜像')!.trigger('click')
  81. await flushPromises()
  82. expect(apiRequest).toHaveBeenCalledWith('/api/app/admin/local-vm/prepare',expect.objectContaining({body:expect.objectContaining({imageKey:'cursor'})}))
  83. expect(wrapper.text()).toContain('同一共享桌面的所有机器人使用一个镜像')
  84. }finally{wrapper.unmount()}
  85. })
  86. it('selects a prepared desktop image and locks the selection for an existing shared instance',async()=>{
  87. vi.spyOn(document,'hidden','get').mockReturnValue(false)
  88. let detail={...vm,enabled:true,container:'missing',ready:false,inUse:false,imageKey:'standard'}
  89. vi.mocked(apiRequest).mockImplementation(async path=>{
  90. if(path.endsWith('/desktop-environment'))return {selected:null,local:{supported:false},browser:{available:false}} as any
  91. if(path.endsWith('/cloud-computer'))return {configured:false} as any
  92. return detail as any
  93. })
  94. const wrapper=mount(Panel,{props:{agents:[agent],isAdmin:true,active:true},global:{stubs:{AppIcon:true,GrokAuthPanel:true}}})
  95. try{
  96. await flushPromises()
  97. const select=wrapper.findAll('select').find(s=>s.text().includes('Cursor Universal'))!
  98. await select.setValue('cursor');await flushPromises()
  99. expect(apiRequest).toHaveBeenCalledWith('/api/app/agents/image-fixture/local-vm/image',{method:'PUT',body:{imageKey:'cursor'}})
  100. detail={...detail,mode:'shared',container:'stopped'}
  101. await wrapper.setProps({active:false});await wrapper.setProps({active:true});await flushPromises()
  102. expect(select.attributes('disabled')).toBeDefined()
  103. expect(wrapper.text()).toContain('同一共享桌面只能使用一个镜像')
  104. }finally{wrapper.unmount()}
  105. })