| """ |
| 测试Telegram客户端适配器 |
| """ |
| |
| import pytest |
| from unittest.mock import Mock, AsyncMock |
| from telegram import Bot, Message |
| |
| import sys |
| from pathlib import Path |
| sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent / "src")) |
| |
| from claude_agent.telegram.client_adapter import TelegramClientAdapter |
| |
| |
| class TestTelegramClientAdapter: |
| """测试Telegram客户端适配器""" |
| |
| @pytest.fixture |
| def mock_bot(self): |
| """创建模拟Bot实例""" |
| return Mock(spec=Bot) |
| |
| @pytest.fixture |
| def client_adapter(self, mock_bot): |
| """创建客户端适配器实例""" |
| return TelegramClientAdapter(mock_bot) |
| |
| def test_client_adapter_initialization(self, client_adapter, mock_bot): |
| """测试客户端适配器初始化""" |
| assert client_adapter.bot == mock_bot |
| |
| @pytest.mark.asyncio |
| async def test_send_message(self, client_adapter, mock_bot): |
| """测试发送消息""" |
| mock_message = Mock(spec=Message) |
| mock_bot.send_message = AsyncMock(return_value=mock_message) |
| |
| result = await client_adapter.send_message( |
| chat_id=123456, |
| text="Test message" |
| ) |
| |
| assert result == mock_message |
| mock_bot.send_message.assert_called_once_with( |
| chat_id=123456, |
| text="Test message", |
| parse_mode=None, |
| reply_to_message_id=None |
| ) |
| |
| @pytest.mark.asyncio |
| async def test_send_message_with_options(self, client_adapter, mock_bot): |
| """测试带选项的发送消息""" |
| mock_message = Mock(spec=Message) |
| mock_bot.send_message = AsyncMock(return_value=mock_message) |
| |
| result = await client_adapter.send_message( |
| chat_id=123456, |
| text="Test message", |
| parse_mode="HTML", |
| reply_to_message_id=789 |
| ) |
| |
| assert result == mock_message |
| mock_bot.send_message.assert_called_once_with( |
| chat_id=123456, |
| text="Test message", |
| parse_mode="HTML", |
| reply_to_message_id=789 |
| ) |
| |
| @pytest.mark.asyncio |
| async def test_edit_message_text(self, client_adapter, mock_bot): |
| """测试编辑消息""" |
| mock_message = Mock(spec=Message) |
| mock_bot.edit_message_text = AsyncMock(return_value=mock_message) |
| |
| result = await client_adapter.edit_message_text( |
| text="Edited message", |
| chat_id=123456, |
| message_id=789 |
| ) |
| |
| assert result == mock_message |
| mock_bot.edit_message_text.assert_called_once_with( |
| text="Edited message", |
| chat_id=123456, |
| message_id=789, |
| parse_mode=None |
| ) |
| |
| @pytest.mark.asyncio |
| async def test_get_file(self, client_adapter, mock_bot): |
| """测试获取文件""" |
| mock_file = Mock() |
| mock_bot.get_file = AsyncMock(return_value=mock_file) |
| |
| result = await client_adapter.get_file("file_id_123") |
| |
| assert result == mock_file |
| mock_bot.get_file.assert_called_once_with("file_id_123") |
| |
| @pytest.mark.asyncio |
| async def test_send_photo(self, client_adapter, mock_bot): |
| """测试发送图片""" |
| mock_message = Mock(spec=Message) |
| mock_bot.send_photo = AsyncMock(return_value=mock_message) |
| |
| result = await client_adapter.send_photo( |
| chat_id=123456, |
| photo="photo_data", |
| caption="Test photo" |
| ) |
| |
| assert result == mock_message |
| mock_bot.send_photo.assert_called_once_with( |
| chat_id=123456, |
| photo="photo_data", |
| caption="Test photo" |
| ) |
| |
| @pytest.mark.asyncio |
| async def test_send_document(self, client_adapter, mock_bot): |
| """测试发送文档""" |
| mock_message = Mock(spec=Message) |
| mock_bot.send_document = AsyncMock(return_value=mock_message) |
| |
| result = await client_adapter.send_document( |
| chat_id=123456, |
| document="document_data", |
| caption="Test document" |
| ) |
| |
| assert result == mock_message |
| mock_bot.send_document.assert_called_once_with( |
| chat_id=123456, |
| document="document_data", |
| caption="Test document" |
| ) |
| |
| @pytest.mark.asyncio |
| async def test_error_handling(self, client_adapter, mock_bot): |
| """测试错误处理""" |
| mock_bot.send_message = AsyncMock(side_effect=Exception("API Error")) |
| |
| with pytest.raises(Exception): |
| await client_adapter.send_message( |
| chat_id=123456, |
| text="Test message" |
| ) |