63e42047e3
The hasValuableContent() function in streamHelpers.ts returned undefined instead of explicit false when checking empty delta chunks. This caused JavaScript type coercion issues where undefined !== '' evaluated to true, passing empty chunks through to clients. Fix: Replace implicit returns with explicit boolean returns using typeof checks and length comparisons for all content fields (content, reasoning_content, tool_calls, text, thinking, partial_json). Test: Added unit tests covering OpenAI, Claude, and Gemini format edge cases. Co-authored-by: oyi77 <oyi77@users.noreply.github.com>
73 lines
2.8 KiB
JavaScript
73 lines
2.8 KiB
JavaScript
import { describe, it } from "node:test";
|
|
import assert from "node:assert";
|
|
import { hasValuableContent } from "../../open-sse/utils/streamHelpers.ts";
|
|
import { FORMATS } from "../../open-sse/translator/formats.ts";
|
|
|
|
describe("hasValuableContent", () => {
|
|
describe("OpenAI format", () => {
|
|
it("returns true for content with text", () => {
|
|
const chunk = { choices: [{ delta: { content: "Hello" } }] };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.OPENAI), true);
|
|
});
|
|
|
|
it("returns false for empty delta", () => {
|
|
const chunk = { choices: [{ delta: {} }] };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.OPENAI), false);
|
|
});
|
|
|
|
it("returns false for delta with empty string content", () => {
|
|
const chunk = { choices: [{ delta: { content: "" } }] };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.OPENAI), false);
|
|
});
|
|
|
|
it("returns true for reasoning_content", () => {
|
|
const chunk = { choices: [{ delta: { reasoning_content: "thinking" } }] };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.OPENAI), true);
|
|
});
|
|
|
|
it("returns true for finish_reason", () => {
|
|
const chunk = { choices: [{ delta: {}, finish_reason: "stop" }] };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.OPENAI), true);
|
|
});
|
|
|
|
it("returns true for role delta", () => {
|
|
const chunk = { choices: [{ delta: { role: "assistant" } }] };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.OPENAI), true);
|
|
});
|
|
});
|
|
|
|
describe("Claude format", () => {
|
|
it("returns true for content_block_delta with text", () => {
|
|
const chunk = { type: "content_block_delta", delta: { text: "Hello" } };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.CLAUDE), true);
|
|
});
|
|
|
|
it("returns false for empty content_block_delta", () => {
|
|
const chunk = { type: "content_block_delta", delta: {} };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.CLAUDE), false);
|
|
});
|
|
|
|
it("returns true for thinking blocks", () => {
|
|
const chunk = { type: "content_block_delta", delta: { thinking: "reasoning" } };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.CLAUDE), true);
|
|
});
|
|
});
|
|
|
|
describe("Gemini format", () => {
|
|
it("returns true for content with text", () => {
|
|
const chunk = { candidates: [{ content: { parts: [{ text: "Hello" }] } }] };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.GEMINI), true);
|
|
});
|
|
|
|
it("returns false for empty parts", () => {
|
|
const chunk = { candidates: [{ content: { parts: [] } }] };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.GEMINI), false);
|
|
});
|
|
|
|
it("returns true for finishReason", () => {
|
|
const chunk = { candidates: [{ finishReason: "STOP" }] };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.GEMINI), true);
|
|
});
|
|
});
|
|
});
|