|
| 1 | +"""Regression guard: send_slash_confirm must use format_message + MARKDOWN_V2.""" |
| 2 | + |
| 3 | +import sys |
| 4 | +from pathlib import Path |
| 5 | +from types import SimpleNamespace |
| 6 | +from unittest.mock import AsyncMock, MagicMock |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +_repo = str(Path(__file__).resolve().parents[2]) |
| 11 | +if _repo not in sys.path: |
| 12 | + sys.path.insert(0, _repo) |
| 13 | + |
| 14 | + |
| 15 | +def _ensure_telegram_mock(): |
| 16 | + if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"): |
| 17 | + return |
| 18 | + mod = MagicMock() |
| 19 | + mod.ext.ContextTypes.DEFAULT_TYPE = type(None) |
| 20 | + mod.constants.ParseMode.MARKDOWN = "Markdown" |
| 21 | + mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2" |
| 22 | + mod.constants.ParseMode.HTML = "HTML" |
| 23 | + mod.constants.ChatType.PRIVATE = "private" |
| 24 | + mod.constants.ChatType.GROUP = "group" |
| 25 | + mod.constants.ChatType.SUPERGROUP = "supergroup" |
| 26 | + mod.constants.ChatType.CHANNEL = "channel" |
| 27 | + mod.error.NetworkError = type("NetworkError", (OSError,), {}) |
| 28 | + mod.error.TimedOut = type("TimedOut", (OSError,), {}) |
| 29 | + mod.error.BadRequest = type("BadRequest", (Exception,), {}) |
| 30 | + for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"): |
| 31 | + sys.modules.setdefault(name, mod) |
| 32 | + sys.modules.setdefault("telegram.error", mod.error) |
| 33 | + |
| 34 | + |
| 35 | +_ensure_telegram_mock() |
| 36 | + |
| 37 | +from gateway.platforms.telegram import TelegramAdapter |
| 38 | +from gateway.config import PlatformConfig |
| 39 | + |
| 40 | + |
| 41 | +def _make_adapter(): |
| 42 | + config = PlatformConfig(enabled=True, token="test-token", extra={}) |
| 43 | + adapter = TelegramAdapter(config) |
| 44 | + adapter._bot = AsyncMock() |
| 45 | + adapter._app = MagicMock() |
| 46 | + return adapter |
| 47 | + |
| 48 | + |
| 49 | +class TestSendSlashConfirm: |
| 50 | + |
| 51 | + @pytest.mark.asyncio |
| 52 | + async def test_uses_markdown_v2_and_escapes_special_chars(self): |
| 53 | + """send_slash_confirm must pass preview through format_message and use |
| 54 | + MARKDOWN_V2 — so commands with underscores, dots, or brackets don't |
| 55 | + raise BadRequest: Can't parse entities.""" |
| 56 | + adapter = _make_adapter() |
| 57 | + sent = {} |
| 58 | + |
| 59 | + async def mock_send(**kwargs): |
| 60 | + sent.update(kwargs) |
| 61 | + return SimpleNamespace(message_id=7) |
| 62 | + |
| 63 | + adapter._bot.send_message = AsyncMock(side_effect=mock_send) |
| 64 | + |
| 65 | + result = await adapter.send_slash_confirm( |
| 66 | + chat_id="100", |
| 67 | + title="Confirm", |
| 68 | + message="/run script_name.sh --flag=value [option]", |
| 69 | + session_key="sk", |
| 70 | + confirm_id="cid1", |
| 71 | + ) |
| 72 | + |
| 73 | + assert result.success is True |
| 74 | + assert "MARKDOWN_V2" in repr(sent["parse_mode"]) |
| 75 | + # Underscores and dots must be escaped by format_message |
| 76 | + assert "script\\_name" in sent["text"] |
| 77 | + assert "\\." in sent["text"] |
| 78 | + |
| 79 | + @pytest.mark.asyncio |
| 80 | + async def test_stores_slash_confirm_state(self): |
| 81 | + adapter = _make_adapter() |
| 82 | + adapter._bot.send_message = AsyncMock( |
| 83 | + return_value=SimpleNamespace(message_id=8) |
| 84 | + ) |
| 85 | + |
| 86 | + await adapter.send_slash_confirm( |
| 87 | + chat_id="100", |
| 88 | + title="Confirm", |
| 89 | + message="reload-mcp", |
| 90 | + session_key="my-session", |
| 91 | + confirm_id="cid2", |
| 92 | + ) |
| 93 | + |
| 94 | + assert adapter._slash_confirm_state["cid2"] == "my-session" |
| 95 | + |
| 96 | + @pytest.mark.asyncio |
| 97 | + async def test_not_connected_returns_failure(self): |
| 98 | + adapter = _make_adapter() |
| 99 | + adapter._bot = None |
| 100 | + |
| 101 | + result = await adapter.send_slash_confirm( |
| 102 | + chat_id="100", |
| 103 | + title="Confirm", |
| 104 | + message="reload-mcp", |
| 105 | + session_key="sk", |
| 106 | + confirm_id="cid3", |
| 107 | + ) |
| 108 | + |
| 109 | + assert result.success is False |
0 commit comments