desktopActivation.test.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // @vitest-environment node
  2. import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
  3. import { existsSync, mkdtempSync, rmSync, statSync, writeFileSync } from 'node:fs'
  4. import { tmpdir } from 'node:os'
  5. import { join } from 'node:path'
  6. import { DesktopActivation } from '../../src/server/desktopActivation'
  7. import { deferDesktopActivation, desktopActivationPath } from '../../bin/lib/desktop-activation.mjs'
  8. let home: string
  9. beforeEach(() => { home = mkdtempSync(join(tmpdir(), 'yaoyao-runtime-activation-')) })
  10. afterEach(() => { rmSync(home, { recursive: true, force: true }) })
  11. describe('desktop environment preparation', () => {
  12. it('keeps Hermes stopped until entering local mode and starts it once', () => {
  13. const launch = vi.fn(), activation = new DesktopActivation(home, true, launch)
  14. activation.start(); activation.start()
  15. expect(activation.required).toBe(true)
  16. expect(launch).not.toHaveBeenCalled()
  17. expect(existsSync(desktopActivationPath(home))).toBe(false)
  18. activation.activate(); activation.activate(); activation.start()
  19. expect(activation.required).toBe(false)
  20. expect(launch).toHaveBeenCalledTimes(1)
  21. if (process.platform !== 'win32') expect(statSync(desktopActivationPath(home)).mode & 0o777).toBe(0o600)
  22. })
  23. it('resumes an authorized independent background process after restart', () => {
  24. const launch = vi.fn(), previous = new DesktopActivation(home, true, launch)
  25. previous.activate()
  26. const replacement = new DesktopActivation(home, true, launch)
  27. replacement.start()
  28. expect(replacement.required).toBe(false)
  29. expect(launch).toHaveBeenCalledTimes(2)
  30. })
  31. it('requires a fresh entry when a later environment check starts the service', () => {
  32. const launch = vi.fn(), previous = new DesktopActivation(home, true, launch)
  33. previous.activate()
  34. deferDesktopActivation(home)
  35. const replacement = new DesktopActivation(home, true, launch)
  36. replacement.start()
  37. expect(replacement.required).toBe(true)
  38. expect(launch).toHaveBeenCalledTimes(1)
  39. replacement.activate()
  40. expect(launch).toHaveBeenCalledTimes(2)
  41. })
  42. it('preserves explicitly configured independent service supervision', () => {
  43. const launch = vi.fn(), activation = new DesktopActivation(home, false, launch)
  44. activation.start(); activation.activate()
  45. expect(activation.required).toBe(false)
  46. expect(launch).toHaveBeenCalledTimes(1)
  47. })
  48. it('does not trust a malformed or unsupported saved activation', () => {
  49. for (const value of ['broken', '{}', '{"version":2}']) {
  50. writeFileSync(desktopActivationPath(home), value)
  51. const launch = vi.fn(), activation = new DesktopActivation(home, true, launch)
  52. activation.start()
  53. expect(activation.required).toBe(true)
  54. expect(launch).not.toHaveBeenCalled()
  55. }
  56. })
  57. })