|
7 | 7 | isErrorResponse, |
8 | 8 | buildTextChunks, |
9 | 9 | buildToolCallChunks, |
| 10 | + buildTextCompletion, |
| 11 | + buildToolCallCompletion, |
10 | 12 | } from "../helpers.js"; |
11 | 13 |
|
12 | 14 | describe("generateId", () => { |
@@ -261,3 +263,72 @@ describe("buildToolCallChunks", () => { |
261 | 263 | expect(argChunks[0].choices[0].delta.tool_calls![0].function!.arguments).toBe('{"x":1}'); |
262 | 264 | }); |
263 | 265 | }); |
| 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