updater.test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { lstatSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } 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 { backupPlugin, currentTarget, formatCommandFailure, restorePlugin, 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. it('backs up and restores plugin code without touching durable plugin data', () => {
  46. const root = mkdtempSync(join(tmpdir(), 'hermes-updater-plugin-'))
  47. roots.push(root)
  48. const hermesHome = join(root, 'hermes')
  49. const dataHome = join(root, 'web-data')
  50. const dashboard = join(hermesHome, 'plugins', 'yaoyao', 'dashboard')
  51. const durable = join(hermesHome, 'plugin-data', 'yaoyao')
  52. mkdirSync(dashboard, { recursive: true })
  53. mkdirSync(durable, { recursive: true })
  54. writeFileSync(join(dashboard, 'manifest.json'), JSON.stringify({ version: '1.7.1' }))
  55. writeFileSync(join(dashboard, 'plugin.py'), 'old plugin')
  56. writeFileSync(join(hermesHome, 'plugins', 'yaoyao', 'plugin.yaml'), 'version: "1.7.1"\n')
  57. writeFileSync(join(durable, 'archive.db'), 'durable data')
  58. const backup = backupPlugin({ hermesHome, dataHome }, '11111111-1111-4111-8111-111111111111')
  59. writeFileSync(join(dashboard, 'plugin.py'), 'new plugin')
  60. restorePlugin(backup.pluginRoot, backup.backupRoot, 'restore', true)
  61. expect(readFileSync(join(dashboard, 'plugin.py'), 'utf8')).toBe('old plugin')
  62. expect(readFileSync(join(durable, 'archive.db'), 'utf8')).toBe('durable data')
  63. })
  64. })