From 04c1590a64127c43898c3c88bcbdd624d54008f6 Mon Sep 17 00:00:00 2001 From: Robert Craigie Date: Thu, 8 Aug 2024 15:28:51 +0100 Subject: [PATCH] chore(tests): add more API request tests --- tests/lib/__snapshots__/parser.test.ts.snap | 28 +++++++++++++ tests/lib/parser.test.ts | 44 +++++++++++++++++++++ tests/utils/mock-snapshots.ts | 7 +++- 3 files changed, 77 insertions(+), 2 deletions(-) diff --git a/tests/lib/__snapshots__/parser.test.ts.snap b/tests/lib/__snapshots__/parser.test.ts.snap index e6f2799af..715c268ff 100644 --- a/tests/lib/__snapshots__/parser.test.ts.snap +++ b/tests/lib/__snapshots__/parser.test.ts.snap @@ -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", diff --git a/tests/lib/parser.test.ts b/tests/lib/parser.test.ts index 9668cf7d0..3fb3c948a 100644 --- a/tests/lib/parser.test.ts +++ b/tests/lib/parser.test.ts @@ -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, jane@openai.com. john smith, born march 1, enigneer at openai, john@openai.com', + }, + ], + 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: jane@openai.com"},"person2":{"name":"John Smith","phone_number":"+0987654321","differentField":"Engineer at OpenAI. Email: john@openai.com"}}", + "parsed": { + "person1": { + "description": "Engineer at OpenAI. Email: jane@openai.com", + "name": "Jane Doe", + "phone_number": "+1234567890", + "roles": [ + "other", + ], + }, + "person2": { + "differentField": "Engineer at OpenAI. Email: john@openai.com", + "name": "John Smith", + "phone_number": "+0987654321", + }, + }, + "refusal": null, + "role": "assistant", + "tool_calls": [], + } + `); }); }); }); diff --git a/tests/utils/mock-snapshots.ts b/tests/utils/mock-snapshots.ts index 2bf09eda7..317bf6b0f 100644 --- a/tests/utils/mock-snapshots.ts +++ b/tests/utils/mock-snapshots.ts @@ -5,7 +5,10 @@ import { RequestInfo } from 'openai/_shims/auto/types'; import { mockFetch } from './mock-fetch'; import { Readable } from 'stream'; -export async function makeSnapshotRequest(requestFn: (client: OpenAI) => Promise): Promise { +export async function makeSnapshotRequest( + requestFn: (client: OpenAI) => Promise, + snapshotIndex = 1, +): Promise { if (process.env['UPDATE_API_SNAPSHOTS'] === '1') { var capturedResponseContent: string | null = null; @@ -27,7 +30,7 @@ export async function makeSnapshotRequest(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).delete(qualifiedSnapshotName);