wishforge-ide/tests/model-catalog.test.ts

78 lines
3.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* The project settings list the models the operator's gateway offers. The console gets names only:
* the key stays with the control plane, a redirect never carries it elsewhere, and a person who is
* not an owner or admin cannot ask.
*/
import { describe, expect, it } from 'vitest';
import type { Actor } from '@wishforge/contracts';
import { ModelCatalog } from '../apps/api/src/domain/model-catalog.js';
const owner: Actor = { tenantId: 'demo', userId: 'owner', role: 'owner', kind: 'human' };
const env = {
NODE_ENV: 'test',
WF_DEPLOYMENT_MODE: 'development',
WF_PROVISIONER_BACKEND: 'local',
WF_TENANT_ID: 'demo',
WF_CLAUDE_BIN: '/usr/local/bin/claude',
WF_CLAUDE_LOCAL_AUTH_HOME: '/home/api',
};
const settings = async () => ({ ANTHROPIC_BASE_URL: 'https://gateway.invalid/', ANTHROPIC_API_KEY: 'sk-fixture-key' });
const answer =
(body: unknown, status = 200) =>
async () =>
new Response(JSON.stringify(body), { status });
describe('model catalog', () => {
it('lists the gateway’s models, sorted and each once, and asks with the operator’s key', async () => {
const asked: { url: string; headers: Record<string, string> }[] = [];
const catalog = new ModelCatalog(
env,
(async (url: string, init: RequestInit) => {
asked.push({ url, headers: init.headers as Record<string, string> });
return new Response(
JSON.stringify({
data: [
{ id: 'deepseek-pro@latest' },
{ id: 'deepseek-flash@latest' },
{ id: 'deepseek-pro@latest' },
{ id: 'not a model name' },
{ id: 7 },
null,
],
}),
);
}) as unknown as typeof fetch,
settings,
);
expect(await catalog.list(owner)).toEqual({ models: ['deepseek-flash@latest', 'deepseek-pro@latest'] });
expect(asked[0]!.url).toBe('https://gateway.invalid/v1/models?limit=1000');
expect(asked[0]!.headers).toMatchObject({ 'x-api-key': 'sk-fixture-key', Authorization: 'Bearer sk-fixture-key' });
});
it('is for owners and admins, and only where a gateway is configured', async () => {
const catalog = new ModelCatalog(env, answer({ data: [] }) as unknown as typeof fetch, settings);
await expect(catalog.list({ ...owner, role: 'developer' })).rejects.toMatchObject({ code: 'FORBIDDEN' });
await expect(
new ModelCatalog({ ...env, WF_CLAUDE_BIN: '' }, answer({ data: [] }) as unknown as typeof fetch, settings).list(
owner,
),
).rejects.toMatchObject({ code: 'MODELS_UNAVAILABLE' });
await expect(
new ModelCatalog(env, answer({ data: [] }) as unknown as typeof fetch, async () => ({})).list(owner),
).rejects.toMatchObject({ code: 'MODELS_UNAVAILABLE' });
expect(catalog.available('demo')).toBe(true);
});
it('reports a gateway that fails without repeating what it said', async () => {
const failing = (fetcher: unknown) => new ModelCatalog(env, fetcher as typeof fetch, settings).list(owner);
await expect(failing(answer({ error: 'secret-detail sk-fixture-key' }, 401))).rejects.toMatchObject({
code: 'MODELS_FAILED',
message: 'The model gateway answered 401 to the model list',
});
await expect(failing(answer({ models: [] }))).rejects.toMatchObject({ code: 'MODELS_FAILED' });
await expect(
failing(async () => {
throw new Error('connect ECONNREFUSED sk-fixture-key');
}),
).rejects.toMatchObject({ code: 'MODELS_FAILED', message: expect.not.stringContaining('sk-fixture-key') });
});
});