hermes-agent/tests/gateway/test_voice_mode_platform_isolation.py
Teknium 39975613b1
test: prune wave 2 + speed fixes — 28,106 → 19,757 test functions, suite wall 315s → 294s
Second, deeper pass over tools/gateway/hermes_cli plus first pass over
the trees wave 1 missed (acp, acp_adapter, skills, computer_use, docker,
dashboard, conformance, monitoring, secret_sources, hermes_state,
providers). Same rubric as wave 1 (AGENTS.md test policy); security,
alternation/caching invariants, issue-number regressions, and E2E kept.

Real test-quality fixes found and rooted out along the way:
- tests/tools/test_command_guards.py made real auxiliary-LLM HTTPS calls
  (DEFAULT_CONFIG smart-approval leaked in) — pinned approval
  mode=manual via autouse fixture: 17.4s → 0.4s.
- test_model_switch_custom_providers.py / test_user_providers_model_switch.py
  silently probed live provider catalogs (~2s/test) — stubbed
  cached_provider_model_ids/provider_model_ids/fetch_api_models.
- test_telegram_noise_filter.py: 15-platform copy-paste matrix over
  shared gateway.run logic → 3 representative platforms (55s → 3.9s).
- test_gateway_shutdown.py: stop()'s 5s interrupt-deadline loop spun on
  MagicMock agents — interrupt.side_effect now clears _running_agents
  (22s → 1.0s).
- test_gateway_inactivity_timeout.py poll-harness timings shrunk 3-5x
  (24s → 1.1s); test_mcp_stability.py backoff/SIGTERM-grace sleeps
  patched (15.4s → 2.5s); test_async_delegation.py negative-drain wait
  5s → 0.5s.
- test_telegram_init_deadline.py: loop-block margin restored to 1.0s
  with rationale comment — the watchdog-dump assertion needs the loop
  blocked well past deadline+grace under parallel load (flaked once in
  the 40-worker verification run at a 0.2s margin).

Verification: full hermetic suite via scripts/run_tests.sh —
2,438 files, 21,718 tests passed, 0 failed, 293.9s wall.
Suite totals vs original baseline: 46,820 → 19,757 test functions
(−57.8%), wall 583.5s → 293.9s (−50%), subprocess CPU 13,564s → 11,623s.
2026-07-29 13:39:40 -07:00

128 lines
5.1 KiB
Python

"""Tests for voice mode platform isolation (bug #12542).
Voice mode state stored as {chat_id: mode} without a platform namespace
caused collisions: Telegram chat '123' and Slack chat '123' shared the
same key. The fix prefixes keys with platform value: 'telegram:123' vs
'slack:123'.
"""
import json
import tempfile
from pathlib import Path
from unittest.mock import MagicMock, patch
from gateway.config import Platform
from gateway.run import GatewayRunner
class TestVoiceKeyHelper:
"""Test the _voice_key helper method."""
def test_voice_key_different_platforms_same_chat_id(self):
"""Same chat_id on different platforms yields different keys."""
runner = _make_runner()
key_telegram = runner._voice_key(Platform.TELEGRAM, "123")
key_slack = runner._voice_key(Platform.SLACK, "123")
key_discord = runner._voice_key(Platform.DISCORD, "123")
assert key_telegram != key_slack
assert key_slack != key_discord
assert key_telegram == "telegram:123"
assert key_slack == "slack:123"
assert key_discord == "discord:123"
class TestVoiceModePlatformIsolation:
"""Test that voice mode state is isolated by platform."""
def test_telegram_and_slack_voice_mode_independent(self):
"""Setting voice mode for Telegram chat '123' does not affect Slack chat '123'."""
runner = _make_runner()
# Enable voice mode for Telegram chat '123'
runner._voice_mode[runner._voice_key(Platform.TELEGRAM, "123")] = "all"
# Enable voice mode for Slack chat '123' to a different mode
runner._voice_mode[runner._voice_key(Platform.SLACK, "123")] = "voice_only"
# Verify they are independent
assert runner._voice_mode.get(runner._voice_key(Platform.TELEGRAM, "123")) == "all"
assert runner._voice_mode.get(runner._voice_key(Platform.SLACK, "123")) == "voice_only"
# Disabling Telegram should not affect Slack
runner._voice_mode[runner._voice_key(Platform.TELEGRAM, "123")] = "off"
assert runner._voice_mode.get(runner._voice_key(Platform.TELEGRAM, "123")) == "off"
assert runner._voice_mode.get(runner._voice_key(Platform.SLACK, "123")) == "voice_only"
class TestLegacyKeyMigration:
"""Test migration of legacy unprefixed keys in _load_voice_modes."""
def test_load_voice_modes_skips_legacy_keys(self):
"""_load_voice_modes skips keys without ':' prefix and logs a warning."""
runner = _make_runner()
# Simulate legacy persisted data with unprefixed keys
legacy_data = {
"123": "all",
"456": "voice_only",
# Also includes a properly prefixed key (from after the fix)
"telegram:789": "off",
}
with tempfile.TemporaryDirectory() as tmpdir:
voice_path = Path(tmpdir) / "gateway_voice_mode.json"
voice_path.write_text(json.dumps(legacy_data))
with patch.object(runner, "_VOICE_MODE_PATH", voice_path):
with patch("gateway.run.logger") as mock_logger:
result = runner._load_voice_modes()
# Legacy keys without ':' should be skipped
assert "123" not in result
assert "456" not in result
# Prefixed key should be preserved
assert result.get("telegram:789") == "off"
# Warning should be logged for each legacy key
assert mock_logger.warning.called
warning_calls = [str(call) for call in mock_logger.warning.call_args_list]
assert any("Skipping legacy unprefixed voice mode key" in str(c) for c in warning_calls)
class TestSyncVoiceModeStateToAdapter:
"""Test _sync_voice_mode_state_to_adapter filters by platform."""
def test_sync_only_includes_platform_chats(self):
"""Only chats matching the adapter's platform are synced."""
runner = _make_runner()
# Set up voice mode state with multiple platforms
runner._voice_mode = {
"telegram:123": "off", # Should sync
"telegram:456": "all", # Should NOT sync (mode is not "off")
"slack:123": "off", # Should NOT sync (different platform)
"discord:789": "off", # Should NOT sync (different platform)
}
# Create a mock Telegram adapter
mock_adapter = MagicMock()
mock_adapter.platform = Platform.TELEGRAM
mock_adapter._auto_tts_disabled_chats = set()
runner._sync_voice_mode_state_to_adapter(mock_adapter)
# Only telegram:123 should be in disabled_chats (mode="off" for telegram)
assert mock_adapter._auto_tts_disabled_chats == {"123"}
# ---------------------------------------------------------------------------
# Helper
# ---------------------------------------------------------------------------
def _make_runner() -> GatewayRunner:
"""Create a minimal GatewayRunner for testing."""
with patch("gateway.run.GatewayRunner._load_voice_modes", return_value={}):
runner = GatewayRunner.__new__(GatewayRunner)
runner._voice_mode = {}
runner.adapters = {}
return runner