Files
hermes-ui/tests/server/chat-run-api-controller.test.ts
T
toor 1e9ea89d86
Build / build (push) Canceled after 0s
NPM Lockfile Check / npm ci --ignore-scripts (push) Canceled after 0s
Playwright / e2e (push) Canceled after 0s
chore: bootstrap hermes-ui skeleton from 0.6.29 snapshot
Import the client, server, skills, tests, and build scripts as the starting
point for the rebuild, then trim the surfaces this project does not ship:
Electron desktop packaging, the marketing website, and the ESP32 firmware
tree. CI keeps the web build, e2e, lockfile, release, and Docker workflows.

Rename the package and CLI entry points to hermes-ui, drop the prepare hook so
npm ci no longer triggers a build, and drop the removed tsconfig and nodemon
references. Rewrite AGENTS.md, ARCHITECTURE.md, DEVELOPMENT.md, and the READMEs
so the documented package map and hard rules match this repository.

reference_code/ is ignored: it is a read-only snapshot, not an input to the build.
2026-10-16 10:34:35 +08:00

153 lines
4.6 KiB
TypeScript

import { EventEmitter } from 'events'
import { beforeEach, describe, expect, it, vi } from 'vitest'
const ioMock = vi.hoisted(() => vi.fn())
vi.mock('socket.io-client', () => ({
io: ioMock,
}))
function makeSocket() {
const emitter = new EventEmitter() as EventEmitter & {
emit: ReturnType<typeof vi.fn>
disconnect: ReturnType<typeof vi.fn>
emitNative: (event: string, payload?: unknown) => boolean
}
const nativeEmit = EventEmitter.prototype.emit.bind(emitter)
emitter.emitNative = nativeEmit
emitter.emit = vi.fn((event: string, payload?: unknown) => {
if (event === 'run') {
process.nextTick(() => {
nativeEmit('run.started', { event: 'run.started', run_id: 'run-1' })
nativeEmit('message.delta', { event: 'message.delta', run_id: 'run-1', delta: 'hello' })
nativeEmit('run.completed', { event: 'run.completed', run_id: 'run-1' })
})
}
return true
}) as any
emitter.disconnect = vi.fn()
return emitter
}
describe('chat-run HTTP API controller', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('runs chat-run through Socket.IO and returns a completed HTTP response', async () => {
const socket = makeSocket()
ioMock.mockReturnValue(socket)
const { runOnce } = await import('../../packages/server/src/controllers/chat-run')
const ctx = {
get: vi.fn((name: string) => name.toLowerCase() === 'authorization' ? 'Bearer token-1' : ''),
state: { profile: { name: 'default' } },
request: {
body: {
session_id: 'session-1',
input: 'hello',
include_events: true,
},
},
status: 200,
body: undefined as any,
}
const pending = runOnce(ctx as any)
socket.emitNative('connect')
await pending
expect(ioMock).toHaveBeenCalledWith(expect.stringContaining('/chat-run'), expect.objectContaining({
auth: { token: 'token-1' },
query: { profile: 'default' },
}))
expect(socket.emit).toHaveBeenCalledWith('run', expect.objectContaining({
session_id: 'session-1',
input: 'hello',
profile: 'default',
}))
expect(ctx.status).toBe(200)
expect(ctx.body).toMatchObject({
ok: true,
status: 'completed',
session_id: 'session-1',
run_id: 'run-1',
output: 'hello',
})
expect(ctx.body.events).toHaveLength(3)
})
it('generates a session id when none is provided', async () => {
const socket = makeSocket()
ioMock.mockReturnValue(socket)
const { runOnce } = await import('../../packages/server/src/controllers/chat-run')
const ctx = {
get: vi.fn((name: string) => name.toLowerCase() === 'authorization' ? 'Bearer token-1' : ''),
state: { profile: { name: 'default' } },
request: {
body: {
source: 'cli',
input: 'start a new chat',
},
},
status: 200,
body: undefined as any,
}
const pending = runOnce(ctx as any)
socket.emitNative('connect')
await pending
const emittedPayload = socket.emit.mock.calls.find(call => call[0] === 'run')?.[1] as Record<string, unknown>
expect(emittedPayload.session_id).toEqual(expect.stringMatching(/^[0-9a-f-]{36}$/))
expect(emittedPayload).toMatchObject({
source: 'cli',
input: 'start a new chat',
profile: 'default',
})
expect(ctx.status).toBe(200)
expect(ctx.body).toMatchObject({
ok: true,
status: 'completed',
session_id: emittedPayload.session_id,
})
})
it('generates a session id for global-agent runs when none is provided', async () => {
const socket = makeSocket()
ioMock.mockReturnValue(socket)
const { runOnce } = await import('../../packages/server/src/controllers/chat-run')
const ctx = {
get: vi.fn((name: string) => name.toLowerCase() === 'authorization' ? 'Bearer token-1' : ''),
state: { profile: { name: 'default' } },
request: {
body: {
source: 'global_agent',
input: 'start a global run',
},
},
status: 200,
body: undefined as any,
}
const pending = runOnce(ctx as any)
socket.emitNative('connect')
await pending
const emittedPayload = socket.emit.mock.calls.find(call => call[0] === 'run')?.[1] as Record<string, unknown>
expect(emittedPayload.session_id).toEqual(expect.stringMatching(/^[0-9a-f-]{36}$/))
expect(emittedPayload).toMatchObject({
source: 'global_agent',
input: 'start a global run',
profile: 'default',
})
expect(ctx.body).toMatchObject({
ok: true,
status: 'completed',
session_id: emittedPayload.session_id,
})
})
})