From 29746c7c1e82b00041b25e11541908d36959fcfc Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 17 Oct 2026 17:56:33 +0800 Subject: [PATCH] qoder: test that a BOM before a stream chunk's body doesn't drop its text #185 trims a chunk's body before parsing it in events(), and nothing tested it. The envelope carries the BOM JSON-escaped, so the response's own decoding doesn't strip it. Run: bun test packages/qoder: 138 pass, 0 fail. With JSON.parse(inner) back, the new test goes red (1 pass, 1 fail in stream.test.mjs). --- packages/qoder/stream.test.mjs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/qoder/stream.test.mjs b/packages/qoder/stream.test.mjs index 8146ccb..3f602c1 100644 --- a/packages/qoder/stream.test.mjs +++ b/packages/qoder/stream.test.mjs @@ -31,3 +31,14 @@ test("the usage after the finish is read", async () => { }, }) }) + +// #185: a chunk's body led by a BOM (JSON-escaped in the envelope, so the +// response's own decoding doesn't strip it) is still read, not skipped. +test("a BOM before a chunk's body doesn't drop its text", async () => { + const chunk = (content, fr) => JSON.stringify({ choices: [{ delta: { content }, ...(fr ? { finish_reason: fr } : {}), index: 0 }], object: "chat.completion.chunk" }) + const sse = line("" + chunk("Hi")) + line("" + chunk("", "stop")) + line("[DONE]") + const out = [] + for await (const e of _internal.events(new Response(sse).body)) out.push(e) + expect(out.map((e) => e.text).filter(Boolean).join("")).toBe("Hi") + expect(out.at(-1)?.stop).toBe("stop") +})