From cdde51c608c84b11d77c9ad7555e069271887c38 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Fri, 13 Mar 2026 21:40:01 +0000 Subject: [PATCH] test: tighten shared text chunking coverage --- src/shared/chat-content.test.ts | 13 +++++++++++++ src/shared/text-chunking.test.ts | 9 +++++++++ 2 files changed, 22 insertions(+) diff --git a/src/shared/chat-content.test.ts b/src/shared/chat-content.test.ts index d66ec203c72..0131865cef8 100644 --- a/src/shared/chat-content.test.ts +++ b/src/shared/chat-content.test.ts @@ -12,6 +12,7 @@ describe("shared/chat-content", () => { { type: "text", text: " hello " }, { type: "image_url", image_url: "https://example.com" }, { type: "text", text: "world" }, + { text: "ignored without type" }, null, ]), ).toBe("hello world"); @@ -37,6 +38,18 @@ describe("shared/chat-content", () => { }, ), ).toBe("hello\nworld"); + + expect( + extractTextFromChatContent( + [ + { type: "text", text: "keep" }, + { type: "text", text: "drop" }, + ], + { + sanitizeText: (text) => (text === "drop" ? " " : text), + }, + ), + ).toBe("keep"); }); it("returns null for unsupported or empty content", () => { diff --git a/src/shared/text-chunking.test.ts b/src/shared/text-chunking.test.ts index be1fb518750..83b0de77ae5 100644 --- a/src/shared/text-chunking.test.ts +++ b/src/shared/text-chunking.test.ts @@ -5,12 +5,14 @@ describe("shared/text-chunking", () => { it("returns empty for blank input and the full text when under limit", () => { expect(chunkTextByBreakResolver("", 10, () => 5)).toEqual([]); expect(chunkTextByBreakResolver("hello", 10, () => 2)).toEqual(["hello"]); + expect(chunkTextByBreakResolver("hello", 0, () => 2)).toEqual(["hello"]); }); it("splits at resolver-provided breakpoints and trims separator boundaries", () => { expect( chunkTextByBreakResolver("alpha beta gamma", 10, (window) => window.lastIndexOf(" ")), ).toEqual(["alpha", "beta gamma"]); + expect(chunkTextByBreakResolver("abcd efgh", 4, () => 4)).toEqual(["abcd", "efgh"]); }); it("falls back to hard limits for invalid break indexes", () => { @@ -28,4 +30,11 @@ describe("shared/text-chunking", () => { chunkTextByBreakResolver("word next", 5, (window) => window.lastIndexOf(" ")), ).toEqual(["word", "next"]); }); + + it("trims trailing whitespace from emitted chunks before continuing", () => { + expect(chunkTextByBreakResolver("abc def", 6, (window) => window.lastIndexOf(" "))).toEqual([ + "abc", + "def", + ]); + }); });