serviceCli.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { mkdtempSync, rmSync, symlinkSync } from 'node:fs'
  2. import { homedir, tmpdir } from 'node:os'
  3. import { join } from 'node:path'
  4. import { describe, expect, it, vi } from 'vitest'
  5. import { bootstrapLaunchAgent, isMainModule, launchAgentPlist } from '../bin/hermes-yaoyao.mjs'
  6. describe('hermes-yaoyao LaunchAgent plist', () => {
  7. it('includes the local Hermes CLI directory in PATH', () => {
  8. const plist = launchAgentPlist()
  9. expect(plist).toContain(`${join(homedir(), '.local', 'bin')}:`)
  10. })
  11. it('enables Dashboard supervision by default', () => {
  12. expect(launchAgentPlist()).toContain('<key>HERMES_YAOYAO_SUPERVISE_DASHBOARD</key>')
  13. })
  14. it('can point the managed service at the stable current release link', () => {
  15. const root = join(homedir(), '.local', 'share', 'hermes-yaoyao', 'current')
  16. const plist = launchAgentPlist({ serviceRoot: root })
  17. expect(plist).toContain(`<string>${join(root, 'dist-server', 'server', 'index.js')}</string>`)
  18. expect(plist).toContain(`<key>WorkingDirectory</key><string>${root}</string>`)
  19. })
  20. it('runs the CLI when its entry path uses the stable current symlink', () => {
  21. const root = mkdtempSync(join(tmpdir(), 'hermes-service-cli-'))
  22. const entry = join(root, 'hermes-yaoyao.mjs')
  23. symlinkSync(join(process.cwd(), 'bin', 'hermes-yaoyao.mjs'), entry)
  24. try {
  25. expect(isMainModule(entry)).toBe(true)
  26. } finally {
  27. rmSync(root, { recursive: true, force: true })
  28. }
  29. })
  30. it('retries the transient launchctl bootstrap race after bootout', async () => {
  31. const error = Object.assign(new Error('launchctl bootstrap failed'), {
  32. stderr: 'Bootstrap failed: 5: Input/output error',
  33. })
  34. const run = vi.fn()
  35. .mockImplementationOnce(() => { throw error })
  36. .mockReturnValue('')
  37. const wait = vi.fn(async () => {})
  38. await expect(bootstrapLaunchAgent(run, wait)).resolves.toBe('')
  39. expect(run).toHaveBeenCalledTimes(2)
  40. expect(wait).toHaveBeenCalledWith(250)
  41. })
  42. it('keeps retrying until a slow launchd removal finishes', async () => {
  43. const error = Object.assign(new Error('launchctl bootstrap failed'), {
  44. stderr: 'Bootstrap failed: 5: Input/output error',
  45. })
  46. let elapsedMs = 0
  47. const run = vi.fn(() => {
  48. if (elapsedMs < 5_000) throw error
  49. return ''
  50. })
  51. const wait = vi.fn(async (waitMs: number) => { elapsedMs += waitMs })
  52. await expect(bootstrapLaunchAgent(run, wait, {
  53. timeoutMs: 20_000,
  54. now: () => elapsedMs,
  55. })).resolves.toBe('')
  56. expect(elapsedMs).toBeGreaterThanOrEqual(5_000)
  57. expect(run).toHaveBeenCalledTimes(6)
  58. expect(wait.mock.calls.map(([waitMs]) => waitMs)).toEqual([250, 500, 1_000, 2_000, 2_000])
  59. })
  60. it('stops retrying a transient launchctl error at the deadline', async () => {
  61. const error = Object.assign(new Error('launchctl bootstrap failed'), {
  62. stderr: 'Bootstrap failed: 5: Input/output error',
  63. })
  64. let elapsedMs = 0
  65. const run = vi.fn(() => { throw error })
  66. const wait = vi.fn(async (waitMs: number) => { elapsedMs += waitMs })
  67. await expect(bootstrapLaunchAgent(run, wait, {
  68. timeoutMs: 2_000,
  69. now: () => elapsedMs,
  70. })).rejects.toThrow(error)
  71. expect(elapsedMs).toBe(2_000)
  72. expect(run).toHaveBeenCalledTimes(5)
  73. })
  74. it('does not retry permanent launchctl bootstrap failures', async () => {
  75. const error = new Error('Bootstrap failed: 125: Domain does not support specified action')
  76. const run = vi.fn(() => { throw error })
  77. const wait = vi.fn(async () => {})
  78. await expect(bootstrapLaunchAgent(run, wait)).rejects.toThrow(error)
  79. expect(run).toHaveBeenCalledTimes(1)
  80. expect(wait).not.toHaveBeenCalled()
  81. })
  82. })