from unittest.mock import patch import pytest from aiogram.types import Chat, Message, User from bot.config import load_config from bot.main import FromLinkedChannel def _make_message(*, is_automatic_forward: bool = False) -> Message: return Message( message_id=1, date=0, chat=Chat(id=-1001234567890, type="supergroup"), from_user=User(id=1, is_bot=False, first_name="Test"), is_automatic_forward=True if is_automatic_forward else None, ) @pytest.mark.asyncio async def test_from_linked_channel_matches_automatic_forward() -> None: filt = FromLinkedChannel() assert await filt(_make_message(is_automatic_forward=True)) is True @pytest.mark.asyncio async def test_from_linked_channel_ignores_regular_message() -> None: filt = FromLinkedChannel() assert await filt(_make_message(is_automatic_forward=False)) is False def test_load_config_success(tmp_path, monkeypatch) -> None: comment = tmp_path / "comment.txt" comment.write_text("Hello world", encoding="utf-8") monkeypatch.setenv("BOT_TOKEN", "123:ABC") monkeypatch.setenv("DISCUSSION_CHAT_ID", "-100123") monkeypatch.setenv("COMMENT_FILE", str(comment)) with patch("bot.config.load_dotenv"): config = load_config() assert config.bot_token == "123:ABC" assert config.discussion_chat_id == -100123 assert config.comment_text == "Hello world" def test_load_config_missing_env(monkeypatch) -> None: monkeypatch.delenv("BOT_TOKEN", raising=False) monkeypatch.delenv("DISCUSSION_CHAT_ID", raising=False) with patch("bot.config.load_dotenv"): with pytest.raises(SystemExit, match="BOT_TOKEN"): load_config() def test_load_config_bad_chat_id(monkeypatch, tmp_path) -> None: comment = tmp_path / "comment.txt" comment.write_text("ok", encoding="utf-8") monkeypatch.setenv("BOT_TOKEN", "123:ABC") monkeypatch.setenv("DISCUSSION_CHAT_ID", "not-an-int") monkeypatch.setenv("COMMENT_FILE", str(comment)) with patch("bot.config.load_dotenv"): with pytest.raises(SystemExit, match="must be an integer"): load_config() def test_load_config_empty_comment(monkeypatch, tmp_path) -> None: comment = tmp_path / "comment.txt" comment.write_text(" \n", encoding="utf-8") monkeypatch.setenv("BOT_TOKEN", "123:ABC") monkeypatch.setenv("DISCUSSION_CHAT_ID", "-100123") monkeypatch.setenv("COMMENT_FILE", str(comment)) with patch("bot.config.load_dotenv"): with pytest.raises(SystemExit, match="empty"): load_config()