From e115abd5cd49c62e3d9e298e00fe5dedbe2b2d52 Mon Sep 17 00:00:00 2001 From: uinstinct <61635505+uinstinct@users.noreply.github.com> Date: Mon, 11 Aug 2025 08:43:43 +0530 Subject: [PATCH] tests for codeChunker function definition --- core/indexing/chunk/code.test.ts | 57 +++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) 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)); + }); +});