|
1 | 1 | import asyncio |
2 | 2 | import importlib |
3 | 3 | import sys |
4 | | -from unittest.mock import MagicMock, patch |
| 4 | +from unittest.mock import AsyncMock, MagicMock, patch |
5 | 5 |
|
6 | 6 | import pytest |
7 | 7 |
|
@@ -106,3 +106,37 @@ async def test_telegram_video_caption_populates_message_text_and_plain(): |
106 | 106 | isinstance(component, Comp.Plain) and component.text == "这段视频讲了什么" |
107 | 107 | for component in result.message |
108 | 108 | ) |
| 109 | + |
| 110 | + |
| 111 | +@pytest.mark.asyncio |
| 112 | +async def test_telegram_voice_message_creates_record_component(tmp_path): |
| 113 | + TelegramPlatformAdapter = _load_telegram_adapter() |
| 114 | + adapter = TelegramPlatformAdapter( |
| 115 | + make_platform_config("telegram"), |
| 116 | + {}, |
| 117 | + asyncio.Queue(), |
| 118 | + ) |
| 119 | + voice = create_mock_file("https://api.telegram.org/file/test/voice.oga") |
| 120 | + update = create_mock_update( |
| 121 | + message_text=None, |
| 122 | + voice=voice, |
| 123 | + ) |
| 124 | + wav_path = tmp_path / "voice.oga.wav" |
| 125 | + convert_message_globals = adapter.convert_message.__func__.__globals__ |
| 126 | + |
| 127 | + with patch.dict( |
| 128 | + convert_message_globals, |
| 129 | + { |
| 130 | + "get_astrbot_temp_path": MagicMock(return_value=str(tmp_path)), |
| 131 | + "download_file": AsyncMock(), |
| 132 | + "convert_audio_to_wav": AsyncMock(return_value=str(wav_path)), |
| 133 | + }, |
| 134 | + ): |
| 135 | + result = await adapter.convert_message(update, _build_context()) |
| 136 | + |
| 137 | + assert result is not None |
| 138 | + assert len(result.message) == 1 |
| 139 | + assert isinstance(result.message[0], Comp.Record) |
| 140 | + assert result.message[0].file == str(wav_path) |
| 141 | + assert result.message[0].path == str(wav_path) |
| 142 | + assert result.message[0].url == str(wav_path) |
0 commit comments