from __future__ import annotations import pytest 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.attachment import Attachment class TestUnifiedMessage: def test_basic_creation(self): msg = UnifiedMessage( message_id="msg123", channel_type="web", sender=Peer(id="user1", name="User"), content="Hello", ) assert msg.message_id == "msg123" assert msg.channel_type == "web" assert msg.content == "Hello" def test_to_dict(self): msg = UnifiedMessage( message_id="msg123", channel_type="web", sender=Peer(id="user1", name="User"), content="Hello", metadata={"key": "value"}, ) d = msg.to_dict() assert d["message_id"] == "msg123" assert d["content"] == "Hello" assert d["metadata"] == {"key": "value"} def test_from_dict(self): d = { "message_id": "msg123", "channel_type": "web", "sender": {"id": "user1", "name": "User"}, "content": "Hello", "metadata": {"key": "value"}, } msg = UnifiedMessage.from_dict(d) assert msg.message_id == "msg123" assert msg.sender.id == "user1" assert msg.metadata == {"key": "value"} def test_with_attachments(self): msg = UnifiedMessage( message_id="msg123", channel_type="web", sender=Peer(id="user1", name="User"), content="Hello", attachments=[ Attachment(url="http://example.com/image.png", media_type="image", filename="image.png"), ], ) assert len(msg.attachments) == 1 assert msg.attachments[0].media_type == "image" def test_empty_message(self): msg = UnifiedMessage( message_id="", channel_type="", sender=Peer(id="", name=""), content="", ) assert msg.message_id == "" assert msg.content == "" class TestPeer: def test_creation(self): peer = Peer(id="user1", name="User") assert peer.id == "user1" assert peer.name == "User" def test_to_dict(self): peer = Peer(id="user1", name="User") d = peer.to_dict() assert d == {"id": "user1", "name": "User"} def test_from_dict(self): d = {"id": "user1", "name": "User"} peer = Peer.from_dict(d) assert peer.id == "user1" assert peer.name == "User" class TestAttachment: def test_creation(self): att = Attachment(url="http://example.com/file.pdf", media_type="document", filename="file.pdf") assert att.url == "http://example.com/file.pdf" assert att.media_type == "document" assert att.filename == "file.pdf" def test_to_dict(self): att = Attachment(url="http://example.com/file.pdf", media_type="document", filename="file.pdf") d = att.to_dict() assert d["url"] == "http://example.com/file.pdf" assert d["media_type"] == "document" def test_from_dict(self): d = {"url": "http://example.com/file.pdf", "media_type": "document", "filename": "file.pdf"} att = Attachment.from_dict(d) assert att.url == "http://example.com/file.pdf" assert att.filename == "file.pdf"