Files
promptfoo--promptfoo/test/providers/azure/responses-nested-schema.integration.test.ts
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

217 lines
7.0 KiB
TypeScript

/**
* Integration test for Azure Responses API nested schema loading.
*
* This test verifies the bug fix where nested `schema: file://...` references
* inside response_format configurations were not being loaded correctly.
*
* Unlike the unit tests in responses.test.ts which mock maybeLoadResponseFormatFromExternalFile,
* this integration test uses real file system operations to verify the full loading chain.
*/
import * as fs from 'fs';
import * as path from 'path';
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
import { AzureResponsesProvider } from '../../../src/providers/azure/responses';
import { mockProcessEnv } from '../../util/utils';
// Only mock the network layer, not file operations
vi.mock('../../../src/cache');
const { fetchWithCache } = await import('../../../src/cache');
const mockFetchWithCache = vi.mocked(fetchWithCache);
describe('Azure Responses - Nested Schema Loading Integration', () => {
const tempDir = path.join(__dirname, '.temp-nested-schema-test');
let authHeadersValue: Record<string, string>;
let restoreEnv: () => void;
beforeAll(() => {
// Create temp directory and test files
fs.mkdirSync(tempDir, { recursive: true });
// Create the nested schema file
const nestedSchema = {
type: 'object',
properties: {
event_name: { type: 'string' },
date: { type: 'string' },
location: { type: 'string' },
},
required: ['event_name', 'date', 'location'],
additionalProperties: false,
};
fs.writeFileSync(
path.join(tempDir, 'event-schema.json'),
JSON.stringify(nestedSchema, null, 2),
);
// Create the response_format file with nested file reference
const responseFormat = {
type: 'json_schema',
name: 'event_extraction',
schema: `file://${path.join(tempDir, 'event-schema.json')}`,
};
fs.writeFileSync(
path.join(tempDir, 'nested-format.json'),
JSON.stringify(responseFormat, null, 2),
);
// Create a flat response_format file (no nested reference)
const flatFormat = {
type: 'json_schema',
name: 'flat_schema',
schema: {
type: 'object',
properties: { name: { type: 'string' } },
required: ['name'],
additionalProperties: false,
},
};
fs.writeFileSync(path.join(tempDir, 'flat-format.json'), JSON.stringify(flatFormat, null, 2));
});
afterAll(() => {
// Clean up temp directory
fs.rmSync(tempDir, { recursive: true, force: true });
});
beforeEach(() => {
vi.resetAllMocks();
restoreEnv = mockProcessEnv({
AZURE_API_KEY: 'test-key',
AZURE_API_HOST: 'test.openai.azure.com',
});
authHeadersValue = { 'api-key': 'test-key' };
// Mock the auth headers getter
Object.defineProperty(AzureResponsesProvider.prototype, 'authHeaders', {
get: vi.fn(() => authHeadersValue),
configurable: true,
});
// Mock ensureInitialized and getApiBaseUrl
AzureResponsesProvider.prototype.ensureInitialized = vi.fn().mockResolvedValue(void 0);
AzureResponsesProvider.prototype.getApiBaseUrl = vi
.fn()
.mockReturnValue('https://test.openai.azure.com');
// Mock successful API response
mockFetchWithCache.mockResolvedValue({
data: {
output: [
{
type: 'message',
role: 'assistant',
content: [
{
type: 'output_text',
text: '{"event_name": "Conference", "date": "2025-01-15", "location": "NYC"}',
},
],
},
],
usage: { input_tokens: 10, output_tokens: 20 },
},
cached: false,
status: 200,
statusText: 'OK',
});
});
afterEach(() => {
restoreEnv();
});
it('should load nested schema from file reference (regression test for Azure bug)', async () => {
const provider = new AzureResponsesProvider('gpt-4.1-test', {
config: {
response_format: `file://${path.join(tempDir, 'nested-format.json')}` as any,
},
});
// Call the API - this should successfully load both the outer format and nested schema
const result = await provider.callApi('Extract event info from: Conference in NYC on Jan 15');
// Verify no error occurred (the bug would cause an error or malformed request)
expect(result.error).toBeUndefined();
expect(result.output).toBeDefined();
// Verify the API was called with the correct format structure
expect(mockFetchWithCache).toHaveBeenCalled();
const callArgs = mockFetchWithCache.mock.calls[0]!;
const requestBody = JSON.parse(callArgs[1]!.body as string);
// The nested schema should be fully loaded (not a string file reference)
expect(requestBody.text.format.type).toBe('json_schema');
expect(requestBody.text.format.name).toBe('event_extraction');
expect(requestBody.text.format.schema).toEqual({
type: 'object',
properties: {
event_name: { type: 'string' },
date: { type: 'string' },
location: { type: 'string' },
},
required: ['event_name', 'date', 'location'],
additionalProperties: false,
});
// Should NOT be a file reference string
expect(typeof requestBody.text.format.schema).not.toBe('string');
});
it('should handle flat response_format file without nested references', async () => {
const provider = new AzureResponsesProvider('gpt-4.1-test', {
config: {
response_format: `file://${path.join(tempDir, 'flat-format.json')}` as any,
},
});
const result = await provider.callApi('Test prompt');
expect(result.error).toBeUndefined();
const callArgs = mockFetchWithCache.mock.calls[0]!;
const requestBody = JSON.parse(callArgs[1]!.body as string);
expect(requestBody.text.format.type).toBe('json_schema');
expect(requestBody.text.format.name).toBe('flat_schema');
expect(requestBody.text.format.schema).toEqual({
type: 'object',
properties: { name: { type: 'string' } },
required: ['name'],
additionalProperties: false,
});
});
it('should handle inline response_format without file loading', async () => {
const provider = new AzureResponsesProvider('gpt-4.1-test', {
config: {
// Using shorthand format (name/schema at top level) which the runtime code normalizes
response_format: {
type: 'json_schema',
name: 'inline_test',
schema: {
type: 'object',
properties: { result: { type: 'string' } },
additionalProperties: false,
},
} as any,
},
});
const result = await provider.callApi('Test prompt');
expect(result.error).toBeUndefined();
const callArgs = mockFetchWithCache.mock.calls[0]!;
const requestBody = JSON.parse(callArgs[1]!.body as string);
expect(requestBody.text.format.name).toBe('inline_test');
expect(requestBody.text.format.schema).toEqual({
type: 'object',
properties: { result: { type: 'string' } },
additionalProperties: false,
});
});
});