desktopEnvironments.test.ts 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. // @vitest-environment node
  2. import {afterEach,beforeEach,expect,it,vi} from 'vitest'
  3. import {mkdtempSync,rmSync} from 'node:fs'
  4. import {tmpdir} from 'node:os'
  5. import {join} from 'node:path'
  6. import {randomUUID,createHash} from 'node:crypto'
  7. import Koa from 'koa'
  8. import {bodyParser} from '@koa/bodyparser'
  9. import request from 'supertest'
  10. import {WorkspaceStore} from '../../src/server/workspaceStore'
  11. import {DesktopEnvironments,DESKTOP_ENVIRONMENT_TOOLS,DESKTOP_FILE_TOOL_IDS} from '../../src/server/desktopEnvironments'
  12. import type {WorkspaceAgent} from '../../src/shared/workspace'
  13. import {saveHostTools} from '../../src/server/hostToolSettings'
  14. import {HttpError} from '../../src/server/errors'
  15. import {acquireServiceInstance} from '../../src/server/serviceInstance'
  16. let home:string,store:WorkspaceStore,service:DesktopEnvironments,agent:WorkspaceAgent,version:number,host:any,poller:ReturnType<typeof setInterval>|undefined
  17. const auth={require:(ctx:any)=>({id:ctx.get('x-user')||'owner'}),requireAdmin:(ctx:any)=>{if(ctx.get('x-user'))throw new HttpError(403,'仅限管理员','admin_required')},pushAuthorizationVersion:()=>version} as any
  18. const nodes={localNodeID:'local-node',requireSource:()=>{}} as any
  19. const ownerKey=(owner:string)=>createHash('sha256').update('local-node:'+owner).digest('hex')
  20. beforeEach(()=>{home=mkdtempSync(join(tmpdir(),'desktop-env-'));store=new WorkspaceStore(home);service=new DesktopEnvironments(store,auth,nodes);version=1;agent=store.createAgent('owner',{name:'Desktop',nodeId:'local',profile:'default',computer:'local'});host={id:randomUUID(),name:'测试 Mac',platform:'darwin',screen:true,accessibility:true,approved:[ownerKey('owner')]}})
  21. const pollers=new Set<ReturnType<typeof setInterval>>()
  22. afterEach(()=>{clearInterval(poller);for(const p of pollers)clearInterval(p);pollers.clear();service.close();store.close();rmSync(home,{recursive:true,force:true});vi.useRealTimers()})
  23. const app=()=>{const a=new Koa();a.use(async(ctx,next)=>{try{await next()}catch(e:any){ctx.status=e.status??500;ctx.body={error:e.message,code:e.code}}});a.use(bodyParser());a.use(service.router().routes());return a.callback()}
  24. const base=()=>'/api/app/agents/'+agent.id
  25. it('uses global desktop capabilities for legacy, modern and temporary Bots',async()=>{
  26. drive()
  27. for(const patch of [{computer:'off',envs:undefined},{computer:'vm',execution:'computer'},{envs:{}},{temporaryGoalId:randomUUID()}]){
  28. const candidate={...agent,...patch} as WorkspaceAgent
  29. expect(service.selected('owner',candidate)).toBe('local')
  30. expect(service.envTools('owner',candidate).view).toBe(true)
  31. }
  32. saveHostTools(home,{scriptMachine:false,serverComputer:false})
  33. expect(service.envTools('owner',agent).view).toBe(false)
  34. await expect(service.call('owner',agent.id,'desktop_environment_view',{host:'server'},new AbortController().signal,()=>{},'local',service.epoch)).rejects.toMatchObject({code:'desktop_host_disabled'})
  35. })
  36. function drive(handle:(c:any)=>unknown=c=>c.operation==='view'?{data:Buffer.from('fixture frame').toString('base64'),width:1280,height:800}:{ok:true}){let results:any[]=[];service.exchange({host,results});const p=setInterval(()=>{const value=service.exchange({host,results});results=value.commands.map(c=>({id:c.id,value:handle(c)}))},5);pollers.add(p);poller=p}
  37. const HOST_A='11111111-aaaa-4bbb-8ccc-111111111111',HOST_B='22222222-aaaa-4bbb-8ccc-222222222222'
  38. function driveRemote(id:string,hostInfo:any,handle:(c:any)=>unknown=c=>c.operation==='view'?{data:Buffer.from('remote frame').toString('base64'),width:1280,height:800}:{ok:true}){let results:any[]=[];service.remoteExchange(id,{host:hostInfo,results});const p=setInterval(()=>{const value=service.remoteExchange(id,{host:hostInfo,results});results=value.commands.map(c=>({id:c.id,value:handle(c)}))},5);pollers.add(p);poller=p}
  39. it('defaults bot work to the customer machine and uses that binding while the server desktop is also online',async()=>{
  40. agent=store.updateAgent('owner',agent.id,{computer:'local',desktopHost:HOST_A})
  41. const remoteHost={...host,name:'客厅 Mac Pro'}
  42. driveRemote(HOST_A,remoteHost)
  43. drive()
  44. const state=(await request(app()).get(base()+'/desktop-environment')).body
  45. expect(state.hostName??state.host?.name).toBe('客厅 Mac Pro')
  46. const named=(await request(app()).get(base()+'/computer?backend=desktop&host='+encodeURIComponent('客厅 Mac Pro'))).body
  47. expect(named.hostName).toBe('客厅 Mac Pro')
  48. vi.useFakeTimers()
  49. clearInterval(poller)
  50. vi.advanceTimersByTime(9000)
  51. const fallback=(await request(app()).get(base()+'/desktop-environment')).body
  52. expect(fallback.hostName??fallback.host?.name).toBe('客厅 Mac Pro')
  53. const epoch=service.hostEpoch('owner',agent)
  54. expect(epoch).toBe(remoteHost.id)
  55. const result=await service.call('owner',agent.id,'desktop_environment_view',{},new AbortController().signal,()=>{},'local',epoch,HOST_A)
  56. expect(result.content[0].type).toBe('image')
  57. expect(JSON.parse(result.content[1].text).host).toBe('客厅 Mac Pro')
  58. vi.useRealTimers()
  59. const hosts=(await request(app()).get(base()+'/desktop-environment')).body
  60. expect(hosts.hosts.map((h:any)=>h.id)).toEqual(['local',HOST_A])
  61. expect(hosts.hosts.find((h:any)=>h.id===HOST_A)).toMatchObject({name:'客厅 Mac Pro',online:true})
  62. })
  63. it('keeps 本机 on the computer running YaoYao when another Mac is online',async()=>{
  64. agent=store.updateAgent('owner',agent.id,{computer:'local'})
  65. drive()
  66. driveRemote(HOST_A,{...host,name:'客厅 Mac'})
  67. const state=(await request(app()).get(base()+'/desktop-environment')).body
  68. expect(state.host.id).toBe('local')
  69. const view=await service.call('owner',agent.id,'desktop_environment_view',{host:'本机'},new AbortController().signal,()=>{},'local',service.hostEpochs('owner',agent),'local')
  70. expect(JSON.parse(view.content[1].text).host).toBe('测试 Mac')
  71. })
  72. it('requires an explicit binding when several remote hosts are connected',async()=>{
  73. agent=store.updateAgent('owner',agent.id,{computer:'local'})
  74. service.remoteExchange(HOST_A,{host:{...host,name:'书房'},results:[]})
  75. service.remoteExchange(HOST_B,{host:{...host,name:'客厅'},results:[]})
  76. const state=(await request(app()).get(base()+'/desktop-environment')).body
  77. expect(state.online).toBe(false)
  78. expect(state.hosts).toHaveLength(2)
  79. await request(app()).get(base()+'/computer/frame').expect(409)
  80. agent=store.updateAgent('owner',agent.id,{desktopHost:HOST_B})
  81. driveRemote(HOST_B,{...host,name:'客厅'})
  82. const status=(await request(app()).get(base()+'/computer')).body
  83. expect(status.hostName).toBe('客厅')
  84. const ticket=await take()
  85. await request(app()).post(base()+'/computer/giveback').send(ticket).expect(200)
  86. })
  87. it('lets the manual viewer take over any connected host via the host parameter',async()=>{
  88. agent=store.updateAgent('owner',agent.id,{computer:'local',desktopHost:HOST_B})
  89. driveRemote(HOST_A,{...host,name:'书房'},c=>c.operation==='view'?{data:Buffer.from('study frame').toString('base64'),width:1280,height:800}:{ok:true})
  90. driveRemote(HOST_B,{...host,name:'客厅'},c=>c.operation==='view'?{data:Buffer.from('living frame').toString('base64'),width:1280,height:800}:{ok:true})
  91. const study='?backend=desktop&host='+encodeURIComponent(HOST_A)
  92. const status=(await request(app()).get(base()+'/computer'+study)).body
  93. expect(status.hostName).toBe('书房');expect(status.mode).toBe('idle')
  94. const ticket=(await request(app()).post(base()+'/computer/take'+study).send({requestId:randomUUID()}).expect(200)).body
  95. expect(ticket.hostName).toBe('书房')
  96. const bound=(await request(app()).get(base()+'/computer?backend=desktop')).body
  97. expect(bound.hostName).toBe('客厅');expect(bound.mode).toBe('idle')
  98. const frame=await request(app()).get(base()+'/computer/frame'+study).expect(200)
  99. expect(Buffer.from(frame.body.data,'base64').toString()).toBe('study frame')
  100. await request(app()).post(base()+'/computer/giveback'+study).send({controlId:ticket.controlId,token:ticket.token}).expect(200)
  101. const response=await request(app()).get(base()+'/computer?backend=desktop&host=nope').expect(409)
  102. expect(response.body.code).toBe('desktop_host_unknown')
  103. })
  104. it('revoking a computer fences only its dispatched commands',async()=>{
  105. agent=store.updateAgent('owner',agent.id,{computer:'local',desktopHost:HOST_A})
  106. service.remoteExchange(HOST_A,{host,results:[]})
  107. const call=service.call('owner',agent.id,'desktop_environment_view',{},new AbortController().signal,()=>{},'local',service.hostEpoch('owner',agent),HOST_A)
  108. await vi.waitFor(()=>{expect(service.remoteExchange(HOST_A,{host,results:[]}).commands).toHaveLength(1)})
  109. service.dropHost(HOST_A)
  110. await expect(call).rejects.toMatchObject({code:'desktop_disconnected'})
  111. })
  112. it('gates file and shell tools behind the separate full grant',async()=>{
  113. agent=store.updateAgent('owner',agent.id,{computer:'local'})
  114. service.exchange({host,results:[]})
  115. expect(service.fileTools('owner',agent)).toBe(false)
  116. await expect(service.call('owner',agent.id,'desktop_file_read',{host:'server',path:'notes.txt'},new AbortController().signal,()=>{},'local',service.hostEpoch('owner',agent))).rejects.toMatchObject({code:'desktop_full_required'})
  117. await expect(service.call('owner',agent.id,'desktop_shell',{host:'server',command:'ls'},new AbortController().signal,()=>{},'local',service.hostEpoch('owner',agent))).rejects.toMatchObject({code:'desktop_full_required'})
  118. const seen:any[]=[]
  119. drive(c=>{seen.push(c);return c.operation==='view'?{data:'Zm9v',width:1280,height:800}:{op:'list',entries:[]}})
  120. host.full=[ownerKey('owner')]
  121. service.exchange({host,results:[]})
  122. expect(service.fileTools('owner',agent)).toBe(true)
  123. const listing=await service.call('owner',agent.id,'desktop_file_list',{host:'server',path:'Documents'},new AbortController().signal,()=>{},'local',service.hostEpoch('owner',agent))
  124. expect(listing.entries).toEqual([])
  125. expect(seen.find(c=>c.operation==='file')?.action).toMatchObject({op:'list',path:'Documents'})
  126. await expect(service.call('owner',agent.id,'desktop_file_write',{host:'server',path:'a.txt',data:'!!!not base64!!!'},new AbortController().signal,()=>{},'local',service.hostEpoch('owner',agent))).rejects.toMatchObject({status:400})
  127. })
  128. it('passes the authorize scope through to the computer',async()=>{
  129. const seen:any[]=[]
  130. drive(c=>{seen.push(c);return {ok:true}})
  131. await request(app()).post(base()+'/desktop-environment/authorize').send({scope:'full'}).expect(200)
  132. expect(seen.find(c=>c.operation==='authorize')?.scope).toBe('full')
  133. })
  134. it('copies files between the explicitly named hosts and server without chat attachments or model payloads',async()=>{
  135. const bytes=Buffer.alloc(300_000);for(let i=0;i<bytes.length;i++)bytes[i]=i%256
  136. const seen:Array<{host:string;action:any}>=[]
  137. const handle=(id:string)=>(c:any)=>{seen.push({host:id,action:c.action});return c.action.op==='read'?{path:c.action.path,size:bytes.length,data:bytes.toString('base64')}:{path:c.action.path,size:Buffer.from(c.action.data,'base64').length,sha256:createHash('sha256').update(Buffer.from(c.action.data,'base64')).digest('hex')}}
  138. host.full=[ownerKey('owner')]
  139. drive(handle('server'))
  140. driveRemote(HOST_A,{...host,id:randomUUID(),name:'mac1'},handle('mac1'))
  141. driveRemote(HOST_B,{...host,id:randomUUID(),name:'mac2'},handle('mac2'))
  142. for(const [sourceHost,targetHost,targetId] of [['mac1','mac2',HOST_B],['mac1','server','local'],['server','本机',HOST_A]]){
  143. const result=await service.call('owner',agent.id,'desktop_file_copy',{sourceHost,sourcePath:'Desktop/a.txt',targetHost,targetPath:'Desktop/a.txt'},new AbortController().signal,()=>{},'local',service.hostEpochs('owner',agent),HOST_A)
  144. expect(result).toMatchObject({copied:true,target:{host:targetId,path:'Desktop/a.txt'},size:bytes.length,sha256:createHash('sha256').update(bytes).digest('hex')})
  145. expect(JSON.stringify(result).length).toBeLessThan(700)
  146. }
  147. expect(seen.map(({host,action})=>[host,action.op])).toEqual([['mac1','read'],['mac2','receive'],['mac1','read'],['server','receive'],['server','read'],['mac1','receive']])
  148. for(const {action} of seen.filter(item=>item.action.op==='receive')){expect(action.overwrite).toBe(false);expect(Buffer.from(action.data,'base64')).toEqual(bytes)}
  149. expect(store.list('owner','file')).toEqual([])
  150. expect(DESKTOP_FILE_TOOL_IDS.has('desktop_file_copy')).toBe(true)
  151. expect(DESKTOP_ENVIRONMENT_TOOLS.some(tool=>tool.id==='desktop_file_copy')).toBe(true)
  152. })
  153. it('serializes opposing transfers without deadlocking or changing the target',async()=>{
  154. host.full=[ownerKey('owner')]
  155. const handle=(c:any)=>c.action.op==='read'?{size:1,data:'YQ=='}:{path:c.action.path,size:1,sha256:c.action.sha256}
  156. driveRemote(HOST_A,{...host,name:'mac1'},handle);driveRemote(HOST_B,{...host,id:randomUUID(),name:'mac2'},handle)
  157. const copy=(sourceHost:string,targetHost:string)=>service.call('owner',agent.id,'desktop_file_copy',{sourceHost,sourcePath:'Desktop/a',targetHost,targetPath:'Desktop/b'},new AbortController().signal,()=>{},'local',service.hostEpochs('owner',agent))
  158. const values=await Promise.all([copy('mac1','mac2'),copy('mac2','mac1'),copy('mac1','mac1')])
  159. expect(values.map(value=>value.target.host)).toEqual([HOST_B,HOST_A,HOST_A])
  160. })
  161. it('requires both host grants and never falls back for missing or ambiguous hosts',async()=>{
  162. host.full=[ownerKey('owner')];drive()
  163. driveRemote(HOST_A,{...host,id:randomUUID(),name:'mac1',full:[]})
  164. const copy=(sourceHost:string,targetHost:string)=>service.call('owner',agent.id,'desktop_file_copy',{sourceHost,sourcePath:'Desktop/a',targetHost,targetPath:'Desktop/a'},new AbortController().signal,()=>{},'local',service.hostEpochs('owner',agent))
  165. await expect(copy('server','mac1')).rejects.toMatchObject({code:'desktop_full_required'})
  166. await expect(copy('mac1','server')).rejects.toMatchObject({code:'desktop_full_required'})
  167. await expect(copy('server','本机')).rejects.toMatchObject({code:'desktop_source_required'})
  168. await expect(copy('missing','server')).rejects.toMatchObject({code:'desktop_host_unknown'})
  169. driveRemote(HOST_B,{...host,id:randomUUID(),name:'mac1'})
  170. await expect(copy('mac1','server')).rejects.toMatchObject({code:'desktop_host_ambiguous'})
  171. })
  172. it.each(['disconnect','cancel','revoke','corrupt','oversized'])('does not write the target when the source transfer is %s',async failure=>{
  173. host.full=[ownerKey('owner')];const controller=new AbortController(),writes:any[]=[]
  174. driveRemote(HOST_A,{...host,name:'mac1'},()=>{
  175. if(failure==='disconnect')service.dropHost(HOST_B)
  176. if(failure==='cancel')controller.abort()
  177. if(failure==='revoke')version++
  178. return {size:failure==='oversized'?10*1024*1024+1:failure==='corrupt'?2:1,data:'YQ=='}
  179. })
  180. driveRemote(HOST_B,{...host,id:randomUUID(),name:'mac2'},c=>{writes.push(c);return {}})
  181. await expect(service.call('owner',agent.id,'desktop_file_copy',{sourceHost:'mac1',sourcePath:'Desktop/a',targetHost:'mac2',targetPath:'Desktop/a'},controller.signal,()=>{},'local',service.hostEpochs('owner',agent))).rejects.toThrow()
  182. expect(writes).toEqual([])
  183. })
  184. it('reports a failed destination receipt without claiming success or retrying',async()=>{
  185. host.full=[ownerKey('owner')];const receives:any[]=[]
  186. driveRemote(HOST_A,{...host,name:'mac1'},()=>({size:0,data:''}))
  187. driveRemote(HOST_B,{...host,id:randomUUID(),name:'mac2'},c=>{receives.push(c);return {path:c.action.path,size:0,sha256:'wrong'}})
  188. await expect(service.call('owner',agent.id,'desktop_file_copy',{sourceHost:'mac1',sourcePath:'Desktop/a',targetHost:'mac2',targetPath:'Desktop/a',overwrite:true},new AbortController().signal,()=>{},'local',service.hostEpochs('owner',agent))).rejects.toMatchObject({code:'desktop_transfer_unconfirmed'})
  189. expect(receives).toHaveLength(1);expect(receives[0].action.overwrite).toBe(true)
  190. })
  191. it('waits for a target taken over during the source read and resumes after giveback',async()=>{
  192. host.full=[ownerKey('owner')]
  193. const source={...host,name:'mac1'},target={...host,id:randomUUID(),name:'mac2'},writes:any[]=[]
  194. service.remoteExchange(HOST_A,{host:source,results:[]})
  195. driveRemote(HOST_B,target,c=>{if(c.operation!=='file')return {ok:true};writes.push(c);return {path:c.action.path,size:1,sha256:c.action.sha256}})
  196. const copying=service.call('owner',agent.id,'desktop_file_copy',{sourceHost:'mac1',sourcePath:'Desktop/a',targetHost:'mac2',targetPath:'Desktop/a'},new AbortController().signal,()=>{},'local',service.hostEpochs('owner',agent))
  197. let read:any
  198. await vi.waitFor(()=>{read=service.remoteExchange(HOST_A,{host:source,results:[]}).commands[0];expect(read).toBeDefined()})
  199. const query='?backend=desktop&host='+HOST_B
  200. const ticket=(await request(app()).post(base()+'/computer/take'+query).send({requestId:randomUUID()}).expect(200)).body
  201. service.remoteExchange(HOST_A,{host:source,results:[{id:read.id,value:{size:1,data:'YQ=='}}]})
  202. await new Promise(resolve=>setTimeout(resolve,180));expect(writes).toEqual([])
  203. await request(app()).post(base()+'/computer/giveback'+query).send({controlId:ticket.controlId,token:ticket.token,notes:'已交还 mac2'}).expect(200)
  204. expect(await copying).toMatchObject({copied:true,handoffNotes:['mac2:已交还 mac2']});expect(writes).toHaveLength(1)
  205. })
  206. it('routes parallel desktop tools to the host named in the call',async()=>{
  207. agent=store.updateAgent('owner',agent.id,{computer:'local',envs:{desktop:true,cloud:true}})
  208. const study={...host,name:'书房 iMac'},living={...host,id:randomUUID(),name:'客厅 Mac mini'}
  209. driveRemote(HOST_A,study)
  210. let resultsB:any[]=[]
  211. service.remoteExchange(HOST_B,{host:living,results:[]})
  212. const pollerB=setInterval(()=>{const value=service.remoteExchange(HOST_B,{host:living,results:resultsB});resultsB=value.commands.map(c=>({id:c.id,value:c.operation==='view'?{data:Buffer.from('living-room').toString('base64'),width:1280,height:800}:{ok:true}}))},5)
  213. try{
  214. expect(service.envTools('owner',agent)).toEqual({view:true,browser:false,file:false})
  215. const frame=await service.call('owner',agent.id,'desktop_environment_view',{host:'客厅 Mac mini'},new AbortController().signal,()=>{},'local',service.hostEpochs('owner',agent))
  216. expect(Buffer.from(frame.content[0].data,'base64').toString()).toBe('living-room')
  217. await expect(service.call('owner',agent.id,'desktop_environment_view',{host:'书房 iMac'},new AbortController().signal,()=>{},'local',service.hostEpochs('owner',agent))).resolves.toMatchObject({content:[{type:'image'},{type:'text'}]})
  218. await expect(service.call('owner',agent.id,'desktop_environment_view',{host:'地下室 MacBook'},new AbortController().signal,()=>{},'local',service.hostEpochs('owner',agent))).rejects.toMatchObject({code:'desktop_host_unknown'})
  219. expect(service.onlineHostsLine('owner')).toContain('书房 iMac')
  220. }finally{clearInterval(pollerB)}
  221. })
  222. it('ignores a stored browser environment and keeps desktop tools',async()=>{
  223. agent=store.updateAgent('owner',agent.id,{envs:{vm:true,desktop:true,browser:true,cloud:false}})
  224. drive()
  225. expect(service.selected('owner',agent)).toBe('local')
  226. expect(service.envTools('owner',agent)).toEqual({view:true,browser:false,file:false})
  227. await expect(service.call('owner',agent.id,'desktop_browser',{kind:'navigate',url:'https://example.test/both'},new AbortController().signal,()=>{},'browser',service.epoch)).rejects.toMatchObject({status:404,code:'tool_not_found'})
  228. const view=await service.call('owner',agent.id,'desktop_environment_view',{host:'server'},new AbortController().signal,()=>{},'local',service.epoch)
  229. expect(view.content[0].type).toBe('image')
  230. })
  231. it('degrades envs desktop tools when no host is ready or online',async()=>{
  232. agent=store.updateAgent('owner',agent.id,{computer:'off',envs:{desktop:true,browser:true}})
  233. expect(service.envTools('owner',agent)).toEqual({view:false,browser:false,file:false})
  234. service.exchange({host,results:[]})
  235. expect(service.envTools('owner',agent)).toEqual({view:true,browser:false,file:false})
  236. host.full=[ownerKey('owner')]
  237. service.exchange({host,results:[]})
  238. expect(service.envTools('owner',agent)).toEqual({view:true,browser:false,file:true})
  239. })
  240. it('keeps manual takeover scoped per computer',async()=>{
  241. agent=store.updateAgent('owner',agent.id,{computer:'local',desktopHost:HOST_A})
  242. const second=store.createAgent('owner',{name:'Second',nodeId:'local',profile:'default',computer:'local',desktopHost:HOST_B})
  243. driveRemote(HOST_A,{...host,name:'书房'})
  244. let resultsB:any[]=[]
  245. const pollerB=setInterval(()=>{const value=service.remoteExchange(HOST_B,{host:{...host,name:'客厅'},results:resultsB});resultsB=value.commands.map(c=>({id:c.id,value:{ok:true}}))},5)
  246. try{
  247. const ticketA=await take()
  248. const ticketB=(await request(app()).post('/api/app/agents/'+second.id+'/computer/take').send({requestId:randomUUID()}).expect(200)).body
  249. expect(ticketB.hostName).toBe('客厅')
  250. const records=store.list<any>('_system','computer-control')
  251. expect(records).toHaveLength(2)
  252. expect(new Set(records.map(r=>r.environmentId.split(':')[1])).size).toBe(2)
  253. await request(app()).post(base()+'/computer/renew').send(ticketA).expect(200)
  254. await request(app()).post(base()+'/computer/giveback').send(ticketA).expect(200)
  255. await request(app()).post('/api/app/agents/'+second.id+'/computer/giveback').send(ticketB).expect(200)
  256. }finally{clearInterval(pollerB)}
  257. })
  258. async function take(){return (await request(app()).post(base()+'/computer/take').send({requestId:randomUUID()}).expect(200)).body}
  259. async function frame(){return (await request(app()).get(base()+'/computer/frame').expect(200)).body}
  260. it('keeps capability private and refuses origins before parsing native commands',async()=>{const instance=acquireServiceInstance(home,'0.4.3'),a=new Koa().use(instance.middleware(async()=>{},()=>true,ctx=>service.bridge(ctx)));try{await request(a.callback()).post('/desktop/environment').send({host,results:[]}).expect(403);await request(a.callback()).post('/desktop/environment').set('x-yaoyao-desktop-token',instance.record.token).set('Origin','http://evil.test').send({host,results:[]}).expect(403);await request(a.callback()).post('/desktop/environment').set('x-yaoyao-desktop-token',instance.record.token).send({host,results:[]}).expect(200);expect(service.online).toBe(true)}finally{instance.release()}})
  261. it('auto selects only an approved and ready local host; observing never opens a browser',async()=>{agent=store.updateAgent('owner',agent.id,{computer:'auto'});expect(service.selected('owner',agent)).toBe('local');expect(service.exchange({host:{...host,approved:[]},results:[]}).commands).toEqual([]);expect(service.selected('owner',agent)).toBe('local');service.exchange({host,results:[]});expect(service.selected('owner',agent)).toBe('local');host.screen=false;service.exchange({host,results:[]});expect(service.selected('owner',agent)).toBe('local');await request(app()).get(base()+'/desktop-environment').set('x-user','other').expect(404)})
  262. it('shares the physical desktop across owners and robots but scopes manual tokens to their owner and robot',async()=>{agent=store.updateAgent('owner',agent.id,{computer:'local'});const other=store.createAgent('other',{name:'Other',nodeId:'local',profile:'default',computer:'local'});host.approved.push(ownerKey('other'));drive();const ticket=await take();await request(app()).post('/api/app/agents/'+other.id+'/computer/take').set('x-user','other').send({requestId:randomUUID()}).expect(409);await request(app()).post('/api/app/agents/'+other.id+'/computer/renew').set('x-user','other').send(ticket).expect(410);const state=(await request(app()).get('/api/app/agents/'+other.id+'/computer').set('x-user','other')).body;expect(state.controlId).toBeUndefined();await request(app()).post(base()+'/computer/giveback').send(ticket).expect(200)})
  263. it('rejects stale frames and replays an input result exactly once',async()=>{const calls:any[]=[];drive(c=>{calls.push(c);return c.operation==='view'?{data:'Zm9v',width:1280,height:800}:{ok:true}});const t=await take(),f=await frame(),body={controlId:t.controlId,token:t.token,requestId:randomUUID(),generation:f.generation,frameId:f.id,action:{kind:'click',x:10,y:10}};await request(app()).post(base()+'/computer/input').send({...body,frameId:randomUUID()}).expect(409);await request(app()).post(base()+'/computer/input').send(body).expect(200);await request(app()).post(base()+'/computer/input').send(body).expect(200);expect(calls.filter(c=>c.operation==='input')).toHaveLength(1);await request(app()).post(base()+'/computer/input').send({...body,action:{kind:'text',text:'different'}}).expect(409);await request(app()).put(base()+'/browser-profile').send({profile:'temporary'}).expect(409)})
  264. it('expired control remains paused until a new takeover is explicitly returned',async()=>{drive();const t=await take();const original=Date.now;const time=vi.spyOn(Date,'now').mockImplementation(()=>original()+31000);service.exchange({host,results:[]});await request(app()).post(base()+'/computer/renew').send(t).expect(410);expect((await request(app()).get(base()+'/computer')).body.mode).toBe('error');time.mockRestore();const next=await take();await request(app()).post(base()+'/computer/giveback').send({...next,notes:'已完成登录'}).expect(200);expect((await request(app()).get(base()+'/computer')).body.mode).toBe('idle')})
  265. it('pins tool calls to the host epoch and does not dispatch after permissions change',async()=>{drive();const epoch=service.epoch,signal=new AbortController().signal;await service.call('owner',agent.id,'desktop_environment_view',{host:'server'},signal,()=>{},'local',epoch);host.id=randomUUID();service.exchange({host,results:[]});await expect(service.call('owner',agent.id,'desktop_environment_action',{host:'server',kind:'text',text:'no'},signal,()=>{},'local',epoch)).rejects.toThrow('连接已改变');version++;const t=await take();version++;await request(app()).post(base()+'/computer/renew').send(t).expect(410)})
  266. it('waits for human release before the robot runs and forwards handoff notes',async()=>{drive();const t=await take();let complete=false;const call=service.call('owner',agent.id,'desktop_environment_view',{host:'server'},new AbortController().signal,()=>{},'local',service.epoch).then(value=>{complete=true;return value});await new Promise(resolve=>setTimeout(resolve,30));expect(complete).toBe(false);await request(app()).post(base()+'/computer/giveback').send({...t,notes:'已登录测试账号'}).expect(200);const value=await call;expect(JSON.stringify(value)).toContain('已登录测试账号')})
  267. it('refuses dangerous navigation and prevents changing profile during active work',async()=>{drive();const t=await take(),f=await frame();await request(app()).post(base()+'/computer/browser').send({controlId:t.controlId,token:t.token,requestId:randomUUID(),generation:f.generation,frameId:f.id,action:{kind:'navigate',url:'file:///etc/passwd'}}).expect(409);await request(app()).post(base()+'/computer/giveback').send(t).expect(200);store.put('owner','turn','turn',{agentId:agent.id,status:'running'});await request(app()).put(base()+'/browser-profile').send({profile:'temporary'}).expect(409)})
  268. it('resolves 本机 to the messaging device when deviceHost is a computer',async()=>{
  269. agent=store.updateAgent('owner',agent.id,{computer:'local'})
  270. drive()
  271. driveRemote(HOST_A,{...host,name:'客厅 Mac'})
  272. const view=await service.call('owner',agent.id,'desktop_environment_view',{host:'本机'},new AbortController().signal,()=>{},'local',service.hostEpochs('owner',agent),HOST_A)
  273. expect(JSON.parse(view.content[1].text).host).toBe('客厅 Mac')
  274. const omitted=await service.call('owner',agent.id,'desktop_environment_view',{},new AbortController().signal,()=>{},'local',service.hostEpochs('owner',agent),HOST_A)
  275. expect(JSON.parse(omitted.content[1].text).host).toBe('客厅 Mac')
  276. })
  277. it('keeps 本机 on the server when deviceHost is local even if a computer is bound',async()=>{
  278. agent=store.updateAgent('owner',agent.id,{computer:'local',desktopHost:HOST_A})
  279. drive()
  280. driveRemote(HOST_A,{...host,name:'客厅 Mac'})
  281. const view=await service.call('owner',agent.id,'desktop_environment_view',{host:'本机'},new AbortController().signal,()=>{},'local',service.hostEpochs('owner',agent),'local')
  282. expect(JSON.parse(view.content[1].text).host).toBe('测试 Mac')
  283. })
  284. it('deviceContextLine names the messaging host',()=>{
  285. drive()
  286. driveRemote(HOST_A,{...host,name:'客厅 Mac'})
  287. expect(service.deviceContextLine('owner',HOST_A)).toContain('客厅 Mac')
  288. expect(service.deviceContextLine('owner',HOST_A)).toContain('本机')
  289. expect(service.onlineHostsLine('owner',HOST_A)).toContain('本机')
  290. })
  291. it('never falls back when the sending computer is unknown, disconnected or disabled',async()=>{
  292. drive()
  293. driveRemote(HOST_A,{...host,id:randomUUID(),name:'发送电脑'})
  294. for(const args of [{},{host:'本机'},{host:'local'}]){
  295. await expect(service.call('owner',agent.id,'desktop_environment_view',args,new AbortController().signal,()=>{},'local',service.hostEpochs('owner',agent))).rejects.toMatchObject({code:'desktop_source_required'})
  296. }
  297. service.dropHost(HOST_A)
  298. for(const args of [{},{host:'本机'}])await expect(service.call('owner',agent.id,'desktop_environment_view',args,new AbortController().signal,()=>{},'local',service.hostEpochs('owner',agent),HOST_A)).rejects.toMatchObject({code:'desktop_offline'})
  299. driveRemote(HOST_A,{...host,id:randomUUID(),name:'发送电脑'})
  300. saveHostTools(home,{scriptMachine:false,serverComputer:true})
  301. await expect(service.call('owner',agent.id,'desktop_environment_view',{},new AbortController().signal,()=>{},'local',service.hostEpochs('owner',agent),HOST_A)).rejects.toMatchObject({code:'desktop_host_disabled'})
  302. const view=await service.call('owner',agent.id,'desktop_environment_view',{host:'服务器'},new AbortController().signal,()=>{},'local',service.hostEpochs('owner',agent),HOST_A)
  303. expect(JSON.parse(view.content[1].text).host).toBe('测试 Mac')
  304. })
  305. it('can use files and commands without screen or accessibility authorization',async()=>{
  306. host.screen=false;host.accessibility=false;host.approved=[];host.full=[ownerKey('owner')]
  307. const seen:any[]=[];drive(c=>{seen.push(c);return {ok:true}})
  308. expect(service.envTools('owner',agent)).toEqual({view:false,browser:false,file:true})
  309. for(const [id,args] of [['desktop_file_read',{path:'notes.txt'}],['desktop_shell',{command:'pwd'}]] as const){
  310. await expect(service.call('owner',agent.id,id,args,new AbortController().signal,()=>{},'local',service.hostEpochs('owner',agent),'local')).resolves.toEqual({ok:true})
  311. }
  312. expect(seen.map(c=>c.operation)).toEqual(['file','shell'])
  313. await expect(service.call('owner',agent.id,'desktop_environment_view',{},new AbortController().signal,()=>{},'local',service.hostEpochs('owner',agent),'local')).rejects.toMatchObject({code:'desktop_permission_required'})
  314. })
  315. it('keeps concurrent message origins separate and lets explicit targets override them',async()=>{
  316. drive();driveRemote(HOST_A,{...host,id:randomUUID(),name:'电脑 A'});driveRemote(HOST_B,{...host,id:randomUUID(),name:'电脑 B'})
  317. const view=(source:string,args:object={})=>service.call('owner',agent.id,'desktop_environment_view',args,new AbortController().signal,()=>{},'local',service.hostEpochs('owner',agent),source).then(v=>JSON.parse(v.content[1].text).host)
  318. expect(await Promise.all([view(HOST_A),view(HOST_B),view(HOST_A,{host:'server'})])).toEqual(['电脑 A','电脑 B','测试 Mac'])
  319. })
  320. it('rejects malformed or ambiguous explicit targets instead of using the message source',async()=>{
  321. drive();driveRemote(HOST_A,{...host,id:randomUUID(),name:'重复名称'});driveRemote(HOST_B,{...host,id:randomUUID(),name:'重复名称'})
  322. for(const target of [17,null,''])await expect(service.call('owner',agent.id,'desktop_environment_view',{host:target},new AbortController().signal,()=>{},'local',service.hostEpochs('owner',agent),'local')).rejects.toMatchObject({status:400})
  323. await expect(service.call('owner',agent.id,'desktop_environment_view',{host:'重复名称'},new AbortController().signal,()=>{},'local',service.hostEpochs('owner',agent),'local')).rejects.toMatchObject({code:'desktop_host_ambiguous'})
  324. const view=await service.call('owner',agent.id,'desktop_environment_view',{host:HOST_A},new AbortController().signal,()=>{},'local',service.hostEpochs('owner',agent),'local')
  325. expect(JSON.parse(view.content[1].text).host).toBe('重复名称')
  326. })
  327. it('routes chunk copies from any connected computer to the Bot VM and snapshots the configured limit',async()=>{
  328. host.full=[ownerKey('owner')];host.fileTransferVersion=1
  329. const bytes=Buffer.alloc(524289,9),sha256=createHash('sha256').update(bytes).digest('hex'),actions:any[]=[]
  330. driveRemote(HOST_A,{...host,name:'书房'},c=>{
  331. const a=c.action;actions.push(a)
  332. if(a.op==='transfer-read-open'){saveHostTools(home,{fileTransferMaxMiB:1});return {size:bytes.length,sha256,path:a.path}}
  333. if(a.op==='transfer-read')return {offset:a.offset,data:bytes.subarray(a.offset,a.offset+524288).toString('base64')}
  334. return {ok:true}
  335. })
  336. saveHostTools(home,{fileTransferMaxMiB:2})
  337. const received:Buffer[]=[],vmActions:any[]=[]
  338. const vm=async(a:Record<string,any>)=>{
  339. vmActions.push(a)
  340. if(a.op==='transfer-write-open')return {}
  341. if(a.op==='transfer-append'){received.push(Buffer.from(a.data,'base64'));return {received:Buffer.concat(received).length}}
  342. if(a.op==='transfer-finish')return {complete:true,path:'target.bin',size:bytes.length,sha256}
  343. return {ok:true}
  344. }
  345. const result=await service.call('owner',agent.id,'desktop_file_copy',{sourceHost:HOST_A,sourcePath:'source.bin',targetHost:'vm',targetPath:'target.bin'},new AbortController().signal,()=>{},'local',service.hostEpochs('owner',agent),HOST_A,vm)
  346. expect(result).toMatchObject({copied:true,target:{host:'vm'}})
  347. expect(vmActions[0]).toMatchObject({maxBytes:2*1024*1024,path:'target.bin',overwrite:false})
  348. expect(Buffer.concat(received).equals(bytes)).toBe(true)
  349. expect(actions.filter(a=>a.op==='transfer-read')).toHaveLength(2)
  350. expect(actions.at(-1).op).toBe('transfer-abort')
  351. await expect(service.call('owner',agent.id,'desktop_file_copy',{sourceHost:HOST_A,sourcePath:'source.bin',targetHost:'vm',targetPath:'target.bin',maxBytes:100*1024*1024},new AbortController().signal,()=>{},'local',service.hostEpochs('owner',agent),HOST_A,vm)).rejects.toThrow()
  352. })
  353. it('lists stable tool targets and separate per-host capabilities without claiming browser availability', () => {
  354. const filesOnly = { ...host, id: randomUUID(), name: '同名电脑', screen: false, accessibility: false, approved: [], full: [ownerKey('owner')] }
  355. service.exchange({ host: { ...host, name: '同名电脑' }, results: [] })
  356. service.remoteExchange(HOST_A, { host: filesOnly, results: [] })
  357. service.remoteExchange(HOST_B, { host: { ...host, id: randomUUID(), name: 'Linux', platform: 'linux', full: [ownerKey('owner')] }, results: [] })
  358. const text = service.onlineHostsLine('owner', HOST_A)
  359. expect(text).toContain('host="server"')
  360. expect(text).toContain(`host="${HOST_A}"`)
  361. expect(text).toContain('电脑·本机')
  362. expect(text).toContain('屏幕控制=就绪;文件与命令=未授权')
  363. expect(text).toContain('屏幕控制=未授权;文件与命令=就绪')
  364. expect(text).toContain('屏幕控制=不支持;文件与命令=不支持')
  365. expect(text).not.toContain('浏览器可用')
  366. const ungranted = service.onlineHostsLine('other', HOST_A)
  367. expect(ungranted).not.toContain('=就绪')
  368. saveHostTools(home, { scriptMachine: false })
  369. expect(service.snapshot('owner', agent, HOST_A).hosts.find(host => host.id === HOST_A)?.capabilities.shell.status).toBe('disabled')
  370. expect(service.onlineHostsLine('owner', HOST_A)).toContain('host="server"')
  371. })
  372. const environmentMetadata={version:1 as const,osRelease:'25.0.0',arch:'arm64',shell:'/bin/zsh',homeDirectory:'/Users/fixture',defaultCwd:'/Users/fixture',fileRoots:['/Users/fixture'],shellScope:'user' as const,timezone:'Asia/Shanghai'}
  373. it('snapshots negotiated host facts per owner without inventing paths for old clients',()=>{
  374. const result=service.exchange({host:{...host,full:[ownerKey('owner')],fileTransferVersion:1,environment:environmentMetadata},results:[]})
  375. expect(result.capabilities).toEqual({environmentMetadata:1})
  376. service.remoteExchange(HOST_A,{host:{...host,id:randomUUID(),name:'旧客户端'},results:[]})
  377. const snapshot=service.snapshot('owner',agent,'local'),server=snapshot.hosts.find(host=>host.id==='local')!
  378. expect(server.metadata).toEqual(environmentMetadata)
  379. expect(server.capabilities.shell).toEqual({enabled:true,status:'ready'})
  380. expect(server.transfer).toEqual({protocol:'chunked',readMaxMiB:25,writeMaxMiB:25})
  381. expect(snapshot.hosts.find(host=>host.id===HOST_A)?.metadata).toBeUndefined()
  382. expect(JSON.stringify(service.snapshot('other',agent,'local'))).not.toContain('/Users/fixture')
  383. expect(JSON.stringify(snapshot)).not.toContain(ownerKey('owner'))
  384. server.metadata!.fileRoots!.push('/changed')
  385. expect(service.snapshot('owner',agent).hosts.find(host=>host.id==='local')?.metadata?.fileRoots).toEqual(['/Users/fixture'])
  386. })
  387. it('keeps offline and disabled registered computers visible with actionable reasons',()=>{
  388. store.put('_system','desktop-host',HOST_B,{id:HOST_B,name:'尚未连接',enabled:false,tokenHash:'private',createdAt:1})
  389. service.remoteExchange(HOST_A,{host:{...host,id:randomUUID(),name:'连接电脑',full:[ownerKey('owner')],environment:environmentMetadata},results:[]})
  390. const saved=service.snapshot('owner',agent,HOST_A)
  391. vi.useFakeTimers();vi.advanceTimersByTime(16000)
  392. const offline=service.snapshot('owner',agent,HOST_A)
  393. expect(offline.hosts.find(host=>host.id===HOST_A)).toMatchObject({source:true,online:false,capabilities:{shell:{enabled:false,status:'offline'}}})
  394. expect(offline.hosts.find(host=>host.id===HOST_A)?.metadata).toBeUndefined()
  395. expect(offline.hosts.find(host=>host.id===HOST_B)).toMatchObject({name:'尚未连接',open:false,capabilities:{view:{enabled:false,status:'disabled'}}})
  396. expect(saved.hosts.find(host=>host.id===HOST_A)?.capabilities.shell.status).toBe('ready')
  397. expect(service.deviceContextLine('owner',HOST_A)).toContain('不能回退到服务器')
  398. expect(JSON.stringify(offline)).not.toContain('private')
  399. })
  400. it('distinguishes human takeover from grants and rechecks revocation after collecting a snapshot',async()=>{
  401. host.full=[ownerKey('owner')];drive()
  402. const ticket=await take(),snapshot=service.snapshot('owner',agent,'local'),server=snapshot.hosts[0]!
  403. expect(server.capabilities.shell).toEqual({enabled:true,status:'human_control'})
  404. await request(app()).post(base()+'/computer/giveback').send({controlId:ticket.controlId,token:ticket.token}).expect(200)
  405. expect(service.snapshot('owner',agent).hosts[0]?.capabilities.shell.status).toBe('ready')
  406. host.full=[];service.exchange({host,results:[]})
  407. const epochs=Object.fromEntries(snapshot.hosts.map(host=>[host.id,host.epoch!]))
  408. await expect(service.call('owner',agent.id,'desktop_file_list',{host:'server',path:'.'},new AbortController().signal,()=>{},'local',epochs,'local')).rejects.toMatchObject({code:'desktop_full_required'})
  409. expect(snapshot.hosts[0]?.capabilities.shell.enabled).toBe(true)
  410. expect(service.snapshot('owner',agent).hosts[0]?.capabilities.shell).toEqual({enabled:false,status:'not_authorized'})
  411. })
  412. it('rejects malformed metadata without replacing the last admitted host state',()=>{
  413. service.exchange({host,results:[]})
  414. expect(()=>service.exchange({host:{...host,environment:{...environmentMetadata,homeDirectory:'bad\npath'}},results:[]})).toThrow()
  415. expect(service.snapshot('owner',agent).hosts[0]?.epoch).toBe(host.id)
  416. expect(service.snapshot('owner',agent).hosts[0]?.metadata).toBeUndefined()
  417. })
  418. it('does not dispatch to a newly connected target outside the captured host set',async()=>{
  419. drive()
  420. const snapshot=service.snapshot('owner',agent,'local'),epochs=Object.fromEntries(snapshot.hosts.map(host=>[host.id,host.epoch!]))
  421. const commands:unknown[]=[]
  422. driveRemote(HOST_A,{...host,id:randomUUID()},command=>{commands.push(command);return {ok:true}})
  423. await expect(service.call('owner',agent.id,'desktop_environment_view',{host:HOST_A},new AbortController().signal,()=>{},'local',epochs,'local')).rejects.toMatchObject({code:'desktop_context_changed'})
  424. expect(commands).toEqual([])
  425. })