mirror of https://github.com/openclaw/openclaw.git
test: add shared chat helper coverage
This commit is contained in:
parent
d291148e93
commit
9666188da8
|
|
@ -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",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -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"]);
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Reference in New Issue