from __future__ import annotations from unittest.mock import AsyncMock, MagicMock import pytest from yuxi.channel.channels.dingtalk.adapter import DingTalkAdapter from yuxi.channel.domain.model.message.unified_message import UnifiedMessage from yuxi.channel.domain.model.message.peer import Peer from yuxi.channel.domain.model.message.dispatch_result import SendResult class TestDingTalkAdapter: @pytest.fixture def adapter(self): return DingTalkAdapter(config={"client_id": "test", "client_secret": "test"}) @pytest.mark.asyncio async def test_open(self, adapter): await adapter.open() assert adapter.is_open is True @pytest.mark.asyncio async def test_close(self, adapter): await adapter.open() await adapter.close() assert adapter.is_open is False @pytest.mark.asyncio async def test_send_message(self, adapter): await adapter.open() msg = UnifiedMessage( message_id="msg123", channel_type="dingtalk", sender=Peer(id="bot", name="Bot"), content="Hello", ) result = await adapter.send_message("thread123", msg) assert isinstance(result, SendResult) @pytest.mark.asyncio async def test_send_media(self, adapter): await adapter.open() result = await adapter.send_media("thread123", "http://example.com/image.png", "image") assert isinstance(result, SendResult) @pytest.mark.asyncio async def test_send_typing(self, adapter): await adapter.open() result = await adapter.send_typing("thread123") assert isinstance(result, SendResult) def test_capabilities(self, adapter): assert adapter.capabilities.text is True assert adapter.capabilities.media is True assert adapter.capabilities.typing is True assert adapter.capabilities.group is True def test_bot_id(self, adapter): assert adapter.bot_id is None def test_ws_connection(self, adapter): assert adapter.ws_connection is None @pytest.mark.asyncio async def test_send_message_not_open(self, adapter): msg = UnifiedMessage( message_id="msg123", channel_type="dingtalk", sender=Peer(id="bot", name="Bot"), content="Hello", ) result = await adapter.send_message("thread123", msg) assert result.success is False