Files
hermes-agent/tests/plugins/platforms/photon/test_poll_clarify.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.8 KiB
Python

"""Native-poll clarify tests for PhotonAdapter.
iMessage has a native poll bubble (spectrum-ts `poll()` builder). A
multiple-choice ``clarify`` renders as that poll; the user taps a choice and
the vote streams back inbound as a ``poll_option`` event. These tests cover
both directions without spawning the Node sidecar or binding ports:
* outbound — ``send_clarify`` with choices POSTs ``/send-poll`` and flips the
clarify into text-capture mode; with no choices it stays plain text;
* inbound — a ``poll_option`` selection is dispatched as a plain-text message
carrying the chosen option (so the gateway clarify-intercept resolves it),
a deselection is dropped, and an empty-title vote is dropped.
"""
from __future__ import annotations
from typing import Any, Dict, List, Optional, Tuple
import pytest
from gateway.config import PlatformConfig
from gateway.platforms.base import SendResult
from gateway.platforms.event import MessageEvent, MessageType
from plugins.platforms.photon.adapter import PhotonAdapter
def _make_adapter(monkeypatch: pytest.MonkeyPatch) -> PhotonAdapter:
monkeypatch.setenv("PHOTON_PROJECT_ID", "test-project-id")
monkeypatch.setenv("PHOTON_PROJECT_SECRET", "test-project-secret")
cfg = PlatformConfig(enabled=True, token="", extra={})
return PhotonAdapter(cfg)
def _capture(
adapter: PhotonAdapter, monkeypatch: pytest.MonkeyPatch
) -> List[MessageEvent]:
captured: List[MessageEvent] = []
async def fake_handle(event: MessageEvent) -> None:
captured.append(event)
monkeypatch.setattr(adapter, "handle_message", fake_handle)
return captured
def _poll_option_event(
*, title: str, selected: bool = True, msg_id: str = "spc-msg-vote"
) -> Dict[str, Any]:
return {
"messageId": msg_id,
"platform": "iMessage",
"space": {"id": "+155****4567", "type": "dm", "phone": "+155****4567"},
"sender": {"id": "+155****4567"},
"content": {
"type": "poll_option",
"title": title,
"selected": selected,
"pollTitle": "Pick one",
},
"timestamp": "2026-05-14T19:06:32.000Z",
}
# ---------------------------------------------------------------------------
# Inbound: a poll vote becomes the clarify answer.
@pytest.mark.asyncio
async def test_poll_vote_dispatched_as_choice_text(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""A poll selection is forwarded as a plain-text message carrying the
chosen option, so the gateway clarify text-intercept can resolve it."""
adapter = _make_adapter(monkeypatch)
captured = _capture(adapter, monkeypatch)
await adapter._dispatch_inbound(
_poll_option_event(title="Yes — native tappable buttons")
)
assert len(captured) == 1
ev = captured[0]
assert ev.text == "Yes — native tappable buttons"
assert ev.message_type == MessageType.TEXT
assert ev.source.chat_id == "+155****4567"
# ---------------------------------------------------------------------------
# Outbound: send_clarify renders a native poll for choices.
def _stub_sidecar_poll(
adapter: PhotonAdapter, monkeypatch: pytest.MonkeyPatch, *, ok: bool = True
) -> List[Tuple[str, str, list]]:
calls: List[Tuple[str, str, list]] = []
async def fake_send_poll(space_id: str, title: str, options: list):
calls.append((space_id, title, list(options)))
return SendResult(
success=ok,
message_id="spc-msg-poll" if ok else None,
error=None if ok else "boom",
)
monkeypatch.setattr(adapter, "_sidecar_send_poll", fake_send_poll)
return calls
def _stub_sidecar_text(
adapter: PhotonAdapter, monkeypatch: pytest.MonkeyPatch
) -> List[Tuple[str, str]]:
sends: List[Tuple[str, str]] = []
async def fake_send(space_id: str, text: str):
sends.append((space_id, text))
return SendResult(success=True, message_id="spc-msg-text")
monkeypatch.setattr(adapter, "_sidecar_send", fake_send)
return sends
@pytest.mark.asyncio
async def test_send_clarify_with_choices_sends_native_poll(
monkeypatch: pytest.MonkeyPatch,
) -> None:
adapter = _make_adapter(monkeypatch)
poll_calls = _stub_sidecar_poll(adapter, monkeypatch)
marked: List[str] = []
import tools.clarify_gateway as cg
monkeypatch.setattr(cg, "mark_awaiting_text", lambda cid: marked.append(cid))
result = await adapter.send_clarify(
chat_id="+155****4567",
question="Pick one",
choices=["A", "B", "C"],
clarify_id="clar-1",
session_key="sess-1",
)
assert result.success
assert len(poll_calls) == 1
space_id, title, options = poll_calls[0]
assert space_id == "+155****4567"
assert title == "Pick one"
assert options == ["A", "B", "C"]
# The vote returns as text, so text-capture must be enabled.
assert marked == ["clar-1"]