2025-12-27 11:59:04 +01:00
import os
2025-12-28 19:59:31 +01:00
import sys
from pathlib import Path
2025-12-27 11:59:04 +01:00
import pytest
from unittest . mock import MagicMock , patch
2025-12-28 19:59:31 +01:00
sys . path . append ( str ( Path ( __file__ ) . parent . parent ) )
2025-12-27 11:59:04 +01:00
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 ( )
2025-12-28 19:59:31 +01:00
assert " Unknown command " in mock_send_message . call_args [ 0 ] [ 0 ]
@patch ( " telegram_bot.TelegramBot._send_photo " )
@patch ( " telegram_bot.TelegramBot._send_message " )
def test_handle_plot_command ( mock_send_message , mock_send_photo , telegram_bot ) :
telegram_bot . app_handler . _generate_weekly_plot = MagicMock ( return_value = " /path/to/plot.png " )
telegram_bot . _handle_plot_command ( )
mock_send_photo . assert_called_once_with ( " /path/to/plot.png " , " 📊 <b>Weekly Listing Patterns</b> \n \n This shows when new listings typically appear throughout the week. " )
@patch ( " telegram_bot.TelegramBot._send_message " )
def test_handle_plot_command_no_data ( mock_send_message , telegram_bot ) :
telegram_bot . app_handler . _generate_weekly_plot = MagicMock ( return_value = " " )
telegram_bot . _handle_plot_command ( )
mock_send_message . assert_called_once_with ( " 📊 Not enough data to generate plot yet. Keep monitoring! " )
@patch ( " telegram_bot.TelegramBot._send_photo " )
@patch ( " telegram_bot.TelegramBot._send_message " )
def test_handle_error_rate_command ( mock_send_message , mock_send_photo , telegram_bot ) :
telegram_bot . app_handler . _generate_error_rate_plot = MagicMock ( return_value = ( " /path/to/error_rate.png " , " Summary text " ) )
telegram_bot . _handle_error_rate_command ( )
mock_send_photo . assert_called_once_with ( " /path/to/error_rate.png " , " 📉 <b>Autopilot Success vs Failure</b> \n \n Summary text " )
@patch ( " telegram_bot.TelegramBot._send_message " )
def test_handle_error_rate_command_no_data ( mock_send_message , telegram_bot ) :
telegram_bot . app_handler . _generate_error_rate_plot = MagicMock ( return_value = ( " " , " " ) )
telegram_bot . _handle_error_rate_command ( )
mock_send_message . assert_called_once_with ( " 📉 Not enough application data to generate errorrate plot. " )