|
| 1 | +"""Tests for markdown_to_html in base_converter. |
| 2 | +
|
| 3 | +Verifies CommonMark-compliant parsing via markdown-it-py, including |
| 4 | +leading-space headings, table interactions, and fenced code blocks. |
| 5 | +""" |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from app.services.base_converter import BaseConverterService |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def converter(): |
| 14 | + return BaseConverterService() |
| 15 | + |
| 16 | + |
| 17 | +class TestHeadingParsing: |
| 18 | + """ATX headings with various leading-space patterns.""" |
| 19 | + |
| 20 | + @pytest.mark.asyncio |
| 21 | + async def test_heading_no_leading_space(self, converter): |
| 22 | + html = await converter.markdown_to_html("# Heading") |
| 23 | + assert "<h1>" in html |
| 24 | + |
| 25 | + @pytest.mark.asyncio |
| 26 | + @pytest.mark.parametrize("spaces", [1, 2, 3]) |
| 27 | + async def test_heading_with_leading_spaces(self, converter, spaces): |
| 28 | + md = " " * spaces + "## Heading" |
| 29 | + html = await converter.markdown_to_html(md) |
| 30 | + assert "<h2>" in html |
| 31 | + |
| 32 | + @pytest.mark.asyncio |
| 33 | + async def test_four_spaces_is_code_block(self, converter): |
| 34 | + html = await converter.markdown_to_html(" # Not a heading") |
| 35 | + assert "<h1>" not in html |
| 36 | + assert "<code>" in html |
| 37 | + |
| 38 | + @pytest.mark.asyncio |
| 39 | + async def test_all_heading_levels(self, converter): |
| 40 | + md = "\n\n".join(f"{'#' * i} Level {i}" for i in range(1, 7)) |
| 41 | + html = await converter.markdown_to_html(md) |
| 42 | + for i in range(1, 7): |
| 43 | + assert f"<h{i}>" in html |
| 44 | + |
| 45 | + |
| 46 | +class TestHeadingAfterTable: |
| 47 | + """Headings following tables — the original bug that motivated normalization.""" |
| 48 | + |
| 49 | + @pytest.mark.asyncio |
| 50 | + async def test_heading_after_table_no_blank_line(self, converter): |
| 51 | + md = "| a | b |\n|---|---|\n| c | d |\n### Heading" |
| 52 | + html = await converter.markdown_to_html(md) |
| 53 | + assert "<h3>" in html |
| 54 | + assert "<table>" in html |
| 55 | + |
| 56 | + @pytest.mark.asyncio |
| 57 | + async def test_heading_after_table_with_blank_line(self, converter): |
| 58 | + md = "| a | b |\n|---|---|\n| c | d |\n\n### Heading" |
| 59 | + html = await converter.markdown_to_html(md) |
| 60 | + assert "<h3>" in html |
| 61 | + assert "<table>" in html |
| 62 | + |
| 63 | + @pytest.mark.asyncio |
| 64 | + async def test_multiple_tables_with_headings(self, converter): |
| 65 | + md = "### A\n\n| h1 | h2 |\n|---|---|\n| c1 | c2 |\n### B\n\n| h3 | h4 |\n|---|---|\n| c3 | c4 |" |
| 66 | + html = await converter.markdown_to_html(md) |
| 67 | + assert html.count("</h3>") == 2 |
| 68 | + assert html.count("</table>") == 2 |
| 69 | + |
| 70 | + |
| 71 | +class TestFencedCodeBlocks: |
| 72 | + """Hash characters inside code fences must not become headings.""" |
| 73 | + |
| 74 | + @pytest.mark.asyncio |
| 75 | + async def test_hash_in_backtick_fence(self, converter): |
| 76 | + md = "```python\n# comment\ndef foo():\n pass\n```" |
| 77 | + html = await converter.markdown_to_html(md) |
| 78 | + assert "<h1>" not in html |
| 79 | + assert "# comment" in html |
| 80 | + |
| 81 | + @pytest.mark.asyncio |
| 82 | + async def test_hash_in_tilde_fence(self, converter): |
| 83 | + md = "~~~\n# not a heading\n~~~" |
| 84 | + html = await converter.markdown_to_html(md) |
| 85 | + assert "<h1>" not in html |
| 86 | + |
| 87 | + @pytest.mark.asyncio |
| 88 | + async def test_heading_after_code_block(self, converter): |
| 89 | + md = "```\ncode\n```\n## Heading" |
| 90 | + html = await converter.markdown_to_html(md) |
| 91 | + assert "<h2>" in html |
| 92 | + |
| 93 | + |
| 94 | +class TestInlineFormatting: |
| 95 | + """Bold, italic, and other inline syntax.""" |
| 96 | + |
| 97 | + @pytest.mark.asyncio |
| 98 | + async def test_bold_text(self, converter): |
| 99 | + html = await converter.markdown_to_html("**bold text**") |
| 100 | + assert "<strong>" in html |
| 101 | + |
| 102 | + @pytest.mark.asyncio |
| 103 | + async def test_italic_text(self, converter): |
| 104 | + html = await converter.markdown_to_html("*italic*") |
| 105 | + assert "<em>" in html |
| 106 | + |
| 107 | + |
| 108 | +class TestRealisticLLMOutput: |
| 109 | + """End-to-end test with actual LLM output patterns.""" |
| 110 | + |
| 111 | + @pytest.mark.asyncio |
| 112 | + async def test_contract_comparison_report(self, converter): |
| 113 | + md = """ # Bericht zum Vertragsvergleich |
| 114 | +
|
| 115 | +**Dokumentenversionen:** file1.docx → file2.docx → file3.docx |
| 116 | +
|
| 117 | +**Anzahl analysierter Versionstransitionen:** 2 |
| 118 | +
|
| 119 | +--- |
| 120 | +
|
| 121 | +# Zentrale Erkenntnisse |
| 122 | +
|
| 123 | +### Verhandlungsverlauf |
| 124 | +
|
| 125 | +Der Verhandlungsprozess durchläuft einen fundamentalen Strukturwandel. |
| 126 | +
|
| 127 | +### Risikoverlagerungen |
| 128 | +
|
| 129 | +Die Risikoallokation erfährt eine bemerkenswerte Pendelbewegung. |
| 130 | +
|
| 131 | +### Methodik |
| 132 | +
|
| 133 | +Deterministischer Textvergleich auf Absatzebene.""" |
| 134 | + |
| 135 | + html = await converter.markdown_to_html(md) |
| 136 | + assert html.count("<h1>") == 2 |
| 137 | + assert html.count("<h3>") == 3 |
| 138 | + assert "<strong>" in html |
| 139 | + assert "<hr" in html |
| 140 | + |
| 141 | + @pytest.mark.asyncio |
| 142 | + async def test_frequency_table_section(self, converter): |
| 143 | + md = """ ### Am häufigsten verhandelte Klauseln |
| 144 | +
|
| 145 | +| Clause Family | Substantive | Editorial | Total | |
| 146 | +|---|---|---|---| |
| 147 | +| Allgemeine Bestimmungen | 6 | 3 | 11 | |
| 148 | +| Schadloshaltungen | 8 | 0 | 8 | |
| 149 | +
|
| 150 | +--- |
| 151 | +
|
| 152 | +# Detaillierte Evolutionsanalyse""" |
| 153 | + |
| 154 | + html = await converter.markdown_to_html(md) |
| 155 | + assert "<h3>" in html |
| 156 | + assert "<table>" in html |
| 157 | + assert "<h1>" in html |
| 158 | + assert "<hr" in html |
0 commit comments