sharedComputers.test.ts 6.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // @vitest-environment node
  2. import {expect,it} from 'vitest'
  3. import {mkdtempSync,rmSync} from 'node:fs'
  4. import {tmpdir} from 'node:os'
  5. import {join} from 'node:path'
  6. import {randomUUID} from 'node:crypto'
  7. import {WorkspaceStore} from '../../src/server/workspaceStore'
  8. import {WorkspaceNodes} from '../../src/server/workspaceGateway'
  9. import {loadServerConfig} from '../../src/server/config'
  10. import {SharedComputers} from '../../src/server/sharedComputers'
  11. import type {WorkspaceAgent} from '../../src/shared/workspace'
  12. it('requires an explicit human grant, one compatible source and idle permanent members',()=>{
  13. const home=mkdtempSync(join(tmpdir(),'yaoyao-shared-')),store=new WorkspaceStore(home),nodes=new WorkspaceNodes(store,loadServerConfig({HERMES_YAOYAO_HOME:home}),{} as any)
  14. const service=new SharedComputers(store,{} as any,nodes,{sharedComputerRunner:()=>({id:'runner'})} as any)
  15. try{
  16. const first=store.createAgent('owner',{name:'第一位',execution:'computer',profile:'default'}),second=store.createAgent('owner',{name:'第二位',execution:'computer',profile:'default'})
  17. const command={requestId:randomUUID(),name:'可信研发电脑',memberIds:[first.id,second.id],trusted:true}
  18. expect(()=>service.create('owner',{...command,trusted:false})).toThrow()
  19. expect(()=>store.createAgent('owner',{name:'模型新建',profile:'default',computerEnvironmentId:randomUUID()})).toThrow()
  20. store.put('owner','turn','busy',{agentId:first.id,status:'queued'})
  21. expect(()=>service.create('owner',command)).toThrow('先停止')
  22. store.remove('owner','turn','busy')
  23. const shared=service.create('owner',command)
  24. expect(service.create('owner',command).id).toBe(shared.id)
  25. const linked=store.require<WorkspaceAgent>('owner','agent',first.id)
  26. expect(linked.computerEnvironmentId).toBe(shared.id)
  27. expect(()=>nodes.requireSource('other',linked)).toThrow()
  28. expect(()=>nodes.requireSource('owner',{...linked,id:randomUUID()})).toThrow()
  29. expect(()=>store.updateAgent('owner',first.id,{execution:'profile'})).toThrow('解除电脑共享')
  30. store.put('_system','computer-control','fixture',{owner:'owner',agentId:first.id,environmentId:shared.id,expiresAt:Date.now()+30000})
  31. expect(()=>service.remove('owner',shared.id)).toThrow('交还')
  32. store.remove('_system','computer-control','fixture')
  33. service.remove('owner',shared.id)
  34. expect(store.require<WorkspaceAgent>('owner','agent',first.id).computerEnvironmentId).toBeUndefined()
  35. expect(()=>nodes.requireSource('owner',linked)).toThrow('授权')
  36. expect(store.require<any>('owner','shared-computer',shared.id).archived).toBe(true)
  37. }finally{nodes.close();store.close();rmSync(home,{recursive:true,force:true})}
  38. })
  39. it('does not grant temporary helpers or mixed profiles a shared desktop',()=>{
  40. const home=mkdtempSync(join(tmpdir(),'yaoyao-shared-')),store=new WorkspaceStore(home),nodes=new WorkspaceNodes(store,loadServerConfig({HERMES_YAOYAO_HOME:home}),{} as any),service=new SharedComputers(store,{} as any,nodes,{sharedComputerRunner:()=>({id:'runner'})} as any)
  41. try{
  42. const first=store.createAgent('owner',{name:'第一位',execution:'computer',profile:'default'}),second=store.createAgent('owner',{name:'第二位',execution:'computer',profile:'different'})
  43. const command={requestId:randomUUID(),name:'共享',memberIds:[first.id,second.id],trusted:true}
  44. expect(()=>service.create('owner',command)).toThrow('同一来源')
  45. store.put('owner','agent',second.id,{...second,profile:'default',temporaryGoalId:randomUUID()})
  46. expect(()=>service.create('owner',command)).toThrow('持久隔离成员')
  47. }finally{nodes.close();store.close();rmSync(home,{recursive:true,force:true})}
  48. })
  49. it('changes only persistent local VM members and leaves unrelated running chats untouched',()=>{
  50. const home=mkdtempSync(join(tmpdir(),'yaoyao-shared-scope-')),store=new WorkspaceStore(home),nodes=new WorkspaceNodes(store,loadServerConfig({HERMES_YAOYAO_HOME:home}),{} as any),service=new SharedComputers(store,{} as any,nodes,{} as any)
  51. try{
  52. const vm=store.createAgent('owner',{name:'虚拟机成员',execution:'computer',profile:'default'})
  53. const chat=store.createAgent('owner',{name:'正在聊天',profile:'server'})
  54. const archived=store.createAgent('owner',{name:'已归档',execution:'computer',profile:'default'})
  55. store.put('owner','agent',archived.id,{...archived,archived:true})
  56. const helper=store.createAgent('owner',{name:'临时助手',execution:'computer',profile:'default'},{createdByAgentId:vm.id,createdFromRunId:randomUUID(),temporaryGoalId:randomUUID()})
  57. const remote=store.createAgent('owner',{name:'远端成员',execution:'computer',profile:'default'})
  58. store.put('owner','agent',remote.id,{...remote,nodeId:randomUUID()})
  59. const unaffected=[chat,archived,helper,remote].map(a=>store.require<WorkspaceAgent>('owner','agent',a.id))
  60. for(const agent of unaffected)store.put('owner','turn',agent.id,{agentId:agent.id,status:'running'})
  61. service.setLocalVmMode('owner','shared','runner')
  62. const linked=store.require<WorkspaceAgent>('owner','agent',vm.id)
  63. expect(linked.computerEnvironmentId).toBeTruthy()
  64. for(const agent of unaffected)expect(store.require('owner','agent',agent.id)).toEqual(agent)
  65. service.setLocalVmMode('owner','shared','runner')
  66. expect(store.require('owner','agent',vm.id)).toEqual(linked)
  67. // Explicit per-Agent execution changes must still guard an ordinary chat.
  68. expect(()=>service.assertLocalVmIdle('owner',[chat.id])).toThrow('正在聊天')
  69. for(const status of ['queued','running','waiting','uncertain','cancelling']){
  70. store.put('owner','turn','active-vm',{agentId:vm.id,status})
  71. expect(()=>service.setLocalVmMode('owner','per-bot','runner')).toThrow('请先停止「虚拟机成员」')
  72. expect(store.require('owner','agent',vm.id)).toEqual(linked)
  73. }
  74. store.remove('owner','turn','active-vm')
  75. // A controller acquired through another member still owns this same VM.
  76. store.put('_system','computer-control','human',{owner:'owner',agentId:archived.id,environmentId:linked.computerEnvironmentId,expiresAt:Date.now()+30000})
  77. expect(()=>service.setLocalVmMode('owner','per-bot','runner')).toThrow('交还')
  78. store.remove('_system','computer-control','human')
  79. service.setLocalVmMode('owner','per-bot','runner')
  80. expect(store.require<WorkspaceAgent>('owner','agent',vm.id).computerEnvironmentId).toBeUndefined()
  81. for(const agent of unaffected)expect(store.require('owner','agent',agent.id)).toEqual(agent)
  82. }finally{nodes.close();store.close();rmSync(home,{recursive:true,force:true})}
  83. })