systemUpdateRoutes.test.ts 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
  2. import { tmpdir } from 'node:os'
  3. import { join } from 'node:path'
  4. import request from 'supertest'
  5. import { afterEach, describe, expect, it } from 'vitest'
  6. import { createApplication, type ApplicationRuntime } from '../../src/server/app.js'
  7. import type { ServerConfig } from '../../src/server/config.js'
  8. import { SystemUpdateManager } from '../../src/server/updateManager.js'
  9. const runtimes: ApplicationRuntime[] = []
  10. const roots: string[] = []
  11. afterEach(() => {
  12. for (const runtime of runtimes.splice(0)) runtime.close()
  13. for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true })
  14. })
  15. function cookieHeader(response: request.Response): string {
  16. const values = response.headers['set-cookie'] as unknown as string[]
  17. return values.map(value => value.split(';', 1)[0]).join('; ')
  18. }
  19. function json(value: unknown, init: ResponseInit = {}) {
  20. return new Response(JSON.stringify(value), {
  21. ...init,
  22. headers: { 'content-type': 'application/json', ...init.headers },
  23. })
  24. }
  25. describe('system update routes', () => {
  26. it('checks and queues a fixed Web/plugin release through authenticated CSRF routes', async () => {
  27. const root = mkdtempSync(join(tmpdir(), 'hermes-system-update-routes-'))
  28. roots.push(root)
  29. const projectRoot = join(root, 'project')
  30. mkdirSync(projectRoot)
  31. writeFileSync(join(projectRoot, 'release.json'), JSON.stringify({
  32. schemaVersion: 1, releaseVersion: '0.2.0', webVersion: '0.2.0', pluginVersion: '1.7.1', gitTag: 'v0.2.0',
  33. }))
  34. const config: ServerConfig = {
  35. host: '127.0.0.1', port: 8800, upstream: new URL('http://127.0.0.1:9119'),
  36. allowedHosts: new Set(), home: join(root, 'data'), mediaRoot: join(root, 'media'),
  37. attachmentsRoot: join(root, 'attachments'), imagesRoot: join(root, 'images'), mediaOwner: 'tester',
  38. allowInsecureLan: false, insecureLan: false, production: false,
  39. releaseRoot: join(root, 'installed'), releaseSource: 'https://example.test/hermes-yaoyao.git',
  40. }
  41. const launched: string[] = []
  42. const latest = {
  43. schemaVersion: 1 as const, releaseVersion: '0.3.0', webVersion: '0.3.0', pluginVersion: '1.8.0', gitTag: 'v0.3.0',
  44. }
  45. const updates = new SystemUpdateManager(config, {
  46. projectRoot,
  47. inspectRemote: async () => ({ manifest: latest, commit: 'b'.repeat(40) }),
  48. launchUpdater: path => { launched.push(path) },
  49. platform: 'darwin',
  50. })
  51. const fetchImpl = (async (input: string | URL | Request) => {
  52. const path = new URL(input instanceof Request ? input.url : String(input)).pathname
  53. if (path === '/api/status') return json({ auth_required: true }, { headers: { 'set-cookie': 'hermes_session_at=session; Path=/; HttpOnly' } })
  54. if (path === '/api/auth/me') return json({ user_id: 'admin', display_name: '管理员' })
  55. if (path === '/api/profiles') return json({ profiles: [{ name: 'default', is_default: true }] })
  56. if (path === '/api/plugins/yaoyao/profiles') return json({ profiles: [] })
  57. if (path === '/api/dashboard/plugins') return json([{ name: 'yaoyao', version: '1.7.1' }])
  58. if (path === '/api/plugins/yaoyao/maintenance/storage') return json({ ready: true })
  59. return json({ ok: true })
  60. }) as typeof fetch
  61. const runtime = createApplication({ config, fetchImpl, updates })
  62. runtimes.push(runtime)
  63. const bootstrap = await request(runtime.app.callback())
  64. .get('/api/app/bootstrap').set('Host', '127.0.0.1:8800').expect(200)
  65. const cookie = cookieHeader(bootstrap)
  66. const mutation = (path: string) => request(runtime.app.callback())
  67. .post(path)
  68. .set('Host', '127.0.0.1:8800')
  69. .set('Origin', 'http://127.0.0.1:8800')
  70. .set('Cookie', cookie)
  71. .set('X-CSRF-Token', bootstrap.body.csrfToken)
  72. const status = await request(runtime.app.callback())
  73. .get('/api/app/system/update/status').set('Host', '127.0.0.1:8800').set('Cookie', cookie).expect(200)
  74. expect(status.body).toMatchObject({ installedPluginVersion: '1.7.1', versionsMatch: true, updateAvailable: false })
  75. const check = await mutation('/api/app/system/update/check').send({}).expect(200)
  76. expect(check.body).toMatchObject({ latest, updateAvailable: true })
  77. const queued = await mutation('/api/app/system/update/apply').send({ targetVersion: '0.3.0' }).expect(202)
  78. expect(queued.body).toMatchObject({ operation: 'update', state: 'queued', target: latest })
  79. expect(launched).toHaveLength(1)
  80. const jobResponse = await request(runtime.app.callback())
  81. .get(`/api/app/system/update/jobs/${queued.body.id}`)
  82. .set('Host', '127.0.0.1:8800').set('Cookie', cookie)
  83. .expect(200)
  84. expect(jobResponse.body).toMatchObject({ id: queued.body.id, state: 'queued' })
  85. })
  86. })