hermes-agent/tests/gateway/test_slack_channel_session_scope.py
Teknium 39975613b1
test: prune wave 2 + speed fixes — 28,106 → 19,757 test functions, suite wall 315s → 294s
Second, deeper pass over tools/gateway/hermes_cli plus first pass over
the trees wave 1 missed (acp, acp_adapter, skills, computer_use, docker,
dashboard, conformance, monitoring, secret_sources, hermes_state,
providers). Same rubric as wave 1 (AGENTS.md test policy); security,
alternation/caching invariants, issue-number regressions, and E2E kept.

Real test-quality fixes found and rooted out along the way:
- tests/tools/test_command_guards.py made real auxiliary-LLM HTTPS calls
  (DEFAULT_CONFIG smart-approval leaked in) — pinned approval
  mode=manual via autouse fixture: 17.4s → 0.4s.
- test_model_switch_custom_providers.py / test_user_providers_model_switch.py
  silently probed live provider catalogs (~2s/test) — stubbed
  cached_provider_model_ids/provider_model_ids/fetch_api_models.
- test_telegram_noise_filter.py: 15-platform copy-paste matrix over
  shared gateway.run logic → 3 representative platforms (55s → 3.9s).
- test_gateway_shutdown.py: stop()'s 5s interrupt-deadline loop spun on
  MagicMock agents — interrupt.side_effect now clears _running_agents
  (22s → 1.0s).
- test_gateway_inactivity_timeout.py poll-harness timings shrunk 3-5x
  (24s → 1.1s); test_mcp_stability.py backoff/SIGTERM-grace sleeps
  patched (15.4s → 2.5s); test_async_delegation.py negative-drain wait
  5s → 0.5s.
- test_telegram_init_deadline.py: loop-block margin restored to 1.0s
  with rationale comment — the watchdog-dump assertion needs the loop
  blocked well past deadline+grace under parallel load (flaked once in
  the 40-worker verification run at a 0.2s margin).

Verification: full hermetic suite via scripts/run_tests.sh —
2,438 files, 21,718 tests passed, 0 failed, 293.9s wall.
Suite totals vs original baseline: 46,820 → 19,757 test functions
(−57.8%), wall 583.5s → 293.9s (−50%), subprocess CPU 13,564s → 11,623s.
2026-07-29 13:39:40 -07:00

164 lines
5.9 KiB
Python

"""Regression guard for #15421 bug 1 — Slack channel session scoping.
Before this fix, every top-level Slack channel message got a unique
``thread_id`` (the message's own ``ts``) stamped onto its
``MessageSource``. The gateway session store keys sessions by
``(platform, channel_id, thread_id)``, so each top-level message
spawned a **brand new session** and channel context never accumulated
across messages — even when the operator set ``reply_in_thread: false``
in ``config.yaml`` expecting channel-wide conversation.
The fix: when ``reply_in_thread: false`` is configured, top-level
channel messages now land on ``thread_id = None`` so the session store
groups them under a single channel-scoped session. Genuine thread
replies (``event.thread_ts != ts``) still scope sessions per thread in
both modes — threading UX is unchanged when the operator actually
asks for it.
These tests drive the real ``SlackAdapter._handle_slack_message`` code
path with mocked aiohttp / user-resolution so the ``MessageEvent``
that reaches ``handle_message`` exposes exactly what the session store
will key on. Asserting on the event keeps the seam tight against the
production function's behaviour rather than a re-implementation.
"""
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from gateway.config import PlatformConfig
from plugins.platforms.slack.adapter import SlackAdapter
@pytest.fixture
def adapter():
config = PlatformConfig(enabled=True, token="xoxb-fake-token")
a = SlackAdapter(config)
a._app = MagicMock()
a._app.client = AsyncMock()
a._bot_user_id = "U_BOT"
a._running = True
a.handle_message = AsyncMock()
return a
@pytest.fixture(autouse=True)
def _redirect_cache(tmp_path, monkeypatch):
"""Point document cache to tmp_path so tests don't touch ~/.hermes."""
monkeypatch.setattr(
"gateway.platforms.base.DOCUMENT_CACHE_DIR", tmp_path / "doc_cache"
)
def _channel_event(text: str, ts: str, thread_ts: str = None) -> dict:
"""Build a minimal ``message`` event for the Slack Events API
resembling what ``handle_message_event`` would pass through."""
event = {
"channel": "C_CHAN",
"channel_type": "channel",
"user": "U_USER",
"text": text,
"ts": ts,
}
if thread_ts is not None:
event["thread_ts"] = thread_ts
return event
class TestChannelSessionScopeDefault:
"""``reply_in_thread: true`` is the historical default. Top-level
channel messages still map ``thread_id = ts`` so each new message
becomes its own threaded session — unchanged from the pre-#15421
behaviour."""
@pytest.mark.asyncio
async def test_top_level_maps_to_ts_when_reply_in_thread_true(self, adapter):
adapter.config.extra["reply_in_thread"] = True
event = _channel_event(
"<@U_BOT> hello",
ts="1700000000.000001",
)
captured = []
adapter.handle_message = AsyncMock(
side_effect=lambda e: captured.append(e)
)
with patch.object(
adapter, "_resolve_user_name",
new=AsyncMock(return_value="testuser"),
):
await adapter._handle_slack_message(event)
assert len(captured) == 1, (
"handler dropped the top-level channel mention — "
"mention gating misfired"
)
source = captured[0].source
assert source.thread_id == "1700000000.000001", (
"legacy default (reply_in_thread=true) must keep stamping "
"thread_id = ts so each top-level message gets its own "
"threaded session — regression guard"
)
class TestChannelSessionScopeShared:
"""``reply_in_thread: false`` is the #15421 fix: top-level channel
messages get ``thread_id = None`` so all of them share one
channel-scoped session. Genuine thread replies still get their
real ``thread_ts``."""
@pytest.mark.asyncio
async def test_top_level_maps_to_none_when_reply_in_thread_false(self, adapter):
adapter.config.extra["reply_in_thread"] = False
event = _channel_event(
"<@U_BOT> hello",
ts="1700000000.000003",
)
captured = []
adapter.handle_message = AsyncMock(
side_effect=lambda e: captured.append(e)
)
with patch.object(
adapter, "_resolve_user_name",
new=AsyncMock(return_value="testuser"),
):
await adapter._handle_slack_message(event)
assert len(captured) == 1
source = captured[0].source
assert source.thread_id is None, (
"reply_in_thread=false must set thread_id=None for top-level "
"channel messages so the session store groups them under a "
"single channel-scoped session (#15421 bug 1)"
)
class TestThreadReplyAlwaysScopesByThread:
"""Cross-cutting invariant: genuine thread replies always scope by
``thread_ts`` regardless of ``reply_in_thread``. If this ever
regresses, every thread-scoped conversation leaks across threads."""
@pytest.mark.asyncio
@pytest.mark.parametrize("reply_in_thread", [True, False])
async def test_thread_reply_keyed_by_thread_ts(self, adapter, reply_in_thread):
adapter.config.extra["reply_in_thread"] = reply_in_thread
event = _channel_event(
"<@U_BOT> thread reply",
ts="1700000000.000010",
thread_ts="1700000000.000009",
)
captured = []
adapter.handle_message = AsyncMock(
side_effect=lambda e: captured.append(e)
)
with patch.object(
adapter, "_resolve_user_name",
new=AsyncMock(return_value="testuser"),
):
await adapter._handle_slack_message(event)
assert len(captured) == 1, (
f"thread reply dropped with reply_in_thread={reply_in_thread}"
)
assert captured[0].source.thread_id == "1700000000.000009"