Files
hermes-agent/tests/gateway/test_telegram_channel_posts.py
T
kshitijk4poor ab2f4602de refactor: MessageEvent to gateway/platforms/event.py; ElicitationHandler takes a call_context thunk
Breaks the two import cycles that forced Protocol stand-ins in the F821 sweep, so the two
sites now name the real types.

gateway/platforms/event.py (new leaf): MessageType, ProcessingOutcome, MessageEvent moved
out of base.py verbatim. Their only dependency is gateway.session.SessionSource; base.py
imported helpers.py at module level, so helpers could not name MessageEvent. Now
TextBatchAggregator is typed by the real MessageEvent. 249 importers repointed
(`from gateway.platforms.base import` -> `.event`, preserving each import's layout);
gateway.platforms.__init__ re-exports from .event. The three revert-scheduled PLUGIN-COMPAT
pointers that named these symbols (gateway.slash_commands → MessageType, dingtalk → MessageType,
photon → ProcessingOutcome) and their COMPAT_MANIFEST rows now target gateway.platforms.event.
Docs updated: ADDING_A_PLATFORM.md, adding-platform-adapters.md (en + zh-Hans).

tools/mcp_tool_sampling.py: ElicitationHandler no longer holds a back-reference to its
MCPServerTask (mcp_tool imports sampling, so the task type cannot be named there). It only
ever read owner._pending_call_context, so it takes `call_context: Callable[[], Context | None]`
and MCPServerTask passes `lambda: self._pending_call_context`. The consent call is one
`functools.partial`, run directly or inside the captured Context.

ty on the 11 touched production files vs origin/main: 0 new diagnostics, 14 resolved.
(The one `source: SessionSource = None` diagnostic moves with the class; typing it Optional
exposes ~60 unguarded call sites — separate follow-up.)

Tests: tests/gateway + tests/plugins + tests/tools + touched files, 18,235 passed; the 31
failures reproduce identically on origin/main (macOS /private/tmp, systemd socket,
long-path fixtures, live-service tests).
2026-09-07 22:47:33 +05:30

151 lines
4.7 KiB
Python

"""Regression tests for Telegram channel_post updates.
Telegram channel broadcasts are delivered as ``Update.channel_post`` rather than
``Update.message``. The adapter should use ``effective_message`` so channel
posts are converted into Hermes gateway events instead of being silently
ignored.
"""
import importlib
import importlib.util
import sys
import types
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock
import pytest
from gateway.config import PlatformConfig
from gateway.platforms.event import MessageType
def _build_telegram_stubs():
telegram_mod = types.ModuleType("telegram")
telegram_mod.Update = object
telegram_mod.Bot = object
telegram_mod.Message = object
telegram_mod.InlineKeyboardButton = object
telegram_mod.InlineKeyboardMarkup = object
telegram_mod.LinkPreviewOptions = object
telegram_ext_mod = types.ModuleType("telegram.ext")
telegram_ext_mod.Application = object
telegram_ext_mod.CommandHandler = object
telegram_ext_mod.CallbackQueryHandler = object
telegram_ext_mod.InlineQueryHandler = object
telegram_ext_mod.MessageHandler = object
telegram_ext_mod.ContextTypes = SimpleNamespace(DEFAULT_TYPE=type(None))
telegram_ext_mod.filters = SimpleNamespace()
telegram_constants_mod = types.ModuleType("telegram.constants")
telegram_constants_mod.ParseMode = SimpleNamespace(MARKDOWN_V2="MarkdownV2")
telegram_constants_mod.ChatType = SimpleNamespace(
GROUP="group",
SUPERGROUP="supergroup",
CHANNEL="channel",
PRIVATE="private",
)
telegram_request_mod = types.ModuleType("telegram.request")
telegram_request_mod.HTTPXRequest = object
telegram_mod.ext = telegram_ext_mod
telegram_mod.constants = telegram_constants_mod
telegram_mod.request = telegram_request_mod
return {
"telegram": telegram_mod,
"telegram.ext": telegram_ext_mod,
"telegram.constants": telegram_constants_mod,
"telegram.request": telegram_request_mod,
}
@pytest.fixture
def telegram_adapter_cls(monkeypatch):
"""Import TelegramAdapter without leaking temporary telegram stubs."""
module_name = "plugins.platforms.telegram.adapter"
existing_module = sys.modules.get(module_name)
if existing_module is not None:
yield existing_module.TelegramAdapter
return
telegram_pkg = sys.modules.get("telegram")
installed = isinstance(getattr(telegram_pkg, "__file__", None), str)
if telegram_pkg is None:
try:
installed = importlib.util.find_spec("telegram") is not None
except ValueError:
installed = False
if not installed:
for name, module in _build_telegram_stubs().items():
monkeypatch.setitem(sys.modules, name, module)
module = importlib.import_module(module_name)
try:
yield module.TelegramAdapter
finally:
if not installed:
sys.modules.pop(module_name, None)
def _make_adapter(telegram_adapter_cls):
a = telegram_adapter_cls(PlatformConfig(enabled=True, token="***", extra={}))
# Channel posts have from_user=None. After PR #28494's fail-closed
# auth, the empty-allowlist adapter rejects all messages including
# channel posts. These tests focus on routing, not auth gating.
a._is_callback_user_authorized = lambda user_id, **_kw: True
return a
def _make_channel_message(text="channel id test @hermes_bot"):
chat = SimpleNamespace(
id=-1003950368353,
type="channel",
title="wzrd",
full_name=None,
is_forum=False,
)
return SimpleNamespace(
chat=chat,
from_user=None,
text=text,
caption=None,
entities=[],
caption_entities=[],
message_thread_id=None,
is_topic_message=False,
message_id=11,
reply_to_message=None,
quote=None,
date=None,
forum_topic_created=None,
)
def _make_channel_update(msg):
return SimpleNamespace(
update_id=12345,
message=None,
channel_post=msg,
effective_message=msg,
)
def test_build_message_event_uses_channel_identity_for_channel_posts(telegram_adapter_cls):
adapter = _make_adapter(telegram_adapter_cls)
msg = _make_channel_message()
event = adapter._build_message_event(msg, MessageType.TEXT, update_id=12345)
assert event.source.chat_type == "channel"
assert event.source.chat_id == "-1003950368353"
# Channel posts often have no from_user. Preserve an identity so the
# gateway authorization layer can allowlist the channel by numeric ID.
assert event.source.user_id == "-1003950368353"
assert event.source.user_name == "wzrd"
assert event.platform_update_id == 12345