The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor: remove 27 duplicate _ensure_telegram_mock() copies from gateway tests
kshitijk4poorc1693d7dcc71
diff --git a/tests/gateway/test_gateway_platform_event_hook.py b/tests/gateway/test_gateway_platform_event_hook.pyindex 694d3924411..e65dc1418f5 100644--- a/tests/gateway/test_gateway_platform_event_hook.py+++ b/tests/gateway/test_gateway_platform_event_hook.py@@ -36,26 +36,6 @@ if _repo not in sys.path:sys.path.insert(0, _repo)-# ----------------------------------------------------------------------------# python-telegram-bot is an optional dep; mock it so the adapter imports-# (same shim as test_telegram_network_reconnect / test_telegram_plugin_handlers).-# ----------------------------------------------------------------------------def _ensure_telegram_mock() -> None:- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return- telegram_mod = MagicMock()- telegram_mod.ext.ContextTypes.DEFAULT_TYPE = type(None)- telegram_mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2"- telegram_mod.constants.ChatType.GROUP = "group"- telegram_mod.constants.ChatType.SUPERGROUP = "supergroup"- telegram_mod.constants.ChatType.CHANNEL = "channel"- telegram_mod.constants.ChatType.PRIVATE = "private"- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, telegram_mod)---_ensure_telegram_mock()-from plugins.platforms.telegram.adapter import TelegramAdapter # noqa: E402from gateway.run import GatewayRunner # noqa: E402from gateway.profile_routing import ProfileRoute # noqa: E402diff --git a/tests/gateway/test_send_image_file.py b/tests/gateway/test_send_image_file.pyindex 103ea2fb54a..374dc1dd5c2 100644--- a/tests/gateway/test_send_image_file.py+++ b/tests/gateway/test_send_image_file.py@@ -42,27 +42,6 @@ class TestExtractMediaImages:# ---------------------------------------------------------------------------# Telegram send_image_file tests# ------------------------------------------------------------------------------def _ensure_telegram_mock():- """Install mock telegram modules so TelegramAdapter can be imported."""- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return-- telegram_mod = MagicMock()- telegram_mod.ext.ContextTypes.DEFAULT_TYPE = type(None)- telegram_mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2"- telegram_mod.constants.ChatType.GROUP = "group"- telegram_mod.constants.ChatType.SUPERGROUP = "supergroup"- telegram_mod.constants.ChatType.CHANNEL = "channel"- telegram_mod.constants.ChatType.PRIVATE = "private"-- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, telegram_mod)---_ensure_telegram_mock()-from plugins.platforms.telegram.adapter import TelegramAdapter # noqa: E402diff --git a/tests/gateway/test_send_multiple_images.py b/tests/gateway/test_send_multiple_images.pyindex 3bff6b79d09..2773b9f770e 100644--- a/tests/gateway/test_send_multiple_images.py+++ b/tests/gateway/test_send_multiple_images.py@@ -88,27 +88,6 @@ class TestBaseDefaultLoop:assert a.sent_files[0][1] == "/tmp/foo.png"-# ----------------------------------------------------------------------------# Telegram mocks setup (shared with test_send_image_file pattern)-# ------------------------------------------------------------------------------def _ensure_telegram_mock():- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return- telegram_mod = MagicMock()- telegram_mod.ext.ContextTypes.DEFAULT_TYPE = type(None)- telegram_mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2"- telegram_mod.constants.ChatType.GROUP = "group"- telegram_mod.constants.ChatType.SUPERGROUP = "supergroup"- telegram_mod.constants.ChatType.CHANNEL = "channel"- telegram_mod.constants.ChatType.PRIVATE = "private"- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, telegram_mod)---_ensure_telegram_mock()-from plugins.platforms.telegram.adapter import TelegramAdapter # noqa: E402diff --git a/tests/gateway/test_telegram_approval_buttons.py b/tests/gateway/test_telegram_approval_buttons.pyindex 0f6ba60db35..c9dcf38edd4 100644--- a/tests/gateway/test_telegram_approval_buttons.py+++ b/tests/gateway/test_telegram_approval_buttons.py@@ -16,36 +16,6 @@ if _repo not in sys.path:sys.path.insert(0, _repo)-# ----------------------------------------------------------------------------# Minimal Telegram mock so TelegramAdapter can be imported-# ----------------------------------------------------------------------------def _ensure_telegram_mock():- """Wire up the minimal mocks required to import TelegramAdapter."""- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return-- mod = MagicMock()- mod.ext.ContextTypes.DEFAULT_TYPE = type(None)- mod.constants.ParseMode.MARKDOWN = "Markdown"- mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2"- mod.constants.ParseMode.HTML = "HTML"- mod.constants.ChatType.PRIVATE = "private"- mod.constants.ChatType.GROUP = "group"- mod.constants.ChatType.SUPERGROUP = "supergroup"- mod.constants.ChatType.CHANNEL = "channel"- # Provide real exception classes so ``except (NetworkError, ...)`` in- # connect() doesn't blow up under xdist when this mock leaks.- mod.error.NetworkError = type("NetworkError", (OSError,), {})- mod.error.TimedOut = type("TimedOut", (OSError,), {})- mod.error.BadRequest = type("BadRequest", (Exception,), {})-- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, mod)- sys.modules.setdefault("telegram.error", mod.error)---_ensure_telegram_mock()-from plugins.platforms.telegram.adapter import TelegramAdapterfrom gateway.config import Platform, PlatformConfigdiff --git a/tests/gateway/test_telegram_clarify_buttons.py b/tests/gateway/test_telegram_clarify_buttons.pyindex 7d8bdbd0d69..a8a02f07659 100644--- a/tests/gateway/test_telegram_clarify_buttons.py+++ b/tests/gateway/test_telegram_clarify_buttons.py@@ -23,30 +23,6 @@ if _repo not in sys.path:# Minimal Telegram mock so TelegramAdapter can be imported (mirrors# test_telegram_approval_buttons.py)# ----------------------------------------------------------------------------def _ensure_telegram_mock():- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return-- mod = MagicMock()- mod.ext.ContextTypes.DEFAULT_TYPE = type(None)- mod.constants.ParseMode.MARKDOWN = "Markdown"- mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2"- mod.constants.ParseMode.HTML = "HTML"- mod.constants.ChatType.PRIVATE = "private"- mod.constants.ChatType.GROUP = "group"- mod.constants.ChatType.SUPERGROUP = "supergroup"- mod.constants.ChatType.CHANNEL = "channel"- mod.error.NetworkError = type("NetworkError", (OSError,), {})- mod.error.TimedOut = type("TimedOut", (OSError,), {})- mod.error.BadRequest = type("BadRequest", (Exception,), {})-- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, mod)- sys.modules.setdefault("telegram.error", mod.error)---_ensure_telegram_mock()-from plugins.platforms.telegram.adapter import TelegramAdapterfrom gateway.config import PlatformConfigdiff --git a/tests/gateway/test_telegram_closewait_limits_31599.py b/tests/gateway/test_telegram_closewait_limits_31599.pyindex 3c46bf5760a..cc3e71df8f9 100644--- a/tests/gateway/test_telegram_closewait_limits_31599.py+++ b/tests/gateway/test_telegram_closewait_limits_31599.py@@ -26,31 +26,12 @@ client-level limits when a custom transport is supplied."""import asyncio-import sys-from unittest.mock import MagicMock, patch+from unittest.mock import MagicMockimport httpximport pytestfrom gateway.config import PlatformConfig---def _ensure_telegram_mock():- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return- telegram_mod = MagicMock()- telegram_mod.ext.ContextTypes.DEFAULT_TYPE = type(None)- telegram_mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2"- telegram_mod.constants.ChatType.GROUP = "group"- telegram_mod.constants.ChatType.SUPERGROUP = "supergroup"- telegram_mod.constants.ChatType.CHANNEL = "channel"- telegram_mod.constants.ChatType.PRIVATE = "private"- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, telegram_mod)---_ensure_telegram_mock()-from plugins.platforms.telegram import adapter as tg_adapter # noqa: E402from plugins.platforms.telegram.adapter import TelegramAdapter # noqa: E402diff --git a/tests/gateway/test_telegram_conflict.py b/tests/gateway/test_telegram_conflict.pyindex 711f8748541..4fc9c6db951 100644--- a/tests/gateway/test_telegram_conflict.py+++ b/tests/gateway/test_telegram_conflict.py@@ -1,39 +1,10 @@import asyncio-import sysfrom types import SimpleNamespacefrom unittest.mock import AsyncMock, MagicMockimport pytestfrom gateway.config import PlatformConfig---def _ensure_telegram_mock():- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return-- telegram_mod = MagicMock()- telegram_mod.ext.ContextTypes.DEFAULT_TYPE = type(None)- telegram_mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2"- telegram_mod.constants.ChatType.GROUP = "group"- telegram_mod.constants.ChatType.SUPERGROUP = "supergroup"- telegram_mod.constants.ChatType.CHANNEL = "channel"- telegram_mod.constants.ChatType.PRIVATE = "private"-- # Provide real exception classes so ``except (NetworkError, ...)`` in- # connect() doesn't blow up with "catching classes that do not inherit- # from BaseException" when another xdist worker pollutes sys.modules.- telegram_mod.error.NetworkError = type("NetworkError", (OSError,), {})- telegram_mod.error.TimedOut = type("TimedOut", (OSError,), {})- telegram_mod.error.BadRequest = type("BadRequest", (Exception,), {})-- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, telegram_mod)- sys.modules.setdefault("telegram.error", telegram_mod.error)---_ensure_telegram_mock()-from plugins.platforms.telegram.adapter import TelegramAdapter # noqa: E402diff --git a/tests/gateway/test_telegram_connect.py b/tests/gateway/test_telegram_connect.pyindex 801d9a406ab..721323f6ec7 100644--- a/tests/gateway/test_telegram_connect.py+++ b/tests/gateway/test_telegram_connect.py@@ -5,37 +5,10 @@ must set a non-retryable fatal error so the gateway does not queue it forbackground reconnection (#31049)."""-import sys-from unittest.mock import MagicMockimport pytestfrom gateway.config import PlatformConfig---def _ensure_telegram_mock():- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return-- telegram_mod = MagicMock()- telegram_mod.ext.ContextTypes.DEFAULT_TYPE = type(None)- telegram_mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2"- telegram_mod.constants.ChatType.GROUP = "group"- telegram_mod.constants.ChatType.SUPERGROUP = "supergroup"- telegram_mod.constants.ChatType.CHANNEL = "channel"- telegram_mod.constants.ChatType.PRIVATE = "private"-- telegram_mod.error.NetworkError = type("NetworkError", (OSError,), {})- telegram_mod.error.TimedOut = type("TimedOut", (OSError,), {})- telegram_mod.error.BadRequest = type("BadRequest", (Exception,), {})-- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, telegram_mod)- sys.modules.setdefault("telegram.error", telegram_mod.error)---_ensure_telegram_mock()-import plugins.platforms.telegram.adapter as telegram_mod # noqa: E402from plugins.platforms.telegram.adapter import TelegramAdapter # noqa: E402diff --git a/tests/gateway/test_telegram_documents.py b/tests/gateway/test_telegram_documents.pyindex 6a537fd38b4..a82003ce9d0 100644--- a/tests/gateway/test_telegram_documents.py+++ b/tests/gateway/test_telegram_documents.py@@ -10,7 +10,6 @@ We mock the telegram module at import time to avoid collection errors.import asyncioimport os-import sysfrom types import SimpleNamespacefrom unittest.mock import AsyncMock, MagicMock, patch@@ -28,28 +27,6 @@ from gateway.platforms.base import (# ---------------------------------------------------------------------------# Mock the telegram package if it's not installed# -----------------------------------------------------------------------------def _ensure_telegram_mock():- """Install mock telegram modules so TelegramAdapter can be imported."""- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- # Real library is installed — no mocking needed- return-- telegram_mod = MagicMock()- # ContextTypes needs DEFAULT_TYPE as an actual attribute for the annotation- telegram_mod.ext.ContextTypes.DEFAULT_TYPE = type(None)- telegram_mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2"- telegram_mod.constants.ChatType.GROUP = "group"- telegram_mod.constants.ChatType.SUPERGROUP = "supergroup"- telegram_mod.constants.ChatType.CHANNEL = "channel"- telegram_mod.constants.ChatType.PRIVATE = "private"-- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, telegram_mod)---_ensure_telegram_mock()-# Now we can safely importfrom plugins.platforms.telegram.adapter import TelegramAdapter # noqa: E402diff --git a/tests/gateway/test_telegram_format.py b/tests/gateway/test_telegram_format.pyindex ab6fb4d4996..2195179b28f 100644--- a/tests/gateway/test_telegram_format.py+++ b/tests/gateway/test_telegram_format.py@@ -6,7 +6,6 @@ or corrupt user-visible content."""import re-import sysfrom types import SimpleNamespacefrom unittest.mock import AsyncMock, MagicMock@@ -18,23 +17,6 @@ from gateway.config import PlatformConfig# ---------------------------------------------------------------------------# Mock the telegram package if it's not installed# -----------------------------------------------------------------------------def _ensure_telegram_mock():- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return- mod = MagicMock()- mod.ext.ContextTypes.DEFAULT_TYPE = type(None)- mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2"- mod.constants.ChatType.GROUP = "group"- mod.constants.ChatType.SUPERGROUP = "supergroup"- mod.constants.ChatType.CHANNEL = "channel"- mod.constants.ChatType.PRIVATE = "private"- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, mod)---_ensure_telegram_mock()-from plugins.platforms.telegram.adapter import ( # noqa: E402TelegramAdapter,_escape_mdv2,diff --git a/tests/gateway/test_telegram_init_deadline.py b/tests/gateway/test_telegram_init_deadline.pyindex 643187c552e..c62882947d7 100644--- a/tests/gateway/test_telegram_init_deadline.py+++ b/tests/gateway/test_telegram_init_deadline.py@@ -1,30 +1,6 @@-import sys-from unittest.mock import AsyncMock, MagicMock-import pytestfrom gateway.config import PlatformConfig---def _ensure_telegram_mock():- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return- telegram_mod = MagicMock()- telegram_mod.ext.ContextTypes.DEFAULT_TYPE = type(None)- telegram_mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2"- telegram_mod.constants.ChatType.GROUP = "group"- telegram_mod.constants.ChatType.SUPERGROUP = "supergroup"- telegram_mod.constants.ChatType.CHANNEL = "channel"- telegram_mod.constants.ChatType.PRIVATE = "private"- telegram_mod.error.NetworkError = type("NetworkError", (OSError,), {})- telegram_mod.error.TimedOut = type("TimedOut", (OSError,), {})- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, telegram_mod)- sys.modules.setdefault("telegram.error", telegram_mod.error)---_ensure_telegram_mock()-from plugins.platforms.telegram import adapter as tg_adapter # noqa: E402from plugins.platforms.telegram.adapter import TelegramAdapter # noqa: E402diff --git a/tests/gateway/test_telegram_max_doc_bytes.py b/tests/gateway/test_telegram_max_doc_bytes.pyindex 34aab9a01ff..8d361d229ce 100644--- a/tests/gateway/test_telegram_max_doc_bytes.py+++ b/tests/gateway/test_telegram_max_doc_bytes.py@@ -5,30 +5,8 @@ The public Telegram Bot API caps `getFile` at 20MB. A locally-hostedof `extra.base_url` as the explicit opt-in to the higher cap."""-import sys-from unittest.mock import MagicMockfrom gateway.config import PlatformConfig---def _ensure_telegram_mock():- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return-- telegram_mod = MagicMock()- telegram_mod.ext.ContextTypes.DEFAULT_TYPE = type(None)- telegram_mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2"- telegram_mod.constants.ChatType.GROUP = "group"- telegram_mod.constants.ChatType.SUPERGROUP = "supergroup"- telegram_mod.constants.ChatType.CHANNEL = "channel"- telegram_mod.constants.ChatType.PRIVATE = "private"-- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, telegram_mod)---_ensure_telegram_mock()-from plugins.platforms.telegram.adapter import TelegramAdapter # noqa: E402diff --git a/tests/gateway/test_telegram_media_read_timeout.py b/tests/gateway/test_telegram_media_read_timeout.pyindex 941faa5b775..017245a6d8a 100644--- a/tests/gateway/test_telegram_media_read_timeout.py+++ b/tests/gateway/test_telegram_media_read_timeout.py@@ -15,38 +15,9 @@ that actually reaches the Bot API.from __future__ import annotations-import sysfrom unittest.mock import AsyncMock, MagicMockimport pytest---def _ensure_telegram_mock():- """Install mock telegram modules only when the real library is absent.-- Probe with a real import rather than inspecting ``sys.modules``: when PTB- is installed but not yet imported, a ``sys.modules`` check alone would- install the mock first and poison every later test that needs the real- library (``tests/test_telegram_polling_progress_ptb.py`` exercises the- genuine ``BaseRequest``).- """- try:- import telegram # noqa: F401-- return- except ImportError:- pass- telegram_mod = MagicMock()- telegram_mod.ext.ContextTypes.DEFAULT_TYPE = type(None)- telegram_mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2"- for name in ("GROUP", "SUPERGROUP", "CHANNEL", "PRIVATE"):- setattr(telegram_mod.constants.ChatType, name, name.lower())- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, telegram_mod)---_ensure_telegram_mock()-from gateway.config import PlatformConfig # noqa: E402from plugins.platforms.telegram import adapter as tg # noqa: E402from plugins.platforms.telegram.adapter import TelegramAdapter # noqa: E402diff --git a/tests/gateway/test_telegram_model_picker.py b/tests/gateway/test_telegram_model_picker.pyindex 72a0891245f..259aaa072a5 100644--- a/tests/gateway/test_telegram_model_picker.py+++ b/tests/gateway/test_telegram_model_picker.py@@ -1,36 +1,9 @@"""Tests for Telegram model picker thread fallback."""-import sysfrom types import SimpleNamespacefrom unittest.mock import AsyncMock, MagicMockimport pytest---def _ensure_telegram_mock():- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return-- mod = MagicMock()- mod.ext.ContextTypes.DEFAULT_TYPE = type(None)- mod.constants.ParseMode.MARKDOWN = "Markdown"- mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2"- mod.constants.ParseMode.HTML = "HTML"- mod.constants.ChatType.PRIVATE = "private"- mod.constants.ChatType.GROUP = "group"- mod.constants.ChatType.SUPERGROUP = "supergroup"- mod.constants.ChatType.CHANNEL = "channel"- mod.error.NetworkError = type("NetworkError", (OSError,), {})- mod.error.TimedOut = type("TimedOut", (OSError,), {})- mod.error.BadRequest = type("BadRequest", (Exception,), {})-- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, mod)- sys.modules.setdefault("telegram.error", mod.error)---_ensure_telegram_mock()-from gateway.config import PlatformConfigfrom plugins.platforms.telegram.adapter import TelegramAdapterdiff --git a/tests/gateway/test_telegram_network_reconnect.py b/tests/gateway/test_telegram_network_reconnect.pyindex 20681d6683e..73b4b1611ee 100644--- a/tests/gateway/test_telegram_network_reconnect.py+++ b/tests/gateway/test_telegram_network_reconnect.py@@ -9,32 +9,11 @@ rather than silently leaving polling dead.import astimport asynciofrom pathlib import Path-import sysfrom unittest.mock import AsyncMock, MagicMock, patchimport pytestfrom gateway.config import GatewayConfig, Platform, PlatformConfig---def _ensure_telegram_mock():- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return-- telegram_mod = MagicMock()- telegram_mod.ext.ContextTypes.DEFAULT_TYPE = type(None)- telegram_mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2"- telegram_mod.constants.ChatType.GROUP = "group"- telegram_mod.constants.ChatType.SUPERGROUP = "supergroup"- telegram_mod.constants.ChatType.CHANNEL = "channel"- telegram_mod.constants.ChatType.PRIVATE = "private"-- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, telegram_mod)---_ensure_telegram_mock()-from plugins.platforms.telegram import adapter as tg_adapter # noqa: E402from plugins.platforms.telegram.adapter import TelegramAdapter # noqa: E402from gateway.run import GatewayRunner # noqa: E402diff --git a/tests/gateway/test_telegram_pending_update_probe.py b/tests/gateway/test_telegram_pending_update_probe.pyindex cd8d3ba5711..e5f2ac97425 100644--- a/tests/gateway/test_telegram_pending_update_probe.py+++ b/tests/gateway/test_telegram_pending_update_probe.py@@ -12,28 +12,11 @@ entirely (``running=False``) with no reconnect in flight — the long-poll taskis gone, so the gateway silently stops receiving messages while the processstays alive (#55769) — and feeds it into the same recovery ladder."""-import sysfrom unittest.mock import AsyncMock, MagicMock, patchimport pytestfrom gateway.config import PlatformConfig---def _ensure_telegram_mock():- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return- mod = MagicMock()- mod.error.NetworkError = type("NetworkError", (OSError,), {})- mod.error.TimedOut = type("TimedOut", (OSError,), {})- mod.error.BadRequest = type("BadRequest", (Exception,), {})- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, mod)- sys.modules.setdefault("telegram.error", mod.error)---_ensure_telegram_mock()-from plugins.platforms.telegram.adapter import TelegramAdapter # noqa: E402diff --git a/tests/gateway/test_telegram_polling_health_confirmation.py b/tests/gateway/test_telegram_polling_health_confirmation.pyindex 74be6364feb..612ad8467d6 100644--- a/tests/gateway/test_telegram_polling_health_confirmation.py+++ b/tests/gateway/test_telegram_polling_health_confirmation.py@@ -10,28 +10,6 @@ reconnect is a reliable hung-poll signature.import asyncioimport logging-import sys-from unittest.mock import MagicMock--def _ensure_telegram_mock():- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return- telegram_mod = MagicMock()- telegram_mod.ext.ContextTypes.DEFAULT_TYPE = type(None)- telegram_mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2"- telegram_mod.constants.ChatType.GROUP = "group"- telegram_mod.constants.ChatType.SUPERGROUP = "supergroup"- telegram_mod.constants.ChatType.CHANNEL = "channel"- telegram_mod.constants.ChatType.PRIVATE = "private"- telegram_mod.error.NetworkError = type("NetworkError", (OSError,), {})- telegram_mod.error.TimedOut = type("TimedOut", (OSError,), {})- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, telegram_mod)- sys.modules.setdefault("telegram.error", telegram_mod.error)---_ensure_telegram_mock()-from gateway.config import Platform # noqa: E402from plugins.platforms.telegram.adapter import TelegramAdapter # noqa: E402diff --git a/tests/gateway/test_telegram_reply_mode.py b/tests/gateway/test_telegram_reply_mode.pyindex f37a5b8f898..ce96d0f791d 100644--- a/tests/gateway/test_telegram_reply_mode.py+++ b/tests/gateway/test_telegram_reply_mode.py@@ -6,31 +6,11 @@ Covers the threading behavior control for multi-chunk replies:- "all": All chunks thread to original message"""import os-import sysfrom unittest.mock import MagicMock, AsyncMock, patchimport pytestfrom gateway.config import PlatformConfig, GatewayConfig, Platform, _apply_env_overrides, load_gateway_config---def _ensure_telegram_mock():- """Mock the telegram package if it's not installed."""- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return- mod = MagicMock()- mod.ext.ContextTypes.DEFAULT_TYPE = type(None)- mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2"- mod.constants.ChatType.GROUP = "group"- mod.constants.ChatType.SUPERGROUP = "supergroup"- mod.constants.ChatType.CHANNEL = "channel"- mod.constants.ChatType.PRIVATE = "private"- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, mod)---_ensure_telegram_mock()-from plugins.platforms.telegram.adapter import TelegramAdapter # noqa: E402diff --git a/tests/gateway/test_telegram_reply_quote.py b/tests/gateway/test_telegram_reply_quote.pyindex 4238192673e..7150ed1dc8e 100644--- a/tests/gateway/test_telegram_reply_quote.py+++ b/tests/gateway/test_telegram_reply_quote.py@@ -8,31 +8,9 @@ message as ``reply_to_text``, which can cause it to act on unrelatedactionable-looking text the user did not quote (#22619)."""-import sysfrom types import SimpleNamespace-from unittest.mock import MagicMockfrom gateway.config import PlatformConfig---def _ensure_telegram_mock():- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return-- telegram_mod = MagicMock()- telegram_mod.ext.ContextTypes.DEFAULT_TYPE = type(None)- telegram_mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2"- telegram_mod.constants.ChatType.GROUP = "group"- telegram_mod.constants.ChatType.SUPERGROUP = "supergroup"- telegram_mod.constants.ChatType.CHANNEL = "channel"- telegram_mod.constants.ChatType.PRIVATE = "private"-- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, telegram_mod)---_ensure_telegram_mock()-from plugins.platforms.telegram.adapter import TelegramAdapter # noqa: E402diff --git a/tests/gateway/test_telegram_send_draft_format.py b/tests/gateway/test_telegram_send_draft_format.pyindex 1588901615f..91e2a2ec867 100644--- a/tests/gateway/test_telegram_send_draft_format.py+++ b/tests/gateway/test_telegram_send_draft_format.py@@ -13,28 +13,11 @@ These tests pin:killing draft streaming for the whole response.3. A non-BadRequest failure propagates so the caller falls back to edit."""-import sysfrom unittest.mock import AsyncMock, MagicMockimport pytestfrom gateway.config import PlatformConfig---def _ensure_telegram_mock():- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return- mod = MagicMock()- mod.error.NetworkError = type("NetworkError", (OSError,), {})- mod.error.TimedOut = type("TimedOut", (OSError,), {})- mod.error.BadRequest = type("BadRequest", (Exception,), {})- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, mod)- sys.modules.setdefault("telegram.error", mod.error)---_ensure_telegram_mock()-import plugins.platforms.telegram.adapter as tg_mod # noqa: E402from plugins.platforms.telegram.adapter import TelegramAdapter # noqa: E402diff --git a/tests/gateway/test_telegram_send_path_health.py b/tests/gateway/test_telegram_send_path_health.pyindex b449c278684..f172021f626 100644--- a/tests/gateway/test_telegram_send_path_health.py+++ b/tests/gateway/test_telegram_send_path_health.py@@ -5,28 +5,11 @@ can enter a wedged state where ``bot.send_message()`` returns a valid Messagebut nothing reaches the recipient. ``_send_path_degraded`` short-circuits``send()`` so cron's live-adapter branch falls through to standalone HTTP."""-import sysfrom unittest.mock import AsyncMock, MagicMock, patchimport pytestfrom gateway.config import PlatformConfig---def _ensure_telegram_mock():- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return- mod = MagicMock()- mod.error.NetworkError = type("NetworkError", (OSError,), {})- mod.error.TimedOut = type("TimedOut", (OSError,), {})- mod.error.BadRequest = type("BadRequest", (Exception,), {})- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, mod)- sys.modules.setdefault("telegram.error", mod.error)---_ensure_telegram_mock()-from plugins.platforms.telegram.adapter import TelegramAdapter # noqa: E402diff --git a/tests/gateway/test_telegram_slash_confirm.py b/tests/gateway/test_telegram_slash_confirm.pyindex bed23d4cd0f..7d7f6a27a60 100644--- a/tests/gateway/test_telegram_slash_confirm.py+++ b/tests/gateway/test_telegram_slash_confirm.py@@ -10,30 +10,6 @@ import pytest_repo = str(Path(__file__).resolve().parents[2])if _repo not in sys.path:sys.path.insert(0, _repo)---def _ensure_telegram_mock():- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return- mod = MagicMock()- mod.ext.ContextTypes.DEFAULT_TYPE = type(None)- mod.constants.ParseMode.MARKDOWN = "Markdown"- mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2"- mod.constants.ParseMode.HTML = "HTML"- mod.constants.ChatType.PRIVATE = "private"- mod.constants.ChatType.GROUP = "group"- mod.constants.ChatType.SUPERGROUP = "supergroup"- mod.constants.ChatType.CHANNEL = "channel"- mod.error.NetworkError = type("NetworkError", (OSError,), {})- mod.error.TimedOut = type("TimedOut", (OSError,), {})- mod.error.BadRequest = type("BadRequest", (Exception,), {})- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, mod)- sys.modules.setdefault("telegram.error", mod.error)---_ensure_telegram_mock()-from plugins.platforms.telegram.adapter import TelegramAdapterfrom gateway.config import PlatformConfigdiff --git a/tests/gateway/test_telegram_start_polling_timeout.py b/tests/gateway/test_telegram_start_polling_timeout.pyindex 8228eaae975..c2037cc2a35 100644--- a/tests/gateway/test_telegram_start_polling_timeout.py+++ b/tests/gateway/test_telegram_start_polling_timeout.py@@ -14,31 +14,9 @@ feeds the existing retry ladder. These tests patch the timeout down to keep thesuite fast."""import asyncio-import sysfrom unittest.mock import AsyncMock, MagicMock, patchimport pytest---def _ensure_telegram_mock():- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return- telegram_mod = MagicMock()- telegram_mod.ext.ContextTypes.DEFAULT_TYPE = type(None)- telegram_mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2"- telegram_mod.constants.ChatType.GROUP = "group"- telegram_mod.constants.ChatType.SUPERGROUP = "supergroup"- telegram_mod.constants.ChatType.CHANNEL = "channel"- telegram_mod.constants.ChatType.PRIVATE = "private"- telegram_mod.error.NetworkError = type("NetworkError", (OSError,), {})- telegram_mod.error.TimedOut = type("TimedOut", (OSError,), {})- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, telegram_mod)- sys.modules.setdefault("telegram.error", telegram_mod.error)---_ensure_telegram_mock()-from plugins.platforms.telegram import adapter as tg_adapter # noqa: E402from plugins.platforms.telegram.adapter import TelegramAdapter # noqa: E402diff --git a/tests/gateway/test_telegram_status_indicator.py b/tests/gateway/test_telegram_status_indicator.pyindex 28486bad945..16e57653bd9 100644--- a/tests/gateway/test_telegram_status_indicator.py+++ b/tests/gateway/test_telegram_status_indicator.py@@ -7,32 +7,11 @@ is enabled, the adapter sets it to "Online" on connect and "Offline" on cleandisconnect so users can tell whether the gateway is up."""-import sysfrom unittest.mock import AsyncMock, MagicMockimport pytestfrom gateway.config import PlatformConfig---def _ensure_telegram_mock():- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return-- telegram_mod = MagicMock()- telegram_mod.ext.ContextTypes.DEFAULT_TYPE = type(None)- telegram_mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2"- telegram_mod.constants.ChatType.GROUP = "group"- telegram_mod.constants.ChatType.SUPERGROUP = "supergroup"- telegram_mod.constants.ChatType.CHANNEL = "channel"- telegram_mod.constants.ChatType.PRIVATE = "private"-- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, telegram_mod)---_ensure_telegram_mock()-from plugins.platforms.telegram.adapter import TelegramAdapter # noqa: E402diff --git a/tests/gateway/test_telegram_typing_backoff.py b/tests/gateway/test_telegram_typing_backoff.pyindex efaa2b95de2..20379de8d42 100644--- a/tests/gateway/test_telegram_typing_backoff.py+++ b/tests/gateway/test_telegram_typing_backoff.py@@ -2,40 +2,13 @@import sysfrom pathlib import Path-from unittest.mock import AsyncMock, MagicMock+from unittest.mock import AsyncMockimport pytest_repo = str(Path(__file__).resolve().parents[2])if _repo not in sys.path:sys.path.insert(0, _repo)---def _ensure_telegram_mock():- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return-- mod = MagicMock()- mod.ext.ContextTypes.DEFAULT_TYPE = type(None)- mod.constants.ParseMode.MARKDOWN = "Markdown"- mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2"- mod.constants.ParseMode.HTML = "HTML"- mod.constants.ChatType.PRIVATE = "private"- mod.constants.ChatType.GROUP = "group"- mod.constants.ChatType.SUPERGROUP = "supergroup"- mod.constants.ChatType.CHANNEL = "channel"- mod.error.NetworkError = type("NetworkError", (OSError,), {})- mod.error.TimedOut = type("TimedOut", (OSError,), {})- mod.error.RetryAfter = type("RetryAfter", (Exception,), {"__init__": lambda self, retry_after=1: setattr(self, "retry_after", retry_after)})- mod.error.BadRequest = type("BadRequest", (Exception,), {})-- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, mod)- sys.modules.setdefault("telegram.error", mod.error)---_ensure_telegram_mock()-from gateway.config import PlatformConfigfrom plugins.platforms.telegram.adapter import TelegramAdapterdiff --git a/tests/gateway/test_telegram_voice_caption_markdown.py b/tests/gateway/test_telegram_voice_caption_markdown.pyindex e0c62899964..33096cdaf06 100644--- a/tests/gateway/test_telegram_voice_caption_markdown.py+++ b/tests/gateway/test_telegram_voice_caption_markdown.py@@ -7,28 +7,11 @@ caption to MarkdownV2 (when the formatted text fits the 1024-char captioncap) and falls back to the plain truncated caption when the Bot API rejectsthe entities or formatting overflows."""-import sysfrom unittest.mock import AsyncMock, MagicMockimport pytestfrom gateway.config import PlatformConfig---def _ensure_telegram_mock():- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return- mod = MagicMock()- mod.error.NetworkError = type("NetworkError", (OSError,), {})- mod.error.TimedOut = type("TimedOut", (OSError,), {})- mod.error.BadRequest = type("BadRequest", (Exception,), {})- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, mod)- sys.modules.setdefault("telegram.error", mod.error)---_ensure_telegram_mock()-from plugins.platforms.telegram import adapter as telegram_mod # noqa: E402from plugins.platforms.telegram.adapter import TelegramAdapter # noqa: E402diff --git a/tests/gateway/test_telegram_voice_duration.py b/tests/gateway/test_telegram_voice_duration.pyindex ef45593b74a..f87781c5dca 100644--- a/tests/gateway/test_telegram_voice_duration.py+++ b/tests/gateway/test_telegram_voice_duration.py@@ -24,22 +24,6 @@ from unittest.mock import AsyncMock, MagicMockimport pytestfrom gateway.config import PlatformConfig---def _ensure_telegram_mock():- if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):- return- mod = MagicMock()- mod.error.NetworkError = type("NetworkError", (OSError,), {})- mod.error.TimedOut = type("TimedOut", (OSError,), {})- mod.error.BadRequest = type("BadRequest", (Exception,), {})- for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):- sys.modules.setdefault(name, mod)- sys.modules.setdefault("telegram.error", mod.error)---_ensure_telegram_mock()-from plugins.platforms.telegram import adapter as telegram_mod # noqa: E402from plugins.platforms.telegram.adapter import ( # noqa: E402TelegramAdapter,