serviceCli.test.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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('keeps the managed installation on LAN even with stale loopback environment', () => {
  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>0.0.0.0</string>')
  28. expect(plist).toContain('<key>HERMES_YAOYAO_ALLOW_INSECURE_LAN</key>\n <string>1</string>')
  29. })
  30. it('preserves APNs provider settings in the LaunchAgent', () => {
  31. const plist = launchAgentPlist({
  32. environment: {
  33. HERMES_YAOYAO_APNS_KEY_FILE: '/Users/test/AuthKey_ABC123.p8',
  34. HERMES_YAOYAO_APNS_KEY_ID: 'ABC123',
  35. HERMES_YAOYAO_APNS_TEAM_ID: 'TEAM123',
  36. HERMES_YAOYAO_APNS_TOPIC: 'cn.samien.yaoyao.hermes',
  37. },
  38. })
  39. expect(plist).toContain('<key>HERMES_YAOYAO_APNS_KEY_FILE</key>')
  40. expect(plist).toContain('/Users/test/AuthKey_ABC123.p8')
  41. expect(plist).toContain('<key>HERMES_YAOYAO_APNS_KEY_ID</key>')
  42. expect(plist).toContain('<key>HERMES_YAOYAO_APNS_TEAM_ID</key>')
  43. expect(plist).toContain('<key>HERMES_YAOYAO_APNS_TOPIC</key>')
  44. })
  45. it('can point the managed service at the stable current release link', () => {
  46. const root = join(homedir(), '.local', 'share', 'hermes-yaoyao', 'current')
  47. const plist = launchAgentPlist({ serviceRoot: root })
  48. expect(plist).toContain(`<string>${join(root, 'dist-server', 'server', 'index.js')}</string>`)
  49. expect(plist).toContain(`<key>WorkingDirectory</key><string>${root}</string>`)
  50. })
  51. it('runs the CLI when its entry path uses the stable current symlink', () => {
  52. const root = mkdtempSync(join(tmpdir(), 'hermes-service-cli-'))
  53. const entry = join(root, 'hermes-yaoyao.mjs')
  54. symlinkSync(join(process.cwd(), 'bin', 'hermes-yaoyao.mjs'), entry)
  55. try {
  56. expect(isMainModule(entry)).toBe(true)
  57. } finally {
  58. rmSync(root, { recursive: true, force: true })
  59. }
  60. })
  61. it('retries the transient launchctl bootstrap race after bootout', async () => {
  62. const error = Object.assign(new Error('launchctl bootstrap failed'), {
  63. stderr: 'Bootstrap failed: 5: Input/output error',
  64. })
  65. const run = vi.fn()
  66. .mockImplementationOnce(() => { throw error })
  67. .mockReturnValue('')
  68. const wait = vi.fn(async () => {})
  69. await expect(bootstrapLaunchAgent(run, wait)).resolves.toBe('')
  70. expect(run).toHaveBeenCalledTimes(2)
  71. expect(wait).toHaveBeenCalledWith(250)
  72. })
  73. it('keeps retrying until a slow launchd removal finishes', async () => {
  74. const error = Object.assign(new Error('launchctl bootstrap failed'), {
  75. stderr: 'Bootstrap failed: 5: Input/output error',
  76. })
  77. let elapsedMs = 0
  78. const run = vi.fn(() => {
  79. if (elapsedMs < 5_000) throw error
  80. return ''
  81. })
  82. const wait = vi.fn(async (waitMs: number) => { elapsedMs += waitMs })
  83. await expect(bootstrapLaunchAgent(run, wait, {
  84. timeoutMs: 20_000,
  85. now: () => elapsedMs,
  86. })).resolves.toBe('')
  87. expect(elapsedMs).toBeGreaterThanOrEqual(5_000)
  88. expect(run).toHaveBeenCalledTimes(6)
  89. expect(wait.mock.calls.map(([waitMs]) => waitMs)).toEqual([250, 500, 1_000, 2_000, 2_000])
  90. })
  91. it('stops retrying a transient launchctl error at the deadline', async () => {
  92. const error = Object.assign(new Error('launchctl bootstrap failed'), {
  93. stderr: 'Bootstrap failed: 5: Input/output error',
  94. })
  95. let elapsedMs = 0
  96. const run = vi.fn(() => { throw error })
  97. const wait = vi.fn(async (waitMs: number) => { elapsedMs += waitMs })
  98. await expect(bootstrapLaunchAgent(run, wait, {
  99. timeoutMs: 2_000,
  100. now: () => elapsedMs,
  101. })).rejects.toThrow(error)
  102. expect(elapsedMs).toBe(2_000)
  103. expect(run).toHaveBeenCalledTimes(5)
  104. })
  105. it('does not retry permanent launchctl bootstrap failures', async () => {
  106. const error = new Error('Bootstrap failed: 125: Domain does not support specified action')
  107. const run = vi.fn(() => { throw error })
  108. const wait = vi.fn(async () => {})
  109. await expect(bootstrapLaunchAgent(run, wait)).rejects.toThrow(error)
  110. expect(run).toHaveBeenCalledTimes(1)
  111. expect(wait).not.toHaveBeenCalled()
  112. })
  113. })