| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- // @vitest-environment node
- import {expect,it} from 'vitest'
- import {mkdtempSync,rmSync} from 'node:fs'
- import {tmpdir} from 'node:os'
- import {join} from 'node:path'
- import {randomUUID} from 'node:crypto'
- import {WorkspaceStore} from '../../src/server/workspaceStore'
- import {WorkspaceNodes} from '../../src/server/workspaceGateway'
- import {loadServerConfig} from '../../src/server/config'
- import {SharedComputers} from '../../src/server/sharedComputers'
- import type {WorkspaceAgent} from '../../src/shared/workspace'
- it('requires an explicit human grant, one compatible source and idle permanent members',()=>{
- const home=mkdtempSync(join(tmpdir(),'yaoyao-shared-')),store=new WorkspaceStore(home),nodes=new WorkspaceNodes(store,loadServerConfig({HERMES_YAOYAO_HOME:home}),{} as any)
- const service=new SharedComputers(store,{} as any,nodes,{sharedComputerRunner:()=>({id:'runner'})} as any)
- try{
- const first=store.createAgent('owner',{name:'第一位',execution:'computer',profile:'default'}),second=store.createAgent('owner',{name:'第二位',execution:'computer',profile:'default'})
- const command={requestId:randomUUID(),name:'可信研发电脑',memberIds:[first.id,second.id],trusted:true}
- expect(()=>service.create('owner',{...command,trusted:false})).toThrow()
- expect(()=>store.createAgent('owner',{name:'模型新建',profile:'default',computerEnvironmentId:randomUUID()})).toThrow()
- store.put('owner','turn','busy',{agentId:first.id,status:'queued'})
- expect(()=>service.create('owner',command)).toThrow('先停止')
- store.remove('owner','turn','busy')
- const shared=service.create('owner',command)
- expect(service.create('owner',command).id).toBe(shared.id)
- const linked=store.require<WorkspaceAgent>('owner','agent',first.id)
- expect(linked.computerEnvironmentId).toBe(shared.id)
- expect(()=>nodes.requireSource('other',linked)).toThrow()
- expect(()=>nodes.requireSource('owner',{...linked,id:randomUUID()})).toThrow()
- expect(()=>store.updateAgent('owner',first.id,{execution:'profile'})).toThrow('解除电脑共享')
- store.put('_system','computer-control','fixture',{owner:'owner',agentId:first.id,environmentId:shared.id,expiresAt:Date.now()+30000})
- expect(()=>service.remove('owner',shared.id)).toThrow('交还')
- store.remove('_system','computer-control','fixture')
- service.remove('owner',shared.id)
- expect(store.require<WorkspaceAgent>('owner','agent',first.id).computerEnvironmentId).toBeUndefined()
- expect(()=>nodes.requireSource('owner',linked)).toThrow('授权')
- expect(store.require<any>('owner','shared-computer',shared.id).archived).toBe(true)
- }finally{nodes.close();store.close();rmSync(home,{recursive:true,force:true})}
- })
- it('does not grant temporary helpers or mixed profiles a shared desktop',()=>{
- 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)
- try{
- const first=store.createAgent('owner',{name:'第一位',execution:'computer',profile:'default'}),second=store.createAgent('owner',{name:'第二位',execution:'computer',profile:'different'})
- const command={requestId:randomUUID(),name:'共享',memberIds:[first.id,second.id],trusted:true}
- expect(()=>service.create('owner',command)).toThrow('同一来源')
- store.put('owner','agent',second.id,{...second,profile:'default',temporaryGoalId:randomUUID()})
- expect(()=>service.create('owner',command)).toThrow('持久隔离成员')
- }finally{nodes.close();store.close();rmSync(home,{recursive:true,force:true})}
- })
|