|
| 1 | +import { LINE_ENDINGS, SplitBuffer } from "../../../src/common/split-stream"; |
| 2 | + |
| 3 | +interface Chunk { |
| 4 | + chunk: string; |
| 5 | + lines: string[]; |
| 6 | +} |
| 7 | + |
| 8 | +function checkLines( |
| 9 | + buffer: SplitBuffer, |
| 10 | + expectedLinesForChunk: string[], |
| 11 | + chunkIndex: number | "end", |
| 12 | +): void { |
| 13 | + expectedLinesForChunk.forEach((expectedLine, lineIndex) => { |
| 14 | + const line = buffer.getNextLine(); |
| 15 | + const location = `[chunk ${chunkIndex}, line ${lineIndex}]: `; |
| 16 | + expect(location + line).toEqual(location + expectedLine); |
| 17 | + }); |
| 18 | + expect(buffer.getNextLine()).toBeUndefined(); |
| 19 | +} |
| 20 | + |
| 21 | +function testSplitBuffer(chunks: Chunk[], endLines: string[]): void { |
| 22 | + const buffer = new SplitBuffer(LINE_ENDINGS); |
| 23 | + chunks.forEach((chunk, chunkIndex) => { |
| 24 | + buffer.addChunk(Buffer.from(chunk.chunk, "utf-8")); |
| 25 | + checkLines(buffer, chunk.lines, chunkIndex); |
| 26 | + }); |
| 27 | + buffer.end(); |
| 28 | + checkLines(buffer, endLines, "end"); |
| 29 | +} |
| 30 | + |
| 31 | +describe("split buffer", () => { |
| 32 | + it("should handle a one-chunk string with no terminator", async () => { |
| 33 | + // Won't return the line until we call `end()`. |
| 34 | + testSplitBuffer([{ chunk: "some text", lines: [] }], ["some text"]); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should handle a one-chunk string with a one-byte terminator", async () => { |
| 38 | + // Won't return the line until we call `end()` because the actual terminator is shorter than the |
| 39 | + // longest terminator. |
| 40 | + testSplitBuffer([{ chunk: "some text\n", lines: [] }], ["some text"]); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should handle a one-chunk string with a two-byte terminator", async () => { |
| 44 | + testSplitBuffer([{ chunk: "some text\r\n", lines: ["some text"] }], []); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should handle a multi-chunk string with terminators at the end of each chunk", async () => { |
| 48 | + testSplitBuffer( |
| 49 | + [ |
| 50 | + { chunk: "first line\n", lines: [] }, // Waiting for second potential terminator byte |
| 51 | + { chunk: "second line\r", lines: ["first line"] }, // Waiting for second potential terminator byte |
| 52 | + { chunk: "third line\r\n", lines: ["second line", "third line"] }, // No wait, because we're at the end |
| 53 | + ], |
| 54 | + [], |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it("should handle a multi-chunk string with terminators at random offsets", async () => { |
| 59 | + testSplitBuffer( |
| 60 | + [ |
| 61 | + { chunk: "first line\nsecond", lines: ["first line"] }, |
| 62 | + { |
| 63 | + chunk: " line\rthird line", |
| 64 | + lines: ["second line"], |
| 65 | + }, |
| 66 | + { chunk: "\r\n", lines: ["third line"] }, |
| 67 | + ], |
| 68 | + [], |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should handle a terminator split between chunks", async () => { |
| 73 | + testSplitBuffer( |
| 74 | + [ |
| 75 | + { chunk: "first line\r", lines: [] }, |
| 76 | + { |
| 77 | + chunk: "\nsecond line", |
| 78 | + lines: ["first line"], |
| 79 | + }, |
| 80 | + ], |
| 81 | + ["second line"], |
| 82 | + ); |
| 83 | + }); |
| 84 | +}); |
0 commit comments