hermes-agent/tests/tools/test_telegram_send_message_caption.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

101 lines
3.6 KiB
Python

"""Standalone Telegram MEDIA:<path> caption delivery.
When `hermes send --to telegram "MEDIA:/x.png This Caption"` carries a single
captionable file plus short text, the text must ride on the media bubble as the
sendPhoto/sendVideo/sendDocument ``caption`` rather than being posted as a
separate sendMessage beforehand. Longer text (> Telegram's 1024 caption cap)
falls back to a separate message. The ``telegram`` package is stubbed.
"""
from __future__ import annotations
import asyncio
import os
import sys
import tempfile
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock
import pytest
def _install_telegram_mock(monkeypatch: pytest.MonkeyPatch, bot_factory: MagicMock) -> None:
parse_mode = SimpleNamespace(MARKDOWN_V2="MarkdownV2", HTML="HTML")
constants_mod = SimpleNamespace(ParseMode=parse_mode)
_MessageEntity = lambda **_kw: SimpleNamespace(**_kw)
telegram_mod = SimpleNamespace(
Bot=bot_factory,
MessageEntity=_MessageEntity,
constants=constants_mod,
)
monkeypatch.setitem(sys.modules, "telegram", telegram_mod)
monkeypatch.setitem(sys.modules, "telegram.constants", constants_mod)
def _make_bot() -> MagicMock:
bot = MagicMock()
bot.send_message = AsyncMock(return_value=SimpleNamespace(message_id=1))
bot.send_photo = AsyncMock(return_value=SimpleNamespace(message_id=2))
bot.send_video = AsyncMock(return_value=SimpleNamespace(message_id=3))
bot.send_document = AsyncMock(return_value=SimpleNamespace(message_id=4))
return bot
def _no_proxy(monkeypatch: pytest.MonkeyPatch) -> None:
for var in (
"TELEGRAM_PROXY", "HTTPS_PROXY", "https_proxy", "HTTP_PROXY",
"http_proxy", "ALL_PROXY", "all_proxy", "NO_PROXY", "no_proxy",
):
monkeypatch.delenv(var, raising=False)
monkeypatch.setattr("gateway.run._gateway_runner_ref", lambda: None, raising=False)
monkeypatch.setattr(sys, "platform", "linux")
def _tmpfile(suffix: str) -> str:
f = tempfile.NamedTemporaryFile(suffix=suffix, delete=False)
f.write(b"x")
f.close()
return f.name
def test_image_caption_rides_bubble_no_separate_text(monkeypatch: pytest.MonkeyPatch) -> None:
from tools.send_message_tool import _send_telegram
_no_proxy(monkeypatch)
bot = _make_bot()
_install_telegram_mock(monkeypatch, MagicMock(return_value=bot))
img = _tmpfile(".png")
try:
res = asyncio.run(
_send_telegram("tok", "123", "This Caption", media_files=[(img, False)])
)
assert res["success"] is True
# No separate text message; caption rides the photo.
bot.send_message.assert_not_awaited()
bot.send_photo.assert_awaited_once()
assert bot.send_photo.await_args.kwargs.get("caption") == "This Caption"
finally:
os.unlink(img)
def test_multi_file_keeps_separate_text(monkeypatch: pytest.MonkeyPatch) -> None:
from tools.send_message_tool import _send_telegram
_no_proxy(monkeypatch)
bot = _make_bot()
_install_telegram_mock(monkeypatch, MagicMock(return_value=bot))
img = _tmpfile(".png")
img2 = _tmpfile(".jpg")
try:
res = asyncio.run(
_send_telegram("tok", "123", "two pics", media_files=[(img, False), (img2, False)])
)
assert res["success"] is True
# Ambiguous caption→file association: text stays a separate message.
bot.send_message.assert_awaited()
assert bot.send_photo.await_count == 2
for call in bot.send_photo.await_args_list:
assert not call.kwargs.get("caption")
finally:
os.unlink(img)
os.unlink(img2)