"""Tests for Markdown -> Telegram HTML conversion."""
from __future__ import annotations
from integrations.telegram.formatting import (
looks_like_telegram_html,
markdown_to_telegram_html,
)
def test_bold_becomes_b_tag() -> None:
assert (
markdown_to_telegram_html("- **CLI / shell** questions") == "• CLI / shell questions"
)
def test_double_underscore_bold() -> None:
assert markdown_to_telegram_html("__strong__") == "strong"
def test_inline_code_becomes_code_tag() -> None:
assert markdown_to_telegram_html("you have `github` connected") == (
"you have github connected"
)
def test_italic_underscore() -> None:
assert markdown_to_telegram_html("_also_") == "also"
def test_slack_single_star_bold() -> None:
assert markdown_to_telegram_html("*Blocked PRs:*") == "Blocked PRs:"
def test_link_becomes_anchor() -> None:
assert markdown_to_telegram_html("[docs](https://x.com/a)") == (
'docs'
)
def test_slack_link_becomes_anchor() -> None:
assert markdown_to_telegram_html("") == (
'#3811'
)
def test_html_special_chars_are_escaped() -> None:
assert markdown_to_telegram_html("a < b & c > d") == "a < b & c > d"
def test_code_content_is_escaped_and_not_reformatted() -> None:
assert markdown_to_telegram_html("`x = a ** b < c`") == "x = a ** b < c"
def test_fenced_code_block_becomes_pre() -> None:
out = markdown_to_telegram_html("```py\nx = 1 < 2\n```")
assert out == "x = 1 < 2\n
"
def test_unbalanced_markdown_left_as_text() -> None:
assert markdown_to_telegram_html("**oops no close") == "**oops no close"
def test_plain_text_passthrough() -> None:
assert markdown_to_telegram_html("just plain words") == "just plain words"
def test_atx_header_becomes_bold() -> None:
assert markdown_to_telegram_html("## Configured Monitors") == "Configured Monitors"
def test_horizontal_rule_becomes_blank_line() -> None:
assert markdown_to_telegram_html("before\n---\nafter") == "before\n\nafter"
def test_bullet_list_uses_bullet_glyph() -> None:
assert markdown_to_telegram_html("- Triggers when CPU idle drops below 10%") == (
"• Triggers when CPU idle drops below 10%"
)
def test_numbered_list_preserved() -> None:
assert markdown_to_telegram_html("1. **List open pull requests**") == (
"1. List open pull requests"
)
def test_markdown_table_becomes_mobile_friendly_rows() -> None:
text = (
"| Monitor | Type | Current State |\n"
"|---|---|---|\n"
"| CPU usage is high | Query Alert | No Data |"
)
out = markdown_to_telegram_html(text)
assert out == ("• CPU usage is high — Query Alert · No Data")
def test_pr_table_becomes_mobile_friendly_rows() -> None:
text = (
"| # | Title | Author | Status | Checks |\n"
"|---|---|---|---|---|\n"
"| 3811 | docs: fix stale cli | Aditya | Blocked | ❌ Failed |"
)
out = markdown_to_telegram_html(text)
assert out == ("• 3811 — docs: fix stale cli · Aditya · Blocked · ❌ Failed")
def test_pre_rendered_html_is_left_untouched() -> None:
html = "Daily Reliability Summary\n\nGenerated by OpenSRE"
assert markdown_to_telegram_html(html) == html
assert looks_like_telegram_html(html) is True
def test_double_star_bold_is_not_wrapped_twice() -> None:
text = "• **Resolve** — Identifies which integrations to query"
out = markdown_to_telegram_html(text)
assert out == "• Resolve — Identifies which integrations to query"
assert out.count("") == 1
assert "**" not in out
def test_investigation_pipeline_bullets_render_once() -> None:
text = (
"When you trigger an investigation, it runs through these stages:\n\n"
"• **Resolve** — Identifies which integrations to query\n"
"• **Intake** — Parses the raw alert into structured state\n"
"• **Gather evidence** — AI agent queries your connected tools\n"
"• **Diagnose** — Extracts structured RCA fields\n"
"• **Report** — Delivers findings to terminal, Slack, GitLab, etc.\n\n"
"Your current session"
)
out = markdown_to_telegram_html(text)
assert out.count("Resolve") == 1
assert out.count("Intake") == 1
assert out.count("Gather evidence") == 1
assert "• Resolve — Identifies which integrations to query" in out
assert "**" not in out
def test_triple_star_bold_does_not_double_wrap() -> None:
out = markdown_to_telegram_html("***Resolve***")
assert out.count("") == 1
assert "" not in out