|
| 1 | +import { processNestedCodeBlocks, transformThinkingTags, parseMarkdownContent } from "../markdownUtils"; |
| 2 | +import '@testing-library/jest-dom'; |
| 3 | + |
| 4 | +describe('processNestedCodeBlocks', () => { |
| 5 | + it('should handle nested code blocks', () => { |
| 6 | + const input = `\`\`\`markdown |
| 7 | +Here's a nested block |
| 8 | +\`\`\`python |
| 9 | +print("hello") |
| 10 | +\`\`\` |
| 11 | +\`\`\``; |
| 12 | + |
| 13 | + const expected = `\`\`\`markdown |
| 14 | +Here's a nested block |
| 15 | +\`\`\`python |
| 16 | +print("hello") |
| 17 | +\`\`\` |
| 18 | +\`\`\``; |
| 19 | + |
| 20 | + const result = processNestedCodeBlocks(input); |
| 21 | + expect(result.processedContent).toBe(expected); |
| 22 | + expect(result.langtags.filter(Boolean)).toEqual(['markdown', 'python']); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should not modify single code blocks', () => { |
| 26 | + const input = `\`\`\`python |
| 27 | +print("hello") |
| 28 | +\`\`\``; |
| 29 | + |
| 30 | + const result = processNestedCodeBlocks(input); |
| 31 | + expect(result.processedContent).toBe(input); |
| 32 | + expect(result.langtags.filter(Boolean)).toEqual(['python']); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should handle multiple nested blocks', () => { |
| 36 | + const input = `\`\`\`markdown |
| 37 | +First block |
| 38 | +\`\`\`python |
| 39 | +print("hello") |
| 40 | +\`\`\` |
| 41 | +Second block |
| 42 | +\`\`\`javascript |
| 43 | +console.log("world") |
| 44 | +\`\`\` |
| 45 | +\`\`\``; |
| 46 | + |
| 47 | + const expected = `\`\`\`markdown |
| 48 | +First block |
| 49 | +\`\`\`python |
| 50 | +print("hello") |
| 51 | +\`\`\` |
| 52 | +Second block |
| 53 | +\`\`\`javascript |
| 54 | +console.log("world") |
| 55 | +\`\`\` |
| 56 | +\`\`\``; |
| 57 | + |
| 58 | + const result = processNestedCodeBlocks(input); |
| 59 | + expect(result.processedContent).toBe(expected); |
| 60 | + expect(result.langtags.filter(Boolean)).toEqual(['markdown', 'python', 'javascript']); |
| 61 | + }); |
| 62 | + |
| 63 | + it('returns original content when no code blocks', () => { |
| 64 | + const input = "Hello world"; |
| 65 | + const result = processNestedCodeBlocks(input); |
| 66 | + expect(result.processedContent).toBe(input); |
| 67 | + expect(result.langtags).toEqual([]); |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +describe('transformThinkingTags', () => { |
| 72 | + it('should transform thinking tags to details/summary', () => { |
| 73 | + const input = 'Before <thinking>Some thoughts</thinking> After'; |
| 74 | + const expected = 'Before <details><summary>💭 Thinking</summary>\n\nSome thoughts\n\n</details> After'; |
| 75 | + expect(transformThinkingTags(input)).toBe(expected); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should handle multiple thinking tags', () => { |
| 79 | + const input = '<thinking>First thought</thinking> Middle <thinking>Second thought</thinking>'; |
| 80 | + const expected = '<details><summary>💭 Thinking</summary>\n\nFirst thought\n\n</details> Middle <details><summary>💭 Thinking</summary>\n\nSecond thought\n\n</details>'; |
| 81 | + expect(transformThinkingTags(input)).toBe(expected); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should not transform thinking tags within code blocks', () => { |
| 85 | + const input = '`<thinking>Code block</thinking>`'; |
| 86 | + expect(transformThinkingTags(input)).toBe(input); |
| 87 | + }); |
| 88 | + |
| 89 | + it('preserves content outside thinking tags', () => { |
| 90 | + const input = 'Before <thinking>thinking</thinking> after'; |
| 91 | + const expected = 'Before <details><summary>💭 Thinking</summary>\n\nthinking\n\n</details> after'; |
| 92 | + expect(transformThinkingTags(input)).toBe(expected); |
| 93 | + }); |
| 94 | +}); |
| 95 | + |
| 96 | +describe('parseMarkdownContent', () => { |
| 97 | + it('parses basic markdown', () => { |
| 98 | + const input = "# Hello\n\nThis is a test"; |
| 99 | + const result = parseMarkdownContent(input); |
| 100 | + expect(result).toContain("<h1>Hello</h1>"); |
| 101 | + expect(result).toContain("<p>This is a test</p>"); |
| 102 | + }); |
| 103 | + |
| 104 | + it('handles code blocks with language tags', () => { |
| 105 | + const input = "```python\nprint('hello')\n```"; |
| 106 | + const result = parseMarkdownContent(input); |
| 107 | + expect(result).toContain("<summary>💻 python</summary>"); |
| 108 | + expect(result).toContain('<span class="hljs-built_in">print</span>'); |
| 109 | + expect(result).toContain('<span class="hljs-string">'hello'</span>'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('detects file paths in code blocks', () => { |
| 113 | + const input = "```src/test.py\nprint('hello')\n```"; |
| 114 | + const result = parseMarkdownContent(input); |
| 115 | + expect(result).toContain("<summary>📄 src/test.py</summary>"); |
| 116 | + }); |
| 117 | + |
| 118 | + it('detects tool commands in code blocks', () => { |
| 119 | + const input = "```shell\nls -la\n```"; |
| 120 | + const result = parseMarkdownContent(input); |
| 121 | + expect(result).toContain("<summary>🛠️ shell</summary>"); |
| 122 | + }); |
| 123 | + |
| 124 | + it('detects output blocks', () => { |
| 125 | + const input = "```stdout\nHello world\n```"; |
| 126 | + const result = parseMarkdownContent(input); |
| 127 | + expect(result).toContain("<summary>📤 stdout</summary>"); |
| 128 | + }); |
| 129 | + |
| 130 | + it('detects write operations in code blocks', () => { |
| 131 | + const input = "```save test.txt\nHello world\n```"; |
| 132 | + const result = parseMarkdownContent(input); |
| 133 | + expect(result).toContain("<summary>📝 save test.txt</summary>"); |
| 134 | + }); |
| 135 | + |
| 136 | + it('handles thinking tags', () => { |
| 137 | + const input = "<thinking>Some thought</thinking>"; |
| 138 | + const result = parseMarkdownContent(input); |
| 139 | + expect(result).toContain("<summary>💭 Thinking</summary>"); |
| 140 | + expect(result).toContain("Some thought"); |
| 141 | + }); |
| 142 | + |
| 143 | + it('handles nested code blocks', () => { |
| 144 | + const input = "```markdown\nHere's a nested block\n```python\nprint('hello')\n```\n```"; |
| 145 | + const result = parseMarkdownContent(input); |
| 146 | + expect(result).toContain("<summary>💻 markdown</summary>"); |
| 147 | + expect(result).toContain('<span class="hljs-code">```python'); |
| 148 | + expect(result).toContain('print('hello')'); |
| 149 | + }); |
| 150 | + |
| 151 | + it('handles complex message with multiple content types', () => { |
| 152 | + const input = `The gptme web UI offers several advantages over the CLI interface: |
| 153 | +
|
| 154 | +1. **Rich Message Display**: |
| 155 | + - Syntax highlighted code blocks |
| 156 | + - Collapsible sections for code and thinking |
| 157 | + - Different styles for user/assistant/system messages |
| 158 | + - Emoji indicators for different types of content: |
| 159 | + - 📄 File paths |
| 160 | + - 🛠️ Tool usage |
| 161 | + - 📤 Command output |
| 162 | + - 💻 Code blocks |
| 163 | +
|
| 164 | +2. **Interactive Features**: |
| 165 | + - Real-time streaming of responses |
| 166 | + - Easy navigation between conversations |
| 167 | + - Ability to view and restore conversation history |
| 168 | +
|
| 169 | +3. **Integration with gptme-server**: |
| 170 | + - Connects to your local gptme instance |
| 171 | + - Access to all local tools and capabilities |
| 172 | + - Secure local execution of commands |
| 173 | +
|
| 174 | +Here's an example showing different types of content: |
| 175 | +
|
| 176 | +\`\`\`/path/to/file.py |
| 177 | +# This shows as a file path |
| 178 | +\`\`\` |
| 179 | +
|
| 180 | +\`\`\`shell |
| 181 | +# This shows as a tool |
| 182 | +ls -la |
| 183 | +\`\`\` |
| 184 | +
|
| 185 | +\`\`\`stdout |
| 186 | +# This shows as command output |
| 187 | +total 0 |
| 188 | +drwxr-xr-x 2 user user 4096 Jan 29 10:48 . |
| 189 | +\`\`\` |
| 190 | +
|
| 191 | +<thinking> |
| 192 | +Thinking blocks are collapsible and help show my reasoning process |
| 193 | +</thinking> |
| 194 | +
|
| 195 | +You can try the web UI by: |
| 196 | +1. Starting a local gptme-server: \`gptme-server --cors-origin='http://localhost:8080'\` |
| 197 | +2. Running the web UI: \`npm run dev\` |
| 198 | +3. Opening http://localhost:5173 in your browser`; |
| 199 | + |
| 200 | + const result = parseMarkdownContent(input); |
| 201 | + |
| 202 | + // Check markdown formatting is preserved |
| 203 | + expect(result).toContain('<p>The gptme web UI offers several advantages over the CLI interface:</p>'); |
| 204 | + expect(result).toContain('<li><p><strong>Rich Message Display</strong>:</p>'); |
| 205 | + |
| 206 | + // Check list items are preserved |
| 207 | + expect(result).toContain('<li>Syntax highlighted code blocks</li>'); |
| 208 | + expect(result).toContain('<li>📄 File paths</li>'); |
| 209 | + |
| 210 | + // Check code blocks with correct emoji indicators |
| 211 | + expect(result).toContain('<summary>📄 /path/to/file.py</summary>'); |
| 212 | + expect(result).toContain('<summary>🛠️ shell</summary>'); |
| 213 | + expect(result).toContain('<summary>📤 stdout</summary>'); |
| 214 | + |
| 215 | + // Check code block content with syntax highlighting |
| 216 | + expect(result).toContain('<span class="hljs-comment"># This shows as a file path</span>'); |
| 217 | + expect(result).toContain('<span class="language-bash">This shows as a tool</span>'); |
| 218 | + expect(result).toContain('# This shows as command output'); |
| 219 | + expect(result).toContain('drwxr-xr-x 2 user user'); |
| 220 | + |
| 221 | + // Check final content is included |
| 222 | + expect(result).toContain('You can try the web UI by:'); |
| 223 | + expect(result).toContain('<code>gptme-server --cors-origin='); |
| 224 | + expect(result).toContain('<code>npm run dev</code>'); |
| 225 | + expect(result).toContain('http://localhost:5173'); |
| 226 | + |
| 227 | + // Check thinking block |
| 228 | + expect(result).toContain('<details><summary>💭 Thinking</summary>'); |
| 229 | + expect(result).toContain('Thinking blocks are collapsible'); |
| 230 | + }); |
| 231 | +}); |
0 commit comments