"""Tests for the voice-chat stop phrase (say "stop" and nothing else to end). Contract: - `is_voice_stop_phrase` matches ONLY when the whole utterance equals a configured phrase (case-insensitive, surrounding punctuation stripped). - Default phrase list is ("stop",); `voice.stop_phrases` in config.yaml customizes it; `[]` disables the feature. - In the shared continuous loop, a stop phrase halts the loop (like the silent-cycle limit) and is NEVER delivered to the agent. """ from unittest.mock import patch import pytest from tools.voice_mode import ( DEFAULT_VOICE_STOP_PHRASES, _load_voice_stop_phrases, is_voice_stop_phrase, ) class TestIsVoiceStopPhrase: @pytest.mark.parametrize("utterance", [ "stop", "Stop", "STOP", "stop.", "Stop!", " stop ", '"Stop."', "stop?", ]) def test_bare_stop_matches(self, utterance): assert is_voice_stop_phrase(utterance, ("stop",)) is True @pytest.mark.parametrize("utterance", [ "stop doing that", "please stop", "stop the build and rerun tests", "don't stop", "stopwatch", "", " ", "ok", ]) def test_longer_utterances_pass_through(self, utterance): assert is_voice_stop_phrase(utterance, ("stop",)) is False def test_custom_phrases(self): phrases = ("stop", "goodbye hermes") assert is_voice_stop_phrase("Goodbye Hermes!", phrases) is True assert is_voice_stop_phrase("goodbye hermes, one more thing", phrases) is False def test_empty_phrase_list_disables(self): assert is_voice_stop_phrase("stop", ()) is False def test_uses_config_when_phrases_omitted(self): with patch("tools.voice_mode._load_voice_stop_phrases", return_value=("halt",)): assert is_voice_stop_phrase("halt") is True assert is_voice_stop_phrase("stop") is False class TestLoadVoiceStopPhrases: def _with_cfg(self, voice_cfg): return patch( "hermes_cli.config.load_config", return_value={"voice": voice_cfg}, ) def test_default(self): with self._with_cfg({}): assert _load_voice_stop_phrases() == DEFAULT_VOICE_STOP_PHRASES def test_custom_list(self): with self._with_cfg({"stop_phrases": ["Stop", " Goodbye Hermes "]}): assert _load_voice_stop_phrases() == ("stop", "goodbye hermes") def test_empty_list_disables(self): with self._with_cfg({"stop_phrases": []}): assert _load_voice_stop_phrases() == () def test_bare_string_coerced(self): with self._with_cfg({"stop_phrases": "halt"}): assert _load_voice_stop_phrases() == ("halt",) def test_malformed_falls_back(self): with self._with_cfg({"stop_phrases": {"bad": "shape"}}): assert _load_voice_stop_phrases() == DEFAULT_VOICE_STOP_PHRASES def test_config_error_falls_back(self): with patch("hermes_cli.config.load_config", side_effect=RuntimeError): assert _load_voice_stop_phrases() == DEFAULT_VOICE_STOP_PHRASES class TestContinuousLoopStopPhrase: """The shared continuous-loop silence callback ends the loop on a stop phrase and never forwards it to on_transcript.""" def _run_silence_cycle(self, transcript_text): import hermes_cli.voice as v delivered = [] silent_limit_fired = [] class _FakeRecorder: _peak_rms = 500 def stop(self): return "/tmp/fake.wav" def cancel(self): pass def start(self, on_silence_stop=None): pass fake_result = {"success": True, "transcript": transcript_text} with patch.object(v, "_continuous_active", True), \ patch.object(v, "_continuous_recorder", _FakeRecorder()), \ patch.object(v, "_continuous_on_transcript", delivered.append), \ patch.object(v, "_continuous_on_status", None), \ patch.object(v, "_continuous_on_silent_limit", lambda: silent_limit_fired.append(True)), \ patch.object(v, "_continuous_no_speech_count", 0), \ patch.object(v, "transcribe_recording", return_value=fake_result), \ patch.object(v, "_play_beep", lambda **kw: None), \ patch.object(v.os.path, "isfile", return_value=False): v._continuous_on_silence() still_active = v._continuous_active return delivered, silent_limit_fired, still_active def test_stop_phrase_halts_loop_and_is_not_delivered(self): delivered, silent_limit, still_active = self._run_silence_cycle("Stop.") assert delivered == [] assert silent_limit == [True] assert still_active is False def test_normal_transcript_is_delivered(self): delivered, silent_limit, _ = self._run_silence_cycle("stop the build and rerun") assert delivered == ["stop the build and rerun"] assert silent_limit == []