feat: harden intelligence runtime and llm providers
This commit is contained in:
36
test/fetch-utils.test.mjs
Normal file
36
test/fetch-utils.test.mjs
Normal file
@@ -0,0 +1,36 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { safeFetch, safeFetchText, getFetchMetrics } from '../apis/utils/fetch.mjs';
|
||||
|
||||
test('safeFetch reports HTML as degraded JSON response', async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = async () => ({
|
||||
ok: true,
|
||||
status: 200,
|
||||
headers: { get: () => 'text/html' },
|
||||
text: async () => '<html>not json</html>',
|
||||
});
|
||||
try {
|
||||
const data = await safeFetch('https://example.test/json', { retries: 0, source: 'unit' });
|
||||
assert.match(data.error, /Expected JSON/);
|
||||
assert.ok(getFetchMetrics().bySource.unit.requests >= 1);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
});
|
||||
|
||||
test('safeFetchText returns text and byte count', async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = async () => ({
|
||||
ok: true,
|
||||
status: 200,
|
||||
text: async () => 'hello',
|
||||
});
|
||||
try {
|
||||
const data = await safeFetchText('https://example.test/rss', { retries: 0, source: 'rss-unit' });
|
||||
assert.equal(data.text, 'hello');
|
||||
assert.equal(data.bytes, 5);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
});
|
||||
@@ -13,19 +13,19 @@ describe('OllamaProvider', () => {
|
||||
const provider = new OllamaProvider({});
|
||||
assert.equal(provider.name, 'ollama');
|
||||
assert.equal(provider.model, 'llama3.1:8b');
|
||||
assert.equal(provider.baseUrl, 'http://localhost:11434');
|
||||
assert.equal(provider.baseUrl, 'http://localhost:11434/v1');
|
||||
assert.equal(provider.isConfigured, true);
|
||||
});
|
||||
|
||||
it('should accept custom model and base URL', () => {
|
||||
const provider = new OllamaProvider({ model: 'qwen2.5:14b', baseUrl: 'http://192.168.1.10:11434' });
|
||||
assert.equal(provider.model, 'qwen2.5:14b');
|
||||
assert.equal(provider.baseUrl, 'http://192.168.1.10:11434');
|
||||
assert.equal(provider.baseUrl, 'http://192.168.1.10:11434/v1');
|
||||
});
|
||||
|
||||
it('should strip trailing slashes from base URL', () => {
|
||||
const provider = new OllamaProvider({ baseUrl: 'http://localhost:11434/' });
|
||||
assert.equal(provider.baseUrl, 'http://localhost:11434');
|
||||
assert.equal(provider.baseUrl, 'http://localhost:11434/v1');
|
||||
});
|
||||
|
||||
it('should throw on API error', async () => {
|
||||
@@ -38,7 +38,7 @@ describe('OllamaProvider', () => {
|
||||
await assert.rejects(
|
||||
() => provider.complete('system', 'user'),
|
||||
(err) => {
|
||||
assert.match(err.message, /Ollama API 404/);
|
||||
assert.match(err.message, /ollama API 404/);
|
||||
return true;
|
||||
}
|
||||
);
|
||||
@@ -164,7 +164,7 @@ describe('createLLMProvider — ollama', () => {
|
||||
it('should pass baseUrl from config', () => {
|
||||
const provider = createLLMProvider({ provider: 'ollama', apiKey: null, model: 'mistral:7b', baseUrl: 'http://gpu-box:11434' });
|
||||
assert.ok(provider instanceof OllamaProvider);
|
||||
assert.equal(provider.baseUrl, 'http://gpu-box:11434');
|
||||
assert.equal(provider.baseUrl, 'http://gpu-box:11434/v1');
|
||||
assert.equal(provider.model, 'mistral:7b');
|
||||
});
|
||||
});
|
||||
|
||||
61
test/llm-openai-compatible.test.mjs
Normal file
61
test/llm-openai-compatible.test.mjs
Normal file
@@ -0,0 +1,61 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { OpenAICompatibleProvider } from '../lib/llm/openai-compatible.mjs';
|
||||
import { createLLMProvider } from '../lib/llm/index.mjs';
|
||||
|
||||
test('OpenAI-compatible provider sends configurable payload', async () => {
|
||||
const provider = new OpenAICompatibleProvider({
|
||||
name: 'local-openai',
|
||||
baseUrl: 'http://localhost:1234/v1/',
|
||||
apiKey: 'local-key',
|
||||
model: 'local-model',
|
||||
temperature: 0.1,
|
||||
maxTokens: 123,
|
||||
timeoutMs: 5000,
|
||||
});
|
||||
|
||||
let capturedUrl;
|
||||
let capturedBody;
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = async (url, opts) => {
|
||||
capturedUrl = url;
|
||||
capturedBody = JSON.parse(opts.body);
|
||||
assert.equal(opts.headers.Authorization, 'Bearer local-key');
|
||||
return {
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
choices: [{ message: { content: 'ok' } }],
|
||||
usage: { prompt_tokens: 2, completion_tokens: 3 },
|
||||
model: 'local-model',
|
||||
}),
|
||||
};
|
||||
};
|
||||
|
||||
try {
|
||||
const result = await provider.complete('system', 'user');
|
||||
assert.equal(capturedUrl, 'http://localhost:1234/v1/chat/completions');
|
||||
assert.equal(capturedBody.model, 'local-model');
|
||||
assert.equal(capturedBody.temperature, 0.1);
|
||||
assert.equal(capturedBody.max_tokens, 123);
|
||||
assert.equal(result.text, 'ok');
|
||||
assert.deepEqual(result.usage, { inputTokens: 2, outputTokens: 3 });
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
});
|
||||
|
||||
test('factory supports lmstudio and openai-compatible aliases', () => {
|
||||
const lmstudio = createLLMProvider({ provider: 'lmstudio', model: 'local-model' });
|
||||
assert.ok(lmstudio instanceof OpenAICompatibleProvider);
|
||||
assert.equal(lmstudio.baseUrl, 'http://localhost:1234/v1');
|
||||
assert.equal(lmstudio.isConfigured, true);
|
||||
|
||||
const compatible = createLLMProvider({
|
||||
provider: 'openai-compatible',
|
||||
baseUrl: 'http://llm:8000/v1',
|
||||
apiKey: 'token',
|
||||
model: 'qwen',
|
||||
});
|
||||
assert.ok(compatible instanceof OpenAICompatibleProvider);
|
||||
assert.equal(compatible.baseUrl, 'http://llm:8000/v1');
|
||||
});
|
||||
@@ -80,7 +80,7 @@ test('OpenRouterProvider Unit Tests', async (t) => {
|
||||
provider.complete('system', 'user'),
|
||||
{
|
||||
name: 'Error',
|
||||
message: 'OpenRouter API 401: Unauthorized access'
|
||||
message: 'openrouter auth failed (401): Unauthorized access'
|
||||
}
|
||||
);
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user