serviceCli.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 to the trusted LAN and enables local Dashboard supervision 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('preserves FCM provider settings in the LaunchAgent', () => {
  46. const plist = launchAgentPlist({
  47. environment: {
  48. HERMES_YAOYAO_FCM_SERVICE_ACCOUNT_FILE: '/Users/test/firebase-service-account.json',
  49. HERMES_YAOYAO_FCM_PROJECT_ID: 'yaoyao-test-project',
  50. HERMES_YAOYAO_FCM_PACKAGE_NAME: 'cn.samien.yaoyao.hermes',
  51. },
  52. })
  53. expect(plist).toContain('<key>HERMES_YAOYAO_FCM_SERVICE_ACCOUNT_FILE</key>')
  54. expect(plist).toContain('/Users/test/firebase-service-account.json')
  55. expect(plist).toContain('<key>HERMES_YAOYAO_FCM_PROJECT_ID</key>')
  56. expect(plist).toContain('yaoyao-test-project')
  57. expect(plist).toContain('<key>HERMES_YAOYAO_FCM_PACKAGE_NAME</key>')
  58. expect(plist).toContain('cn.samien.yaoyao.hermes')
  59. })
  60. it('can point the managed service at the stable current release link', () => {
  61. const root = join(homedir(), '.local', 'share', 'hermes-yaoyao', 'current')
  62. const plist = launchAgentPlist({ serviceRoot: root })
  63. expect(plist).toContain(`<string>${join(root, 'dist-server', 'server', 'index.js')}</string>`)
  64. expect(plist).toContain(`<key>WorkingDirectory</key><string>${root}</string>`)
  65. })
  66. it('runs the CLI when its entry path uses the stable current symlink', () => {
  67. const root = mkdtempSync(join(tmpdir(), 'hermes-service-cli-'))
  68. const entry = join(root, 'hermes-yaoyao.mjs')
  69. symlinkSync(join(process.cwd(), 'bin', 'hermes-yaoyao.mjs'), entry)
  70. try {
  71. expect(isMainModule(entry)).toBe(true)
  72. } finally {
  73. rmSync(root, { recursive: true, force: true })
  74. }
  75. })
  76. it('retries the transient launchctl bootstrap race after bootout', async () => {
  77. const error = Object.assign(new Error('launchctl bootstrap failed'), {
  78. stderr: 'Bootstrap failed: 5: Input/output error',
  79. })
  80. const run = vi.fn()
  81. .mockImplementationOnce(() => { throw error })
  82. .mockReturnValue('')
  83. const wait = vi.fn(async () => {})
  84. await expect(bootstrapLaunchAgent(run, wait)).resolves.toBe('')
  85. expect(run).toHaveBeenCalledTimes(2)
  86. expect(wait).toHaveBeenCalledWith(250)
  87. })
  88. it('keeps retrying until a slow launchd removal finishes', async () => {
  89. const error = Object.assign(new Error('launchctl bootstrap failed'), {
  90. stderr: 'Bootstrap failed: 5: Input/output error',
  91. })
  92. let elapsedMs = 0
  93. const run = vi.fn(() => {
  94. if (elapsedMs < 5_000) throw error
  95. return ''
  96. })
  97. const wait = vi.fn(async (waitMs: number) => { elapsedMs += waitMs })
  98. await expect(bootstrapLaunchAgent(run, wait, {
  99. timeoutMs: 20_000,
  100. now: () => elapsedMs,
  101. })).resolves.toBe('')
  102. expect(elapsedMs).toBeGreaterThanOrEqual(5_000)
  103. expect(run).toHaveBeenCalledTimes(6)
  104. expect(wait.mock.calls.map(([waitMs]) => waitMs)).toEqual([250, 500, 1_000, 2_000, 2_000])
  105. })
  106. it('stops retrying a transient launchctl error at the deadline', async () => {
  107. const error = Object.assign(new Error('launchctl bootstrap failed'), {
  108. stderr: 'Bootstrap failed: 5: Input/output error',
  109. })
  110. let elapsedMs = 0
  111. const run = vi.fn(() => { throw error })
  112. const wait = vi.fn(async (waitMs: number) => { elapsedMs += waitMs })
  113. await expect(bootstrapLaunchAgent(run, wait, {
  114. timeoutMs: 2_000,
  115. now: () => elapsedMs,
  116. })).rejects.toThrow(error)
  117. expect(elapsedMs).toBe(2_000)
  118. expect(run).toHaveBeenCalledTimes(5)
  119. })
  120. it('does not retry permanent launchctl bootstrap failures', async () => {
  121. const error = new Error('Bootstrap failed: 125: Domain does not support specified action')
  122. const run = vi.fn(() => { throw error })
  123. const wait = vi.fn(async () => {})
  124. await expect(bootstrapLaunchAgent(run, wait)).rejects.toThrow(error)
  125. expect(run).toHaveBeenCalledTimes(1)
  126. expect(wait).not.toHaveBeenCalled()
  127. })
  128. })