| """ |
| Bot交互功能测试 |
| 测试Bot之间的相互交互功能 |
| 注意:当前实现中Bot交互功能尚未完成,相关测试已暂时跳过 |
| """ |
| |
| import pytest |
| from unittest.mock import Mock, AsyncMock, patch |
| import time |
| |
| import sys |
| from pathlib import Path |
| sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent / "src")) |
| |
| from claude_agent.telegram.message_handler import MessageHandler |
| |
| |
| @pytest.mark.skip(reason="Bot交互功能尚未在当前实现中完成") |
| class TestBotInteraction: |
| """Bot交互功能测试""" |
| |
| @pytest.fixture |
| def mock_components(self): |
| """创建模拟组件""" |
| return { |
| 'telegram_client': Mock(), |
| 'context_manager': Mock(), |
| 'file_handler': Mock(), |
| 'claude_agent': Mock(), |
| 'stream_sender': Mock() |
| } |
| |
| @pytest.fixture |
| def bot_interaction_config(self): |
| """Bot交互配置""" |
| return { |
| 'bot_interaction': { |
| 'enabled': True, |
| 'respond_to_bot_mentions': True, |
| 'respond_to_bot_keywords': True, |
| 'bot_interaction_cooldown': 1 |
| }, |
| 'group_participation': { |
| 'bot_names': ['claude', 'Claude', 'AI', 'ai'], |
| 'random_participation_range': [5, 10] |
| } |
| } |
| |
| @pytest.fixture |
| def message_handler(self, mock_components, bot_interaction_config): |
| """创建消息处理器实例""" |
| return MessageHandler( |
| telegram_client=mock_components['telegram_client'], |
| context_manager=mock_components['context_manager'], |
| file_handler=mock_components['file_handler'], |
| claude_agent=mock_components['claude_agent'], |
| stream_sender=mock_components['stream_sender'], |
| allowed_users=[123], |
| allowed_groups=[456], |
| telegram_config=bot_interaction_config |
| ) |
| |
| def test_bot_interaction_config_loading(self, message_handler): |
| """测试Bot交互配置加载""" |
| assert message_handler.bot_interaction_enabled is True |
| assert message_handler.respond_to_bot_mentions is True |
| assert message_handler.respond_to_bot_keywords is True |
| assert message_handler.bot_interaction_cooldown == 1 |
| |
| @pytest.mark.asyncio |
| async def test_bot_authorization(self, message_handler): |
| """测试Bot用户权限验证""" |
| # 创建Bot用户的Update |
| update = Mock() |
| update.effective_user = Mock() |
| update.effective_user.is_bot = True |
| update.effective_user.id = 789 |
| update.effective_chat = Mock() |
| update.effective_chat.type = "supergroup" |
| update.effective_chat.id = 456 # 在白名单中 |
| |
| # Bot用户在白名单群组中应该被授权 |
| authorized = await message_handler._is_authorized(update) |
| assert authorized is True |
| |
| @pytest.mark.asyncio |
| async def test_bot_authorization_not_in_group(self, message_handler): |
| """测试Bot用户在非白名单群组中不被授权""" |
| update = Mock() |
| update.effective_user = Mock() |
| update.effective_user.is_bot = True |
| update.effective_user.id = 789 |
| update.effective_chat = Mock() |
| update.effective_chat.type = "supergroup" |
| update.effective_chat.id = 999 # 不在白名单中 |
| |
| # Bot用户在非白名单群组中不应该被授权 |
| authorized = await message_handler._is_authorized(update) |
| assert authorized is False |
| |
| @pytest.mark.asyncio |
| async def test_bot_mention_detection(self, message_handler): |
| """测试Bot@提及检测""" |
| update = Mock() |
| update.effective_user = Mock() |
| update.effective_user.is_bot = True |
| update.effective_user.id = 789 |
| update.get_bot = Mock(return_value=Mock(username="testbot")) |
| |
| chat_id = "456" |
| text = "Hey @testbot, how are you?" |
| |
| should_respond = await message_handler._should_respond_to_bot(update, text, chat_id) |
| assert should_respond is True |
| |
| @pytest.mark.asyncio |
| async def test_bot_keyword_detection(self, message_handler): |
| """测试Bot关键字检测""" |
| update = Mock() |
| update.effective_user = Mock() |
| update.effective_user.is_bot = True |
| update.effective_user.id = 789 |
| update.get_bot = Mock(return_value=Mock(username="testbot")) |
| |
| chat_id = "456" |
| text = "Hey Claude, what do you think?" |
| |
| should_respond = await message_handler._should_respond_to_bot(update, text, chat_id) |
| assert should_respond is True |
| |
| @pytest.mark.asyncio |
| async def test_bot_interaction_cooldown(self, message_handler): |
| """测试Bot交互冷却时间""" |
| update = Mock() |
| update.effective_user = Mock() |
| update.effective_user.is_bot = True |
| update.effective_user.id = 789 |
| update.get_bot = Mock(return_value=Mock(username="testbot")) |
| |
| chat_id = "456" |
| text = "Hey @testbot, hello!" |
| |
| # 第一次应该响应 |
| should_respond1 = await message_handler._should_respond_to_bot(update, text, chat_id) |
| assert should_respond1 is True |
| |
| # 立即第二次应该被冷却阻止 |
| should_respond2 = await message_handler._should_respond_to_bot(update, text, chat_id) |
| assert should_respond2 is False |
| |
| # 等待冷却时间后应该可以响应 |
| message_handler._bot_interaction_last_reply[chat_id] = time.time() - 2 # 模拟2秒前 |
| should_respond3 = await message_handler._should_respond_to_bot(update, text, chat_id) |
| assert should_respond3 is True |
| |
| @pytest.mark.asyncio |
| async def test_bot_interaction_disabled(self, mock_components): |
| """测试Bot交互功能禁用""" |
| disabled_config = { |
| 'bot_interaction': { |
| 'enabled': False, |
| 'respond_to_bot_mentions': True, |
| 'respond_to_bot_keywords': True |
| }, |
| 'group_participation': { |
| 'bot_names': ['claude'], |
| 'random_participation_range': [5, 10] |
| } |
| } |
| |
| handler = MessageHandler( |
| telegram_client=mock_components['telegram_client'], |
| context_manager=mock_components['context_manager'], |
| file_handler=mock_components['file_handler'], |
| claude_agent=mock_components['claude_agent'], |
| stream_sender=mock_components['stream_sender'], |
| allowed_users=[123], |
| allowed_groups=[456], |
| telegram_config=disabled_config |
| ) |
| |
| # Bot交互功能应该被禁用 |
| assert handler.bot_interaction_enabled is False |
| |
| # Bot用户应该不被授权 |
| update = Mock() |
| update.effective_user = Mock() |
| update.effective_user.is_bot = True |
| update.effective_chat = Mock() |
| update.effective_chat.type = "supergroup" |
| update.effective_chat.id = 456 |
| |
| authorized = await handler._is_authorized(update) |
| assert authorized is False |
| |
| def test_build_bot_user_info(self, message_handler): |
| """测试构建Bot用户信息""" |
| bot_user = Mock() |
| bot_user.id = 789 |
| bot_user.username = "testbot" |
| bot_user.first_name = "Test" |
| bot_user.last_name = "Bot" |
| bot_user.language_code = "en" |
| |
| chat = Mock() |
| chat.id = 456 |
| chat.type = "supergroup" |
| chat.title = "Test Group" |
| |
| user_info = message_handler._build_bot_user_info(bot_user, chat) |
| |
| assert user_info['user_id'] == 789 |
| assert user_info['username'] == "testbot" |
| assert user_info['first_name'] == "Test" |
| assert user_info['last_name'] == "Bot" |
| assert user_info['language_code'] == "en" |
| assert user_info['chat_id'] == 456 |
| assert user_info['chat_type'] == "supergroup" |
| assert user_info['chat_title'] == "Test Group" |
| assert user_info['is_bot_user'] is True |