Files
wehub-resource-sync 0d3cb498a3
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Waiting to run
Test and Publish Multi-arch Docker Image / test (push) Waiting to run
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Blocked by required conditions
Validate Renovate Config / Validate Renovate Configuration (push) Waiting to run
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:24:08 +08:00

134 lines
4.2 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
import { fetchWithCache } from '../../src/cache';
import { LlamaProvider } from '../../src/providers/llama';
import { getRequestTimeoutMs } from '../../src/providers/shared';
vi.mock('../../src/cache', async (importOriginal) => {
return {
...(await importOriginal()),
fetchWithCache: vi.fn(),
};
});
describe('LlamaProvider', () => {
const modelName = 'testModel';
const config = {
temperature: 0.7,
};
describe('constructor', () => {
it('should initialize with modelName and config', () => {
const provider = new LlamaProvider(modelName, { config });
expect(provider.modelName).toBe(modelName);
expect(provider.config).toEqual(config);
});
it('should initialize with id function if id is provided', () => {
const id = 'testId';
const provider = new LlamaProvider(modelName, { config, id });
expect(provider.id()).toBe(id);
});
});
describe('id', () => {
it('should return the correct id string', () => {
const provider = new LlamaProvider(modelName);
expect(provider.id()).toBe(`llama:${modelName}`);
});
});
describe('toString', () => {
it('should return the correct string representation', () => {
const provider = new LlamaProvider(modelName);
expect(provider.toString()).toBe(`[Llama Provider ${modelName}]`);
});
});
describe('callApi', () => {
const prompt = 'test prompt';
const response = { data: { content: 'test response' } };
beforeEach(() => {
vi.clearAllMocks();
});
it('should call fetchWithCache with correct parameters', async () => {
vi.mocked(fetchWithCache).mockResolvedValue({
...response,
cached: false,
status: 200,
statusText: 'OK',
});
const provider = new LlamaProvider(modelName, { config });
await provider.callApi(prompt);
expect(fetchWithCache).toHaveBeenCalledWith(
`${process.env.LLAMA_BASE_URL || 'http://localhost:8080'}/completion`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt,
n_predict: 512,
temperature: config.temperature,
top_k: undefined,
top_p: undefined,
n_keep: undefined,
stop: undefined,
repeat_penalty: undefined,
repeat_last_n: undefined,
penalize_nl: undefined,
presence_penalty: undefined,
frequency_penalty: undefined,
mirostat: undefined,
mirostat_tau: undefined,
mirostat_eta: undefined,
seed: undefined,
ignore_eos: undefined,
logit_bias: undefined,
}),
},
getRequestTimeoutMs(),
);
});
it('should return the correct response on success', async () => {
vi.mocked(fetchWithCache).mockResolvedValue({
data: { content: 'test response' },
cached: false,
status: 200,
statusText: 'OK',
});
const provider = new LlamaProvider(modelName, { config });
const result = await provider.callApi(prompt);
expect(result).toEqual({
output: response.data.content,
cached: false,
latencyMs: undefined,
});
});
it('should return an error if fetchWithCache throws an error', async () => {
const error = new Error('API call error');
vi.mocked(fetchWithCache).mockRejectedValue(error);
const provider = new LlamaProvider(modelName, { config });
const result = await provider.callApi(prompt);
expect(result).toEqual({ error: `API call error: ${String(error)}` });
});
it('should return an error if response data is malformed', async () => {
const malformedResponse = { data: null, cached: false, status: 200, statusText: 'OK' };
vi.mocked(fetchWithCache).mockResolvedValue(malformedResponse);
const provider = new LlamaProvider(modelName, { config });
const result = await provider.callApi(prompt);
expect(result).toEqual({
error: `API response error: TypeError: Cannot read properties of null (reading 'content'): null`,
});
});
});
});