Text-to-speech engines, transcription, voice mode and wake word register no tool, so 28 files leave tools/ for a root package with four sub-packages: speech/synthesis, speech/transcription, speech/voice and speech/wake_word. tools/tts_tool.py, which registers text_to_speech, stays, and so does tools/tts_elevenlabs_voices.py, which builds part of that tool's schema. Each __init__.py is a docstring only. File names keep their stem and drop the prefix the folder makes redundant. transcription_tools becomes transcription/transcriber, transcription_common becomes transcription/settings, wake_word becomes wake_word/listener, and tts_tool_openai becomes synthesis/openai_compatible: neutts_synth.py runs by file path from that folder, where an openai.py would shadow the openai package. The NeuTTS samples and the bundled wake-word model move with the code that reads them through __file__. Function bodies are unchanged and every import stays where it was. Logger names follow the modules: a moved module logs under its speech.* path, and a literal that named a moved module names its new path. speech joins the tools log component, so `hermes logs --component tools` shows the same lines. The seven synthesis files that share the logger of tools/tts_tool.py keep it. tools.tts_streaming keeps a generated file at its old path. Every release from v0.21.6 to v0.23.5 imports it inside the CLI and TUI turn, outside any try, and an idle process has not loaded it yet, so a process that outlives `hermes update` would fail its first turn. No other moved name gets a file. speech joins pyproject's packages.find, FIRST_PARTY_MODULE_ROOTS, the plugin isolation audit, the known logger roots of `hermes debug share`, six check scripts, the P33 profile-scope pattern, the subprocess env guard test, the packaging test and three fixtures that copy a source tree. Tests whose subject is a moved module move to tests/speech/ with the fixtures only they use. Pinned catalog plugins import these moved modules by the old name, so each has a key in hermes_agent/compat/moves.py and the plugin guide lists it with the release that may remove it (no earlier than v0.27.0): tools.transcription_cloud tools.transcription_tools tools.tts_text_normalize tools.tts_tool_providers tools.voice_live tools.voice_mode tools.voice_mode_transcript
144 lines
5.8 KiB
Python
144 lines
5.8 KiB
Python
"""Regression tests for #53259.
|
|
|
|
When TTS/STT packages (edge-tts, elevenlabs, mistralai) installed outside
|
|
the venv but importable on sys.path (e.g. via PYTHONPATH, Docker layered
|
|
filesystems), the lazy-import helpers must fall through to the raw import
|
|
instead of re-raising dependency-provisioning failures as ImportError.
|
|
|
|
The PM branch provisions through pm.ensure_import (replacing the old
|
|
pm.ensure_import provisioning); the helper must swallow its failure the
|
|
same way. Uses sys.modules fixtures so builtins.__import__ stays intact.
|
|
"""
|
|
|
|
import sys
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from pm.extras import ensure_import as _pm_ensure_import
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _clean_tts_modules():
|
|
"""Remove TTS packages from sys.modules so each test starts fresh."""
|
|
removed = {}
|
|
for name in ("edge_tts", "elevenlabs", "elevenlabs.client",
|
|
"mistralai", "mistralai.client"):
|
|
if name in sys.modules:
|
|
removed[name] = sys.modules.pop(name)
|
|
yield
|
|
for name in ("edge_tts", "elevenlabs", "elevenlabs.client",
|
|
"mistralai", "mistralai.client"):
|
|
sys.modules.pop(name, None)
|
|
sys.modules.update(removed)
|
|
|
|
|
|
class TestEdgeTtsPythonpathFallback:
|
|
def test_falls_through_on_provisioning_failure(self):
|
|
"""A provisioning failure must not prevent raw import."""
|
|
mock_edge_tts = MagicMock()
|
|
with patch.dict(sys.modules, {"edge_tts": mock_edge_tts}), \
|
|
patch("pm.extras.ensure_import", side_effect=RuntimeError("provisioning failed")):
|
|
from tools.tts_tool import _import_edge_tts
|
|
result = _import_edge_tts()
|
|
assert result is mock_edge_tts
|
|
|
|
def test_raises_when_package_truly_missing(self):
|
|
"""When the package is truly absent, ImportError must propagate."""
|
|
with patch("pm.extras.ensure_import"), \
|
|
patch.dict(sys.modules, {"edge_tts": None}):
|
|
from tools.tts_tool import _import_edge_tts
|
|
with pytest.raises(ImportError):
|
|
_import_edge_tts()
|
|
|
|
|
|
class TestElevenLabsPythonpathFallback:
|
|
def test_falls_through_on_provisioning_failure(self):
|
|
"""A provisioning failure must not prevent raw import."""
|
|
mock_cls = MagicMock()
|
|
mock_client_pkg = MagicMock()
|
|
mock_client_pkg.ElevenLabs = mock_cls
|
|
with patch.dict(sys.modules, {
|
|
"elevenlabs": mock_client_pkg,
|
|
"elevenlabs.client": mock_client_pkg,
|
|
}), patch("pm.extras.ensure_import", side_effect=RuntimeError("provisioning failed")):
|
|
from tools.tts_tool import _import_elevenlabs
|
|
result = _import_elevenlabs()
|
|
assert result is mock_cls
|
|
|
|
def test_raises_when_package_truly_missing(self):
|
|
"""When the package is truly absent, ImportError must propagate."""
|
|
with patch("pm.extras.ensure_import"), \
|
|
patch.dict(sys.modules, {"elevenlabs": None,
|
|
"elevenlabs.client": None}):
|
|
from tools.tts_tool import _import_elevenlabs
|
|
with pytest.raises(ImportError):
|
|
_import_elevenlabs()
|
|
|
|
|
|
class TestMistralPythonpathFallback:
|
|
def test_falls_through_on_provisioning_failure(self):
|
|
"""A provisioning failure must not prevent raw import."""
|
|
mock_cls = MagicMock()
|
|
mock_mistralai = MagicMock()
|
|
mock_mistralai.Mistral = mock_cls
|
|
with patch.dict(sys.modules, {
|
|
"mistralai": mock_mistralai,
|
|
"mistralai.client": mock_mistralai,
|
|
}), patch("pm.extras.ensure_import", side_effect=RuntimeError("provisioning failed")):
|
|
from tools.tts_tool import _import_mistral_client
|
|
result = _import_mistral_client()
|
|
assert result is mock_cls
|
|
|
|
def test_raises_when_package_truly_missing(self):
|
|
"""When the package is truly absent, ImportError must propagate."""
|
|
with patch("pm.extras.ensure_import"), \
|
|
patch.dict(sys.modules, {"mistralai": None,
|
|
"mistralai.client": None}):
|
|
from tools.tts_tool import _import_mistral_client
|
|
with pytest.raises(ImportError):
|
|
_import_mistral_client()
|
|
|
|
|
|
# ── STT: _transcribe_mistral fallthrough ───────────────────────────────────
|
|
|
|
|
|
class TestMistralSttPythonpathFallback:
|
|
def test_transcribe_mistral_falls_through_on_provisioning_failure(
|
|
self, tmp_path,
|
|
):
|
|
"""A provisioning failure for stt.mistral must not block
|
|
transcription when mistralai is importable via PYTHONPATH."""
|
|
from speech.transcription.transcriber import _transcribe_mistral
|
|
|
|
audio_file = tmp_path / "audio.wav"
|
|
audio_file.write_bytes(b"fake-audio")
|
|
|
|
mock_client_cls = MagicMock()
|
|
mock_result = MagicMock()
|
|
mock_result.text = "hello world"
|
|
mock_client_cls.return_value.__enter__ = MagicMock(
|
|
return_value=MagicMock(
|
|
audio=MagicMock(
|
|
transcriptions=MagicMock(
|
|
complete=MagicMock(return_value=mock_result),
|
|
),
|
|
),
|
|
),
|
|
)
|
|
mock_client_cls.return_value.__exit__ = MagicMock(return_value=False)
|
|
|
|
mock_mistralai = MagicMock()
|
|
mock_mistralai.Mistral = mock_client_cls
|
|
|
|
with patch.dict(sys.modules, {
|
|
"mistralai": mock_mistralai,
|
|
"mistralai.client": mock_mistralai,
|
|
}), patch("pm.extras.ensure_import", side_effect=RuntimeError("provisioning failed")), \
|
|
patch("hermes_cli.config.get_env_value",
|
|
return_value="test-key"):
|
|
result = _transcribe_mistral(str(audio_file), "mistral-large-latest")
|
|
|
|
assert result["success"] is True
|
|
assert result["transcript"] == "hello world"
|