Skip to content

Commit 6e1e1af

Browse files
test(api): cover qwen-code degenerate stream shapes for full branch coverage
Codecov's patch report on the previous head flagged 5 partial-coverage lines in qwen-code.ts (lines 328, 338, 344, 398, 399): the defensive branches of the createMessage stream loop — a chunk with no choice (choices[0] ?? fallback), a delta that repeats the previous full content (empty after trimming), a think block that starts the text (empty leading split segment), and a zeroed usage payload (prompt_tokens || 0, completion_tokens || 0). Add one focused degenerate-stream-shapes test to qwen-code-native-tools spec covering all five branches in a single stream. Branch-level lcov cross-reference on the PR's added lines now reports zero partial-coverage added lines in both provider files, and line-level changed-line coverage stays 100% (lm-studio.ts 35/35, qwen-code.ts 65/65 executable added lines).
1 parent abd9827 commit 6e1e1af

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

‎src/api/providers/__tests__/qwen-code-native-tools.spec.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,34 @@ describe("QwenCodeHandler Native Tools", () => {
620620
expect(chunks).not.toContainEqual(expect.objectContaining({ type: "text", text: "b" }))
621621
})
622622

623+
it("should tolerate degenerate stream shapes (empty choice, repeated content, zero usage)", async () => {
624+
// Changed-line coverage: exercises the defensive branches of the stream
625+
// loop — a chunk with no choice, a delta that repeats the previous full
626+
// content (empty after trimming), a think block that starts the text so the
627+
// split yields an empty leading segment, and a usage payload of zeros.
628+
mockCreate.mockImplementationOnce(() =>
629+
asyncStreamFrom([
630+
{ choices: [] },
631+
{ choices: [{ delta: { content: "hi" }, index: 0 }] },
632+
{ choices: [{ delta: { content: "hi" }, index: 0 }] },
633+
{ choices: [{ delta: { content: "<think>thought</think>out" }, index: 0 }] },
634+
{
635+
choices: [{ delta: {}, index: 0, finish_reason: "stop" }],
636+
usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
637+
},
638+
]),
639+
)
640+
641+
const stream = handler.createMessage("test prompt", [], { taskId: "t1" })
642+
const chunks = await collectStream(stream)
643+
644+
const hiChunks = chunks.filter((chunk) => chunk.type === "text" && chunk.text === "hi")
645+
expect(hiChunks).toHaveLength(1) // the repeated content chunk yields nothing
646+
expect(chunks).toContainEqual({ type: "reasoning", text: "thought" })
647+
expect(chunks).toContainEqual({ type: "text", text: "out" })
648+
expect(chunks).toContainEqual({ type: "usage", inputTokens: 0, outputTokens: 0 })
649+
})
650+
623651
it("should not retry after 401 when the abort signal fires during the refresh", async () => {
624652
const external = new AbortController()
625653
const fetchMock = vi.fn().mockImplementation(async () => {

0 commit comments

Comments
 (0)