import os import pytest from unittest.mock import MagicMock, patch from telegram_bot import TelegramBot from dotenv import load_dotenv # Load environment variables from .env file load_dotenv() # Explicitly pass token and chat ID to ensure they are set @pytest.fixture(autouse=True) def mock_env_vars(): os.environ["TELEGRAM_BOT_TOKEN"] = "test_token" os.environ["TELEGRAM_CHAT_ID"] = "test_chat_id" @pytest.fixture def mock_monitor(): monitor = MagicMock() monitor.load_state.return_value = {"autopilot": True} monitor.load_applications.return_value = { "app1": {"company": "CompanyA"}, "app2": {"company": "CompanyB"}, "app3": {"company": "CompanyA"}, } return monitor @pytest.fixture def telegram_bot(mock_monitor): return TelegramBot(mock_monitor, bot_token="test_token", chat_id="test_chat_id") @patch("telegram_bot.requests.post") def test_send_message(mock_post, telegram_bot): mock_post.return_value.ok = True telegram_bot._send_message("Test message") mock_post.assert_called_once() assert mock_post.call_args[1]["json"]["text"] == "Test message" @patch("telegram_bot.requests.post") def test_send_photo(mock_post, telegram_bot): mock_post.return_value.ok = True with patch("builtins.open", create=True): telegram_bot._send_photo("/path/to/photo.jpg", "Test caption") mock_post.assert_called_once() assert mock_post.call_args[1]["data"]["caption"] == "Test caption" @patch("telegram_bot.TelegramBot._send_message") def test_handle_status_command(mock_send_message, telegram_bot): telegram_bot._handle_status_command() mock_send_message.assert_called_once() assert "Autopilot" in mock_send_message.call_args[0][0] @patch("telegram_bot.TelegramBot._send_message") def test_handle_help_command(mock_send_message, telegram_bot): telegram_bot._handle_help_command() mock_send_message.assert_called_once() assert "InBerlin Monitor Commands" in mock_send_message.call_args[0][0] @patch("telegram_bot.TelegramBot._send_message") def test_handle_unknown_command(mock_send_message, telegram_bot): telegram_bot._handle_unknown_command("/unknown") mock_send_message.assert_called_once() assert "Unknown command" in mock_send_message.call_args[0][0]