import { processMarkdown } from '../../../src/renderer/utils/markdownProcessor'; describe('processMarkdown', () => { describe('Math Expressions', () => { test('should process display math expressions with escaped brackets', () => { const input = 'Here is a formula: \\[E = mc^2\\]'; const expected = 'Here is a formula:
E = mc^2
'; const result = processMarkdown(input); expect(result).toBe(expected); }); test('should process inline math expressions with escaped parentheses', () => { const input = 'The equation \\(a^2 + b^2 = c^2\\) is Pythagorean'; const expected = 'The equation a^2 + b^2 = c^2 is Pythagorean'; const result = processMarkdown(input); expect(result).toBe(expected); }); test('should process double dollar display math', () => { const input = 'Formula: $$\\int_0^\\infty e^{-x^2} dx$$'; const expected = 'Formula:
\\int_0^\\infty e^{-x^2} dx
'; const result = processMarkdown(input); expect(result).toBe(expected); }); test('should process single dollar inline math', () => { const input = 'Use $x = 5$ in the equation'; const expected = 'Use x = 5 in the equation'; const result = processMarkdown(input); expect(result).toBe(expected); }); test('should handle complex math expressions with text commands', () => { const input = '\\[\\text{Force} = m \\cdot a\\]'; const expected = '
Force = m cdot a
'; const result = processMarkdown(input); expect(result).toBe(expected); }); test('should handle math with backslashes and commas', () => { const input = '\\(f(x) = \\frac{1}{x}\\)'; const expected = 'f(x) = frac{1}{x}'; const result = processMarkdown(input); expect(result).toBe(expected); }); }); describe('Code Blocks', () => { test('should process inline code', () => { const input = 'Use the `console.log()` function'; const expected = 'Use the console.log() function'; const result = processMarkdown(input); expect(result).toBe(expected); }); test('should handle code with special characters', () => { const input = 'Type `npm install ` in terminal'; const expected = 'Type npm install <package> in terminal'; const result = processMarkdown(input); expect(result).toBe(expected); }); test('should handle multiple inline code blocks', () => { const input = 'Use `function()` and `variable` together'; const expected = 'Use function() and variable together'; const result = processMarkdown(input); expect(result).toBe(expected); }); }); describe('Text Formatting', () => { test('should process bold text with double asterisks', () => { const input = 'This is **bold** text'; const expected = 'This is bold text'; const result = processMarkdown(input); expect(result).toBe(expected); }); test('should process bold text with double underscores', () => { const input = 'This is __bold__ text'; const expected = 'This is bold text'; const result = processMarkdown(input); expect(result).toBe(expected); }); test('should process italic text with single asterisk', () => { const input = 'This is *italic* text'; const expected = 'This is italic text'; const result = processMarkdown(input); expect(result).toBe(expected); }); test('should process italic text with single underscore', () => { const input = 'This is _italic_ text'; const expected = 'This is italic text'; const result = processMarkdown(input); expect(result).toBe(expected); }); test('should handle nested formatting', () => { const input = 'This is ***bold italic*** text'; const expected = 'This is bold italic text'; const result = processMarkdown(input); expect(result).toBe(expected); }); }); describe('Links', () => { test('should process links', () => { const input = 'Visit [Google](https://google.com) for search'; const expected = 'Visit Google for search'; const result = processMarkdown(input); expect(result).toBe(expected); }); test('should handle links with underscores in URL', () => { const input = 'Check [documentation](https://example.com/docs_page)'; const expected = 'Check documentation'; const result = processMarkdown(input); expect(result).toBe(expected); }); test('should handle multiple links', () => { const input = '[Link1](url1) and [Link2](url2)'; const expected = 'Link1 and Link2'; const result = processMarkdown(input); expect(result).toBe(expected); }); }); describe('Headings', () => { test('should process H1 headings', () => { const input = '# Main Title'; const expected = '

Main Title

'; const result = processMarkdown(input); expect(result).toBe(expected); }); test('should process H2 headings', () => { const input = '## Section Title'; const expected = '

Section Title

'; const result = processMarkdown(input); expect(result).toBe(expected); }); test('should process H3 headings', () => { const input = '### Subsection Title'; const expected = '

Subsection Title

'; const result = processMarkdown(input); expect(result).toBe(expected); }); test('should process H4 headings', () => { const input = '#### Detail Title'; const expected = '

Detail Title

'; const result = processMarkdown(input); expect(result).toBe(expected); }); test('should process multiple headings', () => { const input = '# Title\n## Subtitle\n### Section'; const expected = '

Title

\n

Subtitle

\n

Section

'; const result = processMarkdown(input); expect(result).toBe(expected); }); }); describe('Lists', () => { test('should process unordered list with dashes', () => { const input = '- Item 1\n- Item 2\n- Item 3'; const expected = ''; const result = processMarkdown(input); expect(result).toBe(expected); }); test('should process unordered list with asterisks', () => { const input = '* Item 1\n* Item 2'; const expected = ' Item 1\n Item 2'; const result = processMarkdown(input); expect(result).toBe(expected); }); test('should handle single list item', () => { const input = '- Single item'; const expected = ''; const result = processMarkdown(input); expect(result).toBe(expected); }); }); describe('Line Breaks', () => { test('should convert newlines to br tags', () => { const input = 'Line 1\nLine 2\nLine 3'; const expected = 'Line 1\nLine 2\nLine 3'; const result = processMarkdown(input); expect(result).toBe(expected); }); test('should handle mixed content with line breaks', () => { const input = '# Title\n\nSome text\n\n- Item 1\n- Item 2'; const expected = '

Title



Some text\n'; const result = processMarkdown(input); expect(result).toBe(expected); }); }); describe('Complex Content', () => { test('should process complex markdown document', () => { const input = `# Document Title This is a **bold** and *italic* text with \`code\` and [link](url). ## Math Section Here is inline math $E = mc^2$ and display math: $$\\int_0^1 x^2 dx$$ ## List of Items - First item - Second item with **bold** text - Third item ## Code Examples Use \`npm install\` to install packages.`; const result = processMarkdown(input); expect(result).toContain('

bold'); expect(result).toContain('italic'); expect(result).toContain(''); expect(result).toContain('First item'); expect(result).toContain('Second item'); }); test('should handle edge cases', () => { expect(processMarkdown('')).toBe(''); expect(processMarkdown(' \n \n ')).toBe(' \n \n '); expect(processMarkdown('Plain text without markdown')).toBe('Plain text without markdown'); }); test('should handle nested elements correctly', () => { const input = '**Bold with `code`** and *italic with [link](url)*'; const expected = 'Bold with code and italic with link'; const result = processMarkdown(input); expect(result).toBe(expected); }); }); describe('Order of Processing', () => { test('should process elements in correct order', () => { const input = '$`code`$'; const result = processMarkdown(input); expect(result).toContain(' { const input = '**Bold** and *italic*'; const result = processMarkdown(input); expect(result).toContain('Bold'); expect(result).toContain('italic'); }); }); describe('HTML Escaping', () => { test('should handle HTML-like content in code blocks', () => { const input = 'Use `
` in HTML'; const expected = 'Use <div> in HTML'; const result = processMarkdown(input); expect(result).toBe(expected); }); test('should preserve special characters in regular text', () => { const input = 'Price: $5 < $10 > $1'; const result = processMarkdown(input); expect(result).toBe('Price: 5 < 10 > $1'); }); }); describe('Performance and Edge Cases', () => { test('should handle large content efficiently', () => { const largeContent = 'Line\n\n'.repeat(500) + '**bold**'; const result = processMarkdown(largeContent); expect(result).toContain('bold'); expect(result.split('
').length).toBe(1001); // 500 double newlines create 500

tags, splitting gives 1001 parts }); test('should handle empty or malformed patterns', () => { const inputs = [ '$$', // Empty math '``', // Empty code '**', // Empty bold '*', // Single asterisk '[](url)', // Empty link text '[text]()', // Empty link URL '#', // Empty heading '-', // Empty list item ]; inputs.forEach(input => { expect(() => processMarkdown(input)).not.toThrow(); }); }); test('should be idempotent', () => { const input = '**bold** and `code`'; const firstPass = processMarkdown(input); const secondPass = processMarkdown(firstPass); expect(secondPass).toBe(firstPass); }); }); });