From 9666188da839443a7696217de2e27acb2d0fc0c1 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Fri, 13 Mar 2026 20:28:22 +0000 Subject: [PATCH] test: add shared chat helper coverage --- src/shared/chat-envelope.test.ts | 23 ++++++++++++++++++ src/shared/chat-message-content.test.ts | 19 +++++++++++++++ src/shared/text-chunking.test.ts | 31 +++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 src/shared/chat-envelope.test.ts create mode 100644 src/shared/chat-message-content.test.ts create mode 100644 src/shared/text-chunking.test.ts diff --git a/src/shared/chat-envelope.test.ts b/src/shared/chat-envelope.test.ts new file mode 100644 index 00000000000..d04bc014e44 --- /dev/null +++ b/src/shared/chat-envelope.test.ts @@ -0,0 +1,23 @@ +import { describe, expect, it } from "vitest"; +import { stripEnvelope, stripMessageIdHints } from "./chat-envelope.js"; + +describe("shared/chat-envelope", () => { + it("strips recognized channel and timestamp envelope prefixes only", () => { + expect(stripEnvelope("[WhatsApp 2026-01-24 13:36] hello")).toBe("hello"); + expect(stripEnvelope("[2026-01-24T13:36Z] hello")).toBe("hello"); + expect(stripEnvelope("[Custom Sender] hello")).toBe("[Custom Sender] hello"); + }); + + it("keeps non-envelope headers and preserves unmatched text", () => { + expect(stripEnvelope("hello")).toBe("hello"); + expect(stripEnvelope("[note] hello")).toBe("[note] hello"); + }); + + it("removes standalone message id hint lines but keeps inline mentions", () => { + expect(stripMessageIdHints("hello\n[message_id: abc123]")).toBe("hello"); + expect(stripMessageIdHints("hello\n [message_id: abc123] \nworld")).toBe("hello\nworld"); + expect(stripMessageIdHints("I typed [message_id: abc123] inline")).toBe( + "I typed [message_id: abc123] inline", + ); + }); +}); diff --git a/src/shared/chat-message-content.test.ts b/src/shared/chat-message-content.test.ts new file mode 100644 index 00000000000..db2d533cebd --- /dev/null +++ b/src/shared/chat-message-content.test.ts @@ -0,0 +1,19 @@ +import { describe, expect, it } from "vitest"; +import { extractFirstTextBlock } from "./chat-message-content.js"; + +describe("shared/chat-message-content", () => { + it("extracts the first text block from array content", () => { + expect( + extractFirstTextBlock({ + content: [{ text: "hello" }, { text: "world" }], + }), + ).toBe("hello"); + }); + + it("returns undefined for missing, empty, or non-text content", () => { + expect(extractFirstTextBlock(null)).toBeUndefined(); + expect(extractFirstTextBlock({ content: [] })).toBeUndefined(); + expect(extractFirstTextBlock({ content: [{ type: "image" }] })).toBeUndefined(); + expect(extractFirstTextBlock({ content: ["hello"] })).toBeUndefined(); + }); +}); diff --git a/src/shared/text-chunking.test.ts b/src/shared/text-chunking.test.ts new file mode 100644 index 00000000000..be1fb518750 --- /dev/null +++ b/src/shared/text-chunking.test.ts @@ -0,0 +1,31 @@ +import { describe, expect, it } from "vitest"; +import { chunkTextByBreakResolver } from "./text-chunking.js"; + +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"]); + }); + + it("splits at resolver-provided breakpoints and trims separator boundaries", () => { + expect( + chunkTextByBreakResolver("alpha beta gamma", 10, (window) => window.lastIndexOf(" ")), + ).toEqual(["alpha", "beta gamma"]); + }); + + it("falls back to hard limits for invalid break indexes", () => { + expect(chunkTextByBreakResolver("abcdefghij", 4, () => Number.NaN)).toEqual([ + "abcd", + "efgh", + "ij", + ]); + expect(chunkTextByBreakResolver("abcdefghij", 4, () => 99)).toEqual(["abcd", "efgh", "ij"]); + expect(chunkTextByBreakResolver("abcdefghij", 4, () => 0)).toEqual(["abcd", "efgh", "ij"]); + }); + + it("skips empty chunks created by whitespace-only segments", () => { + expect( + chunkTextByBreakResolver("word next", 5, (window) => window.lastIndexOf(" ")), + ).toEqual(["word", "next"]); + }); +});