fix(gateway): preserve legacy /voice off suppression across #12542 migration

Legacy unprefixed voice-mode rows were dropped during the #12542
platform-namespacing migration, causing one unwanted auto-TTS reply on
Telegram chats that had /voice off before the upgrade. Preserve legacy
"off" rows on load and apply them as a cross-platform fallback; explicit
platform-prefixed rows take precedence so a later /voice on/tts cannot
be silently re-suppressed after a restart.

Fixes #14025
This commit is contained in:
Season 2026-04-22 22:56:40 +08:00
parent 432772dbdf
commit 807fd2a69f
3 changed files with 166 additions and 20 deletions

View file

@ -193,6 +193,35 @@ class TestHandleVoiceCommand:
assert restored_runner._voice_mode["telegram:123"] == "off"
assert adapter._auto_tts_disabled_chats == {"123"}
@pytest.mark.asyncio
async def test_voice_on_after_legacy_off_overrides_fallback_after_restart(self, tmp_path):
"""Full lifecycle: a pre-#12542 legacy {chat_id: 'off'} row is
preserved across save/reload, but an explicit /voice on writes a
prefixed entry that overrides the legacy fallback on subsequent
restarts no split-brain where /voice status reports 'on' while
auto-TTS stays suppressed (#14025)."""
from gateway.config import Platform
runner = _make_runner(tmp_path)
runner._VOICE_MODE_PATH.write_text(json.dumps({"123": "off"}))
runner._voice_mode = runner._load_voice_modes()
assert runner._voice_mode == {"123": "off"}
event = _make_event("/voice on", chat_id="123")
await runner._handle_voice_command(event)
restored_runner = _make_runner(tmp_path)
restored_runner._voice_mode = restored_runner._load_voice_modes()
adapter = SimpleNamespace(
_auto_tts_disabled_chats=set(),
platform=Platform.TELEGRAM,
)
restored_runner._sync_voice_mode_state_to_adapter(adapter)
assert restored_runner._voice_mode["123"] == "off"
assert restored_runner._voice_mode["telegram:123"] == "voice_only"
assert "123" not in adapter._auto_tts_disabled_chats
@pytest.mark.asyncio
async def test_per_chat_isolation(self, runner):
e1 = _make_event("/voice on", chat_id="aaa")