CI (SIAX Cloud) / integration (push) Skipped
CI (SIAX Cloud) / sonar (push) Skipped
masterplan-lock / check (push) Successful in 4s
CI (SIAX Cloud) / sonar (pull_request) Skipped
masterplan-lock / check (pull_request) Successful in 5s
CI (SIAX Cloud) / security (push) Successful in 16s
Security Scan (SIAX Cloud) / security (push) Successful in 16s
CI (SIAX Cloud) / security (pull_request) Successful in 15s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 19s
CI (SIAX Cloud) / integration (pull_request) Successful in 29s
CI (SIAX Cloud) / quality (push) Successful in 46s
CI (SIAX Cloud) / quality (pull_request) Successful in 48s
60 lines
4.7 KiB
TypeScript
60 lines
4.7 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest';
|
|
import { Inf0GatewayClient, Inf0OutcomeUnknownError } from '../../src/document-workbench/inf0-gateway-client';
|
|
import type { ModelAdmissionRequest, ModelBudgetReservation, ModelExecutionPolicy } from '../../src/document-workbench/model-admission';
|
|
import type { SiaxRequestContext } from '../../src/platform/siax-context';
|
|
|
|
const context: SiaxRequestContext = { tenantId: 'a', projectId: 'p', actorId: 'user', capability: 'inf0.complete', correlationId: 'corr' };
|
|
const admission: ModelAdmissionRequest = { tenantId: 'a', projectId: 'p', jobId: 'j', modelId: 'local_only', dataClass: 'high', maxInputTokens: 100, maxOutputTokens: 20, maximumCredits: 5 };
|
|
const policy: ModelExecutionPolicy = { tenantId: 'a', projectId: 'p', allowedModelIds: ['local_only'], allowedDataClasses: ['high'], destination: 'internal', externalTransferAllowed: false, maxInputTokens: 100, maxOutputTokens: 20 };
|
|
const reservation: ModelBudgetReservation = { tenantId: 'a', projectId: 'p', jobId: 'j', status: 'reserved', credits: 5 };
|
|
const input = { context, admission, policy, reservation, inputTokens: 10, messages: [{ role: 'user' as const, content: 'source' }] };
|
|
const result = { content: '{"name":"Acme"}', modelExecuted: 'model-v1', finishReason: 'stop', routeDecision: { route: 'local_only', providerKind: 'local' }, cost: { promptTokens: 10, completionTokens: 5, totalTokens: 15 } };
|
|
|
|
describe('INF0 gateway adapter', () => {
|
|
it('admits and scopes a request using the Wave-2 contract without inventing cost', async () => {
|
|
const fetchMock = vi.fn(async () => Response.json(result));
|
|
const client = new Inf0GatewayClient({ baseUrl: 'https://inf0.example.test', serviceToken: 'test', fetchImpl: fetchMock });
|
|
const receipt = await client.complete(input);
|
|
expect(receipt).toMatchObject({ status: 'model_stopped', modelExecuted: 'model-v1', totalTokens: 15 });
|
|
expect(receipt.costUsd).toBeUndefined();
|
|
const [url, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit];
|
|
expect(url).toBe('https://inf0.example.test/v1/inf0/gateway/complete');
|
|
expect(JSON.parse(String(init.body))).toMatchObject({ route: 'local_only', data_classification: 'high', risk_level: 'L3', max_tokens: 20 });
|
|
expect(init.redirect).toBe('error');
|
|
});
|
|
it('denies an unreserved or cross-tenant job before network access', async () => {
|
|
const fetchMock = vi.fn();
|
|
const client = new Inf0GatewayClient({ baseUrl: 'https://inf0.example.test', serviceToken: 'test', fetchImpl: fetchMock });
|
|
await expect(client.complete({ ...input, reservation: undefined })).rejects.toThrow('reservation_required');
|
|
await expect(client.complete({ ...input, context: { ...context, tenantId: 'other' } })).rejects.toThrow('Invalid scoped');
|
|
expect(fetchMock).not.toHaveBeenCalled();
|
|
});
|
|
it('reports incomplete output for truncation or absent usage', async () => {
|
|
const fetchMock = vi.fn(async () => Response.json({ ...result, finishReason: 'length', cost: { promptTokens: 0, completionTokens: 0, totalTokens: 0 } }));
|
|
const client = new Inf0GatewayClient({ baseUrl: 'https://inf0.example.test', serviceToken: 'test', fetchImpl: fetchMock });
|
|
expect((await client.complete(input)).status).toBe('needs_review');
|
|
});
|
|
it('does not retry unknown results, mismatched destinations or transport failures', async () => {
|
|
const fetchMock = vi.fn(async () => Response.json({ ...result, routeDecision: { route: 'local_only', providerKind: 'external' } }));
|
|
const client = new Inf0GatewayClient({ baseUrl: 'https://inf0.example.test', serviceToken: 'test', fetchImpl: fetchMock });
|
|
await expect(client.complete(input)).rejects.toBeInstanceOf(Inf0OutcomeUnknownError);
|
|
expect(fetchMock).toHaveBeenCalledOnce();
|
|
const failing = vi.fn(async () => { throw new Error('socket closed'); });
|
|
await expect(new Inf0GatewayClient({ baseUrl: 'https://inf0.example.test', serviceToken: 'test', fetchImpl: failing }).complete(input)).rejects.toBeInstanceOf(Inf0OutcomeUnknownError);
|
|
expect(failing).toHaveBeenCalledOnce();
|
|
});
|
|
it('checks the provider and usage against the policy fixed at dispatch time', async () => {
|
|
const mutable = structuredClone(input);
|
|
const fetchMock = vi.fn(async () => {
|
|
mutable.policy.destination = 'external';
|
|
mutable.admission.maxOutputTokens = 1000;
|
|
return Response.json({ ...result, routeDecision: { route: 'local_only', providerKind: 'external' },
|
|
cost: { promptTokens: 10, completionTokens: 100, totalTokens: 110 } });
|
|
});
|
|
const client = new Inf0GatewayClient({ baseUrl: 'https://inf0.example.test', serviceToken: 'test', fetchImpl: fetchMock });
|
|
await expect(client.complete(mutable)).rejects.toBeInstanceOf(Inf0OutcomeUnknownError);
|
|
expect(fetchMock).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
});
|