|
| 1 | +import { test, expect } from "bun:test" |
| 2 | +import { coerceNumericToolCallIds, transformSSEStream } from "../../src/util/coerce-tool-call-ids" |
| 3 | + |
| 4 | +test("coerceNumericToolCallIds: coerces numeric id in tool_calls", () => { |
| 5 | + const obj: Record<string, unknown> = { tool_calls: [{ id: 123, function: { name: "read" } }] } |
| 6 | + coerceNumericToolCallIds(obj) |
| 7 | + expect((obj.tool_calls as Record<string, unknown>[])[0].id).toBe("123") |
| 8 | +}) |
| 9 | + |
| 10 | +test("coerceNumericToolCallIds: coerces numeric id in delta.tool_calls", () => { |
| 11 | + const obj: Record<string, unknown> = { delta: { tool_calls: [{ id: 456, function: { name: "write" } }] } } |
| 12 | + coerceNumericToolCallIds(obj) |
| 13 | + const delta = obj.delta as Record<string, unknown> |
| 14 | + expect((delta.tool_calls as Record<string, unknown>[])[0].id).toBe("456") |
| 15 | +}) |
| 16 | + |
| 17 | +test("coerceNumericToolCallIds: leaves string ids unchanged", () => { |
| 18 | + const obj: Record<string, unknown> = { tool_calls: [{ id: "call_abc123", function: { name: "read" } }] } |
| 19 | + coerceNumericToolCallIds(obj) |
| 20 | + expect((obj.tool_calls as Record<string, unknown>[])[0].id).toBe("call_abc123") |
| 21 | +}) |
| 22 | + |
| 23 | +test("coerceNumericToolCallIds: handles nested objects", () => { |
| 24 | + const obj: Record<string, unknown> = { choices: [{ message: { tool_calls: [{ id: 789 }] } }] } |
| 25 | + coerceNumericToolCallIds(obj) |
| 26 | + const choice = (obj.choices as Record<string, unknown>[])[0] as Record<string, unknown> |
| 27 | + const message = choice.message as Record<string, unknown> |
| 28 | + expect((message.tool_calls as Record<string, unknown>[])[0].id).toBe("789") |
| 29 | +}) |
| 30 | + |
| 31 | +test("coerceNumericToolCallIds: handles null and undefined inputs", () => { |
| 32 | + expect(() => coerceNumericToolCallIds(null)).not.toThrow() |
| 33 | + expect(() => coerceNumericToolCallIds(undefined)).not.toThrow() |
| 34 | +}) |
| 35 | + |
| 36 | +test("coerceNumericToolCallIds: handles empty objects", () => { |
| 37 | + expect(() => coerceNumericToolCallIds({})).not.toThrow() |
| 38 | +}) |
| 39 | + |
| 40 | +test("coerceNumericToolCallIds: handles arrays of tool calls", () => { |
| 41 | + const obj: Record<string, unknown> = { tool_calls: [{ id: 1 }, { id: 2 }, { id: 3 }] } |
| 42 | + coerceNumericToolCallIds(obj) |
| 43 | + const tcs = obj.tool_calls as Record<string, unknown>[] |
| 44 | + expect(tcs[0].id).toBe("1") |
| 45 | + expect(tcs[1].id).toBe("2") |
| 46 | + expect(tcs[2].id).toBe("3") |
| 47 | +}) |
| 48 | + |
| 49 | +test("coerceNumericToolCallIds: mixed numeric and string ids", () => { |
| 50 | + const obj: Record<string, unknown> = { tool_calls: [{ id: 42 }, { id: "call_existing" }] } |
| 51 | + coerceNumericToolCallIds(obj) |
| 52 | + const tcs = obj.tool_calls as Record<string, unknown>[] |
| 53 | + expect(tcs[0].id).toBe("42") |
| 54 | + expect(tcs[1].id).toBe("call_existing") |
| 55 | +}) |
| 56 | + |
| 57 | +test("coerceNumericToolCallIds: handles tool_calls with non-object entries", () => { |
| 58 | + const obj = { tool_calls: [null, undefined, "string", 42] } |
| 59 | + expect(() => coerceNumericToolCallIds(obj)).not.toThrow() |
| 60 | +}) |
| 61 | + |
| 62 | +test("coerceNumericToolCallIds: handles primitive inputs", () => { |
| 63 | + expect(() => coerceNumericToolCallIds(42)).not.toThrow() |
| 64 | + expect(() => coerceNumericToolCallIds("string")).not.toThrow() |
| 65 | + expect(() => coerceNumericToolCallIds(true)).not.toThrow() |
| 66 | +}) |
| 67 | + |
| 68 | +function toStream(chunks: string[]): ReadableStream<Uint8Array> { |
| 69 | + const encoder = new TextEncoder() |
| 70 | + return new ReadableStream({ |
| 71 | + start(controller) { |
| 72 | + for (const chunk of chunks) { |
| 73 | + controller.enqueue(encoder.encode(chunk)) |
| 74 | + } |
| 75 | + controller.close() |
| 76 | + }, |
| 77 | + }) |
| 78 | +} |
| 79 | + |
| 80 | +async function collect(stream: ReadableStream<Uint8Array>): Promise<string> { |
| 81 | + const reader = stream.getReader() |
| 82 | + const decoder = new TextDecoder() |
| 83 | + let result = "" |
| 84 | + while (true) { |
| 85 | + const { done, value } = await reader.read() |
| 86 | + if (done) break |
| 87 | + result += decoder.decode(value, { stream: true }) |
| 88 | + } |
| 89 | + return result |
| 90 | +} |
| 91 | + |
| 92 | +test("transformSSEStream: coerces numeric tool call IDs in SSE data", async () => { |
| 93 | + const input = toStream(['data: {"tool_calls":[{"id":123}]}\n\n']) |
| 94 | + const output = await collect(transformSSEStream(input)) |
| 95 | + const parsed = JSON.parse(output.trim().slice(6)) |
| 96 | + expect(parsed.tool_calls[0].id).toBe("123") |
| 97 | +}) |
| 98 | + |
| 99 | +test("transformSSEStream: passes through [DONE] unchanged", async () => { |
| 100 | + const input = toStream(["data: [DONE]\n\n"]) |
| 101 | + const output = await collect(transformSSEStream(input)) |
| 102 | + expect(output).toBe("data: [DONE]\n\n") |
| 103 | +}) |
| 104 | + |
| 105 | +test("transformSSEStream: passes through invalid JSON unchanged", async () => { |
| 106 | + const input = toStream(["data: {not valid json}\n\n"]) |
| 107 | + const output = await collect(transformSSEStream(input)) |
| 108 | + expect(output).toBe("data: {not valid json}\n\n") |
| 109 | +}) |
| 110 | + |
| 111 | +test("transformSSEStream: passes through non-data lines unchanged", async () => { |
| 112 | + const input = toStream(["event: ping\n\n"]) |
| 113 | + const output = await collect(transformSSEStream(input)) |
| 114 | + expect(output).toBe("event: ping\n\n") |
| 115 | +}) |
| 116 | + |
| 117 | +test("transformSSEStream: handles multiple SSE events in one chunk", async () => { |
| 118 | + const input = toStream([ |
| 119 | + 'data: {"tool_calls":[{"id":1}]}\ndata: {"tool_calls":[{"id":2}]}\n\n', |
| 120 | + ]) |
| 121 | + const output = await collect(transformSSEStream(input)) |
| 122 | + const lines = output.split("\n").filter((l) => l.startsWith("data: ")) |
| 123 | + const first = JSON.parse(lines[0].slice(6)) |
| 124 | + const second = JSON.parse(lines[1].slice(6)) |
| 125 | + expect(first.tool_calls[0].id).toBe("1") |
| 126 | + expect(second.tool_calls[0].id).toBe("2") |
| 127 | +}) |
| 128 | + |
| 129 | +test("transformSSEStream: coerces numeric IDs in delta.tool_calls", async () => { |
| 130 | + const input = toStream(['data: {"delta":{"tool_calls":[{"id":999}]}}\n\n']) |
| 131 | + const output = await collect(transformSSEStream(input)) |
| 132 | + const parsed = JSON.parse(output.trim().slice(6)) |
| 133 | + expect(parsed.delta.tool_calls[0].id).toBe("999") |
| 134 | +}) |
0 commit comments