diff --git a/core/indexing/chunk/code.test.ts b/core/indexing/chunk/code.test.ts index 0261b576f..129a0bcfc 100644 --- a/core/indexing/chunk/code.test.ts +++ b/core/indexing/chunk/code.test.ts @@ -1,5 +1,5 @@ import { ChunkWithoutID } from "../.."; - +import { countTokensAsync } from "../../llm/countTokens"; import { codeChunker } from "./code"; async function genToArr(generator: AsyncGenerator): Promise { @@ -71,3 +71,58 @@ describe.skip("codeChunker", () => { expect(chunks).toContain('def method20():\n return "Hello, 20!"'); }); }); + +async function collectContents( + filepath: string, + contents: string, + maxChunkSize: number, +): Promise { + const out: string[] = []; + for await (const c of codeChunker(filepath, contents, maxChunkSize)) { + out.push(c.content); + } + return out; +} + +describe("codeChunker function definition", () => { + test("class methods never exceed maxChunkSize", async () => { + const body = Array.from( + { length: 200 }, + (_, i) => `console.log(${i});`, + ).join("\n"); + const contents = ` +class C { + a() { + ${body} + } + b() { + ${body} + } +} +`; + const max = 50; + const chunks = await collectContents("filename1.ts", contents, max); + const counts = await Promise.all(chunks.map((s) => countTokensAsync(s))); + counts.forEach((n) => expect(n).toBeLessThanOrEqual(max)); + }); + + test("top-level functions never exceed maxChunkSize", async () => { + const body = Array.from( + { length: 150 }, + (_, i) => `const x${i} = ${i};`, + ).join("\n"); + const contents = ` +function f() { + ${body} +} + +function g() { + ${body} +} +`; + const max = 60; + const chunks = await collectContents("filename2.ts", contents, max); + const counts = await Promise.all(chunks.map((s) => countTokensAsync(s))); + counts.forEach((n) => expect(n).toBeLessThanOrEqual(max)); + }); +});