serviceCli.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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('binds managed Web and Dashboard services to the trusted LAN by default', () => {
  15. const plist = launchAgentPlist({ environment: {} })
  16. expect(plist).toContain('<key>HERMES_YAOYAO_HOST</key>\n <string>0.0.0.0</string>')
  17. expect(plist).toContain('<key>HERMES_YAOYAO_ALLOW_INSECURE_LAN</key>\n <string>1</string>')
  18. expect(plist).toContain('<key>HERMES_YAOYAO_SUPERVISE_DASHBOARD</key>\n <string>1</string>')
  19. })
  20. it('allows an explicit loopback-only managed installation', () => {
  21. const plist = launchAgentPlist({
  22. environment: {
  23. HERMES_YAOYAO_HOST: '127.0.0.1',
  24. HERMES_YAOYAO_ALLOW_INSECURE_LAN: '0',
  25. },
  26. })
  27. expect(plist).toContain('<key>HERMES_YAOYAO_HOST</key>\n <string>127.0.0.1</string>')
  28. expect(plist).toContain('<key>HERMES_YAOYAO_ALLOW_INSECURE_LAN</key>\n <string>0</string>')
  29. })
  30. it('can point the managed service at the stable current release link', () => {
  31. const root = join(homedir(), '.local', 'share', 'hermes-yaoyao', 'current')
  32. const plist = launchAgentPlist({ serviceRoot: root })
  33. expect(plist).toContain(`<string>${join(root, 'dist-server', 'server', 'index.js')}</string>`)
  34. expect(plist).toContain(`<key>WorkingDirectory</key><string>${root}</string>`)
  35. })
  36. it('runs the CLI when its entry path uses the stable current symlink', () => {
  37. const root = mkdtempSync(join(tmpdir(), 'hermes-service-cli-'))
  38. const entry = join(root, 'hermes-yaoyao.mjs')
  39. symlinkSync(join(process.cwd(), 'bin', 'hermes-yaoyao.mjs'), entry)
  40. try {
  41. expect(isMainModule(entry)).toBe(true)
  42. } finally {
  43. rmSync(root, { recursive: true, force: true })
  44. }
  45. })
  46. it('retries the transient launchctl bootstrap race after bootout', async () => {
  47. const error = Object.assign(new Error('launchctl bootstrap failed'), {
  48. stderr: 'Bootstrap failed: 5: Input/output error',
  49. })
  50. const run = vi.fn()
  51. .mockImplementationOnce(() => { throw error })
  52. .mockReturnValue('')
  53. const wait = vi.fn(async () => {})
  54. await expect(bootstrapLaunchAgent(run, wait)).resolves.toBe('')
  55. expect(run).toHaveBeenCalledTimes(2)
  56. expect(wait).toHaveBeenCalledWith(250)
  57. })
  58. it('keeps retrying until a slow launchd removal finishes', async () => {
  59. const error = Object.assign(new Error('launchctl bootstrap failed'), {
  60. stderr: 'Bootstrap failed: 5: Input/output error',
  61. })
  62. let elapsedMs = 0
  63. const run = vi.fn(() => {
  64. if (elapsedMs < 5_000) throw error
  65. return ''
  66. })
  67. const wait = vi.fn(async (waitMs: number) => { elapsedMs += waitMs })
  68. await expect(bootstrapLaunchAgent(run, wait, {
  69. timeoutMs: 20_000,
  70. now: () => elapsedMs,
  71. })).resolves.toBe('')
  72. expect(elapsedMs).toBeGreaterThanOrEqual(5_000)
  73. expect(run).toHaveBeenCalledTimes(6)
  74. expect(wait.mock.calls.map(([waitMs]) => waitMs)).toEqual([250, 500, 1_000, 2_000, 2_000])
  75. })
  76. it('stops retrying a transient launchctl error at the deadline', async () => {
  77. const error = Object.assign(new Error('launchctl bootstrap failed'), {
  78. stderr: 'Bootstrap failed: 5: Input/output error',
  79. })
  80. let elapsedMs = 0
  81. const run = vi.fn(() => { throw error })
  82. const wait = vi.fn(async (waitMs: number) => { elapsedMs += waitMs })
  83. await expect(bootstrapLaunchAgent(run, wait, {
  84. timeoutMs: 2_000,
  85. now: () => elapsedMs,
  86. })).rejects.toThrow(error)
  87. expect(elapsedMs).toBe(2_000)
  88. expect(run).toHaveBeenCalledTimes(5)
  89. })
  90. it('does not retry permanent launchctl bootstrap failures', async () => {
  91. const error = new Error('Bootstrap failed: 125: Domain does not support specified action')
  92. const run = vi.fn(() => { throw error })
  93. const wait = vi.fn(async () => {})
  94. await expect(bootstrapLaunchAgent(run, wait)).rejects.toThrow(error)
  95. expect(run).toHaveBeenCalledTimes(1)
  96. expect(wait).not.toHaveBeenCalled()
  97. })
  98. })