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^2a^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} dxx = 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 af(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 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 = '');
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);
});
});
});