free-claude-code/tests/test_robust_formatting.py
Cursor Agent 7496f67ae0 Phase 2: Extract markdown utilities to messaging/telegram_markdown.py
- Create messaging/telegram_markdown.py with all MarkdownV2 helpers
- Move escape_md_v2, escape_md_v2_code, escape_md_v2_link_url, mdv2_bold,
  mdv2_code_inline, format_status, render_markdown_to_mdv2
- Update handler.py to import from telegram_markdown (~300 lines removed)
- Update telegram.py to use shared escape_md_v2 (remove duplicate)
- Update tests to import from messaging.telegram_markdown

Co-authored-by: Ali Khokhar <alishahryar2@gmail.com>
2026-02-15 01:34:27 +00:00

70 lines
2 KiB
Python

import pytest
from unittest.mock import MagicMock
from messaging.transcript import TranscriptBuffer, RenderCtx
from messaging.telegram_markdown import (
escape_md_v2,
escape_md_v2_code,
mdv2_bold,
mdv2_code_inline,
render_markdown_to_mdv2,
)
@pytest.fixture
def handler():
platform = MagicMock()
cli = MagicMock()
store = MagicMock()
return (platform, cli, store)
def _ctx() -> RenderCtx:
return RenderCtx(
bold=mdv2_bold,
code_inline=mdv2_code_inline,
escape_code=escape_md_v2_code,
escape_text=escape_md_v2,
render_markdown=render_markdown_to_mdv2,
)
def test_truncation_closes_code_blocks(handler):
"""Verify that truncation correctly closes open code blocks."""
t = TranscriptBuffer()
t.apply(
{
"type": "thinking_chunk",
"text": "Starting some long thinking process that will definitely cause truncation later on...",
}
)
t.apply(
{
"type": "text_chunk",
"text": "```python\ndef very_long_function():\n # " + ("A" * 4000),
}
)
msg = t.render(_ctx(), limit_chars=3900, status="✅ *Complete*")
# The backtick count must be even to be a valid block.
assert msg.count("```") % 2 == 0
assert msg.endswith("```") or "✅ *Complete*" in msg.split("```")[-1]
def test_truncation_preserves_status(handler):
"""Verify that status is still appended after truncation."""
status = "READY_STATUS"
t = TranscriptBuffer()
t.apply({"type": "thinking_chunk", "text": "Thinking..."})
t.apply({"type": "text_chunk", "text": "A" * 5000})
msg = t.render(_ctx(), limit_chars=3900, status=status)
assert status in msg
def test_empty_components_with_status(handler):
"""Verify message building with just a status."""
status = "Simple Status"
t = TranscriptBuffer()
msg = t.render(_ctx(), limit_chars=3900, status=status)
assert msg == "\n\nSimple Status"