localVmImages.test.ts 8.3 KB

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