0d3cb498a3
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
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Has been cancelled
Test and Publish Multi-arch Docker Image / test (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Has been cancelled
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) Has been cancelled
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Has been cancelled
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Has been cancelled
Validate Renovate Config / Validate Renovate Configuration (push) Has been cancelled
150 lines
4.4 KiB
TypeScript
150 lines
4.4 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { cloudConfig } from '../../src/globalConfig/cloud';
|
|
import logger from '../../src/logger';
|
|
import { PromptfooModelProvider } from '../../src/providers/promptfooModel';
|
|
import type { Mock } from 'vitest';
|
|
|
|
describe('PromptfooModelProvider', () => {
|
|
let mockFetch: Mock;
|
|
let mockCloudConfig: ReturnType<typeof vi.spyOn>;
|
|
const mockLogger = vi.spyOn(logger, 'debug').mockImplementation(function () {});
|
|
|
|
beforeEach(() => {
|
|
mockFetch = vi.fn();
|
|
global.fetch = mockFetch;
|
|
mockCloudConfig = vi.spyOn(cloudConfig, 'getApiKey').mockReturnValue('test-token');
|
|
mockLogger.mockClear();
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.resetAllMocks();
|
|
});
|
|
|
|
it('should initialize with model name', () => {
|
|
const provider = new PromptfooModelProvider('test-model');
|
|
expect(provider.id()).toBe('promptfoo:model:test-model');
|
|
});
|
|
|
|
it('should throw error if model name is not provided', () => {
|
|
expect(() => new PromptfooModelProvider('')).toThrow('Model name is required');
|
|
});
|
|
|
|
it('should call API with string prompt', async () => {
|
|
const provider = new PromptfooModelProvider('test-model');
|
|
const mockResponse = {
|
|
ok: true,
|
|
json: () =>
|
|
Promise.resolve({
|
|
result: {
|
|
choices: [{ message: { content: 'test response' } }],
|
|
usage: {
|
|
total_tokens: 10,
|
|
prompt_tokens: 5,
|
|
completion_tokens: 5,
|
|
},
|
|
},
|
|
}),
|
|
};
|
|
mockFetch.mockResolvedValue(mockResponse);
|
|
|
|
const result = await provider.callApi('test prompt');
|
|
|
|
expect(result).toEqual({
|
|
output: 'test response',
|
|
tokenUsage: {
|
|
total: 10,
|
|
prompt: 5,
|
|
completion: 5,
|
|
numRequests: 1,
|
|
},
|
|
});
|
|
});
|
|
|
|
it('should handle JSON array messages', async () => {
|
|
const provider = new PromptfooModelProvider('test-model');
|
|
const messages = JSON.stringify([
|
|
{ role: 'user', content: 'Hello' },
|
|
{ role: 'assistant', content: 'Hi' },
|
|
]);
|
|
|
|
const mockResponse = {
|
|
ok: true,
|
|
json: () =>
|
|
Promise.resolve({
|
|
result: {
|
|
choices: [{ message: { content: 'test response' } }],
|
|
usage: { total_tokens: 10, prompt_tokens: 5, completion_tokens: 5 },
|
|
},
|
|
}),
|
|
};
|
|
mockFetch.mockResolvedValue(mockResponse);
|
|
|
|
await provider.callApi(messages);
|
|
|
|
expect(mockFetch).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
expect.objectContaining({
|
|
body: expect.stringContaining(
|
|
'"messages":[{"role":"user","content":"Hello"},{"role":"assistant","content":"Hi"}]',
|
|
),
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('should throw error if no auth token', async () => {
|
|
mockCloudConfig.mockReturnValue(undefined);
|
|
const provider = new PromptfooModelProvider('test-model');
|
|
|
|
await expect(provider.callApi('test')).rejects.toThrow('No Promptfoo auth token available');
|
|
});
|
|
|
|
it('should handle API errors', async () => {
|
|
const provider = new PromptfooModelProvider('test-model');
|
|
mockFetch.mockResolvedValue({
|
|
ok: false,
|
|
status: 500,
|
|
text: () => Promise.resolve('Internal server error'),
|
|
});
|
|
|
|
await expect(provider.callApi('test')).rejects.toThrow('PromptfooModel task API error: 500');
|
|
});
|
|
|
|
it('should handle invalid API responses', async () => {
|
|
const provider = new PromptfooModelProvider('test-model');
|
|
mockFetch.mockResolvedValue({
|
|
ok: true,
|
|
json: () => Promise.resolve({}),
|
|
});
|
|
|
|
await expect(provider.callApi('test')).rejects.toThrow(
|
|
'Invalid response from PromptfooModel task API',
|
|
);
|
|
});
|
|
|
|
it('should use config from options', async () => {
|
|
const config = { temperature: 0.7 };
|
|
const provider = new PromptfooModelProvider('test-model', { model: 'test-model', config });
|
|
|
|
const mockResponse = {
|
|
ok: true,
|
|
json: () =>
|
|
Promise.resolve({
|
|
result: {
|
|
choices: [{ message: { content: 'test' } }],
|
|
usage: { total_tokens: 10, prompt_tokens: 5, completion_tokens: 5 },
|
|
},
|
|
}),
|
|
};
|
|
mockFetch.mockResolvedValue(mockResponse);
|
|
|
|
await provider.callApi('test');
|
|
|
|
expect(mockFetch).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
expect.objectContaining({
|
|
body: expect.stringContaining('"config":{"temperature":0.7}'),
|
|
}),
|
|
);
|
|
});
|
|
});
|