xonecas.io
6533
Yo

yoctomarkdown / files

yoctomarkdown / examples / streaming-test.ts

3.2 KB
1/**
2 * Streaming API test app for yoctomarkdown.
3 *
4 * Simulates LLM-style character-by-character streaming to verify
5 * that partial line redraw works without duplicate lines.
6 *
7 * Usage:  bun examples/streaming-test.ts
8 */
9
10import { createHighlighter } from "../src/index.ts";
11
12const markdown = `# Streaming Test
13
14This is **bold text** and *italic text* with \`inline code\`.
15
16## Code Block
17
18\`\`\`
19function hello() {
20  console.log("world");
21}
22\`\`\`
23
24## Lists
25
26- First item with **bold**
27- Second item with \`code\`
28- Third item
29
301. Ordered one
312. Ordered two
32
33> A blockquote with *emphasis*
34
35---
36
37A [link](https://example.com) and some final text.
38
39This is a longer paragraph that should cause terminal wrapping when the terminal is narrow enough. It contains **bold words** and *italic words* and \`inline code snippets\` to verify that partial re-rendering handles wrapped lines correctly without duplicating content on screen.
40`;
41
42const delay = (ms: number) => new Promise((r) => setTimeout(r, ms));
43
44async function streamChars(text: string, prefix = "") {
45  const highlighter = createHighlighter({ theme: "truecolor" });
46
47  if (prefix) process.stdout.write(prefix);
48
49  for (const ch of text) {
50    const output = highlighter.write(ch);
51    if (output) process.stdout.write(output);
52    await delay(8);
53  }
54
55  const final = highlighter.end();
56  if (final) process.stdout.write(final);
57  process.stdout.write("\n");
58}
59
60async function main() {
61  // 1. Standard full-document streaming
62  await streamChars(markdown);
63
64  // 2. Inline prefix preservation — short line (should NOT erase prefix)
65  await streamChars("Here is some **bold** and *italic* text.", "AI: ");
66
67  // 3. Inline prefix preservation — long line that wraps
68  await streamChars(
69    "This is a much longer response that should wrap across multiple terminal lines to test the edge case where cursor-back might overshoot past the prefix on the first visual line.",
70    "Assistant: ",
71  );
72
73  // 4. Multiple prefixed responses in sequence
74  await streamChars("What is `yoctomarkdown`?", "Q: ");
75  await streamChars(
76    "A **tiny** streaming markdown highlighter for the terminal.",
77    "A: ",
78  );
79
80  // 5. Unicode width edge cases
81  // CJK wide characters (each takes 2 cells)
82  await streamChars("日本語のテスト — **太字** and `コード` inline.", "CJK: ");
83
84  // Emoji sequences (ZWJ family = 1 grapheme, 2 cells; skin-toned emoji)
85  await streamChars(
86    "Family 👨‍👩‍👧‍👦 and thumbs 👍🏽 and flag 🇯🇵 with **bold** after.",
87    "Emoji: ",
88  );
89
90  // Ambiguous-width characters (★ ① ☆ ♨ — EAW category "A")
91  await streamChars(
92    "Stars ★☆★☆★ and circled ①②③④⑤ and `♨ hotspring` done.",
93    "Ambiguous: ",
94  );
95
96  // Combining marks (café with combining é = 4 visible chars)
97  await streamChars(
98    "A cafe\u0301 with re\u0301sume\u0301 — **nai\u0308ve** text with `combining` marks.",
99    "Combining: ",
100  );
101
102  // Mixed: CJK + emoji + ambiguous + wrapping
103  await streamChars(
104    "漢字と★emoji👨‍👩‍👧‍👦とambiguous①②③が混ざった**長い文**で折り返しテスト。This line mixes scripts to stress-test `visibleLength` on wrapped redraws.",
105    "Mixed: ",
106  );
107}
108
109main();