updater.test.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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, 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('backs up and restores plugin code without touching durable plugin data', () => {
  32. const root = mkdtempSync(join(tmpdir(), 'hermes-updater-plugin-'))
  33. roots.push(root)
  34. const hermesHome = join(root, 'hermes')
  35. const dataHome = join(root, 'web-data')
  36. const dashboard = join(hermesHome, 'plugins', 'yaoyao', 'dashboard')
  37. const durable = join(hermesHome, 'plugin-data', 'yaoyao')
  38. mkdirSync(dashboard, { recursive: true })
  39. mkdirSync(durable, { recursive: true })
  40. writeFileSync(join(dashboard, 'manifest.json'), JSON.stringify({ version: '1.7.1' }))
  41. writeFileSync(join(dashboard, 'plugin.py'), 'old plugin')
  42. writeFileSync(join(hermesHome, 'plugins', 'yaoyao', 'plugin.yaml'), 'version: "1.7.1"\n')
  43. writeFileSync(join(durable, 'archive.db'), 'durable data')
  44. const backup = backupPlugin({ hermesHome, dataHome }, '11111111-1111-4111-8111-111111111111')
  45. writeFileSync(join(dashboard, 'plugin.py'), 'new plugin')
  46. restorePlugin(backup.pluginRoot, backup.backupRoot, 'restore', true)
  47. expect(readFileSync(join(dashboard, 'plugin.py'), 'utf8')).toBe('old plugin')
  48. expect(readFileSync(join(durable, 'archive.db'), 'utf8')).toBe('durable data')
  49. })
  50. })