localVmImages.test.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 hybrid mode without requiring native screen permissions for host file tools',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();await wrapper.get('[role="tab"][aria-label="本机 + 虚拟机"]').trigger('click');await flushPromises()
  35. expect(apiRequest).toHaveBeenCalledWith('/api/app/agents/image-fixture/computer-selection',{method:'PUT',body:{computer:'hybrid'}})
  36. }finally{wrapper.unmount()}
  37. })
  38. it('keeps Compose lifecycle controls hidden while enabling host tools for an already assigned desktop',async()=>{
  39. vi.spyOn(document,'hidden','get').mockReturnValue(false)
  40. vi.mocked(apiRequest).mockImplementation(async path=>{
  41. if(path==='/api/app/computers')return {computers:[]} as any
  42. if(path.endsWith('/desktop-environment'))return {selected:null,local:{supported:false},browser:{available:false}} as any
  43. if(path.endsWith('/cloud-computer'))return {configured:false} as any
  44. return {...vm,fixedCapacity:true,desktopId:'desktop',desktops:[{id:'desktop',name:'已有桌面',ready:true}],enabled:true,container:'running',ready:false,inUse:false} as any
  45. })
  46. const settings=mount(Settings,{global:{stubs:{AppIcon:true,RunnerSettingsPanel:true}}})
  47. const wrapper=mount(Panel,{props:{agents:[agent],isAdmin:true,active:true},global:{stubs:{AppIcon:true,GrokAuthPanel:true}}})
  48. try{
  49. await flushPromises();expect(settings.find('[aria-label="虚拟机空闲停止时间"]').exists()).toBe(false)
  50. expect(wrapper.findAll('button').some(b=>b.text()==='停止虚拟机')).toBe(false)
  51. await wrapper.get('input[type="checkbox"]').setValue(true);await flushPromises()
  52. expect(apiRequest).toHaveBeenCalledWith('/api/app/agents/image-fixture/computer-selection',{method:'PUT',body:{computer:'hybrid'}})
  53. }finally{settings.unmount();wrapper.unmount()}
  54. })
  55. it('prepares the selected image in Settings and explains the shared image constraint',async()=>{
  56. vi.mocked(apiRequest).mockImplementation(async path=>path==='/api/app/computers'?{computers:[]}:vm as any)
  57. const wrapper=mount(Settings,{global:{stubs:{AppIcon:true,RunnerSettingsPanel:true}}})
  58. try{
  59. await flushPromises()
  60. const select=wrapper.findAll('select').find(select=>select.text().includes('Cursor Universal'))!
  61. await select.setValue('cursor')
  62. await wrapper.findAll('button').find(b=>b.text()==='重新检查所选镜像')!.trigger('click')
  63. await flushPromises()
  64. expect(apiRequest).toHaveBeenCalledWith('/api/app/admin/local-vm/prepare',expect.objectContaining({body:expect.objectContaining({imageKey:'cursor'})}))
  65. expect(wrapper.text()).toContain('同一共享桌面的所有机器人使用一个镜像')
  66. }finally{wrapper.unmount()}
  67. })
  68. it('selects a prepared desktop image and locks the selection for an existing shared instance',async()=>{
  69. vi.spyOn(document,'hidden','get').mockReturnValue(false)
  70. let detail={...vm,enabled:true,container:'missing',ready:false,inUse:false,imageKey:'standard'}
  71. vi.mocked(apiRequest).mockImplementation(async path=>{
  72. if(path.endsWith('/desktop-environment'))return {selected:null,local:{supported:false},browser:{available:false}} as any
  73. if(path.endsWith('/cloud-computer'))return {configured:false} as any
  74. return detail as any
  75. })
  76. const wrapper=mount(Panel,{props:{agents:[agent],isAdmin:true,active:true},global:{stubs:{AppIcon:true,GrokAuthPanel:true}}})
  77. try{
  78. await flushPromises()
  79. const select=wrapper.findAll('select').find(s=>s.text().includes('Cursor Universal'))!
  80. await select.setValue('cursor');await flushPromises()
  81. expect(apiRequest).toHaveBeenCalledWith('/api/app/agents/image-fixture/local-vm/image',{method:'PUT',body:{imageKey:'cursor'}})
  82. detail={...detail,mode:'shared',container:'stopped'}
  83. await wrapper.setProps({active:false});await wrapper.setProps({active:true});await flushPromises()
  84. expect(select.attributes('disabled')).toBeDefined()
  85. expect(wrapper.text()).toContain('同一共享桌面只能使用一个镜像')
  86. }finally{wrapper.unmount()}
  87. })