Skip to content

Commit

Permalink
chore(tests): add more API request tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertCraigie authored and stainless-app[bot] committed Aug 8, 2024
1 parent ad89892 commit 04c1590
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
28 changes: 28 additions & 0 deletions tests/lib/__snapshots__/parser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,34 @@ exports[`.parse() zod deserialises response_format 1`] = `
"
`;

exports[`.parse() zod merged schemas 2`] = `
"{
"id": "chatcmpl-9tyPgktyF5JgREIZd0XZI4XgrBAD2",
"object": "chat.completion",
"created": 1723127296,
"model": "gpt-4o-2024-08-06",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "{\\"person1\\":{\\"name\\":\\"Jane Doe\\",\\"phone_number\\":\\"+1234567890\\",\\"roles\\":[\\"other\\"],\\"description\\":\\"Engineer at OpenAI. Email: jane@openai.com\\"},\\"person2\\":{\\"name\\":\\"John Smith\\",\\"phone_number\\":\\"+0987654321\\",\\"differentField\\":\\"Engineer at OpenAI. Email: john@openai.com\\"}}",
"refusal": null
},
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 61,
"completion_tokens": 72,
"total_tokens": 133
},
"system_fingerprint": "fp_845eaabc1f"
}
"
`;

exports[`.parse() zod top-level recursive schemas 1`] = `
"{
"id": "chatcmpl-9taiMDrRVRIkk1Xg1yE82UjnYuZjt",
Expand Down
44 changes: 44 additions & 0 deletions tests/lib/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,50 @@ describe('.parse()', () => {
"type": "object",
}
`);

const completion = await makeSnapshotRequest(
(openai) =>
openai.beta.chat.completions.parse({
model: 'gpt-4o-2024-08-06',
messages: [
{
role: 'system',
content: 'You are a helpful assistant.',
},
{
role: 'user',
content:
'jane doe, born nov 16, engineer at openai, [email protected]. john smith, born march 1, enigneer at openai, [email protected]',
},
],
response_format: zodResponseFormat(contactPersonSchema, 'contactPerson'),
}),
2,
);

expect(completion.choices[0]?.message).toMatchInlineSnapshot(`
{
"content": "{"person1":{"name":"Jane Doe","phone_number":"+1234567890","roles":["other"],"description":"Engineer at OpenAI. Email: [email protected]"},"person2":{"name":"John Smith","phone_number":"+0987654321","differentField":"Engineer at OpenAI. Email: [email protected]"}}",
"parsed": {
"person1": {
"description": "Engineer at OpenAI. Email: [email protected]",
"name": "Jane Doe",
"phone_number": "+1234567890",
"roles": [
"other",
],
},
"person2": {
"differentField": "Engineer at OpenAI. Email: [email protected]",
"name": "John Smith",
"phone_number": "+0987654321",
},
},
"refusal": null,
"role": "assistant",
"tool_calls": [],
}
`);
});
});
});
7 changes: 5 additions & 2 deletions tests/utils/mock-snapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { RequestInfo } from 'openai/_shims/auto/types';
import { mockFetch } from './mock-fetch';
import { Readable } from 'stream';

export async function makeSnapshotRequest<T>(requestFn: (client: OpenAI) => Promise<T>): Promise<T> {
export async function makeSnapshotRequest<T>(
requestFn: (client: OpenAI) => Promise<T>,
snapshotIndex = 1,
): Promise<T> {
if (process.env['UPDATE_API_SNAPSHOTS'] === '1') {
var capturedResponseContent: string | null = null;

Expand All @@ -27,7 +30,7 @@ export async function makeSnapshotRequest<T>(requestFn: (client: OpenAI) => Prom
return result;
}

const qualifiedSnapshotName = `${expect.getState().currentTestName} 1`;
const qualifiedSnapshotName = [expect.getState().currentTestName, snapshotIndex].join(' ');
const snapshotState = expect.getState()['snapshotState'];
(snapshotState._uncheckedKeys as Set<string>).delete(qualifiedSnapshotName);

Expand Down

0 comments on commit 04c1590

Please sign in to comment.