Files
echo-core/tests/test_router.py
Marius Mutu 4be70440e8 feat(voice): DAVE E2E + full voice UX (squash of voice/dave-recv)
Squashed branch: voice/dave-recv → master. Closes Pas 12 (DAVE E2E) and lands
voice-mode UX polish + verbal voice control on top of the Pas 1-10 scaffolding
already on master.

## DAVE E2E receive-side decrypt (e4f3177)

Vendored fork: discord-ext-voice-recv 0.5.3a+echo.dave1. Patches the receive
pipeline to handle Discord's mandatory DAVE encryption on voice gateway v=8.
- `_maybe_dave_decrypt`: uses davey.can_passthrough(user_id) as primary gate,
  falls through to dave.decrypt for DAVE-epoch peers, drops on decrypt failure
  without killing the reader thread.
- VAD fix: silero-vad v5+ requires exactly 512 samples; our 100ms window
  (1600 samples) was silently raising ValueError → STT never fired. Now slice
  into 512-sample chunks.
- Whisper: bumped beam_size 1→5 and added RO initial_prompt.
- Tests: 11 DAVE unit tests + 2 callback integration tests + contract test
  with fork-version guard.

## Voice UX polish (d1bc77e)

- Killed the 3s "mă gândesc" filler (always collided with Claude p50 4-7s).
- Barge-in via `ttsq.clear()` at top of `on_segment_done`.
- DTX silence-flush poller (200ms tick) — Discord stops sending RTP packets
  when silent, so the inline silence-check in sink.write() never fired for
  trailing audio; background thread handles it.
- `EchoStreamingAudioSource.read()` non-blocking — old `get_frame(timeout=0.1)`
  wrecked Discord's 20ms cadence and the client interpreted bursts as
  stuttering (Marius heard "4 de minute" instead of full sentence).
- RO time expansion: 23:09 → "douăzeci și trei și nouă minute".
- Supertonic Unicode sanitize centralized in tools/tts.py.
- Whisper local_files_only=True — no HF metadata GET on each startup.
- Diagnostic logging through sink → VAD → Claude stream → TTS chain.

## Voice mode iteration (e589e48)

- `personality/VOICE_MODE.md` — voice-tailored system prompt (short, no
  markdown, no abbreviations, time without seconds, distances in
  "mii"/"milioane"); plumbed via build_system_prompt(voice_mode=True).
- Isolated voice session key `voice:<channel_id>` — voice doesn't share
  context with text adapter on the same channel; auto-applied without
  /clear ceremony. /clear drops both keys.
- Metric units + Romanian thousands (normalize.py): "384.000 km" →
  "trei sute optzeci și patru de mii de kilometri" with feminine-correct
  pluralization and "de" particle for ≥20.
- `/voice setvoice <M1-F5>` slash command with native autocomplete; swaps
  live + persists voice.default_voice to config.json.
- Verbal voice change (src/voice/voice_commands.py + 29 tests) — "schimbă
  vocea pe M5", "voce em cinci", with permissive substring fallback for
  Whisper-mangled forms like "Mâcinci"=M5 and "unul cinci"=M5. Whisper
  initial_prompt now lists voice vocabulary to bias STT toward clean
  outputs.
- Fast barge-in: VAD ≥2 consecutive windows (~200ms) on Marius's user
  while Echo has pending TTS frames → cut him off mid-sentence so user
  doesn't wait the full silence + STT cycle. Acoustic echo bleed-through
  still requires headphones (no AEC).

## Test suite

130 voice + router tests pass (test_voice_recv_dave, test_voice_session_cleanup,
test_voice_adapter_contract, test_voice_normalize, test_voice_commands,
test_router).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-27 21:00:27 +00:00

314 lines
12 KiB
Python

"""Tests for src/router.py — message router."""
import pytest
from unittest.mock import MagicMock, patch
from src.router import route_message, _get_channel_config
@pytest.fixture(autouse=True)
def reset_router_config():
"""Reset the module-level _config before each test."""
import src.router
original = src.router._config
src.router._config = None
yield
src.router._config = original
# --- /clear command ---
class TestClearCommand:
@patch("src.router._get_config")
@patch("src.router.clear_session")
def test_clear_active_session(self, mock_clear, mock_get_config):
mock_clear.return_value = True
mock_cfg = MagicMock()
mock_cfg.get.return_value = "sonnet"
mock_get_config.return_value = mock_cfg
response, is_cmd = route_message("ch-1", "user-1", "/clear")
assert response == "Session cleared. Model reset to sonnet."
assert is_cmd is True
# /clear drops both the text-adapter session and the isolated voice
# session for the same Discord channel.
mock_clear.assert_any_call("ch-1")
mock_clear.assert_any_call("voice:ch-1")
@patch("src.router._get_config")
@patch("src.router.clear_session")
def test_clear_no_session(self, mock_clear, mock_get_config):
mock_clear.return_value = False
mock_cfg = MagicMock()
mock_cfg.get.return_value = "sonnet"
mock_get_config.return_value = mock_cfg
response, is_cmd = route_message("ch-1", "user-1", "/clear")
assert response == "No active session."
assert is_cmd is True
@patch("src.router._get_config")
@patch("src.router.clear_session")
def test_clear_mentions_model_reset(self, mock_clear, mock_get_config):
mock_clear.return_value = True
mock_cfg = MagicMock()
mock_cfg.get.return_value = "opus"
mock_get_config.return_value = mock_cfg
response, is_cmd = route_message("ch-1", "user-1", "/clear")
assert "model reset" in response.lower()
assert "opus" in response
# --- /model command ---
class TestModelCommand:
@patch("src.router.get_active_session")
def test_model_show_current_with_session(self, mock_get):
mock_get.return_value = {"model": "opus"}
response, is_cmd = route_message("ch-1", "user-1", "/model")
assert is_cmd is True
assert "opus" in response
assert "haiku" in response # available models listed
@patch("src.router._get_config")
@patch("src.router._get_channel_config")
@patch("src.router.get_active_session")
def test_model_show_current_no_session(self, mock_get, mock_chan_cfg, mock_get_config):
mock_get.return_value = None
mock_chan_cfg.return_value = None
mock_cfg = MagicMock()
mock_cfg.get.return_value = "sonnet"
mock_get_config.return_value = mock_cfg
response, is_cmd = route_message("ch-1", "user-1", "/model")
assert is_cmd is True
assert "sonnet" in response
@patch("src.router.set_session_model")
@patch("src.router.get_active_session")
def test_model_change_opus(self, mock_get, mock_set):
mock_get.return_value = {"model": "sonnet", "session_id": "abc"}
response, is_cmd = route_message("ch-1", "user-1", "/model opus")
assert is_cmd is True
mock_set.assert_called_once_with("ch-1", "opus")
assert "opus" in response
def test_model_invalid_choice(self):
response, is_cmd = route_message("ch-1", "user-1", "/model gpt4")
assert is_cmd is True
assert "invalid" in response.lower()
assert "gpt4" in response
@patch("src.claude_session._save_sessions")
@patch("src.claude_session._load_sessions")
@patch("src.router.get_active_session")
def test_model_change_no_session_presets(self, mock_get, mock_load, mock_save):
mock_get.return_value = None
mock_load.return_value = {}
response, is_cmd = route_message("ch-1", "user-1", "/model haiku")
assert is_cmd is True
mock_save.assert_called_once()
saved = mock_save.call_args[0][0]
assert saved["ch-1"]["model"] == "haiku"
assert "haiku" in response
# --- /status command ---
class TestStatusCommand:
@patch("src.router.get_active_session")
def test_status_active_session(self, mock_get):
mock_get.return_value = {
"model": "sonnet",
"session_id": "abcdef123456789",
"message_count": 5,
}
response, is_cmd = route_message("ch-1", "user-1", "/status")
assert is_cmd is True
assert "sonnet" in response
assert "abcdef123456" in response # first 12 chars
assert "5" in response
@patch("src.router.get_active_session")
def test_status_no_session(self, mock_get):
mock_get.return_value = None
response, is_cmd = route_message("ch-1", "user-1", "/status")
assert response == "No active session."
assert is_cmd is True
# --- Ralph command dispatch ---
class TestRalphDispatch:
def test_p_without_args_returns_usage(self):
response, is_cmd = route_message("ch-1", "user-1", "/p")
assert "Folosire: /p" in response
assert is_cmd is True
def test_whatsapp_appends_redirect_hint_on_usage(self):
"""WhatsApp users see a redirect line pointing them to Discord/TG."""
response, is_cmd = route_message(
"ch-1", "user-1", "/p", adapter_name="whatsapp"
)
assert "Folosire: /p" in response
assert "Discord sau Telegram" in response
def test_discord_does_not_get_whatsapp_redirect(self):
response, is_cmd = route_message(
"ch-1", "user-1", "/p", adapter_name="discord"
)
assert "Folosire: /p" in response
assert "Discord sau Telegram" not in response
# --- Unknown command ---
class TestUnknownCommand:
def test_unknown_command(self):
response, is_cmd = route_message("ch-1", "user-1", "/foo")
assert response == "Unknown command: /foo"
assert is_cmd is True
def test_unknown_command_with_args(self):
response, is_cmd = route_message("ch-1", "user-1", "/bar baz")
assert response == "Unknown command: /bar"
assert is_cmd is True
# --- Regular messages ---
class TestRegularMessage:
@patch("src.router._get_channel_config")
@patch("src.router._get_config")
@patch("src.router.send_message")
def test_sends_to_claude(self, mock_send, mock_get_config, mock_chan_cfg):
mock_send.return_value = "Hello from Claude!"
mock_chan_cfg.return_value = None
mock_cfg = MagicMock()
mock_cfg.get.return_value = "sonnet"
mock_get_config.return_value = mock_cfg
response, is_cmd = route_message("ch-1", "user-1", "hello")
assert response == "Hello from Claude!"
assert is_cmd is False
mock_send.assert_called_once_with("ch-1", "hello", model="sonnet", on_text=None, voice_mode=False)
@patch("src.router.send_message")
def test_model_override(self, mock_send):
mock_send.return_value = "Response"
response, is_cmd = route_message("ch-1", "user-1", "hello", model="opus")
assert response == "Response"
assert is_cmd is False
mock_send.assert_called_once_with("ch-1", "hello", model="opus", on_text=None, voice_mode=False)
@patch("src.router._get_channel_config")
@patch("src.router._get_config")
@patch("src.router.send_message")
def test_claude_error(self, mock_send, mock_get_config, mock_chan_cfg):
mock_send.side_effect = RuntimeError("API timeout")
mock_chan_cfg.return_value = None
mock_cfg = MagicMock()
mock_cfg.get.return_value = "sonnet"
mock_get_config.return_value = mock_cfg
response, is_cmd = route_message("ch-1", "user-1", "hello")
assert "Error: API timeout" in response
assert is_cmd is False
@patch("src.router._get_channel_config")
@patch("src.router._get_config")
@patch("src.router.send_message")
def test_on_text_passed_through(self, mock_send, mock_get_config, mock_chan_cfg):
mock_send.return_value = "ok"
mock_chan_cfg.return_value = None
mock_cfg = MagicMock()
mock_cfg.get.return_value = "sonnet"
mock_get_config.return_value = mock_cfg
cb = lambda t: None
route_message("ch-1", "user-1", "hello", on_text=cb)
mock_send.assert_called_once_with("ch-1", "hello", model="sonnet", on_text=cb, voice_mode=False)
# --- _get_channel_config ---
class TestGetChannelConfig:
@patch("src.router._get_config")
def test_finds_by_id(self, mock_get_config):
mock_cfg = MagicMock()
mock_cfg.get.return_value = {
"general": {"id": "ch-1", "default_model": "haiku"},
"dev": {"id": "ch-2"},
}
mock_get_config.return_value = mock_cfg
result = _get_channel_config("ch-1")
assert result == {"id": "ch-1", "default_model": "haiku"}
@patch("src.router._get_config")
def test_returns_none_when_not_found(self, mock_get_config):
mock_cfg = MagicMock()
mock_cfg.get.return_value = {"general": {"id": "ch-1"}}
mock_get_config.return_value = mock_cfg
result = _get_channel_config("ch-999")
assert result is None
# --- Model resolution ---
class TestModelResolution:
@patch("src.router._get_channel_config")
@patch("src.router._get_config")
@patch("src.router.send_message")
def test_channel_default_model(self, mock_send, mock_get_config, mock_chan_cfg):
"""Channel config default_model takes priority."""
mock_send.return_value = "ok"
mock_chan_cfg.return_value = {"id": "ch-1", "default_model": "haiku"}
route_message("ch-1", "user-1", "hello")
mock_send.assert_called_once_with("ch-1", "hello", model="haiku", on_text=None, voice_mode=False)
@patch("src.router._get_channel_config")
@patch("src.router._get_config")
@patch("src.router.send_message")
def test_global_default_model(self, mock_send, mock_get_config, mock_chan_cfg):
"""Falls back to bot.default_model when channel has no default."""
mock_send.return_value = "ok"
mock_chan_cfg.return_value = {"id": "ch-1"} # no default_model
mock_cfg = MagicMock()
mock_cfg.get.return_value = "opus"
mock_get_config.return_value = mock_cfg
route_message("ch-1", "user-1", "hello")
mock_send.assert_called_once_with("ch-1", "hello", model="opus", on_text=None, voice_mode=False)
@patch("src.router._get_channel_config")
@patch("src.router._get_config")
@patch("src.router.send_message")
def test_sonnet_fallback(self, mock_send, mock_get_config, mock_chan_cfg):
"""Falls back to 'sonnet' when no channel or global default."""
mock_send.return_value = "ok"
mock_chan_cfg.return_value = None
mock_cfg = MagicMock()
mock_cfg.get.side_effect = lambda key, default=None: default
mock_get_config.return_value = mock_cfg
route_message("ch-1", "user-1", "hello")
mock_send.assert_called_once_with("ch-1", "hello", model="sonnet", on_text=None, voice_mode=False)
@patch("src.router.get_active_session")
@patch("src.router.send_message")
def test_session_model_takes_priority(self, mock_send, mock_get_session):
"""Session model takes priority over channel and global defaults."""
mock_send.return_value = "ok"
mock_get_session.return_value = {"model": "opus", "session_id": "abc"}
route_message("ch-1", "user-1", "hello")
mock_send.assert_called_once_with("ch-1", "hello", model="opus", on_text=None, voice_mode=False)