updater.test.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { lstatSync, mkdtempSync, rmSync } from 'node:fs'
  2. import { tmpdir } from 'node:os'
  3. import { join } from 'node:path'
  4. import { afterEach, describe, expect, it, vi } from 'vitest'
  5. import { launchAgentPlist } from '../bin/hermes-yaoyao.mjs'
  6. import {
  7. currentTarget,
  8. formatCommandFailure,
  9. restorePreviousService,
  10. serviceInstallInvocation,
  11. switchCurrent,
  12. validateManifest,
  13. verifyRuntime,
  14. } from '../bin/hermes-yaoyao-updater.mjs'
  15. const roots: string[] = []
  16. afterEach(() => {
  17. for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true })
  18. })
  19. describe('standalone updater primitives', () => {
  20. it('accepts a healthy Web without probing 9119 or upstream-dependent readiness', async () => {
  21. const fetchImpl = vi.fn(async (url: string) => {
  22. if (url !== 'http://127.0.0.1:8800/healthz') throw new Error('9119 offline')
  23. return Response.json({ ok: true })
  24. })
  25. await verifyRuntime(1_000, fetchImpl)
  26. expect(fetchImpl).toHaveBeenCalledTimes(1)
  27. expect(fetchImpl.mock.calls[0]?.[0]).toBe('http://127.0.0.1:8800/healthz')
  28. })
  29. it('still fails an unhealthy Web and retries transient startup failures', async () => {
  30. vi.useFakeTimers()
  31. try {
  32. const unhealthy = vi.fn(async () => Response.json({ ok: false }))
  33. const check = expect(verifyRuntime(1_000, unhealthy)).rejects.toThrow('升级后健康检查失败')
  34. await vi.advanceTimersByTimeAsync(1_000)
  35. await check
  36. const recovering = vi.fn().mockRejectedValueOnce(new Error('starting'))
  37. .mockResolvedValueOnce(Response.json({ ok: true }))
  38. const recovered = verifyRuntime(1_000, recovering)
  39. await vi.advanceTimersByTimeAsync(500)
  40. await recovered
  41. expect(recovering).toHaveBeenCalledTimes(2)
  42. } finally { vi.useRealTimers() }
  43. })
  44. it('atomically switches the stable current symlink', () => {
  45. const root = mkdtempSync(join(tmpdir(), 'hermes-updater-'))
  46. roots.push(root)
  47. const first = join(root, 'releases', '0.2.0-a')
  48. const second = join(root, 'releases', '0.3.0-b')
  49. switchCurrent(root, first, 'first')
  50. expect(currentTarget(root)).toBe(first)
  51. switchCurrent(root, second, 'second')
  52. expect(currentTarget(root)).toBe(second)
  53. expect(lstatSync(join(root, 'current')).isSymbolicLink()).toBe(true)
  54. })
  55. it('rejects a mismatched release manifest before any service transition', () => {
  56. expect(() => validateManifest({
  57. schemaVersion: 1,
  58. releaseVersion: '0.3.0',
  59. webVersion: '0.3.1',
  60. pluginVersion: '1.8.0',
  61. gitTag: 'v0.3.0',
  62. })).toThrow('版本组合不一致')
  63. })
  64. it('keeps useful command output while redacting credentials', () => {
  65. const error = Object.assign(new Error('Command failed'), {
  66. status: 127,
  67. stdout: '\u001B[31m发布版本校验通过\u001B[0m',
  68. stderr: 'https://secret@example.test/release.git\nsh: vue-tsc: command not found',
  69. })
  70. const message = formatCommandFailure('npm', ['run', 'build'], error)
  71. expect(message).toContain('npm run build 失败,退出码 127')
  72. expect(message).toContain('发布版本校验通过')
  73. expect(message).toContain('https://***@example.test/release.git')
  74. expect(message).toContain('vue-tsc: command not found')
  75. expect(message).not.toContain('secret')
  76. expect(message).not.toContain('\u001B')
  77. })
  78. it('uses the new lifecycle code while starting a rolled-back release', () => {
  79. const releaseRoot = '/Users/test/.local/share/hermes-yaoyao'
  80. const oldRelease = join(releaseRoot, 'releases', '0.2.11-old')
  81. const newRelease = join(releaseRoot, 'releases', '0.2.12-new')
  82. const invocation = serviceInstallInvocation(
  83. oldRelease,
  84. { releaseRoot, source: 'https://example.test/hermes-yaoyao.git' },
  85. true,
  86. newRelease,
  87. )
  88. expect(invocation.cli).toBe(join(newRelease, 'bin', 'hermes-yaoyao.mjs'))
  89. expect(invocation.env.HERMES_YAOYAO_SERVICE_ROOT).toBe(join(releaseRoot, 'current'))
  90. expect(invocation.env.HERMES_YAOYAO_RELEASE_SOURCE).toBe('https://example.test/hermes-yaoyao.git')
  91. expect(invocation.env.HERMES_YAOYAO_HOST).toBe('0.0.0.0')
  92. expect(invocation.env.HERMES_YAOYAO_ALLOW_INSECURE_LAN).toBe('1')
  93. const sourceRollback = serviceInstallInvocation(
  94. oldRelease,
  95. { releaseRoot, source: 'https://example.test/hermes-yaoyao.git' },
  96. false,
  97. newRelease,
  98. )
  99. expect(sourceRollback.cli).toBe(join(newRelease, 'bin', 'hermes-yaoyao.mjs'))
  100. expect(sourceRollback.env.HERMES_YAOYAO_SERVICE_ROOT).toBe(oldRelease)
  101. const rollbackPlist = launchAgentPlist({ serviceRoot: sourceRollback.env.HERMES_YAOYAO_SERVICE_ROOT })
  102. expect(rollbackPlist).toContain(`<string>${join(oldRelease, 'dist-server', 'server', 'index.js')}</string>`)
  103. expect(rollbackPlist).toContain(`<key>WorkingDirectory</key><string>${oldRelease}</string>`)
  104. })
  105. it('routes release and source rollbacks through the new lifecycle root', () => {
  106. const plan = {
  107. releaseRoot: '/Users/test/.local/share/hermes-yaoyao',
  108. source: 'https://example.test/hermes-yaoyao.git',
  109. }
  110. const lifecycleRoot = join(plan.releaseRoot, 'releases', '0.2.12-new')
  111. const previousRelease = join(plan.releaseRoot, 'releases', '0.2.11-old')
  112. const previousSource = '/Users/test/git/hermes-yaoyao'
  113. const switchCurrentCommand = vi.fn()
  114. const removeCurrentCommand = vi.fn()
  115. const startServiceCommand = vi.fn()
  116. restorePreviousService({
  117. plan,
  118. previousCurrentTarget: previousRelease,
  119. previousServiceRoot: previousSource,
  120. lifecycleRoot,
  121. token: 'release-rollback',
  122. switchCurrentCommand,
  123. removeCurrentCommand,
  124. startServiceCommand,
  125. })
  126. expect(switchCurrentCommand).toHaveBeenCalledWith(plan.releaseRoot, previousRelease, 'release-rollback')
  127. expect(removeCurrentCommand).not.toHaveBeenCalled()
  128. expect(startServiceCommand).toHaveBeenCalledWith(
  129. join(plan.releaseRoot, 'current'), plan, true, lifecycleRoot,
  130. )
  131. switchCurrentCommand.mockClear()
  132. startServiceCommand.mockClear()
  133. restorePreviousService({
  134. plan,
  135. previousCurrentTarget: undefined,
  136. previousServiceRoot: previousSource,
  137. lifecycleRoot,
  138. token: 'source-rollback',
  139. switchCurrentCommand,
  140. removeCurrentCommand,
  141. startServiceCommand,
  142. })
  143. expect(switchCurrentCommand).not.toHaveBeenCalled()
  144. expect(removeCurrentCommand).toHaveBeenCalledWith(plan.releaseRoot)
  145. expect(startServiceCommand).toHaveBeenCalledWith(previousSource, plan, false, lifecycleRoot)
  146. })
  147. it('refuses rollback without a usable lifecycle root', () => {
  148. expect(() => restorePreviousService({
  149. plan: { releaseRoot: '/tmp/releases', source: 'https://example.test/repo.git' },
  150. previousCurrentTarget: '/tmp/releases/old',
  151. previousServiceRoot: '/tmp/source',
  152. lifecycleRoot: undefined,
  153. token: 'missing-lifecycle',
  154. })).toThrow('当前发布版本不可用')
  155. })
  156. })