@@ -44,4 +44,37 @@ describe("Programmatic Usage: createHighlighter (Streaming)", () => {
4444 expect(res).toContain("Heading");
4545 expect(res).toContain("Some **text**");
4646 });
47+
48+ test("should handle character by character streaming", () => {
49+ const markdown =
50+ "# Title\n\nThis is a **test** of *character* streaming.\n\n- item 1\n- item 2\n";
51+ const highlighter = createHighlighter({ theme: "none" });
52+ let res = "";
53+ for (const char of markdown) {
54+ res += highlighter.write(char);
55+ }
56+ res += highlighter.end();
57+
58+ const expected = highlightSync(markdown, { theme: "none" });
59+ expect(res).toBe(expected);
60+ });
61+
62+ test("should handle random chunk sizes", () => {
63+ const markdown =
64+ "```javascript\nconst x = 1;\n```\n\n# Heading 2\n\nSome text with `code`.\n> Blockquote here\n\n1. One\n2. Two\n";
65+ const highlighter = createHighlighter({ theme: "none" });
66+ let res = "";
67+
68+ // Split into random chunks
69+ let i = 0;
70+ while (i < markdown.length) {
71+ const chunkSize = Math.floor(Math.random() * 10) + 1;
72+ res += highlighter.write(markdown.slice(i, i + chunkSize));
73+ i += chunkSize;
74+ }
75+ res += highlighter.end();
76+
77+ const expected = highlightSync(markdown, { theme: "none" });
78+ expect(res).toBe(expected);
79+ });
4780 });