Elffuss BraidHub

kikocis/continue public

b68735bd51e5 · 3.6 KiBRawPermalink
1import { ChunkWithoutID } from "../..";
2import { countTokensAsync } from "../../llm/countTokens";
3import { codeChunker } from "./code";
4
5async function genToArr<T>(generator: AsyncGenerator<T>): Promise<T[]> {
6 const result: T[] = [];
7 for await (const item of generator) {
8 result.push(item);
9 }
10 return result;
11}
12
13async function genToStrs(
14 generator: AsyncGenerator<ChunkWithoutID>,
15): Promise<string[]> {
16 return (await genToArr(generator)).map((chunk) => chunk.content);
17}
18
19describe.skip("codeChunker", () => {
20 test("should return empty array if file empty", async () => {
21 const chunks = await genToStrs(codeChunker("test.ts", "", 100));
22 expect(chunks).toEqual([]);
23 });
24
25 test("should include entire file if smaller than max chunk size", async () => {
26 const chunks = await genToStrs(codeChunker("test.ts", "abc", 100));
27 expect(chunks).toEqual(["abc"]);
28 });
29
30 test("should capture small class and function from large python file", async () => {
31 const extraLine = "# This is a comment";
32 const myClass = "class MyClass:\n def __init__(self):\n pass";
33 const myFunction = 'def my_function():\n return "Hello, World!"';
34
35 const file =
36 Array(100).fill(extraLine).join("\n") +
37 "\n\n" +
38 myClass +
39 "\n\n" +
40 myFunction +
41 "\n\n" +
42 Array(100).fill(extraLine).join("\n");
43
44 const chunks = await genToStrs(codeChunker("test.py", file, 200));
45 expect(chunks.length).toBeGreaterThan(1);
46 expect(chunks).toContain(myClass);
47 expect(chunks).toContain(myFunction);
48 });
49
50 test("should split large python class into methods and class with truncated methods", async () => {
51 const methodI = (i: number) =>
52 ` def method${i}():\n return "Hello, ${i}!"`;
53
54 const file =
55 "class MyClass:\n" +
56 Array(100)
57 .fill(0)
58 .map((_, i) => methodI(i + 1))
59 .join("\n") +
60 "\n\n";
61
62 console.log(file);
63
64 const chunks = await genToStrs(codeChunker("test.py", file, 200));
65 expect(chunks.length).toBeGreaterThan(1);
66 expect(
67 chunks[0].startsWith("class MyClass:\n def method1():\n ..."),
68 ).toBe(true);
69 // The extra spaces seem to be a bug with tree-sitter-python
70 expect(chunks).toContain('def method1():\n return "Hello, 1!"');
71 expect(chunks).toContain('def method20():\n return "Hello, 20!"');
72 });
73});
74
75async function collectContents(
76 filepath: string,
77 contents: string,
78 maxChunkSize: number,
79): Promise<string[]> {
80 const out: string[] = [];
81 for await (const c of codeChunker(filepath, contents, maxChunkSize)) {
82 out.push(c.content);
83 }
84 return out;
85}
86
87describe("codeChunker function definition", () => {
88 test("class methods never exceed maxChunkSize", async () => {
89 const body = Array.from(
90 { length: 200 },
91 (_, i) => `console.log(${i});`,
92 ).join("\n");
93 const contents = `
94class C {
95 a() {
96 ${body}
97 }
98 b() {
99 ${body}
100 }
101}
102`;
103 const max = 50;
104 const chunks = await collectContents("filename1.ts", contents, max);
105 const counts = await Promise.all(chunks.map((s) => countTokensAsync(s)));
106 counts.forEach((n) => expect(n).toBeLessThanOrEqual(max));
107 });
108
109 test("top-level functions never exceed maxChunkSize", async () => {
110 const body = Array.from(
111 { length: 150 },
112 (_, i) => `const x${i} = ${i};`,
113 ).join("\n");
114 const contents = `
115function f() {
116 ${body}
117}
118
119function g() {
120 ${body}
121}
122`;
123 const max = 60;
124 const chunks = await collectContents("filename2.ts", contents, max);
125 const counts = await Promise.all(chunks.map((s) => countTokensAsync(s)));
126 counts.forEach((n) => expect(n).toBeLessThanOrEqual(max));
127 });
128});