embeddedHermesApi.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { existsSync, 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 { createEmbeddedHermes, type EmbeddedHermes } from '../../src/server/hermes/embedded.js'
  6. import { kernelPaths } from '../../src/server/hermes/paths.js'
  7. const kernel = existsSync(kernelPaths().python)
  8. const env = { ...process.env, YAOVI_HERMES_TEST_OFFLINE: '1' }
  9. describe.skipIf(!kernel)('embedded Hermes dashboard API', () => {
  10. let home = ''
  11. let hermes: EmbeddedHermes | undefined
  12. afterEach(() => { hermes?.close(); hermes = undefined; if (home) rmSync(home, { recursive: true, force: true }) })
  13. const call = async (method: string, path: string, query: Record<string, string> = {}, body?: unknown) => {
  14. const response = await hermes!.api.handle({ method, path, search: new URLSearchParams(query), body })
  15. return { status: response.status, body: JSON.parse(response.body.toString() || 'null') }
  16. }
  17. it('serves native dashboard shapes without any listening Hermes service', async () => {
  18. home = mkdtempSync(join(tmpdir(), 'yaoyao-hermes-api-'))
  19. hermes = createEmbeddedHermes(home, { env })
  20. expect((await call('GET', '/api/status')).body).toMatchObject({ auth_required: false, embedded: true })
  21. expect((await call('GET', '/api/sessions', { limit: '5' })).body).toMatchObject({ sessions: [], total: 0 })
  22. const profiles = await call('GET', '/api/profiles')
  23. expect(profiles.status).toBe(200)
  24. expect(profiles.body.profiles.map((p: { name: string }) => p.name)).toContain('default')
  25. const options = await call('GET', '/api/model/options')
  26. expect(options.status).toBe(200)
  27. expect(Array.isArray(options.body.providers)).toBe(true)
  28. expect((await call('GET', '/api/sessions/missing/messages')).status).toBe(404)
  29. expect((await call('GET', '/api/kanban/boards')).status).toBe(404)
  30. expect((await call('POST', '/api/auth/ws-ticket')).status).toBe(410)
  31. }, 120_000)
  32. it('writes custom model endpoints into the embedded config.yaml', async () => {
  33. home = mkdtempSync(join(tmpdir(), 'yaoyao-hermes-api-'))
  34. hermes = createEmbeddedHermes(home, { env })
  35. const created = await call('POST', '/api/providers/custom-endpoints', {}, {
  36. name: 'local-test', base_url: 'http://127.0.0.1:9/v1', model: 'test-model', api_key: 'sk-local-test-key-0000',
  37. })
  38. expect(created.status, JSON.stringify(created.body)).toBeLessThan(300)
  39. const listed = await call('GET', '/api/providers/custom-endpoints')
  40. expect(JSON.stringify(listed.body)).toContain('local-test')
  41. expect(existsSync(join(home, 'hermes', 'config.yaml'))).toBe(true)
  42. }, 120_000)
  43. })