mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-07 22:09:57 +00:00
fix(telegram): chunk long html outbound messages (#42240)
Merged via squash.
Prepared head SHA: 4d79c41ddf
Co-authored-by: obviyus <22031114+obviyus@users.noreply.github.com>
Co-authored-by: obviyus <22031114+obviyus@users.noreply.github.com>
Reviewed-by: @obviyus
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { markdownToTelegramHtml } from "./format.js";
|
||||
import { markdownToTelegramHtml, splitTelegramHtmlChunks } from "./format.js";
|
||||
|
||||
describe("markdownToTelegramHtml", () => {
|
||||
it("handles core markdown-to-telegram conversions", () => {
|
||||
@@ -112,4 +112,26 @@ describe("markdownToTelegramHtml", () => {
|
||||
expect(res).toContain("<tg-spoiler>secret</tg-spoiler>");
|
||||
expect(res).toContain("trailing ||");
|
||||
});
|
||||
|
||||
it("splits long multiline html text without breaking balanced tags", () => {
|
||||
const chunks = splitTelegramHtmlChunks(`<b>${"A\n".repeat(2500)}</b>`, 4000);
|
||||
expect(chunks.length).toBeGreaterThan(1);
|
||||
expect(chunks.every((chunk) => chunk.length <= 4000)).toBe(true);
|
||||
expect(chunks[0]).toMatch(/^<b>[\s\S]*<\/b>$/);
|
||||
expect(chunks[1]).toMatch(/^<b>[\s\S]*<\/b>$/);
|
||||
});
|
||||
|
||||
it("fails loudly when a leading entity cannot fit inside a chunk", () => {
|
||||
expect(() => splitTelegramHtmlChunks(`A&${"B".repeat(20)}`, 4)).toThrow(/leading entity/i);
|
||||
});
|
||||
|
||||
it("treats malformed leading ampersands as plain text when chunking html", () => {
|
||||
const chunks = splitTelegramHtmlChunks(`&${"A".repeat(5000)}`, 4000);
|
||||
expect(chunks.length).toBeGreaterThan(1);
|
||||
expect(chunks.every((chunk) => chunk.length <= 4000)).toBe(true);
|
||||
});
|
||||
|
||||
it("fails loudly when tag overhead leaves no room for text", () => {
|
||||
expect(() => splitTelegramHtmlChunks("<b><i><u>x</u></i></b>", 10)).toThrow(/tag overhead/i);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user