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") +})