import pytest from unittest.mock import AsyncMock, MagicMock from services.collector import ScrapeResult from services.admin_bot import AdminBotService from db.models import Post, SourceChannel, SourceWebsite def test_collector_summary_fa_with_title(): res = ScrapeResult(scanned=10, collected=5, already_stored=3, duplicates=2) summary = res.summary_fa(channel_id=-100123456, channel_title="کانال خبری فناوری") assert "کانال خبری فناوری" in summary assert "-100123456" not in summary def test_collector_summary_fa_fallback_to_id(): res = ScrapeResult(scanned=10, collected=0, already_stored=10, duplicates=0) summary = res.summary_fa(channel_id=-100123456, channel_title=None) assert "-100123456" in summary @pytest.mark.asyncio async def test_admin_bot_format_raw_post_caption_telegram_channel(): repo = MagicMock() repo.get_source_by_channel_id = AsyncMock(return_value=SourceChannel( id=1, channel_id=-100123456, username="tech_news", title="اخبار روز تکنولوژی" )) bot = AdminBotService(repo=repo, ai_processor=MagicMock(), queue=MagicMock()) post = Post(id=1, source_channel_id=-100123456, source_message_id=10, raw_text="متن تست پست") caption = await bot._format_raw_post_caption(post) assert "اخبار روز تکنولوژی" in caption assert "@tech_news" in caption @pytest.mark.asyncio async def test_admin_bot_format_raw_post_caption_website(): repo = MagicMock() pseudo_id = -900000005 repo.get_source_website_by_id = AsyncMock(return_value=SourceWebsite( id=5, name="دیجیاتو", url="https://digiato.com" )) bot = AdminBotService(repo=repo, ai_processor=MagicMock(), queue=MagicMock()) post = Post(id=2, source_channel_id=pseudo_id, source_message_id=1, raw_text="خبر جدید وبسایت") caption = await bot._format_raw_post_caption(post) assert "دیجیاتو" in caption assert "وبسایت" in caption