Changesets · Branches · Tags · Compare
| Changeset | b68735bd51e59594eaec1073d30cd43664d2f2f45f3c6dc2db99918e7e36669f |
|---|---|
| Author | uinstinct <61635505+uinstinct@users.noreply.github.com> · 2025-08-11 08:43 +05:30 |
| Committer | uinstinct <61635505+uinstinct@users.noreply.github.com> · 2025-08-11 08:43 +05:30 |
| Parents | c5a6b31f5518 |
1 file changed
modified core/indexing/chunk/code.test.ts +56 −1
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | import { ChunkWithoutID } from "../.."; |
| 2 | - | |
| 2 | +import { countTokensAsync } from "../../llm/countTokens"; | |
| 3 | 3 | import { codeChunker } from "./code"; |
| 4 | 4 | |
| 5 | 5 | async function genToArr<T>(generator: AsyncGenerator<T>): Promise<T[]> { |
| @@ -71,3 +71,58 @@ | ||
| 71 | 71 | expect(chunks).toContain('def method20():\n return "Hello, 20!"'); |
| 72 | 72 | }); |
| 73 | 73 | }); |
| 74 | + | |
| 75 | +async 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 | + | |
| 87 | +describe("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 = ` | |
| 94 | +class 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 = ` | |
| 115 | +function f() { | |
| 116 | + ${body} | |
| 117 | +} | |
| 118 | + | |
| 119 | +function 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 | +}); |