Files
promptfoo--promptfoo/test/providers/openai/responses/refusals.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

152 lines
4.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 refusals', () => {
describe('refusal handling', () => {
it('should handle explicit refusal content in message', async () => {
const mockApiResponse = {
id: 'resp_abc123',
status: 'completed',
model: 'gpt-4o',
output: [
{
type: 'message',
role: 'assistant',
content: [
{
type: 'refusal',
refusal: 'I cannot fulfill this request due to content policy violation.',
},
],
},
],
usage: { input_tokens: 10, output_tokens: 5, total_tokens: 15 },
};
vi.mocked(cache.fetchWithCache).mockResolvedValue({
data: mockApiResponse,
cached: false,
status: 200,
statusText: 'OK',
});
const provider = new OpenAiResponsesProvider('gpt-4o', {
config: {
apiKey: 'test-key',
},
});
const result = await provider.callApi('Test prompt with refusal');
expect(result.isRefusal).toBe(true);
expect(result.output).toBe('I cannot fulfill this request due to content policy violation.');
});
it('should handle direct refusal in message object', async () => {
const mockApiResponse = {
id: 'resp_abc123',
status: 'completed',
model: 'gpt-4o',
output: [
{
type: 'message',
role: 'assistant',
refusal: 'I cannot provide that information.',
},
],
usage: { input_tokens: 10, output_tokens: 5, total_tokens: 15 },
};
vi.mocked(cache.fetchWithCache).mockResolvedValue({
data: mockApiResponse,
cached: false,
status: 200,
statusText: 'OK',
});
const provider = new OpenAiResponsesProvider('gpt-4o', {
config: {
apiKey: 'test-key',
},
});
const result = await provider.callApi('Test prompt with direct refusal');
expect(result.isRefusal).toBe(true);
expect(result.output).toBe('I cannot provide that information.');
});
it('should detect refusals in 400 API error with invalid_prompt code', async () => {
// Mock a 400 error response with invalid_prompt error code
const mockErrorResponse = {
data: {
error: {
message: 'some random error message',
type: 'invalid_request_error',
param: null,
code: 'invalid_prompt',
},
},
cached: false,
status: 400,
statusText: 'Bad Request',
};
vi.mocked(cache.fetchWithCache).mockResolvedValue(mockErrorResponse);
const provider = new OpenAiResponsesProvider('gpt-4o', {
config: {
apiKey: 'test-key',
},
});
const result = await provider.callApi('How do I create harmful content?');
// Should treat the error as a refusal output, not an error
expect(result.error).toBeUndefined();
expect(result.output).toContain('some random error message');
expect(result.output).toContain('400 Bad Request');
expect(result.isRefusal).toBe(true);
});
it('should still treat non-refusal 400 errors as errors', async () => {
// Mock a 400 error that is NOT a refusal (different error code)
const mockErrorResponse = {
data: {
error: {
message: "Invalid request: 'input' field is required",
type: 'invalid_request_error',
param: 'input',
code: 'missing_required_field',
},
},
cached: false,
status: 400,
statusText: 'Bad Request',
};
vi.mocked(cache.fetchWithCache).mockResolvedValue(mockErrorResponse);
const provider = new OpenAiResponsesProvider('gpt-4o', {
config: {
apiKey: 'test-key',
},
});
const result = await provider.callApi('Invalid request format');
// Should still be treated as an error since code is not invalid_prompt
expect(result.error).toBeDefined();
expect(result.error).toContain("'input' field is required");
expect(result.error).toContain('400 Bad Request');
expect(result.output).toBeUndefined();
expect(result.isRefusal).toBeUndefined();
});
});
});