Skip to content

Commit 04c1590

Browse files
RobertCraigiestainless-app[bot]
authored andcommitted
chore(tests): add more API request tests
1 parent ad89892 commit 04c1590

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

tests/lib/__snapshots__/parser.test.ts.snap

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,34 @@ exports[`.parse() zod deserialises response_format 1`] = `
2828
"
2929
`;
3030

31+
exports[`.parse() zod merged schemas 2`] = `
32+
"{
33+
"id": "chatcmpl-9tyPgktyF5JgREIZd0XZI4XgrBAD2",
34+
"object": "chat.completion",
35+
"created": 1723127296,
36+
"model": "gpt-4o-2024-08-06",
37+
"choices": [
38+
{
39+
"index": 0,
40+
"message": {
41+
"role": "assistant",
42+
"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\\"}}",
43+
"refusal": null
44+
},
45+
"logprobs": null,
46+
"finish_reason": "stop"
47+
}
48+
],
49+
"usage": {
50+
"prompt_tokens": 61,
51+
"completion_tokens": 72,
52+
"total_tokens": 133
53+
},
54+
"system_fingerprint": "fp_845eaabc1f"
55+
}
56+
"
57+
`;
58+
3159
exports[`.parse() zod top-level recursive schemas 1`] = `
3260
"{
3361
"id": "chatcmpl-9taiMDrRVRIkk1Xg1yE82UjnYuZjt",

tests/lib/parser.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,50 @@ describe('.parse()', () => {
444444
"type": "object",
445445
}
446446
`);
447+
448+
const completion = await makeSnapshotRequest(
449+
(openai) =>
450+
openai.beta.chat.completions.parse({
451+
model: 'gpt-4o-2024-08-06',
452+
messages: [
453+
{
454+
role: 'system',
455+
content: 'You are a helpful assistant.',
456+
},
457+
{
458+
role: 'user',
459+
content:
460+
'jane doe, born nov 16, engineer at openai, jane@openai.com. john smith, born march 1, enigneer at openai, john@openai.com',
461+
},
462+
],
463+
response_format: zodResponseFormat(contactPersonSchema, 'contactPerson'),
464+
}),
465+
2,
466+
);
467+
468+
expect(completion.choices[0]?.message).toMatchInlineSnapshot(`
469+
{
470+
"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"}}",
471+
"parsed": {
472+
"person1": {
473+
"description": "Engineer at OpenAI. Email: jane@openai.com",
474+
"name": "Jane Doe",
475+
"phone_number": "+1234567890",
476+
"roles": [
477+
"other",
478+
],
479+
},
480+
"person2": {
481+
"differentField": "Engineer at OpenAI. Email: john@openai.com",
482+
"name": "John Smith",
483+
"phone_number": "+0987654321",
484+
},
485+
},
486+
"refusal": null,
487+
"role": "assistant",
488+
"tool_calls": [],
489+
}
490+
`);
447491
});
448492
});
449493
});

tests/utils/mock-snapshots.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import { RequestInfo } from 'openai/_shims/auto/types';
55
import { mockFetch } from './mock-fetch';
66
import { Readable } from 'stream';
77

8-
export async function makeSnapshotRequest<T>(requestFn: (client: OpenAI) => Promise<T>): Promise<T> {
8+
export async function makeSnapshotRequest<T>(
9+
requestFn: (client: OpenAI) => Promise<T>,
10+
snapshotIndex = 1,
11+
): Promise<T> {
912
if (process.env['UPDATE_API_SNAPSHOTS'] === '1') {
1013
var capturedResponseContent: string | null = null;
1114

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

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

0 commit comments

Comments
 (0)