Elffuss BraidHub

kikocis/continue public

main · 2.3 KiBRawPermalink
1import { Buffer } from "node:buffer";
2import { describe, expect, it } from "vitest";
3
4// Tests the boundary chunking patch to node fetch made in node-fetch-patch.js (see that file for details).
5describe("Chunked Transfer Encoding Patch Logic", () => {
6 // The LAST_CHUNK marker "0\r\n\r\n"
7 const LAST_CHUNK = Buffer.from("0\r\n\r\n");
8
9 it("should detect complete marker at the end (normal case)", () => {
10 const normalCase = Buffer.from("data data data 0\r\n\r\n");
11 const result = Buffer.compare(normalCase.slice(-5), LAST_CHUNK) === 0;
12 expect(result).toBe(true);
13 });
14
15 it("should handle chunks split across original pattern (0\\r\\n | \\r\\n)", () => {
16 const previousChunk = Buffer.from("data data 0\r\n");
17 const lastChunk = Buffer.from("\r\n");
18
19 const result =
20 Buffer.compare(previousChunk.slice(-3), LAST_CHUNK.slice(0, 3)) === 0 &&
21 Buffer.compare(lastChunk.slice(-2), LAST_CHUNK.slice(3)) === 0;
22
23 expect(result).toBe(true);
24 });
25
26 it("should handle problematic case from issue #1576 with new patch", () => {
27 // The problematic case: data ends with "0\r\n0\r" and the next chunk is "\n\r\n"
28 const previousChunk = Buffer.from([
29 ...Array(10).fill(42),
30 48,
31 13,
32 10,
33 48,
34 13,
35 ]); // some data + "0\r\n0\r"
36 const lastChunk = Buffer.from([10, 13, 10]); // "\n\r\n"
37
38 // Check 1: Direct check for complete marker (should fail)
39 const check1 = Buffer.compare(lastChunk.slice(-5), LAST_CHUNK) === 0;
40 expect(check1).toBe(false);
41
42 // Check 2: Original split pattern check (should fail)
43 const check2 =
44 Buffer.compare(previousChunk.slice(-3), LAST_CHUNK.slice(0, 3)) === 0 &&
45 Buffer.compare(lastChunk.slice(-2), LAST_CHUNK.slice(3)) === 0;
46 expect(check2).toBe(false);
47
48 // Check 3: New patch check (should pass)
49 const lastChunkLength = lastChunk.length;
50 const newPatchCheck =
51 lastChunkLength < 5 &&
52 Buffer.compare(
53 Buffer.from([...previousChunk.slice(-5), ...lastChunk]).slice(-5),
54 LAST_CHUNK,
55 ) === 0;
56 expect(newPatchCheck).toBe(true);
57
58 // Verification of the combined buffer
59 const combinedBuffer = Buffer.from([
60 ...previousChunk.slice(-5),
61 ...lastChunk,
62 ]);
63 const combinedResult =
64 Buffer.compare(combinedBuffer.slice(-5), LAST_CHUNK) === 0;
65 expect(combinedResult).toBe(true);
66 });
67});