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

127 lines
4.7 KiB
Python

"""
Tests for #24870 — Telegram: audio file attachments must NOT be routed to STT.
Telegram distinguishes three kinds of audio payloads:
- message.voice → Opus/OGG voice message → STT pipeline
- message.audio → audio file attachment → file path note, NOT STT
- message.document (audio mime) → generic file route
These tests confirm that:
1. MessageType.VOICE events still flow through the STT pipeline.
2. MessageType.AUDIO events bypass STT and get a file-path context note instead.
3. Mixed media lists (voice + audio) split correctly.
"""
from unittest.mock import patch
import pytest
from gateway.config import GatewayConfig, Platform
from gateway.platforms.base import MessageEvent, MessageType
from gateway.session import SessionSource
def _make_runner(stt_enabled: bool = True) -> "GatewayRunner": # type: ignore[name-defined]
from gateway.run import GatewayRunner
runner = GatewayRunner.__new__(GatewayRunner)
runner.config = GatewayConfig(stt_enabled=stt_enabled)
runner.adapters = {}
runner._model = "test-model"
runner._base_url = ""
runner._has_setup_skill = lambda: False
return runner
def _voice_event(path: str = "/tmp/voice.ogg") -> MessageEvent:
return MessageEvent(
text="",
message_type=MessageType.VOICE,
source=SessionSource(platform=Platform.TELEGRAM, chat_id="1", chat_type="dm"),
media_urls=[path],
media_types=["audio/ogg"],
)
def _audio_event(path: str = "/tmp/song.mp3") -> MessageEvent:
return MessageEvent(
text="",
message_type=MessageType.AUDIO,
source=SessionSource(platform=Platform.TELEGRAM, chat_id="1", chat_type="dm"),
media_urls=[path],
media_types=["audio/mpeg"],
)
# ---------------------------------------------------------------------------
# 1. VOICE still goes through STT
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_voice_message_still_transcribed():
"""MessageType.VOICE must still be sent through _enrich_message_with_transcription."""
runner = _make_runner(stt_enabled=True)
source = SessionSource(platform=Platform.TELEGRAM, chat_id="1", chat_type="dm")
event = _voice_event("/tmp/voice.ogg")
with patch(
"tools.transcription_tools.transcribe_audio",
return_value={"success": True, "transcript": "hello world", "provider": "whisper"},
) as mock_transcribe:
result = await runner._prepare_inbound_message_text(
event=event,
source=source,
history=[],
)
mock_transcribe.assert_called_once_with("/tmp/voice.ogg")
# The transcript passes through as a plain quoted line — no "voice message"
# meta-commentary in the LLM-visible prompt.
assert "hello world" in result
# ---------------------------------------------------------------------------
# 2. AUDIO file attachment bypasses STT
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_audio_attachment_context_note_format():
"""Context note for audio file attachments should include the file path and guidance."""
runner = _make_runner(stt_enabled=True)
source = SessionSource(platform=Platform.TELEGRAM, chat_id="1", chat_type="dm")
event = _audio_event("/tmp/cache_12345_my_song.mp3")
with patch(
"tools.transcription_tools.transcribe_audio",
side_effect=AssertionError("must not be called"),
):
with patch(
"tools.credential_files.to_agent_visible_cache_path",
side_effect=lambda p: p,
):
result = await runner._prepare_inbound_message_text(
event=event,
source=source,
history=[],
)
assert "my_song.mp3" in result
assert "audio file attachment" in result.lower()
# Should NOT contain the voice-message transcription wrapper text
assert "voice message" not in result.lower()
# Guides the agent to transcribe/process the file itself rather than
# punting back to the user (same bug class as the PDF/DOCX note).
assert "transcri" in result.lower()
assert "ask the user what they'd like" not in result.lower()
# ---------------------------------------------------------------------------
# 3. STT disabled still results in no transcription for audio file attachments
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# 4. Telegram gateway: msg.audio → MessageType.AUDIO (not VOICE)
# ---------------------------------------------------------------------------