service-activation.test.mjs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { test } from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import { EventEmitter } from 'node:events'
  4. import { mkdtemp, realpath, rm } from 'node:fs/promises'
  5. import { tmpdir } from 'node:os'
  6. import { join } from 'node:path'
  7. import { DesktopServiceManager } from './service-manager.mjs'
  8. import { grantDesktopActivation, hasDesktopActivation } from '../bin/lib/desktop-activation.mjs'
  9. for (const legacy of [false, true]) test(`inspection preserves active background authorization when reusing a ${legacy ? 'legacy' : 'current'} service`, async () => {
  10. const home = await realpath(await mkdtemp(join(tmpdir(), 'yaoyao-reuse-activation-')))
  11. grantDesktopActivation(home)
  12. const service = { url: 'http://127.0.0.1:15300', version: 'fixture', pid: 123, legacy }
  13. const manager = new DesktopServiceManager({ home, fork: () => assert.fail('must reuse the live service') })
  14. manager.readRecord = async () => legacy ? null : service
  15. manager.verify = async () => service
  16. manager.legacyService = async () => legacy ? service : null
  17. try {
  18. await manager.start()
  19. assert.equal(manager.state.phase, 'ready')
  20. assert.equal(hasDesktopActivation(home), true)
  21. assert.equal(manager.activationRequested, false)
  22. await manager.stop()
  23. assert.equal(hasDesktopActivation(home), true, 'disconnecting must preserve independent background recovery')
  24. } finally { await manager.stop(); await rm(home, { recursive: true, force: true }) }
  25. })
  26. test('inspection clears a previous authorization immediately before forking a new service', async () => {
  27. const home = await realpath(await mkdtemp(join(tmpdir(), 'yaoyao-fork-activation-')))
  28. grantDesktopActivation(home)
  29. let forked = false
  30. const child = new EventEmitter()
  31. child.pid = 123
  32. child.kill = () => child.emit('exit', 0)
  33. const service = { url: 'http://127.0.0.1:15300', version: 'fixture', pid: child.pid }
  34. const manager = new DesktopServiceManager({ home, fork: () => {
  35. assert.equal(hasDesktopActivation(home), false, 'the child must start in inspection mode')
  36. forked = true
  37. return child
  38. } })
  39. manager.readRecord = async () => forked ? service : null
  40. manager.verify = async () => service
  41. manager.legacyService = async () => null
  42. try {
  43. await manager.start()
  44. assert.equal(forked, true)
  45. assert.equal(manager.state.phase, 'ready')
  46. assert.equal(hasDesktopActivation(home), false)
  47. } finally { await manager.stop(); await rm(home, { recursive: true, force: true }) }
  48. })