hermes-agent/tests/gateway/test_telegram_status_update.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

107 lines
3.9 KiB
Python

"""Tests for TelegramAdapter.send_or_update_status (issue #30045).
The status-update path must:
1. Send a fresh message on the first call for a (chat_id, status_key) pair.
2. Edit that same message on subsequent calls with the same key.
3. Fall back to sending fresh when the cached message edit fails.
4. Keep distinct keys independent (no cross-talk).
"""
from __future__ import annotations
import sys
import types
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock
import pytest
from gateway.config import PlatformConfig
from gateway.platforms.base import SendResult
def _install_fake_telegram(monkeypatch):
"""Stub the python-telegram-bot package so TelegramAdapter can be imported."""
fake_telegram = types.ModuleType("telegram")
fake_telegram.Update = SimpleNamespace(ALL_TYPES=())
fake_telegram.Bot = object
fake_telegram.Message = object
fake_telegram.InlineKeyboardButton = object
fake_telegram.InlineKeyboardMarkup = object
fake_error = types.ModuleType("telegram.error")
fake_error.NetworkError = type("NetworkError", (Exception,), {})
fake_error.BadRequest = type("BadRequest", (Exception,), {})
fake_error.TimedOut = type("TimedOut", (Exception,), {})
fake_telegram.error = fake_error
fake_constants = types.ModuleType("telegram.constants")
fake_constants.ParseMode = SimpleNamespace(MARKDOWN_V2="MarkdownV2")
fake_constants.ChatType = SimpleNamespace(
GROUP="group", SUPERGROUP="supergroup",
CHANNEL="channel", PRIVATE="private",
)
fake_telegram.constants = fake_constants
fake_ext = types.ModuleType("telegram.ext")
fake_ext.Application = object
fake_ext.CommandHandler = object
fake_ext.CallbackQueryHandler = object
fake_ext.MessageHandler = object
fake_ext.ContextTypes = SimpleNamespace(DEFAULT_TYPE=object)
fake_ext.filters = object
fake_request = types.ModuleType("telegram.request")
fake_request.HTTPXRequest = object
monkeypatch.setitem(sys.modules, "telegram", fake_telegram)
monkeypatch.setitem(sys.modules, "telegram.error", fake_error)
monkeypatch.setitem(sys.modules, "telegram.constants", fake_constants)
monkeypatch.setitem(sys.modules, "telegram.ext", fake_ext)
monkeypatch.setitem(sys.modules, "telegram.request", fake_request)
@pytest.fixture
def adapter(monkeypatch):
_install_fake_telegram(monkeypatch)
from plugins.platforms.telegram.adapter import TelegramAdapter
a = TelegramAdapter(PlatformConfig(enabled=True, token="fake-token"))
a._bot = MagicMock()
# Patch send / edit_message so tests can drive them directly.
a.send = AsyncMock()
a.edit_message = AsyncMock()
return a
@pytest.mark.asyncio
async def test_first_call_sends_and_caches_message_id(adapter):
"""First call for a (chat, key) pair must send and remember the id."""
adapter.send.return_value = SendResult(success=True, message_id="100")
result = await adapter.send_or_update_status("chat-1", "lifecycle", "starting")
assert result.success is True
assert result.message_id == "100"
adapter.send.assert_awaited_once()
adapter.edit_message.assert_not_awaited()
assert adapter._status_message_ids[("chat-1", "lifecycle")] == "100"
@pytest.mark.asyncio
async def test_distinct_status_keys_do_not_collide(adapter):
"""A different status_key gets its own message; the original isn't touched."""
adapter.send.side_effect = [
SendResult(success=True, message_id="100"),
SendResult(success=True, message_id="200"),
]
await adapter.send_or_update_status("chat-1", "lifecycle", "ctx pressure")
await adapter.send_or_update_status("chat-1", "model-switch", "switched to opus")
assert adapter.send.await_count == 2
adapter.edit_message.assert_not_awaited()
assert adapter._status_message_ids[("chat-1", "lifecycle")] == "100"
assert adapter._status_message_ids[("chat-1", "model-switch")] == "200"