Skip to content

Commit ec10916

Browse files
committed
Add tests for Responses API and non-streaming completions
New responses.test.ts covers input conversion (responsesInputToMessages, responsesToCompletionRequest), streaming text/tool-call responses, non-streaming responses, error handling, journal recording, and CORS for POST /v1/responses. Also adds tests for buildTextCompletion and buildToolCallCompletion in helpers.test.ts, and non-streaming stream=false paths in server.test.ts. (42 new tests, 259 total)
1 parent beb6c2b commit ec10916

3 files changed

Lines changed: 800 additions & 0 deletions

File tree

‎src/__tests__/helpers.test.ts‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
isErrorResponse,
88
buildTextChunks,
99
buildToolCallChunks,
10+
buildTextCompletion,
11+
buildToolCallCompletion,
1012
} from "../helpers.js";
1113

1214
describe("generateId", () => {
@@ -261,3 +263,72 @@ describe("buildToolCallChunks", () => {
261263
expect(argChunks[0].choices[0].delta.tool_calls![0].function!.arguments).toBe('{"x":1}');
262264
});
263265
});
266+
267+
describe("buildTextCompletion", () => {
268+
it("returns a valid chat.completion object", () => {
269+
const result = buildTextCompletion("Hello!", "gpt-4");
270+
expect(result.object).toBe("chat.completion");
271+
expect(result.model).toBe("gpt-4");
272+
expect(result.id).toMatch(/^chatcmpl-/);
273+
expect(result.created).toBeGreaterThan(0);
274+
});
275+
276+
it("has a single choice with assistant role and content", () => {
277+
const result = buildTextCompletion("Hello!", "gpt-4");
278+
expect(result.choices).toHaveLength(1);
279+
expect(result.choices[0].index).toBe(0);
280+
expect(result.choices[0].message.role).toBe("assistant");
281+
expect(result.choices[0].message.content).toBe("Hello!");
282+
expect(result.choices[0].finish_reason).toBe("stop");
283+
});
284+
285+
it("includes usage object", () => {
286+
const result = buildTextCompletion("Hello!", "gpt-4");
287+
expect(result.usage).toEqual({
288+
prompt_tokens: 0,
289+
completion_tokens: 0,
290+
total_tokens: 0,
291+
});
292+
});
293+
});
294+
295+
describe("buildToolCallCompletion", () => {
296+
it("returns a valid chat.completion with tool_calls", () => {
297+
const result = buildToolCallCompletion(
298+
[{ name: "get_weather", arguments: '{"city":"NYC"}' }],
299+
"gpt-4",
300+
);
301+
expect(result.object).toBe("chat.completion");
302+
expect(result.model).toBe("gpt-4");
303+
expect(result.choices).toHaveLength(1);
304+
expect(result.choices[0].finish_reason).toBe("tool_calls");
305+
expect(result.choices[0].message.content).toBeNull();
306+
});
307+
308+
it("maps tool calls with correct structure", () => {
309+
const result = buildToolCallCompletion(
310+
[
311+
{ name: "fn_a", arguments: '{"x":1}' },
312+
{ name: "fn_b", arguments: '{"y":2}', id: "call_custom" },
313+
],
314+
"gpt-4",
315+
);
316+
const tcs = result.choices[0].message.tool_calls!;
317+
expect(tcs).toHaveLength(2);
318+
319+
expect(tcs[0].type).toBe("function");
320+
expect(tcs[0].function.name).toBe("fn_a");
321+
expect(tcs[0].function.arguments).toBe('{"x":1}');
322+
expect(tcs[0].id).toMatch(/^call_/);
323+
324+
expect(tcs[1].id).toBe("call_custom");
325+
expect(tcs[1].function.name).toBe("fn_b");
326+
});
327+
328+
it("generates tool call IDs when not provided", () => {
329+
const result = buildToolCallCompletion([{ name: "fn", arguments: "{}" }], "gpt-4");
330+
const tc = result.choices[0].message.tool_calls![0];
331+
expect(tc.id).toMatch(/^call_/);
332+
expect(tc.id.length).toBeGreaterThan(5);
333+
});
334+
});

0 commit comments

Comments
 (0)