service-manager.test.mjs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import {test} from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import {createServer} from 'node:http'
  4. import {dataKey,DesktopServiceManager} from './service-manager.mjs'
  5. import {mkdtemp,realpath,writeFile,rm} from 'node:fs/promises'
  6. import {tmpdir} from 'node:os'
  7. import {join} from 'node:path'
  8. test('checks recorded and bundled versions while permitting a separately owned older service',async()=>{
  9. const root='/fixture/desktop',record={protocol:1,instanceId:'fixture',pid:1234,dataKey:dataKey(root),version:'0.3.32',token:'fixture'}
  10. let actual={...record};delete actual.token
  11. const server=createServer((request,response)=>{response.setHeader('Content-Type','application/json');response.end(JSON.stringify(actual))})
  12. await new Promise(resolve=>server.listen(0,'127.0.0.1',resolve))
  13. record.url=`http://127.0.0.1:${server.address().port}`
  14. const manager=new DesktopServiceManager({version:'0.3.33'});manager.root=root
  15. try{
  16. assert.equal((await manager.verify(record)).version,'0.3.32')
  17. actual={...actual,version:'0.3.31'}
  18. await assert.rejects(manager.verify(record),/身份/)
  19. actual={...actual,version:'0.3.32'};manager.child={pid:record.pid}
  20. await assert.rejects(manager.verify(record),/版本不匹配/)
  21. }finally{await new Promise(resolve=>server.close(resolve))}
  22. })
  23. test('busy synchronization connects the verified existing Web and remains retryable without accepting other errors',async()=>{
  24. const home=await realpath(await mkdtemp(join(tmpdir(),'yaoyao-startup-busy-')))
  25. const record={protocol:1,instanceId:'busy-fixture',pid:process.pid,dataKey:dataKey(home),version:'0.3.31',token:'fixture'}
  26. const server=createServer((request,response)=>{const {token,...identity}=record;response.setHeader('Content-Type','application/json');response.end(JSON.stringify(identity))})
  27. await new Promise(done=>server.listen(0,'127.0.0.1',done))
  28. record.url=`http://127.0.0.1:${server.address().port}`
  29. await writeFile(join(home,'service-instance.json'),JSON.stringify(record))
  30. let attempts=0
  31. const options={home,version:'0.3.32',fork:()=>{throw new Error('must not fork')},synchronize:async()=>{if(++attempts===1)throw Object.assign(new Error('已有任务,同步待完成'),{code:'service_busy'})}}
  32. const manager=new DesktopServiceManager(options)
  33. try{
  34. const state=await manager.start()
  35. assert.equal(state.phase,'ready');assert.equal(state.external,true);assert.match(state.updateNotice,/同步待完成/)
  36. assert.equal((await manager.retrySynchronization()).updateNotice,undefined);assert.equal(attempts,2)
  37. await manager.stop()
  38. const invalid=new DesktopServiceManager({...options,synchronize:async()=>{throw new Error('invalid package')}})
  39. await assert.rejects(invalid.start(),/invalid package/)
  40. assert.equal(invalid.state.phase,'error')
  41. }finally{await new Promise(done=>server.close(done));await rm(home,{recursive:true,force:true})}
  42. })