"""
Test per il bot Telegram IRIS
"""
import pytest
from unittest.mock import Mock, AsyncMock, patch
from telegram import Update, User, Message, Chat
from telegram.ext import ContextTypes
from src.telegram_bot.bot import IRISBot, create_bot_application
from src.llm_core.prompts.types import PromptType
class TestIRISBot:
"""Test per la classe IRISBot"""
@pytest.fixture
def iris_bot(self):
"""Fixture per creare un'istanza del bot"""
return IRISBot()
@pytest.fixture
def mock_update(self):
"""Fixture per creare un mock Update"""
user = User(id=123, first_name="Test", is_bot=False)
chat = Chat(id=123, type="private")
message = Message(
message_id=1,
date=None,
chat=chat,
from_user=user,
text="/start"
)
update = Update(update_id=1, message=message)
return update
@pytest.fixture
def mock_context(self):
"""Fixture per creare un mock Context"""
return Mock(spec=ContextTypes.DEFAULT_TYPE)
def test_bot_initialization(self, iris_bot):
"""Test inizializzazione bot"""
assert iris_bot.iris_client is not None
assert isinstance(iris_bot.user_sessions, dict)
assert isinstance(iris_bot.user_providers, dict)
assert isinstance(iris_bot.user_prompts, dict)
@pytest.mark.asyncio
async def test_start_command(self, iris_bot, mock_update, mock_context):
"""Test comando /start"""
# Mock reply_text usando patch
with patch.object(mock_update.message, 'reply_text', new_callable=AsyncMock) as mock_reply:
# Esegui comando
await iris_bot.start_command(mock_update, mock_context)
# Verifica che l'utente sia stato inizializzato
user_id = mock_update.effective_user.id
assert user_id in iris_bot.user_providers
assert user_id in iris_bot.user_prompts
assert iris_bot.user_providers[user_id] == "openai"
assert iris_bot.user_prompts[user_id] == PromptType.BUSINESS_ASSISTANT
# Verifica che sia stata inviata una risposta
mock_reply.assert_called_once()
call_args = mock_reply.call_args
assert "Benvenuto in IRIS" in call_args[0][0]
@pytest.mark.asyncio
async def test_help_command(self, iris_bot, mock_update, mock_context):
"""Test comando /help"""
with patch.object(mock_update.message, 'reply_text', new_callable=AsyncMock) as mock_reply:
await iris_bot.help_command(mock_update, mock_context)
mock_reply.assert_called_once()
call_args = mock_reply.call_args
assert "COMANDI IRIS" in call_args[0][0]
assert "/start" in call_args[0][0]
assert "/chat" in call_args[0][0]
@pytest.mark.asyncio
async def test_status_command(self, iris_bot, mock_update, mock_context):
"""Test comando /status"""
with patch.object(mock_update.message, 'reply_text', new_callable=AsyncMock) as mock_reply:
# Mock Redis connection check
with patch.object(iris_bot.iris_client, '_ensure_redis_connected', new_callable=AsyncMock):
await iris_bot.status_command(mock_update, mock_context)
mock_reply.assert_called_once()
call_args = mock_reply.call_args
assert "STATO SISTEMA IRIS" in call_args[0][0]
@pytest.mark.asyncio
async def test_settings_command(self, iris_bot, mock_update, mock_context):
"""Test comando /settings"""
with patch.object(mock_update.message, 'reply_text', new_callable=AsyncMock) as mock_reply:
# Inizializza utente
user_id = mock_update.effective_user.id
iris_bot.user_providers[user_id] = "openai"
iris_bot.user_prompts[user_id] = PromptType.BUSINESS_ASSISTANT
await iris_bot.settings_command(mock_update, mock_context)
mock_reply.assert_called_once()
call_args = mock_reply.call_args
assert "LE TUE IMPOSTAZIONI" in call_args[0][0]
assert "OPENAI" in call_args[0][0]
@pytest.mark.asyncio
async def test_error_handler(self, iris_bot, mock_update, mock_context):
"""Test gestione errori"""
with patch.object(mock_update.effective_message, 'reply_text', new_callable=AsyncMock) as mock_reply:
mock_context.error = Exception("Test error")
await iris_bot.error_handler(mock_update, mock_context)
mock_reply.assert_called_once()
call_args = mock_reply.call_args
assert "Si è verificato un errore" in call_args[0][0]
class TestBotCreation:
"""Test per la creazione del bot"""
@patch('src.telegram_bot.bot.settings')
def test_create_bot_application_success(self, mock_settings):
"""Test creazione bot con successo"""
mock_settings.TELEGRAM_BOT_TOKEN = "test_token"
application, iris_bot = create_bot_application()
assert application is not None
assert isinstance(iris_bot, IRISBot)
@patch('src.telegram_bot.bot.settings')
def test_create_bot_application_no_token(self, mock_settings):
"""Test creazione bot senza token"""
mock_settings.TELEGRAM_BOT_TOKEN = None
with pytest.raises(ValueError, match="TELEGRAM_BOT_TOKEN not configured"):
create_bot_application()
if __name__ == "__main__":
pytest.main([__file__])