Files
DeepTutor/tests/services/partners/test_telegram_markdown_html.py
Hui 7fdadd76cf fix(partners): render every fenced code block as code in Telegram HTML
The Telegram converter protected code with ```[\w]*\n?([\s\S]*?)```,
so an info string such as c++ left "++" as the first code line, a ~~~
block was formatted as prose ("# setup" lost its hash, __init__ became
bold "init"), and a ```` fence showing a ``` example closed at the inner
fence, leaving an empty <pre> and the example leaking out as text.

Replace fenced blocks line by line first: open on ``` or ~~~ and close
on a bare run of the same character that is at least as long. One-line
```code``` and unclosed fences keep the previous regex handling. The
streaming preview strips fences through the same helper.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 21:09:54 -04:00

55 lines
1.6 KiB
Python

"""Fenced code blocks in the Telegram markdown -> HTML converter."""
from __future__ import annotations
import pytest
pytest.importorskip("telegram")
from deeptutor.partners.channels.telegram import ( # noqa: E402
_markdown_to_telegram_html,
_strip_md_block,
)
@pytest.mark.parametrize(
("markdown", "expected"),
[
pytest.param(
"```c++\nint main() {}\n```",
"<pre><code>int main() {}\n</code></pre>",
id="info-string-with-symbols",
),
pytest.param(
"~~~python\n# setup\ndef __init__(self): pass\n~~~",
"<pre><code># setup\ndef __init__(self): pass\n</code></pre>",
id="tilde-fence",
),
pytest.param(
"````markdown\n```python\nx = 1\n```\n````\nAfter **bold**",
"<pre><code>```python\nx = 1\n```\n</code></pre>\nAfter <b>bold</b>",
id="longer-fence-around-example",
),
],
)
def test_fenced_code_is_rendered_as_code(markdown: str, expected: str) -> None:
assert _markdown_to_telegram_html(markdown) == expected
@pytest.mark.parametrize(
("markdown", "expected"),
[
pytest.param(
"```python\nx = __y__\n```",
"<pre><code>x = __y__\n</code></pre>",
id="backtick-fence",
),
],
)
def test_backtick_fence_output_is_unchanged(markdown: str, expected: str) -> None:
assert _markdown_to_telegram_html(markdown) == expected
def test_streaming_preview_strips_tilde_fences() -> None:
assert _strip_md_block("~~~bash\nls\n~~~\nDone") == "ls\n\nDone"