Elffuss BraidHub

kikocis/continue public

main · 4.7 KiBRawPermalink
1import path from "path";
2
3import { Chunk, ChunkWithoutID } from "../../index.js";
4import { cleanupAsyncEncoders } from "../../llm/countTokens.js";
5import { chunkDocument, chunkDocumentWithoutId, shouldChunk } from "./chunk";
6
7describe("shouldChunk", () => {
8 test("should chunk a typescript file", () => {
9 const filePath = path.join("directory", "file.ts");
10 const fileContent = generateString(10000);
11 expect(shouldChunk(filePath, fileContent)).toBe(true);
12 });
13
14 test("should not chunk a large typescript file", () => {
15 const filePath = path.join("directory", "file.ts");
16 const fileContent = generateString(1500000);
17 expect(shouldChunk(filePath, fileContent)).toBe(false);
18 });
19
20 test("should not chunk an empty file", () => {
21 const filePath = path.join("directory", "file.ts");
22 const fileContent = generateString(0);
23 expect(shouldChunk(filePath, fileContent)).toBe(false);
24 });
25
26 test("should not chunk a file without extension", () => {
27 const filePath = path.join("directory", "with.dot", "filename");
28 const fileContent = generateString(10000);
29 expect(shouldChunk(filePath, fileContent)).toBe(false);
30 });
31});
32
33describe("chunkDocument", () => {
34 afterAll(async () => {
35 // Clean up the global async encoders to prevent Jest from hanging
36 await cleanupAsyncEncoders();
37 });
38
39 test("should return multiple chunks for large content with small maxChunkSize", async () => {
40 const filepath = "/test/file.txt";
41 const maxChunkSize = 10; // small limit
42 const digest = "test-digest";
43
44 // Create content with multiple short lines - each line should fit within maxChunkSize
45 const lines = ["short line", "another line", "third line"];
46 const contents = lines.join("\n");
47
48 const chunks: Chunk[] = [];
49 for await (const chunk of chunkDocument({
50 filepath,
51 contents,
52 maxChunkSize,
53 digest,
54 })) {
55 chunks.push(chunk);
56 }
57
58 // Verify chunks have valid content
59 chunks.forEach((chunk) => {
60 expect(chunk.content.length).toBeGreaterThan(0);
61 });
62 });
63
64 test("should filter out chunks that exceed maxChunkSize in tokens", async () => {
65 const filepath = "/test/large.txt";
66 const maxChunkSize = 10; // small limit
67 const digest = "test-digest-2";
68
69 const contents =
70 "This is a much longer line with many words that will likely exceed the maxChunkSize token limit and should be filtered out by the chunkDocument function. This is a much longer line with many words that will likely exceed the maxChunkSize token limit and should be filtered out by the chunkDocument function.";
71
72 const chunks: Chunk[] = [];
73 for await (const chunk of chunkDocument({
74 filepath,
75 contents,
76 maxChunkSize,
77 digest,
78 })) {
79 chunks.push(chunk);
80 }
81
82 // Verify that no chunks were created since the content exceeds maxChunkSize
83 chunks.forEach((chunk) => {
84 expect(chunk.content.length).toBe(0);
85 });
86 });
87});
88
89describe("chunkDocumentWithoutId", () => {
90 afterAll(async () => {
91 await cleanupAsyncEncoders();
92 });
93
94 test("should return no chunks for empty content", async () => {
95 const contents = "";
96 const chunks: ChunkWithoutID[] = [];
97 for await (const chunk of chunkDocumentWithoutId(
98 "file.txt",
99 contents,
100 100,
101 )) {
102 chunks.push(chunk);
103 }
104 expect(chunks).toHaveLength(0);
105 });
106
107 test("should chunk text files", async () => {
108 const contents = "Line 1\nLine 2\nLine 3";
109 const chunks: ChunkWithoutID[] = [];
110 for await (const chunk of chunkDocumentWithoutId(
111 "file.txt",
112 contents,
113 100,
114 )) {
115 chunks.push(chunk);
116 }
117 expect(chunks.length).toBeGreaterThan(0);
118 expect(chunks[0].content).toContain("Line 1");
119 });
120
121 test("should chunk for code files", async () => {
122 const contents = "function test() {\n return 42;\n}";
123 const chunks: ChunkWithoutID[] = [];
124 for await (const chunk of chunkDocumentWithoutId(
125 "file.js",
126 contents,
127 100,
128 )) {
129 chunks.push(chunk);
130 }
131 expect(chunks.length).toBeGreaterThan(0);
132 expect(chunks[0].content).toContain("function test");
133 });
134
135 test("should chunk large content", async () => {
136 const contents = Array(100).fill("short line").join("\n"); // Large content with many lines
137 const maxChunkSize = 50;
138 const chunks: ChunkWithoutID[] = [];
139 for await (const chunk of chunkDocumentWithoutId(
140 "file.txt",
141 contents,
142 maxChunkSize,
143 )) {
144 chunks.push(chunk);
145 }
146 expect(chunks.length).toBeGreaterThan(1); // Should create multiple chunks
147 chunks.forEach((chunk) => {
148 expect(chunk.content.length).toBeGreaterThan(0); // Each chunk should have content
149 });
150 });
151});
152
153function generateString(length: number) {
154 return "a".repeat(length);
155}