updater.test.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 } from 'vitest'
  5. import { currentTarget, formatCommandFailure, switchCurrent, validateManifest } from '../bin/hermes-yaoyao-updater.mjs'
  6. const roots: string[] = []
  7. afterEach(() => {
  8. for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true })
  9. })
  10. describe('standalone updater primitives', () => {
  11. it('atomically switches the stable current symlink', () => {
  12. const root = mkdtempSync(join(tmpdir(), 'hermes-updater-'))
  13. roots.push(root)
  14. const first = join(root, 'releases', '0.2.0-a')
  15. const second = join(root, 'releases', '0.3.0-b')
  16. switchCurrent(root, first, 'first')
  17. expect(currentTarget(root)).toBe(first)
  18. switchCurrent(root, second, 'second')
  19. expect(currentTarget(root)).toBe(second)
  20. expect(lstatSync(join(root, 'current')).isSymbolicLink()).toBe(true)
  21. })
  22. it('rejects a mismatched release manifest before any service transition', () => {
  23. expect(() => validateManifest({
  24. schemaVersion: 1,
  25. releaseVersion: '0.3.0',
  26. webVersion: '0.3.1',
  27. pluginVersion: '1.8.0',
  28. gitTag: 'v0.3.0',
  29. })).toThrow('版本组合不一致')
  30. })
  31. it('keeps useful command output while redacting credentials', () => {
  32. const error = Object.assign(new Error('Command failed'), {
  33. status: 127,
  34. stdout: '\u001B[31m发布版本校验通过\u001B[0m',
  35. stderr: 'https://secret@example.test/release.git\nsh: vue-tsc: command not found',
  36. })
  37. const message = formatCommandFailure('npm', ['run', 'build'], error)
  38. expect(message).toContain('npm run build 失败,退出码 127')
  39. expect(message).toContain('发布版本校验通过')
  40. expect(message).toContain('https://***@example.test/release.git')
  41. expect(message).toContain('vue-tsc: command not found')
  42. expect(message).not.toContain('secret')
  43. expect(message).not.toContain('\u001B')
  44. })
  45. })