Files
Timmy-time-dashboard/tests/timmy/test_voice_loop.py
Hermes Agent dbadfc425d
Some checks failed
Tests / lint (push) Successful in 4s
Tests / test (push) Failing after 14s
feat: sovereign voice loop — timmy voice command
Adds fully local listen-think-speak voice interface.
STT: Whisper, LLM: Ollama, TTS: Piper. No cloud, no network.

- src/timmy/voice_loop.py: VoiceLoop with VAD, Whisper, Piper
- src/timmy/cli.py: new voice command
- pyproject.toml: voice extras updated
- 20 new tests
2026-03-14 13:58:56 -04:00

274 lines
8.5 KiB
Python

"""Tests for the sovereign voice loop.
These tests verify the VoiceLoop components without requiring a microphone,
Whisper model, or Piper installation — all I/O is mocked.
"""
from pathlib import Path
from unittest.mock import MagicMock, patch
import numpy as np
from timmy.voice_loop import VoiceConfig, VoiceLoop
# ── VoiceConfig tests ──────────────────────────────────────────────────────
class TestVoiceConfig:
def test_defaults(self):
cfg = VoiceConfig()
assert cfg.whisper_model == "base.en"
assert cfg.sample_rate == 16000
assert cfg.silence_threshold == 0.015
assert cfg.silence_duration == 1.5
assert cfg.min_utterance == 0.5
assert cfg.max_utterance == 30.0
assert cfg.session_id == "voice"
assert cfg.use_say_fallback is False
def test_custom_values(self):
cfg = VoiceConfig(
whisper_model="tiny.en",
silence_threshold=0.02,
session_id="custom",
use_say_fallback=True,
)
assert cfg.whisper_model == "tiny.en"
assert cfg.silence_threshold == 0.02
assert cfg.session_id == "custom"
assert cfg.use_say_fallback is True
# ── VoiceLoop unit tests ──────────────────────────────────────────────────
class TestVoiceLoopInit:
def test_default_config(self):
loop = VoiceLoop()
assert loop.config.whisper_model == "base.en"
assert loop._running is False
assert loop._speaking is False
def test_custom_config(self):
cfg = VoiceConfig(whisper_model="tiny.en")
loop = VoiceLoop(config=cfg)
assert loop.config.whisper_model == "tiny.en"
class TestPiperFallback:
def test_falls_back_to_say_when_no_voice_file(self):
cfg = VoiceConfig(piper_voice=Path("/nonexistent/voice.onnx"))
loop = VoiceLoop(config=cfg)
loop._ensure_piper()
assert loop.config.use_say_fallback is True
def test_keeps_piper_when_voice_exists(self, tmp_path):
voice_file = tmp_path / "test.onnx"
voice_file.write_bytes(b"fake model")
cfg = VoiceConfig(piper_voice=voice_file)
loop = VoiceLoop(config=cfg)
loop._ensure_piper()
assert loop.config.use_say_fallback is False
class TestTranscribe:
def test_transcribes_audio(self):
"""Whisper transcription returns cleaned text."""
loop = VoiceLoop()
mock_model = MagicMock()
mock_model.transcribe.return_value = {"text": " Hello Timmy "}
loop._whisper_model = mock_model
audio = np.random.randn(16000).astype(np.float32)
result = loop._transcribe(audio)
assert result == "Hello Timmy"
mock_model.transcribe.assert_called_once()
def test_transcribes_empty_returns_empty(self):
loop = VoiceLoop()
mock_model = MagicMock()
mock_model.transcribe.return_value = {"text": " "}
loop._whisper_model = mock_model
audio = np.random.randn(16000).astype(np.float32)
result = loop._transcribe(audio)
assert result == ""
class TestThink:
@patch("timmy.voice_loop.asyncio")
def test_think_returns_response(self, mock_asyncio):
mock_asyncio.run.return_value = "I am Timmy."
loop = VoiceLoop()
result = loop._think("Who are you?")
assert result == "I am Timmy."
@patch("timmy.voice_loop.asyncio")
def test_think_handles_error(self, mock_asyncio):
mock_asyncio.run.side_effect = RuntimeError("Ollama down")
loop = VoiceLoop()
result = loop._think("test")
assert "trouble" in result.lower()
class TestSpeakSay:
@patch("subprocess.Popen")
def test_speak_say_calls_subprocess(self, mock_popen):
mock_proc = MagicMock()
mock_proc.wait.return_value = 0
mock_popen.return_value = mock_proc
cfg = VoiceConfig(use_say_fallback=True)
loop = VoiceLoop(config=cfg)
loop._speak_say("Hello")
mock_popen.assert_called_once()
args = mock_popen.call_args[0][0]
assert args[0] == "say"
assert "Hello" in args
@patch("subprocess.Popen", side_effect=FileNotFoundError)
def test_speak_say_handles_missing(self, mock_popen):
cfg = VoiceConfig(use_say_fallback=True)
loop = VoiceLoop(config=cfg)
# Should not raise
loop._speak_say("Hello")
class TestSpeakPiper:
@patch("timmy.voice_loop.VoiceLoop._play_audio")
@patch("subprocess.run")
def test_speak_piper_generates_and_plays(self, mock_run, mock_play):
mock_run.return_value = MagicMock(returncode=0, stderr="")
voice_path = Path("/tmp/test_voice.onnx")
cfg = VoiceConfig(piper_voice=voice_path)
loop = VoiceLoop(config=cfg)
loop._speak_piper("Hello from Piper")
# Piper was called
mock_run.assert_called_once()
cmd = mock_run.call_args[0][0]
assert cmd[0] == "piper"
assert "--model" in cmd
# Audio was played
mock_play.assert_called_once()
@patch("timmy.voice_loop.VoiceLoop._speak_say")
@patch("subprocess.run")
def test_speak_piper_falls_back_on_error(self, mock_run, mock_say):
mock_run.return_value = MagicMock(returncode=1, stderr="model error")
cfg = VoiceConfig(piper_voice=Path("/tmp/test.onnx"))
loop = VoiceLoop(config=cfg)
loop._speak_piper("test")
# Should fall back to say
mock_say.assert_called_once_with("test")
class TestHallucinationFilter:
"""Whisper tends to hallucinate on silence/noise. The loop should filter these."""
def test_known_hallucinations_filtered(self):
hallucinations = [
"you",
"thanks.",
"Thank you.",
"Bye.",
"Thanks for watching!",
"Thank you for watching!",
]
for text in hallucinations:
assert text.lower() in (
"you",
"thanks.",
"thank you.",
"bye.",
"",
"thanks for watching!",
"thank you for watching!",
), f"'{text}' should be filtered"
class TestExitCommands:
"""Voice loop should recognize exit commands."""
def test_exit_commands(self):
exits = ["goodbye", "exit", "quit", "stop", "goodbye timmy", "stop listening"]
for cmd in exits:
assert cmd.lower().strip().rstrip(".!") in (
"goodbye",
"exit",
"quit",
"stop",
"goodbye timmy",
"stop listening",
), f"'{cmd}' should be an exit command"
class TestPlayAudio:
@patch("subprocess.Popen")
def test_play_audio_calls_afplay(self, mock_popen):
mock_proc = MagicMock()
mock_proc.poll.side_effect = [None, 0] # Running, then done
mock_popen.return_value = mock_proc
loop = VoiceLoop()
loop._play_audio("/tmp/test.wav")
mock_popen.assert_called_once()
args = mock_popen.call_args[0][0]
assert args[0] == "afplay"
@patch("subprocess.Popen")
def test_play_audio_interruptible(self, mock_popen):
mock_proc = MagicMock()
# Simulate running, then we interrupt
call_count = 0
def poll_side_effect():
nonlocal call_count
call_count += 1
return None # Always running
mock_proc.poll.side_effect = poll_side_effect
mock_popen.return_value = mock_proc
loop = VoiceLoop()
loop._interrupted = True # Pre-set interrupt
loop._play_audio("/tmp/test.wav")
mock_proc.terminate.assert_called_once()
class TestStopMethod:
def test_stop_sets_running_false(self):
loop = VoiceLoop()
loop._running = True
loop.stop()
assert loop._running is False
class TestSpeakSetsFlag:
@patch("timmy.voice_loop.VoiceLoop._speak_say")
def test_speaking_flag_set_during_speech(self, mock_say):
cfg = VoiceConfig(use_say_fallback=True)
loop = VoiceLoop(config=cfg)
# Before speak
assert loop._speaking is False
# Mock say to check flag during execution
def check_flag(text):
assert loop._speaking is True
mock_say.side_effect = check_flag
loop._speak("Hello")
# After speak
assert loop._speaking is False