import { describe, expect, it } from "vitest";
import {
markdownToTelegramChunks,
markdownToTelegramHtml,
splitTelegramHtmlChunks,
} from "./format.js";
describe("markdownToTelegramHtml", () => {
it("handles core markdown-to-telegram conversions", () => {
const cases = [
[
"renders basic inline formatting",
"hi _there_ **boss** `code`",
"hi there boss code",
],
[
"renders links as Telegram-safe HTML",
"see [docs](https://example.com)",
'see docs',
],
["escapes raw HTML", "nope", "<b>nope</b>"],
["escapes unsafe characters", "a & b < c", "a & b < c"],
["renders paragraphs with blank lines", "first\n\nsecond", "first\n\nsecond"],
["renders lists without block HTML", "- one\n- two", "• one\n• two"],
["renders ordered lists with numbering", "2. two\n3. three", "2. two\n3. three"],
["flattens headings", "# Title", "Title"],
] as const;
for (const [name, input, expected] of cases) {
expect(markdownToTelegramHtml(input), name).toBe(expected);
}
});
it("renders blockquotes as native Telegram blockquote tags", () => {
const res = markdownToTelegramHtml("> Quote");
expect(res).toContain("
"); expect(res).toContain("Quote"); expect(res).toContain(""); }); it("renders blockquotes with inline formatting", () => { const res = markdownToTelegramHtml("> **bold** quote"); expect(res).toContain("
"); expect(res).toContain("bold"); expect(res).toContain(""); }); it("renders multiline blockquotes as a single Telegram blockquote", () => { const res = markdownToTelegramHtml("> first\n> second"); expect(res).toBe("
first\nsecond"); }); it("renders separated quoted paragraphs as distinct blockquotes", () => { const res = markdownToTelegramHtml("> first\n\n> second"); expect(res).toContain("
first"); expect(res).toContain("second"); expect(res.match(//g)).toHaveLength(2); }); it("renders fenced code blocks", () => { const res = markdownToTelegramHtml("```js\nconst x = 1;\n```"); expect(res).toBe(""); }); it("properly nests overlapping bold and autolink (#4071)", () => { const res = markdownToTelegramHtml("**start https://example.com** end"); expect(res).toMatch( /start https:\/\/example\.com<\/a><\/b> end/, ); }); it("properly nests link inside bold", () => { const res = markdownToTelegramHtml("**bold [link](https://example.com) text**"); expect(res).toBe('bold link text'); }); it("properly nests bold wrapping a link with trailing text", () => { const res = markdownToTelegramHtml("**[link](https://example.com) rest**"); expect(res).toBe('link rest'); }); it("properly nests bold inside a link", () => { const res = markdownToTelegramHtml("[**bold**](https://example.com)"); expect(res).toBe('bold'); }); it("wraps punctuated file references in code tags", () => { const res = markdownToTelegramHtml("See README.md. Also (backup.sh)."); expect(res).toContain("const x = 1;\nREADME.md."); expect(res).toContain("(backup.sh)."); }); it("renders spoiler tags", () => { const res = markdownToTelegramHtml("the answer is ||42||"); expect(res).toBe("the answer is42 "); }); it("renders spoiler with nested formatting", () => { const res = markdownToTelegramHtml("||**secret** text||"); expect(res).toBe("secret text "); }); it("preserves spacing between Telegram bullet blocks and following numbered sections", () => { const input = [ "2. Main invariants:", "", " • Raw Log is source of truth.", " • Autonomy starts only with report/draft.", "3. Cognee is a candidate:", "", " • bake-off first;", " • decide keep/adopt/hybrid later.", "4. Project Flow slices:", ].join("\n"); const res = markdownToTelegramHtml(input, { wrapFileRefs: false }); expect(res).toContain("report/draft.\n\n3. Cognee"); expect(res).toContain("keep/adopt/hybrid later.\n\n4. Project"); }); it("preserves Telegram list boundary spacing in chunked rendering", () => { const input = [ "2. Main invariants:", "", " • Raw Log is source of truth.", " • Autonomy starts only with report/draft.", "3. Cognee is a candidate:", ].join("\n"); const res = markdownToTelegramChunks(input, 4096) .map((chunk) => chunk.html) .join(""); expect(res).toContain("report/draft.\n\n3. Cognee"); }); it("does not insert Telegram list boundary spacing inside fenced code", () => { const input = ["```", " • literal bullet", "3. literal number", "```"].join("\n"); const res = markdownToTelegramHtml(input, { wrapFileRefs: false }); expect(res).toBe(""); }); it("does not insert Telegram list boundary spacing inside indented code", () => { const input = [" • literal bullet", " 3. literal number"].join("\n"); const res = markdownToTelegramHtml(input, { wrapFileRefs: false }); const chunks = markdownToTelegramChunks(input, 4096) .map((chunk) => chunk.html) .join(""); expect(res).toBe("• literal bullet\n3. literal number\n"); expect(chunks).toBe(res); }); it("does not treat single pipe as spoiler", () => { const res = markdownToTelegramHtml("( ̄_ ̄|) face"); expect(res).not.toContain("tg-spoiler"); expect(res).toContain("|"); }); it("does not treat unpaired || as spoiler", () => { const res = markdownToTelegramHtml("before || after"); expect(res).not.toContain("tg-spoiler"); expect(res).toContain("||"); }); it("keeps valid spoiler pairs when a trailing || is unmatched", () => { const res = markdownToTelegramHtml("||secret|| trailing ||"); expect(res).toContain("• literal bullet\n3. literal number\nsecret "); expect(res).toContain("trailing ||"); }); it("splits long multiline html text without breaking balanced tags", () => { const chunks = splitTelegramHtmlChunks(`${"A\n".repeat(2500)}`, 4000); expect(chunks.length).toBeGreaterThan(1); expect(chunks.every((chunk) => chunk.length <= 4000)).toBe(true); expect(chunks[0]).toMatch(/^[\s\S]*<\/b>$/); expect(chunks[1]).toMatch(/^[\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("x", 10)).toThrow(/tag overhead/i); }); });