mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-30 19:09:28 +00:00
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.
276 lines
9.5 KiB
Python
276 lines
9.5 KiB
Python
"""Tests for text message batching across all gateway adapters.
|
|
|
|
When a user sends a long message, the messaging client splits it at the
|
|
platform's character limit. Each adapter should buffer rapid successive
|
|
text messages from the same session and aggregate them before dispatching.
|
|
|
|
Covers: Discord, Matrix, WeCom, and the adaptive delay logic for
|
|
Telegram and Feishu.
|
|
"""
|
|
|
|
import asyncio
|
|
from unittest.mock import AsyncMock
|
|
|
|
import pytest
|
|
|
|
from gateway.config import Platform, PlatformConfig
|
|
from gateway.platforms.base import MessageEvent, MessageType, SessionSource
|
|
|
|
|
|
# =====================================================================
|
|
# Helpers
|
|
# =====================================================================
|
|
|
|
def _make_event(
|
|
text: str,
|
|
platform: Platform,
|
|
chat_id: str = "12345",
|
|
msg_type: MessageType = MessageType.TEXT,
|
|
) -> MessageEvent:
|
|
return MessageEvent(
|
|
text=text,
|
|
message_type=msg_type,
|
|
source=SessionSource(platform=platform, chat_id=chat_id, chat_type="dm"),
|
|
)
|
|
|
|
|
|
# =====================================================================
|
|
# Discord text batching
|
|
# =====================================================================
|
|
|
|
def _make_discord_adapter():
|
|
"""Create a minimal DiscordAdapter for testing text batching."""
|
|
from plugins.platforms.discord.adapter import DiscordAdapter
|
|
|
|
config = PlatformConfig(enabled=True, token="test-token")
|
|
adapter = object.__new__(DiscordAdapter)
|
|
adapter._platform = Platform.DISCORD
|
|
adapter.config = config
|
|
adapter._pending_text_batches = {}
|
|
adapter._pending_text_batch_tasks = {}
|
|
adapter._text_batch_delay_seconds = 0.1 # fast for tests
|
|
adapter._text_batch_split_delay_seconds = 0.3 # fast for tests
|
|
adapter._active_sessions = {}
|
|
adapter._pending_messages = {}
|
|
adapter._message_handler = AsyncMock()
|
|
adapter.handle_message = AsyncMock()
|
|
return adapter
|
|
|
|
|
|
class TestDiscordTextBatching:
|
|
@pytest.mark.asyncio
|
|
async def test_single_message_dispatched_after_delay(self):
|
|
adapter = _make_discord_adapter()
|
|
event = _make_event("hello world", Platform.DISCORD)
|
|
|
|
adapter._enqueue_text_event(event)
|
|
|
|
# Not dispatched yet
|
|
adapter.handle_message.assert_not_called()
|
|
|
|
# Wait for flush
|
|
await asyncio.sleep(0.2)
|
|
|
|
adapter.handle_message.assert_called_once()
|
|
dispatched = adapter.handle_message.call_args[0][0]
|
|
assert dispatched.text == "hello world"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_split_messages_aggregated(self):
|
|
"""Two rapid messages from the same chat should be merged."""
|
|
adapter = _make_discord_adapter()
|
|
|
|
adapter._enqueue_text_event(_make_event("Part one of a long", Platform.DISCORD))
|
|
await asyncio.sleep(0.02)
|
|
adapter._enqueue_text_event(_make_event("message that was split.", Platform.DISCORD))
|
|
|
|
adapter.handle_message.assert_not_called()
|
|
|
|
await asyncio.sleep(0.2)
|
|
|
|
adapter.handle_message.assert_called_once()
|
|
text = adapter.handle_message.call_args[0][0].text
|
|
assert "Part one" in text
|
|
assert "split" in text
|
|
|
|
|
|
# =====================================================================
|
|
# Matrix text batching
|
|
# =====================================================================
|
|
|
|
def _make_matrix_adapter():
|
|
"""Create a minimal MatrixAdapter for testing text batching."""
|
|
from plugins.platforms.matrix.adapter import MatrixAdapter
|
|
|
|
config = PlatformConfig(enabled=True, token="test-token")
|
|
adapter = object.__new__(MatrixAdapter)
|
|
adapter._platform = Platform.MATRIX
|
|
adapter.config = config
|
|
adapter._pending_text_batches = {}
|
|
adapter._pending_text_batch_tasks = {}
|
|
adapter._text_batch_delay_seconds = 0.1
|
|
adapter._text_batch_split_delay_seconds = 0.3
|
|
adapter._active_sessions = {}
|
|
adapter._pending_messages = {}
|
|
adapter._message_handler = AsyncMock()
|
|
adapter.handle_message = AsyncMock()
|
|
return adapter
|
|
|
|
|
|
class TestMatrixTextBatching:
|
|
@pytest.mark.asyncio
|
|
async def test_single_message_dispatched_after_delay(self):
|
|
adapter = _make_matrix_adapter()
|
|
event = _make_event("hello world", Platform.MATRIX)
|
|
|
|
adapter._enqueue_text_event(event)
|
|
|
|
adapter.handle_message.assert_not_called()
|
|
await asyncio.sleep(0.2)
|
|
|
|
adapter.handle_message.assert_called_once()
|
|
assert adapter.handle_message.call_args[0][0].text == "hello world"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_split_messages_aggregated(self):
|
|
adapter = _make_matrix_adapter()
|
|
|
|
adapter._enqueue_text_event(_make_event("first part", Platform.MATRIX))
|
|
await asyncio.sleep(0.02)
|
|
adapter._enqueue_text_event(_make_event("second part", Platform.MATRIX))
|
|
|
|
adapter.handle_message.assert_not_called()
|
|
await asyncio.sleep(0.2)
|
|
|
|
adapter.handle_message.assert_called_once()
|
|
text = adapter.handle_message.call_args[0][0].text
|
|
assert "first part" in text
|
|
assert "second part" in text
|
|
|
|
|
|
# =====================================================================
|
|
# WeCom text batching
|
|
# =====================================================================
|
|
|
|
def _make_wecom_adapter():
|
|
"""Create a minimal WeComAdapter for testing text batching."""
|
|
from plugins.platforms.wecom.adapter import WeComAdapter
|
|
|
|
config = PlatformConfig(enabled=True, token="test-token")
|
|
adapter = object.__new__(WeComAdapter)
|
|
adapter._platform = Platform.WECOM
|
|
adapter.config = config
|
|
adapter._pending_text_batches = {}
|
|
adapter._pending_text_batch_tasks = {}
|
|
adapter._text_batch_delay_seconds = 0.1
|
|
adapter._text_batch_split_delay_seconds = 0.3
|
|
adapter._active_sessions = {}
|
|
adapter._pending_messages = {}
|
|
adapter._message_handler = AsyncMock()
|
|
adapter.handle_message = AsyncMock()
|
|
return adapter
|
|
|
|
|
|
class TestWeComTextBatching:
|
|
@pytest.mark.asyncio
|
|
async def test_single_message_dispatched_after_delay(self):
|
|
adapter = _make_wecom_adapter()
|
|
event = _make_event("hello world", Platform.WECOM)
|
|
|
|
adapter._enqueue_text_event(event)
|
|
|
|
adapter.handle_message.assert_not_called()
|
|
await asyncio.sleep(0.2)
|
|
|
|
adapter.handle_message.assert_called_once()
|
|
assert adapter.handle_message.call_args[0][0].text == "hello world"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_split_messages_aggregated(self):
|
|
adapter = _make_wecom_adapter()
|
|
|
|
adapter._enqueue_text_event(_make_event("first part", Platform.WECOM))
|
|
await asyncio.sleep(0.02)
|
|
adapter._enqueue_text_event(_make_event("second part", Platform.WECOM))
|
|
|
|
adapter.handle_message.assert_not_called()
|
|
await asyncio.sleep(0.2)
|
|
|
|
adapter.handle_message.assert_called_once()
|
|
text = adapter.handle_message.call_args[0][0].text
|
|
assert "first part" in text
|
|
assert "second part" in text
|
|
|
|
|
|
# =====================================================================
|
|
# Telegram adaptive delay (PR #6891)
|
|
# =====================================================================
|
|
|
|
def _make_telegram_adapter():
|
|
"""Create a minimal TelegramAdapter for testing adaptive delay."""
|
|
from plugins.platforms.telegram.adapter import TelegramAdapter
|
|
|
|
config = PlatformConfig(enabled=True, token="test-token")
|
|
adapter = object.__new__(TelegramAdapter)
|
|
adapter._platform = Platform.TELEGRAM
|
|
adapter.config = config
|
|
adapter._pending_text_batches = {}
|
|
adapter._pending_text_batch_tasks = {}
|
|
adapter._text_batch_delay_seconds = 0.1
|
|
adapter._text_batch_split_delay_seconds = 0.3
|
|
adapter._active_sessions = {}
|
|
adapter._pending_messages = {}
|
|
adapter._message_handler = AsyncMock()
|
|
adapter.handle_message = AsyncMock()
|
|
return adapter
|
|
|
|
|
|
class TestTelegramAdaptiveDelay:
|
|
@pytest.mark.asyncio
|
|
async def test_short_chunk_uses_normal_delay(self):
|
|
adapter = _make_telegram_adapter()
|
|
adapter._enqueue_text_event(_make_event("short msg", Platform.TELEGRAM))
|
|
|
|
# Should flush after the normal 0.1s delay
|
|
await asyncio.sleep(0.15)
|
|
adapter.handle_message.assert_called_once()
|
|
|
|
|
|
# =====================================================================
|
|
# Feishu adaptive delay
|
|
# =====================================================================
|
|
|
|
def _make_feishu_adapter():
|
|
"""Create a minimal FeishuAdapter for testing adaptive delay."""
|
|
from plugins.platforms.feishu.adapter import FeishuAdapter, FeishuBatchState
|
|
|
|
config = PlatformConfig(enabled=True, token="test-token")
|
|
adapter = object.__new__(FeishuAdapter)
|
|
adapter._platform = Platform.FEISHU
|
|
adapter.config = config
|
|
batch_state = FeishuBatchState()
|
|
adapter._pending_text_batches = batch_state.events
|
|
adapter._pending_text_batch_tasks = batch_state.tasks
|
|
adapter._pending_text_batch_counts = batch_state.counts
|
|
adapter._text_batch_delay_seconds = 0.1
|
|
adapter._text_batch_split_delay_seconds = 0.3
|
|
adapter._text_batch_max_messages = 20
|
|
adapter._text_batch_max_chars = 50000
|
|
adapter._active_sessions = {}
|
|
adapter._pending_messages = {}
|
|
adapter._message_handler = AsyncMock()
|
|
adapter._handle_message_with_guards = AsyncMock()
|
|
return adapter
|
|
|
|
|
|
class TestFeishuAdaptiveDelay:
|
|
@pytest.mark.asyncio
|
|
async def test_short_chunk_uses_normal_delay(self):
|
|
adapter = _make_feishu_adapter()
|
|
event = _make_event("short msg", Platform.FEISHU)
|
|
await adapter._enqueue_text_event(event)
|
|
|
|
await asyncio.sleep(0.15)
|
|
adapter._handle_message_with_guards.assert_called_once()
|
|
|
|
|