hermes-agent/tests/tools/test_tts_opus_routing.py
Michel Belleau 33313e88f8 fix(matrix): enforce Ogg/Opus at send_voice boundary, probe metadata off-loop
Salvaged from PR #68063 (@malaiwah). MatrixAdapter.send_voice now transcodes
any non-Ogg audio to Ogg/Opus at the adapter boundary (best-effort — the
original file is sent unchanged when ffmpeg is unavailable), so MSC3245
voice bubbles render even when a caller hands the adapter MP3/WAV audio.
_matrix_voice_metadata_for_file probing now runs via asyncio.to_thread so
ffprobe/ffmpeg subprocess timeouts can't stall the adapter event loop.

The PR's tools/tts_tool.py want_opus hunk was dropped: main's
OPUS_VOICE_PLATFORMS set (PR #73072) already includes matrix; the Matrix
opus-routing test is kept.

Refs #14841
2026-07-28 11:55:48 -07:00

97 lines
3.3 KiB
Python

import json
from pathlib import Path
from unittest.mock import Mock
import pytest
from gateway.session_context import _UNSET, _VAR_MAP
from tools import tts_tool
def _reset_session_context() -> None:
for var in _VAR_MAP.values():
var.set(_UNSET)
@pytest.fixture(autouse=True)
def _clean_session_platform(monkeypatch):
_reset_session_context()
monkeypatch.delenv("HERMES_SESSION_PLATFORM", raising=False)
yield
_reset_session_context()
async def _write_edge_output(_text: str, output_path: str, _tts_config: dict) -> str:
Path(output_path).write_bytes(b"mp3")
return output_path
def test_edge_cli_preserves_native_mp3(tmp_path, monkeypatch):
out = tmp_path / "speech.mp3"
convert = Mock()
monkeypatch.setattr(tts_tool, "_load_tts_config", lambda: {"provider": "edge"})
monkeypatch.setattr(tts_tool, "_import_edge_tts", lambda: object())
monkeypatch.setattr(tts_tool, "_generate_edge_tts", _write_edge_output)
monkeypatch.setattr(tts_tool, "_convert_to_opus", convert)
result = json.loads(tts_tool.text_to_speech_tool("hello", output_path=str(out)))
assert result["success"] is True
assert result["file_path"] == str(out)
assert result["voice_compatible"] is False
assert result["media_tag"] == f"MEDIA:{out}"
convert.assert_not_called()
def test_edge_telegram_converts_to_opus_voice(tmp_path, monkeypatch):
out = tmp_path / "speech.mp3"
opus = tmp_path / "speech.ogg"
def fake_convert(path: str) -> str:
assert path == str(out)
opus.write_bytes(b"ogg")
return str(opus)
convert = Mock(side_effect=fake_convert)
monkeypatch.setenv("HERMES_SESSION_PLATFORM", "telegram")
monkeypatch.setattr(tts_tool, "_load_tts_config", lambda: {"provider": "edge"})
monkeypatch.setattr(tts_tool, "_import_edge_tts", lambda: object())
monkeypatch.setattr(tts_tool, "_generate_edge_tts", _write_edge_output)
monkeypatch.setattr(tts_tool, "_convert_to_opus", convert)
result = json.loads(tts_tool.text_to_speech_tool("hello", output_path=str(out)))
assert result["success"] is True
assert result["file_path"] == str(opus)
assert result["voice_compatible"] is True
assert result["media_tag"] == f"[[audio_as_voice]]\nMEDIA:{opus}"
convert.assert_called_once_with(str(out))
def test_edge_matrix_converts_to_opus_voice(tmp_path, monkeypatch):
"""Matrix voice bubbles need Ogg/Opus too (MSC3245, issue #14841)."""
out = tmp_path / "speech.mp3"
opus = tmp_path / "speech.ogg"
def fake_convert(path: str) -> str:
assert path == str(out)
opus.write_bytes(b"ogg")
return str(opus)
convert = Mock(side_effect=fake_convert)
monkeypatch.setenv("HERMES_SESSION_PLATFORM", "matrix")
monkeypatch.setattr(tts_tool, "_load_tts_config", lambda: {"provider": "edge"})
monkeypatch.setattr(tts_tool, "_import_edge_tts", lambda: object())
monkeypatch.setattr(tts_tool, "_generate_edge_tts", _write_edge_output)
monkeypatch.setattr(tts_tool, "_convert_to_opus", convert)
result = json.loads(tts_tool.text_to_speech_tool("hello", output_path=str(out)))
assert result["success"] is True
assert result["file_path"] == str(opus)
assert result["voice_compatible"] is True
assert result["media_tag"] == f"[[audio_as_voice]]\nMEDIA:{opus}"
convert.assert_called_once_with(str(out))