npmUpdates.test.ts 5.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // @vitest-environment node
  2. import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
  3. import { join } from 'node:path'
  4. import { tmpdir } from 'node:os'
  5. import { afterEach, expect, it, vi } from 'vitest'
  6. import { SystemUpdateManager } from '../../src/server/updateManager.js'
  7. import { loadServerConfig } from '../../src/server/config.js'
  8. import { NPM_RELEASE_SOURCE, npmRuntimeDigest, parseNpmRelease } from '../../bin/lib/npm-release.mjs'
  9. import { writeNpmFixture } from '../fixtures/npm-release.mjs'
  10. const roots: string[] = []
  11. afterEach(() => { for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }) })
  12. const npm = { version: '0.3.0', integrity: `sha512-${Buffer.alloc(64, 1).toString('base64')}`, tarball: 'https://registry.npmjs.org/fixture.tgz' }
  13. const target = { schemaVersion: 1 as const, releaseVersion: npm.version, webVersion: npm.version, gitTag: `v${npm.version}` }
  14. function fixture() {
  15. const root = mkdtempSync(join(tmpdir(), 'yaoyao-npm-manager-')); roots.push(root)
  16. const projectRoot = join(root, 'global/node_modules/@lsamien/yaoyao')
  17. writeNpmFixture(projectRoot, '0.2.0')
  18. const config = loadServerConfig({ HERMES_YAOYAO_HOME: join(root, 'data'), HERMES_YAOYAO_RELEASE_ROOT: join(root, 'programs') })
  19. const launchUpdater = vi.fn(), inspectRemote = vi.fn(async () => ({ npm, manifest: target }))
  20. const options = { projectRoot, platform: 'darwin' as const, launchUpdater, inspectRemote }
  21. const manager = new SystemUpdateManager(config, options)
  22. return { root, manager, config, options, launchUpdater, inspectRemote }
  23. }
  24. it('detects npm installs and queues an integrity-pinned download without activating', async () => {
  25. const f = fixture()
  26. expect(await f.manager.check()).toMatchObject({ installationMode: 'npm', updateMethod: 'npm', releaseSource: NPM_RELEASE_SOURCE, updateAvailable: true })
  27. const job = await f.manager.startUpdate(npm.version)
  28. const stored = JSON.parse(readFileSync(f.launchUpdater.mock.calls[0][0], 'utf8'))
  29. expect(stored.plan.npm).toEqual(npm)
  30. expect(stored.plan.prepared).toBeUndefined()
  31. expect(f.manager.job(job.id)).not.toHaveProperty('plan')
  32. expect(() => f.manager.startActivation(job.id)).toThrow('没有可生效')
  33. expect(f.launchUpdater).toHaveBeenCalledTimes(1)
  34. })
  35. it('restores a prepared update after manager recreation and activates offline only after an explicit request', async () => {
  36. const f = fixture(), job = await f.manager.startUpdate(npm.version)
  37. const path = f.launchUpdater.mock.calls[0][0], stored = JSON.parse(readFileSync(path, 'utf8'))
  38. const packageRoot = join(f.config.releaseRoot!, 'releases/new/node_modules/@lsamien/yaoyao')
  39. writeNpmFixture(packageRoot, npm.version)
  40. const prepared = { root: packageRoot, digest: npmRuntimeDigest(packageRoot, npm.version) }
  41. writeFileSync(path, JSON.stringify({ ...stored, state: 'prepared', plan: { ...stored.plan, prepared } }))
  42. rmSync(join(f.config.home, 'updates/active.lock'))
  43. f.inspectRemote.mockRejectedValue(new Error('npm offline'))
  44. const resumed = new SystemUpdateManager(f.config, f.options)
  45. expect(resumed.status().job?.state).toBe('prepared')
  46. expect(() => resumed.startActivation('wrong-job')).toThrow('没有可生效')
  47. const activate = resumed.startActivation(job.id)
  48. expect(activate.state).toBe('queued')
  49. const activationPlan = JSON.parse(readFileSync(f.launchUpdater.mock.calls[1][0], 'utf8')).plan
  50. expect(activationPlan.prepared).toEqual(prepared)
  51. expect(f.inspectRemote).toHaveBeenCalledTimes(1)
  52. expect(() => resumed.startActivation(job.id)).toThrow('没有可生效')
  53. })
  54. it('rejects changed prepared files and retains explicit Git sources for npm installs', async () => {
  55. const f = fixture(), job = await f.manager.startUpdate(npm.version)
  56. const path = f.launchUpdater.mock.calls[0][0], stored = JSON.parse(readFileSync(path, 'utf8'))
  57. const packageRoot = join(f.config.releaseRoot!, 'releases/new/node_modules/@lsamien/yaoyao')
  58. writeNpmFixture(packageRoot, npm.version)
  59. const prepared = { root: packageRoot, digest: npmRuntimeDigest(packageRoot, npm.version) }
  60. writeFileSync(path, JSON.stringify({ ...stored, state: 'prepared', plan: { ...stored.plan, prepared } }))
  61. rmSync(join(f.config.home, 'updates/active.lock'))
  62. writeFileSync(join(packageRoot, 'dist-server/server/index.js'), '// changed after verification')
  63. expect(() => f.manager.startActivation(job.id)).toThrow('文件已改变')
  64. expect(f.manager.status().job?.state).toBe('failed')
  65. expect(f.launchUpdater).toHaveBeenCalledTimes(1)
  66. const git = new SystemUpdateManager({ ...f.config, releaseSource: 'https://example.test/private.git' }, f.options)
  67. expect(git.status()).toMatchObject({ installationMode: 'source', updateMethod: 'git', releaseSource: 'https://example.test/private.git' })
  68. })
  69. it('validates npm metadata and restricts configured npm sources to the official package', () => {
  70. const value = { name: '@lsamien/yaoyao', version: npm.version, dist: npm }
  71. expect(parseNpmRelease(value)).toEqual(npm)
  72. expect(() => parseNpmRelease({ ...value, name: 'other-package' })).toThrow('名称或版本')
  73. expect(() => parseNpmRelease({ ...value, dist: { ...npm, integrity: 'sha1-weak' } })).toThrow('SHA-512')
  74. expect(loadServerConfig({ HERMES_YAOYAO_RELEASE_SOURCE: NPM_RELEASE_SOURCE }).releaseSource).toBe(NPM_RELEASE_SOURCE)
  75. expect(() => loadServerConfig({ HERMES_YAOYAO_RELEASE_SOURCE: 'npm:other-package' })).toThrow()
  76. })