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

89 lines
3 KiB
Python

"""Tests for structured send-error classification (SendResult.error_kind).
Covers the platform-neutral ``classify_send_error`` vocabulary in
``gateway/platforms/base.py`` and its wiring into the Telegram adapter's
``send()`` failure path, so consumers can branch on a typed category instead
of substring-matching the raw provider message.
"""
import pytest
from gateway.platforms.base import (
SEND_ERROR_KINDS,
SendResult,
classify_send_error,
)
class _FakeBadRequest(Exception):
"""Stand-in for a provider BadRequest carrying a message string."""
@pytest.mark.parametrize(
"text,expected",
[
("Message_too_long", "too_long"),
("Bad Request: message is too long", "too_long"),
("Bad Request: can't parse entities: unsupported start tag", "bad_format"),
("Bad Request: can't find end of the entity", "bad_format"),
("Forbidden: bot was blocked by the user", "forbidden"),
("Forbidden: user is deactivated", "forbidden"),
("Bad Request: not enough rights to send text messages", "forbidden"),
("Bad Request: chat not found", "not_found"),
("Bad Request: message to edit not found", "not_found"),
("Too Many Requests: retry after 12", "rate_limited"),
("Flood control exceeded", "rate_limited"),
("ConnectError: connection refused", "transient"),
("ConnectTimeout", "transient"),
("some entirely novel provider message", "unknown"),
("", "unknown"),
],
)
def test_classify_send_error_text(text, expected):
assert classify_send_error(None, text) == expected
def test_every_classification_is_in_the_vocabulary():
samples = [
"message_too_long",
"can't parse entities",
"forbidden",
"chat not found",
"flood",
"connecterror",
"mystery",
"",
]
for s in samples:
assert classify_send_error(None, s) in SEND_ERROR_KINDS
def test_telegram_send_failure_populates_error_kind():
"""Telegram send() failures carry a typed error_kind alongside error."""
import asyncio
from unittest.mock import AsyncMock, MagicMock
from gateway.config import PlatformConfig
from plugins.platforms.telegram.adapter import TelegramAdapter
cfg = PlatformConfig(enabled=True, token="fake-token", extra={})
adapter = TelegramAdapter(cfg)
# Minimal bot whose send_message raises a parse/entity rejection.
bot = MagicMock()
bot.send_message = AsyncMock(
side_effect=Exception("Bad Request: can't parse entities: bad tag")
)
bot.send_chat_action = AsyncMock()
# Force the legacy (non-rich) path and a connected bot.
adapter._bot = bot
adapter._rich_messages_enabled = False
result = asyncio.run(adapter.send("123", "<b>broken"))
assert result.success is False
# Telegram has a plain-text fallback for parse errors inside the send loop,
# so a raw parse failure that still escapes is classified for consumers.
assert result.error_kind in SEND_ERROR_KINDS
assert result.error_kind != "unknown" or result.error