|
| 1 | +import '@testing-library/jest-dom'; |
| 2 | +import { customRenderer } from '../markdownRenderer'; |
| 3 | +import * as smd from '@/utils/smd'; |
| 4 | + |
| 5 | +function parse(markdown: string, streaming: boolean = false, log: boolean = false) { |
| 6 | + const div = document.createElement('div'); |
| 7 | + const renderer = customRenderer(div, log); |
| 8 | + const parser = smd.parser(renderer); |
| 9 | + |
| 10 | + if (streaming) { |
| 11 | + for (const char of markdown) { |
| 12 | + smd.parser_write(parser, char); |
| 13 | + } |
| 14 | + } else { |
| 15 | + smd.parser_write(parser, markdown); |
| 16 | + } |
| 17 | + |
| 18 | + smd.parser_end(parser); |
| 19 | + return div; |
| 20 | +} |
| 21 | + |
| 22 | +describe('simple text rendering', () => { |
| 23 | + const markdown = 'This is a test'; |
| 24 | + it('all at once, should render standard text', () => { |
| 25 | + const div = parse(markdown); |
| 26 | + |
| 27 | + // Output should be: |
| 28 | + // <div> |
| 29 | + // <p> |
| 30 | + // This is a test |
| 31 | + // </p> |
| 32 | + // </div> |
| 33 | + expect(div.innerHTML).toBe('<p>This is a test</p>'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should render standard text, one character at a time', () => { |
| 37 | + const div = parse(markdown, true); |
| 38 | + |
| 39 | + // Output should be: |
| 40 | + // <div> |
| 41 | + // <p> |
| 42 | + // This is a test |
| 43 | + // </p> |
| 44 | + // </div> |
| 45 | + expect(div.innerHTML).toBe('<p>This is a test</p>'); |
| 46 | + }); |
| 47 | +}); |
| 48 | + |
| 49 | +describe('renderThinkingBlocks', () => { |
| 50 | + it('should handle one thinking block at start of text', () => { |
| 51 | + const markdown = `<thinking>This is a thinking block</thinking> some other text`; |
| 52 | + |
| 53 | + // Output should be: |
| 54 | + // <div> |
| 55 | + // <p> |
| 56 | + // <details type="thinking" open="true"> |
| 57 | + // <summary>💭 Thinking</summary> |
| 58 | + // <div style="white-space: pre-wrap; padding-top: 0px; padding-bottom: 0.5rem;">This is a thinking block</div> |
| 59 | + // </details> |
| 60 | + // some other text |
| 61 | + // </p> |
| 62 | + // </div> |
| 63 | + const expected = |
| 64 | + '<p><details type="thinking" open="true"><summary>💭 Thinking</summary><div style="white-space: pre-wrap; padding-top: 0px; padding-bottom: 0.5rem;">This is a thinking block</div></details> some other text</p>'; |
| 65 | + |
| 66 | + let div = parse(markdown); |
| 67 | + expect(div.innerHTML).toBe(expected); |
| 68 | + |
| 69 | + div = parse(markdown, true); |
| 70 | + expect(div.innerHTML).toBe(expected); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should handle one thinking block at end of text', () => { |
| 74 | + const markdown = `some other text <thinking>This is a thinking block</thinking>`; |
| 75 | + |
| 76 | + // Output should be: |
| 77 | + // <div> |
| 78 | + // <p> |
| 79 | + // some other text |
| 80 | + // <details type="thinking" open="true"> |
| 81 | + // <summary>💭 Thinking</summary> |
| 82 | + // <div style="white-space: pre-wrap; padding-top: 0px; padding-bottom: 0.5rem;">This is a thinking block</div> |
| 83 | + // </details> |
| 84 | + // </p> |
| 85 | + // </div> |
| 86 | + const expected = |
| 87 | + '<p>some other text <details type="thinking" open="true"><summary>💭 Thinking</summary><div style="white-space: pre-wrap; padding-top: 0px; padding-bottom: 0.5rem;">This is a thinking block</div></details></p>'; |
| 88 | + |
| 89 | + let div = parse(markdown); |
| 90 | + expect(div.innerHTML).toBe(expected); |
| 91 | + |
| 92 | + div = parse(markdown, true); |
| 93 | + expect(div.innerHTML).toBe(expected); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should handle multiple thinking blocks', () => { |
| 97 | + const markdown = `some other text <thinking>This is a thinking block</thinking> some other text <thinking>This is another thinking block</thinking>`; |
| 98 | + |
| 99 | + // Output should be: |
| 100 | + // <div> |
| 101 | + // <p> |
| 102 | + // some other text |
| 103 | + // <details type="thinking" open="true"> |
| 104 | + // <summary>💭 Thinking</summary> |
| 105 | + // <div style="white-space: pre-wrap; padding-top: 0px; padding-bottom: 0.5rem;">This is a thinking block</div> |
| 106 | + // </details> |
| 107 | + // some other text |
| 108 | + // <details type="thinking" open="true"> |
| 109 | + // <summary>💭 Thinking</summary> |
| 110 | + // <div style="white-space: pre-wrap; padding-top: 0px; padding-bottom: 0.5rem;">This is another thinking block</div> |
| 111 | + // </details> |
| 112 | + // </p> |
| 113 | + // </div> |
| 114 | + const expected = |
| 115 | + '<p>some other text <details type="thinking" open="true"><summary>💭 Thinking</summary><div style="white-space: pre-wrap; padding-top: 0px; padding-bottom: 0.5rem;">This is a thinking block</div></details> some other text <details type="thinking" open="true"><summary>💭 Thinking</summary><div style="white-space: pre-wrap; padding-top: 0px; padding-bottom: 0.5rem;">This is another thinking block</div></details></p>'; |
| 116 | + |
| 117 | + const div = parse(markdown); |
| 118 | + expect(div.innerHTML).toBe(expected); |
| 119 | + |
| 120 | + const div2 = parse(markdown, true); |
| 121 | + expect(div2.innerHTML).toBe(expected); |
| 122 | + }); |
| 123 | +}); |
| 124 | + |
| 125 | +describe('renderCodeBlocks', () => { |
| 126 | + it('should handle one python code block at start of text', () => { |
| 127 | + const markdown = `\`\`\`python\nThis is a code block\n\`\`\` some other text`; |
| 128 | + |
| 129 | + const expected = |
| 130 | + '<details open="true"><summary>💻 python</summary><pre><code class="hljs language-python">This <span class="hljs-keyword">is</span> a code block</code></pre></details><p>some other text</p>'; |
| 131 | + |
| 132 | + let div = parse(markdown); |
| 133 | + expect(div.innerHTML).toBe(expected); |
| 134 | + |
| 135 | + div = parse(markdown, true); |
| 136 | + expect(div.innerHTML).toBe(expected); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should handle one python code block at end of text', () => { |
| 140 | + const markdown = `some other text\n\`\`\`python\nThis is a code block\n\`\`\``; |
| 141 | + |
| 142 | + const expected = |
| 143 | + '<p>some other text<details open="true"><summary>💻 python</summary><pre><code class="hljs language-python">This <span class="hljs-keyword">is</span> a code block</code></pre></details></p>'; |
| 144 | + |
| 145 | + let div = parse(markdown); |
| 146 | + expect(div.innerHTML).toBe(expected); |
| 147 | + |
| 148 | + div = parse(markdown, true); |
| 149 | + expect(div.innerHTML).toBe(expected); |
| 150 | + }); |
| 151 | +}); |
| 152 | + |
| 153 | +describe('renderMarkdownBlocks', () => { |
| 154 | + it('should handle one markdown block at start of text', () => { |
| 155 | + const markdown = `\`\`\`markdown\nThis is a markdown block\n\`\`\`\nsome other text`; |
| 156 | + |
| 157 | + const expected = |
| 158 | + '<details open="true"><summary>💻 markdown</summary><pre><code class="hljs language-markdown">This is a markdown block</code></pre></details><p>some other text</p>'; |
| 159 | + |
| 160 | + let div = parse(markdown, false, true); |
| 161 | + console.log(div.innerHTML); |
| 162 | + expect(div.innerHTML).toBe(expected); |
| 163 | + |
| 164 | + div = parse(markdown, true, false); |
| 165 | + expect(div.innerHTML).toBe(expected); |
| 166 | + }); |
| 167 | +}); |
0 commit comments