247 lines
7.5 KiB
TypeScript
247 lines
7.5 KiB
TypeScript
import { HermesAdapter, HermesError } from './hermes.adapter';
|
|
const health = {
|
|
ok: true,
|
|
version: '0.13',
|
|
commit: 'abc',
|
|
auth_required: true,
|
|
};
|
|
describe('installed Hermes health contract', () => {
|
|
it('uses pinned internal origin, bearer only server side and strips extra fields', async () => {
|
|
const fetcher = jest
|
|
.fn()
|
|
.mockResolvedValue(
|
|
new Response(
|
|
JSON.stringify({ ...health, token: 'never', home: '/secret' }),
|
|
),
|
|
);
|
|
const adapter = new HermesAdapter(
|
|
{
|
|
origin: 'http://canina-hermes:9119',
|
|
token: () => Promise.resolve('private'),
|
|
},
|
|
fetcher,
|
|
);
|
|
expect(await adapter.health()).toEqual(health);
|
|
expect(fetcher.mock.calls[0][0].toString()).toBe(
|
|
'http://canina-hermes:9119/api/health',
|
|
);
|
|
expect(fetcher.mock.calls[0][1].headers.Authorization).toBe(
|
|
'Bearer private',
|
|
);
|
|
expect(fetcher.mock.calls[0][1].redirect).toBe('error');
|
|
});
|
|
it('refuses a runtime reporting that authentication is disabled', async () => {
|
|
const adapter = new HermesAdapter({origin:'http://canina-hermes:9119',token:async()=> 'private'},
|
|
jest.fn().mockResolvedValue(new Response(JSON.stringify({...health,auth_required:false}))));
|
|
await expect(adapter.health()).rejects.toMatchObject({code:'AUTH_REQUIRED'});
|
|
});
|
|
|
|
it('fails closed for bad origin, auth errors, malformed contract and oversized bodies', async () => {
|
|
expect(
|
|
() =>
|
|
new HermesAdapter({
|
|
origin: 'https://evil.test',
|
|
token: () => Promise.resolve(''),
|
|
}),
|
|
).toThrow(HermesError);
|
|
for (const response of [
|
|
new Response('private', { status: 401 }),
|
|
new Response('{}'),
|
|
new Response('x'.repeat(262145)),
|
|
]) {
|
|
const a = new HermesAdapter(
|
|
{
|
|
origin: 'http://canina-hermes:9119',
|
|
token: () => Promise.resolve('secret'),
|
|
},
|
|
jest.fn().mockResolvedValue(response),
|
|
);
|
|
await expect(a.health()).rejects.toBeInstanceOf(HermesError);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('bounded profile-scoped REST inventory', () => {
|
|
const create = (payload: unknown) => {
|
|
const fetcher = jest
|
|
.fn()
|
|
.mockResolvedValue(new Response(JSON.stringify(payload)));
|
|
return {
|
|
a: new HermesAdapter(
|
|
{
|
|
origin: 'http://canina-hermes:9119',
|
|
token: () => Promise.resolve('secret'),
|
|
},
|
|
fetcher,
|
|
),
|
|
fetcher,
|
|
};
|
|
};
|
|
it('projects status, profiles, models and usage without raw paths, prompts or credentials', async () => {
|
|
expect(
|
|
await create({
|
|
version: 'x',
|
|
gateway_running: false,
|
|
gateway_state: null,
|
|
overall: 'degraded',
|
|
active_agents: 0,
|
|
}).a.status('seo'),
|
|
).toEqual({
|
|
version: 'x',
|
|
gateway_running: false,
|
|
gateway_state: null,
|
|
overall: 'degraded',
|
|
active_agents: 0,
|
|
});
|
|
expect(
|
|
await create({
|
|
profiles: [
|
|
{
|
|
name: 'seo',
|
|
is_default: false,
|
|
gateway_running: false,
|
|
path: '/private',
|
|
description: 'token=secret',
|
|
},
|
|
],
|
|
}).a.profiles(),
|
|
).toEqual([{ name: 'seo', is_default: false, gateway_running: false }]);
|
|
expect(
|
|
await create({
|
|
model: 'm',
|
|
provider: 'p',
|
|
effective_context_length: 0,
|
|
api_key: 'secret',
|
|
}).a.modelInfo('seo'),
|
|
).toEqual({ model: 'm', provider: 'p', effective_context_length: 0 });
|
|
expect(
|
|
await create({
|
|
providers: [
|
|
{ slug: 'p', name: 'Provider', models: ['m'], api_key: 'secret' },
|
|
],
|
|
current: { api_key: 'secret' },
|
|
}).a.modelOptions('seo'),
|
|
).toEqual([{ slug: 'p', name: 'Provider', models: ['m'] }]);
|
|
expect(
|
|
await create({
|
|
period_days: 7,
|
|
totals: {
|
|
total_input: 0,
|
|
total_output: 2,
|
|
total_sessions: 1,
|
|
total_estimated_cost: 0,
|
|
},
|
|
tools: { secret: 1 },
|
|
}).a.usage('seo', 7),
|
|
).toEqual({
|
|
period_days: 7,
|
|
totals: {
|
|
total_input: 0,
|
|
total_output: 2,
|
|
total_sessions: 1,
|
|
total_estimated_cost: 0,
|
|
},
|
|
});
|
|
});
|
|
it('uses scoped list contract and rejects unbounded queries or profile escape', async () => {
|
|
const { a, fetcher } = create({
|
|
sessions: [
|
|
{
|
|
id: 's',
|
|
profile: 'seo',
|
|
is_active: false,
|
|
source: 'cron',
|
|
title: 'secret',
|
|
},
|
|
],
|
|
total: 1,
|
|
storage: {},
|
|
});
|
|
expect(await a.sessions('seo', 20, 0)).toEqual({
|
|
sessions: [{ id: 's', profile: 'seo', is_active: false, source: 'cron' }],
|
|
total: 1,
|
|
});
|
|
expect(fetcher.mock.calls[0][0].pathname).toBe('/api/sessions');
|
|
expect(fetcher.mock.calls[0][0].searchParams.get('full')).toBe('false');
|
|
await expect(a.sessions('../escape', 20, 0)).rejects.toThrow();
|
|
await expect(a.sessions('seo', 101, 0)).rejects.toThrow();
|
|
await expect(a.usage('seo', 366)).rejects.toThrow();
|
|
await expect(
|
|
create({
|
|
sessions: [],
|
|
total: 0,
|
|
storage: { seo: 'corrupt' },
|
|
}).a.sessions('seo'),
|
|
).rejects.toThrow();
|
|
await expect(
|
|
create({
|
|
sessions: [
|
|
{ id: 's', profile: 'dev', source: 'cli', is_active: false },
|
|
],
|
|
total: 1,
|
|
}).a.sessions('seo'),
|
|
).rejects.toThrow();
|
|
});
|
|
it('lists scoped cron array; pause/resume use exact POST routes without triggers', async () => {
|
|
expect(
|
|
await create([
|
|
{ id: 'j', profile: 'seo', enabled: false, prompt: 'secret' },
|
|
]).a.crons('seo'),
|
|
).toEqual([{ id: 'j', profile: 'seo', enabled: false }]);
|
|
const { a, fetcher } = create({ id: 'j', profile: 'seo', enabled: false });
|
|
expect(await a.pauseCron('seo', 'j')).toEqual({
|
|
id: 'j',
|
|
profile: 'seo',
|
|
enabled: false,
|
|
});
|
|
expect(fetcher.mock.calls[0][0].pathname).toBe('/api/cron/jobs/j/pause');
|
|
expect(fetcher.mock.calls[0][1].method).toBe('POST');
|
|
const resume = create({ id: 'j', profile: 'seo', enabled: true });
|
|
await resume.a.resumeCron('seo', 'j');
|
|
expect(resume.fetcher.mock.calls[0][0].pathname).toBe(
|
|
'/api/cron/jobs/j/resume',
|
|
);
|
|
await expect(a.pauseCron('seo', '../trigger')).rejects.toThrow();
|
|
});
|
|
});
|
|
|
|
describe('resource budgets and safe errors', () => {
|
|
it('does not leak upstream errors or tokens and limits credential waits', async () => {
|
|
jest.useFakeTimers();
|
|
const a = new HermesAdapter({
|
|
origin: 'http://canina-hermes:9119',
|
|
token: () => new Promise(() => {}),
|
|
});
|
|
const result = expect(a.health()).rejects.toMatchObject({
|
|
code: 'UNAVAILABLE',
|
|
message: 'Hermes unavailable',
|
|
});
|
|
await jest.advanceTimersByTimeAsync(5001);
|
|
await result;
|
|
jest.useRealTimers();
|
|
});
|
|
it('caps concurrency before upstream IO', async () => {
|
|
const resolvers: ((r: Response) => void)[] = [];
|
|
const fetcher = jest.fn(
|
|
() =>
|
|
new Promise<Response>((r) => {
|
|
resolvers.push(r);
|
|
}),
|
|
);
|
|
const a = new HermesAdapter(
|
|
{
|
|
origin: 'http://canina-hermes:9119',
|
|
token: () => Promise.resolve('secret'),
|
|
},
|
|
fetcher,
|
|
);
|
|
const calls = Array.from({ length: 4 }, () => a.health());
|
|
const denied = expect(a.health()).rejects.toMatchObject({ code: 'LIMIT' });
|
|
await Promise.resolve();
|
|
await Promise.resolve();
|
|
resolvers.forEach((r) => r(new Response(JSON.stringify(health))));
|
|
await denied;
|
|
await Promise.all(calls);
|
|
});
|
|
});
|