Skip to content

Commit 8117721

Browse files
committed
Add test to make sure newlines in batch item payloads don't break our newline scanner
1 parent 3a656b7 commit 8117721

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

apps/webapp/test/engine/streamBatchItems.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,25 @@ describe("createNdjsonParserStream", () => {
643643
expect(results).toEqual([{ id: 1 }, { id: 2 }]);
644644
});
645645

646+
it("should handle escaped newlines in JSON string values", async () => {
647+
// JSON.stringify escapes newlines as \n (two chars: backslash + n),
648+
// so they don't break NDJSON line boundaries. This is the normal case
649+
// when the SDK serializes payloads containing newlines.
650+
const item1 = JSON.stringify({ payload: "line1\nline2\nline3" });
651+
const item2 = JSON.stringify({ payload: "no newlines" });
652+
const ndjson = item1 + "\n" + item2 + "\n";
653+
const encoder = new TextEncoder();
654+
const stream = chunksToStream([encoder.encode(ndjson)]);
655+
656+
const parser = createNdjsonParserStream(1024);
657+
const results = await collectStream(stream.pipeThrough(parser));
658+
659+
expect(results).toEqual([
660+
{ payload: "line1\nline2\nline3" },
661+
{ payload: "no newlines" },
662+
]);
663+
});
664+
646665
it("should skip empty lines", async () => {
647666
const ndjson = '{"a":1}\n\n{"b":2}\n \n{"c":3}\n';
648667
const encoder = new TextEncoder();

0 commit comments

Comments
 (0)