Files
hermes-agent/tests/gateway/test_typing_indicator_toggle.py
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

87 lines
2.7 KiB
Python

"""Per-platform typing-indicator toggle (PlatformConfig.typing_indicator).
The "typing…" / "is thinking…" status bubble is driven by the generic
``_keep_typing`` refresh loop that ``_process_message_background`` spawns for
every inbound message on every platform. ``typing_indicator`` (default True)
gates that spawn: when False, the loop is never started, so ``send_typing``
is never called and no status indicator is shown — while message delivery is
otherwise unchanged.
These are behavioral tests against the real dispatch path, not snapshots.
"""
import asyncio
from unittest.mock import AsyncMock
import pytest
from gateway.config import Platform, PlatformConfig
from gateway.platforms.base import (
BasePlatformAdapter,
)
from gateway.platforms.event import MessageEvent, MessageType
from gateway.session import SessionSource, build_session_key
class _StubAdapter(BasePlatformAdapter):
async def connect(self, *, is_reconnect: bool = False):
pass
async def disconnect(self):
pass
async def send(self, chat_id, text, **kwargs):
return None
async def get_chat_info(self, chat_id):
return {}
def _make_adapter(typing_indicator: bool) -> _StubAdapter:
adapter = _StubAdapter(
PlatformConfig(enabled=True, token="t", typing_indicator=typing_indicator),
Platform.SLACK,
)
# Record send_typing calls without performing any platform I/O.
adapter.send_typing = AsyncMock(return_value=None)
adapter._send_with_retry = AsyncMock(return_value=None)
# Handler returns immediately; the typing loop only fires if it was spawned.
adapter._message_handler = AsyncMock(return_value="ok")
return adapter
def _make_event(chat_id="C123"):
return MessageEvent(
text="hi",
message_type=MessageType.TEXT,
source=SessionSource(platform=Platform.SLACK, chat_id=chat_id, chat_type="dm"),
)
def _sk(chat_id="C123"):
return build_session_key(
SessionSource(platform=Platform.SLACK, chat_id=chat_id, chat_type="dm")
)
@pytest.mark.asyncio
async def test_typing_indicator_enabled_spawns_refresh_loop():
"""Default (typing_indicator=True): the refresh loop calls send_typing."""
adapter = _make_adapter(typing_indicator=True)
# Real handlers take time (tool calls); yield long enough for the spawned
# refresh loop to fire at least one send_typing before delivery completes.
async def _slow_handler(_event):
await asyncio.sleep(0.05)
return "ok"
adapter._message_handler = _slow_handler
event = _make_event()
adapter._active_sessions[_sk()] = asyncio.Event()
await adapter._process_message_background(event, _sk())
assert adapter.send_typing.await_count >= 1