tests for codeChunker function definition
This commit is contained in:
parent
2202c03247
commit
e115abd5cd
1 changed files with 56 additions and 1 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import { ChunkWithoutID } from "../..";
|
||||
|
||||
import { countTokensAsync } from "../../llm/countTokens";
|
||||
import { codeChunker } from "./code";
|
||||
|
||||
async function genToArr<T>(generator: AsyncGenerator<T>): Promise<T[]> {
|
||||
|
|
@ -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<string[]> {
|
||||
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));
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue