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.
90 lines
3.0 KiB
TypeScript
90 lines
3.0 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
import { AgentRunGateway, ProviderApiError } from '../../packages/server/src/services/agent-runner/gateway'
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals()
|
|
})
|
|
|
|
describe('agent run gateway', () => {
|
|
it('posts JSON requests with bearer auth and custom headers', async () => {
|
|
const fetchMock = vi.fn(async () => new Response(JSON.stringify({ ok: true }), {
|
|
status: 200,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
}))
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
|
|
const result = await new AgentRunGateway().completeJson({
|
|
url: 'https://api.example.com/v1/messages',
|
|
apiKey: 'sk-test',
|
|
headers: { 'x-api-key': 'sk-test' },
|
|
body: { model: 'm' },
|
|
})
|
|
|
|
expect(result).toEqual({ ok: true })
|
|
expect(fetchMock).toHaveBeenCalledWith('https://api.example.com/v1/messages', expect.objectContaining({
|
|
method: 'POST',
|
|
headers: expect.objectContaining({
|
|
Authorization: 'Bearer sk-test',
|
|
'Content-Type': 'application/json',
|
|
'x-api-key': 'sk-test',
|
|
}),
|
|
body: '{"model":"m"}',
|
|
}))
|
|
})
|
|
|
|
it('throws structured provider errors', async () => {
|
|
vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({
|
|
error: { message: 'bad key' },
|
|
}), { status: 401, headers: { 'Content-Type': 'application/json' } })))
|
|
|
|
await expect(new AgentRunGateway().completeJson({
|
|
url: 'https://api.example.com/v1/messages',
|
|
apiKey: 'sk-test',
|
|
body: {},
|
|
})).rejects.toMatchObject({
|
|
name: 'ProviderApiError',
|
|
status: 401,
|
|
providerError: { error: { message: 'bad key' } },
|
|
message: 'bad key',
|
|
} satisfies Partial<ProviderApiError>)
|
|
})
|
|
|
|
it('returns provider byte streams', async () => {
|
|
const encoder = new TextEncoder()
|
|
vi.stubGlobal('fetch', vi.fn(async () => new Response(new ReadableStream({
|
|
start(controller) {
|
|
controller.enqueue(encoder.encode('chunk'))
|
|
controller.close()
|
|
},
|
|
}), { status: 200, headers: { 'Content-Type': 'text/event-stream' } })))
|
|
|
|
const stream = await new AgentRunGateway().streamBytes({
|
|
url: 'https://api.example.com/v1/responses',
|
|
apiKey: 'sk-test',
|
|
body: { stream: true },
|
|
})
|
|
|
|
const chunks: string[] = []
|
|
for await (const chunk of stream) chunks.push(new TextDecoder().decode(chunk))
|
|
expect(chunks).toEqual(['chunk'])
|
|
})
|
|
|
|
it('rejects JSON error bodies returned with HTTP 200 for stream requests', async () => {
|
|
vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({
|
|
code: 500,
|
|
msg: '404 NOT_FOUND',
|
|
success: false,
|
|
}), { status: 200, headers: { 'Content-Type': 'application/json' } })))
|
|
|
|
await expect(new AgentRunGateway().streamBytes({
|
|
url: 'https://api.example.com/v1/messages',
|
|
apiKey: 'sk-test',
|
|
body: { stream: true },
|
|
})).rejects.toMatchObject({
|
|
name: 'ProviderApiError',
|
|
status: 200,
|
|
providerError: { code: 500, msg: '404 NOT_FOUND', success: false },
|
|
} satisfies Partial<ProviderApiError>)
|
|
})
|
|
})
|