Second voice UX iteration. Targets Marius's live-test pain points from today.
- **Voice-mode system prompt** (personality/VOICE_MODE.md, plumbed via
claude_session.build_system_prompt(voice_mode=True)) — when the voice
adapter starts a session, append voice-tailored instructions: short replies,
no markdown, no abbreviations, time without seconds, distances rounded
to "mii"/"milioane", no curly quotes / em-dash / ellipsis. Marius asked
for a "in-the-car friend" persona for voice.
- **Isolated voice session key** (router.py) — voice mode uses
`voice:<channel_id>` so it doesn't share context with the text adapter
on the same Discord channel. Fresh start, voice prompt applied
automatically without `/clear` ceremony. `/clear` drops both keys.
- **Metric units + Romanian thousands** (src/voice/normalize.py) —
`384.000 km` was being read as "trei sute optzeci și patru virgulă zero
zero zero km" because the dot was treated as decimal separator and `km`
wasn't expanded. New `normalize_thousands` collapses Romanian thousands
separators (`X.000`/`X.000.000`) before number expansion, and
`expand_units` handles km/kg/cm/mm/ml/ha/mp with correct Romanian
pluralization ("un kilometru", "două kilograme", "douăzeci de
centimetri", "o sută de kilometri" with "de" particle).
- **`/voice setvoice <M1-F5>` slash command** (discord_voice.py) — Discord
native autocomplete; swaps the live TTSQueue voice_id AND persists
voice.default_voice to config.json. No restart needed.
- **Verbal voice change** (src/voice/voice_commands.py — new module +
29 tests) — say "schimbă vocea pe M5" / "vorbește cu vocea F3" / "voce
em cinci" from inside the voice channel. Detector requires both a
trigger word (voce/vorbește/schimbă/treci pe) and a recognizable voice
ID (direct "M5", word form "em cinci", or fallback substring match for
Whisper-mangled forms like "unul cinci"=M5 and "Mâcinci"=M5). On
detection: live-swap, persist to config, mirror to chat with
`🎤 ... / 🔊 Voce → M5`, speak short ack in the NEW voice, skip
Claude. "pământinci" still can't be recovered (no recoverable digit
substring); user gets passthrough to Claude in that case.
- **Whisper initial_prompt** now lists the voice-command vocabulary so
STT biases toward producing clean "M5" / "F3" tokens instead of
inventing "pământ" / "unul" phonetic neighbors.
- **Fast barge-in** (pipeline.py EchoVoiceSink) — previously `ttsq.clear()`
only fired in `on_segment_done` (after 800ms silence + 2-3s STT ≈ 3s lag).
Now also fires from the sink as soon as VAD detects ≥2 consecutive
windows (~200ms) of sustained speech on Marius's user while Echo has
pending TTS frames. Single-window glitches don't cut Echo off; sustained
speech does. (Acoustic echo bleed-through still requires headphones —
no AEC in the bot.)
- Tests: 130 voice + router tests pass; updated test_router.py to expect
`/clear` to drop both text and voice session keys.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
314 lines
12 KiB
Python
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)
|