import { describe, expect, it } from 'vitest'; import { DecisionOllamaGatewayClient, assertTrustedDecisionOllamaUrl, } from '../../src/document-workbench/decision-ollama-client'; import { Inf0OutcomeUnknownError } from '../../src/document-workbench/inf0-gateway-client'; import { createInf0Gateway } from '../../src/document-workbench/inf0-gateway'; const context = { tenantId: 't_0pt', projectId: 'prj_default', actorId: 'user_1', capability: '0pt.document.structure.v1', correlationId: 'corr-1', }; const admission = { tenantId: 't_0pt', projectId: 'prj_default', jobId: 'job-1', modelId: 'hermes-qwen3.5:9b', dataClass: 'low', maxInputTokens: 1000, maxOutputTokens: 200, maximumCredits: 0, }; const policy = { tenantId: 't_0pt', projectId: 'prj_default', allowedModelIds: ['hermes-qwen3.5:9b'], allowedDataClasses: ['low'], destination: 'internal' as const, externalTransferAllowed: false, maxInputTokens: 1000, maxOutputTokens: 200, }; const messages = [ { role: 'system' as const, content: 'system' }, { role: 'user' as const, content: 'data' }, ]; function client(fetchImpl: typeof fetch, model = 'hermes-qwen3.5:9b') { return new DecisionOllamaGatewayClient({ baseUrl: 'http://100.96.133.104:11434', model, fetchImpl, }); } describe('decision server Ollama gateway', () => { it('accepts tailnet/RFC1918 HTTP but refuses a public HTTP endpoint', () => { expect(() => assertTrustedDecisionOllamaUrl('http://100.96.133.104:11434')).not.toThrow(); expect(() => assertTrustedDecisionOllamaUrl('http://192.168.1.10:11434')).not.toThrow(); expect(() => assertTrustedDecisionOllamaUrl('https://ai.example.test')).not.toThrow(); expect(() => assertTrustedDecisionOllamaUrl('http://8.8.8.8:11434')).toThrow(); expect(() => assertTrustedDecisionOllamaUrl('http://user:pass@100.96.133.104:11434')).toThrow(); }); it('maps a native Ollama chat response into the INF0 receipt', async () => { const c = client(async (url, init) => { expect(String(url)).toMatch(/\/api\/chat$/); const body = JSON.parse(String(init?.body)); expect(body.format).toBe('json'); expect(body.think).toBe(false); return Response.json({ model: 'hermes-qwen3.5:9b', message: { role: 'assistant', content: '{"value":{}}' }, done: true, done_reason: 'stop', prompt_eval_count: 12, eval_count: 8, }); }); await expect( c.complete({ context, admission, policy, reservation: undefined, inputTokens: 12, messages }), ).resolves.toMatchObject({ status: 'model_stopped', modelExecuted: 'hermes-qwen3.5:9b', providerKind: 'local', totalTokens: 20, }); }); it('refuses an external destination and malformed usage', async () => { const c = client(async () => Response.json({ model: 'hermes-qwen3.5:9b', message: { role: 'assistant', content: 'x' }, done: true, done_reason: 'stop', prompt_eval_count: 1, eval_count: 5000, }), ); await expect( c.complete({ context, admission, policy: { ...policy, destination: 'external' }, reservation: undefined, inputTokens: 1, messages, }), ).rejects.toThrow(/denied/i); await expect( c.complete({ context, admission, policy, reservation: undefined, inputTokens: 1, messages }), ).rejects.toBeInstanceOf(Inf0OutcomeUnknownError); }); it('is preferred over the INF0/LiteLLM path when configured', () => { expect( createInf0Gateway({ DECISION_OLLAMA_URL: 'http://100.96.133.104:11434', DECISION_OLLAMA_MODEL: 'hermes-qwen3.5:9b', INF0_GATEWAY_LITELLM_KEY: 'sk-test', } as unknown as NodeJS.ProcessEnv), ).toBeInstanceOf(DecisionOllamaGatewayClient); expect( createInf0Gateway({ INF0_GATEWAY_LITELLM_KEY: 'sk-test', } as unknown as NodeJS.ProcessEnv), ).not.toBeInstanceOf(DecisionOllamaGatewayClient); }); });