Files
promptfoo--promptfoo/test/providers/openai/responses/toolLoading.test.ts
T
wehub-resource-sync 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
chore: import upstream snapshot with attribution
2026-07-13 13:24:08 +08:00

108 lines
3.6 KiB
TypeScript

// Load-bearing: registers shared vi.mock / beforeEach hooks before any
// module-under-test import below. See ./setup.ts for details.
import './setup';
import { describe, expect, it, vi } from 'vitest';
import * as cache from '../../../../src/cache';
import { OpenAiResponsesProvider } from '../../../../src/providers/openai/responses';
describe('OpenAiResponsesProvider tool loading', () => {
describe('tool loading from external files', () => {
it('should return loaded tools array in config for downstream validation', async () => {
const provider = new OpenAiResponsesProvider('gpt-4o', {
config: {
apiKey: 'test-key',
tools: [{ type: 'web_search_preview' }],
},
});
const context = { prompt: { raw: 'test', label: 'test' }, vars: {} };
const { body, config } = await provider.getOpenAiBody('test prompt', context);
// Verify tools are returned in both body and config
expect(body.tools).toEqual([{ type: 'web_search_preview' }]);
expect(config.tools).toEqual([{ type: 'web_search_preview' }]);
expect(Array.isArray(config.tools)).toBe(true);
});
it('should return undefined tools when not configured', async () => {
const provider = new OpenAiResponsesProvider('gpt-4o', {
config: {
apiKey: 'test-key',
},
});
const context = { prompt: { raw: 'test', label: 'test' }, vars: {} };
const { body, config } = await provider.getOpenAiBody('test prompt', context);
expect(body.tools).toBeUndefined();
expect(config.tools).toBeUndefined();
});
it('should throw clear error for Python tool files', async () => {
const provider = new OpenAiResponsesProvider('gpt-4o', {
config: {
apiKey: 'test-key',
tools: 'file://tools.py:get_tools' as any,
},
});
const context = { prompt: { raw: 'test', label: 'test' }, vars: {} };
await expect(provider.getOpenAiBody('test prompt', context)).rejects.toThrow(
/Failed to load tools/,
);
});
it('should allow deep-research validation to work with loaded tools', async () => {
const provider = new OpenAiResponsesProvider('o4-mini-deep-research', {
config: {
apiKey: 'test-key',
tools: [{ type: 'web_search_preview' }],
},
});
// Mock the API call
vi.mocked(cache.fetchWithCache).mockResolvedValue({
data: {
id: 'resp_123',
object: 'response',
status: 'completed',
model: 'o4-mini-deep-research',
output: [
{
type: 'message',
content: [{ type: 'output_text', text: 'Response' }],
},
],
usage: { input_tokens: 10, output_tokens: 20, total_tokens: 30 },
},
cached: false,
status: 200,
statusText: 'OK',
});
// This should not throw TypeError because config.tools is now an array
const result = await provider.callApi('test');
expect(result.error).toBeUndefined();
});
it('should return error for deep-research without web_search_preview', async () => {
const provider = new OpenAiResponsesProvider('o4-mini-deep-research', {
config: {
apiKey: 'test-key',
tools: [
{
type: 'function',
function: { name: 'test', parameters: { type: 'object', properties: {} } },
},
],
},
});
const result = await provider.callApi('test');
expect(result.error).toContain('requires the web_search_preview tool');
});
});
});