test: chunkDocumentWithoutId

This commit is contained in:
Ting-Wai To 2025-07-23 17:44:17 -07:00
parent 24574d3544
commit 6bdae841d8
2 changed files with 67 additions and 3 deletions

View file

@ -1,8 +1,8 @@
import path from "path";
import { Chunk } from "../../index.js";
import { Chunk, ChunkWithoutID } from "../../index.js";
import { cleanupAsyncEncoders } from "../../llm/countTokens.js";
import { chunkDocument, shouldChunk } from "./chunk";
import { chunkDocument, chunkDocumentWithoutId, shouldChunk } from "./chunk";
describe("shouldChunk", () => {
test("should chunk a typescript file", () => {
@ -86,6 +86,70 @@ describe("chunkDocument", () => {
});
});
describe("chunkDocumentWithoutId", () => {
afterAll(async () => {
await cleanupAsyncEncoders();
});
test("should return no chunks for empty content", async () => {
const contents = "";
const chunks: ChunkWithoutID[] = [];
for await (const chunk of chunkDocumentWithoutId(
"file.txt",
contents,
100,
)) {
chunks.push(chunk);
}
expect(chunks).toHaveLength(0);
});
test("should chunk text files", async () => {
const contents = "Line 1\nLine 2\nLine 3";
const chunks: ChunkWithoutID[] = [];
for await (const chunk of chunkDocumentWithoutId(
"file.txt",
contents,
100,
)) {
chunks.push(chunk);
}
expect(chunks.length).toBeGreaterThan(0);
expect(chunks[0].content).toContain("Line 1");
});
test("should chunk for code files", async () => {
const contents = "function test() {\n return 42;\n}";
const chunks: ChunkWithoutID[] = [];
for await (const chunk of chunkDocumentWithoutId(
"file.js",
contents,
100,
)) {
chunks.push(chunk);
}
expect(chunks.length).toBeGreaterThan(0);
expect(chunks[0].content).toContain("function test");
});
test("should chunk large content", async () => {
const contents = Array(100).fill("short line").join("\n"); // Large content with many lines
const maxChunkSize = 50;
const chunks: ChunkWithoutID[] = [];
for await (const chunk of chunkDocumentWithoutId(
"file.txt",
contents,
maxChunkSize,
)) {
chunks.push(chunk);
}
expect(chunks.length).toBeGreaterThan(1); // Should create multiple chunks
chunks.forEach((chunk) => {
expect(chunk.content.length).toBeGreaterThan(0); // Each chunk should have content
});
});
});
function generateString(length: number) {
return "a".repeat(length);
}

View file

@ -13,7 +13,7 @@ export type ChunkDocumentParam = {
digest: string;
};
async function* chunkDocumentWithoutId(
export async function* chunkDocumentWithoutId(
fileUri: string,
contents: string,
maxChunkSize: number,