import pytest from unittest.mock import AsyncMock from app.fsm import BotState, FSM from app.handlers.greeting import handle_start from app.handlers.consent import handle_consent_request, handle_consent_given, handle_consent_refused from app.handlers.main_menu import handle_main_menu from app.settings_cache import SettingsCache @pytest.mark.asyncio async def test_handle_start_new_user(test_session, mock_max_api, bot): FSM.clear_all() user_data = {"user_id": 12345, "first_name": "Иван", "last_name": "Петров"} await SettingsCache.refresh(test_session) await handle_start(bot, user_data, test_session, mock_max_api) mock_max_api.send_message_with_keyboard.assert_called_once() call_args = mock_max_api.send_message_with_keyboard.call_args assert call_args[0][0] == 12345 assert FSM.get_state(12345) == BotState.IDLE @pytest.mark.asyncio async def test_handle_start_returning_user(test_session, mock_max_api, bot): from app.conversation import get_or_create_user, create_conversation FSM.clear_all() user_data = {"user_id": 67890, "first_name": "Пётр"} user = await get_or_create_user(test_session, 67890, "Пётр") await test_session.flush() await create_conversation(test_session, user.id) await test_session.flush() await SettingsCache.refresh(test_session) await handle_start(bot, user_data, test_session, mock_max_api) mock_max_api.send_message_with_keyboard.assert_called_once() assert FSM.get_state(67890) == BotState.IDLE @pytest.mark.asyncio async def test_handle_consent_request(test_session, mock_max_api, bot): FSM.clear_all() user_data = {"user_id": 111, "first_name": "Анна"} await SettingsCache.refresh(test_session) await handle_consent_request(bot, user_data, test_session, mock_max_api) mock_max_api.send_message_with_keyboard.assert_called_once() assert FSM.get_state(111) == BotState.CONSENT_REQUEST @pytest.mark.asyncio async def test_handle_consent_given(test_session, mock_max_api, bot): from app.conversation import get_or_create_user FSM.clear_all() user_data = {"user_id": 222} user = await get_or_create_user(test_session, 222) await test_session.flush() await handle_consent_given(bot, user_data, test_session, mock_max_api) assert user.consent_given is True mock_max_api.send_message_with_keyboard.assert_called_once() assert FSM.get_state(222) == BotState.CONTACT_REQUEST @pytest.mark.asyncio async def test_handle_consent_refused(test_session, mock_max_api, bot): from app.conversation import get_or_create_user FSM.clear_all() user_data = {"user_id": 333} user = await get_or_create_user(test_session, 333) await test_session.flush() await SettingsCache.refresh(test_session) await handle_consent_refused(bot, user_data, test_session, mock_max_api) mock_max_api.send_message_with_keyboard.assert_called_once() assert FSM.get_state(333) == BotState.IDLE @pytest.mark.asyncio async def test_handle_main_menu_new_inquiry_without_consent(test_session, mock_max_api, bot): FSM.clear_all() user_data = {"user_id": 444, "payload": "new_inquiry"} await SettingsCache.refresh(test_session) await handle_main_menu(bot, user_data, test_session, mock_max_api) # Should go to consent request mock_max_api.send_message_with_keyboard.assert_called_once() @pytest.mark.asyncio async def test_handle_main_menu_cancel(test_session, mock_max_api, bot): FSM.clear_all() user_data = {"user_id": 555, "payload": "cancel"} await SettingsCache.refresh(test_session) await handle_main_menu(bot, user_data, test_session, mock_max_api) mock_max_api.send_message_with_keyboard.assert_called_once() assert FSM.get_state(555) == BotState.IDLE @pytest.mark.asyncio async def test_handle_main_menu_unknown_payload(test_session, mock_max_api, bot): FSM.clear_all() user_data = {"user_id": 666, "payload": "unknown_command"} await SettingsCache.refresh(test_session) await handle_main_menu(bot, user_data, test_session, mock_max_api) mock_max_api.send_message_with_keyboard.assert_called_once() assert FSM.get_state(666) == BotState.IDLE @pytest.mark.asyncio async def test_handle_main_menu_handoff(test_session, mock_max_api, bot): from app.conversation import get_or_create_user from app.handlers.handoff import handle_handoff_request FSM.clear_all() user_data = {"user_id": 668, "payload": "handoff"} await get_or_create_user(test_session, 668, "Тест") await test_session.flush() await SettingsCache.refresh(test_session) # Call directly (goes to out_of_hours if after hours, or handoff template) await handle_handoff_request(bot, user_data, test_session, mock_max_api) # Either send_message (handoff) or send_message_with_keyboard (out_of_hours) assert mock_max_api.send_message.called or mock_max_api.send_message_with_keyboard.called @pytest.mark.asyncio async def test_handle_out_of_hours(test_session, mock_max_api, bot): from app.handlers.out_of_hours import handle_out_of_hours FSM.clear_all() user_data = {"user_id": 777} await SettingsCache.refresh(test_session) await handle_out_of_hours(bot, user_data, test_session, mock_max_api) mock_max_api.send_message_with_keyboard.assert_called_once() assert FSM.get_state(777) == BotState.OUT_OF_HOURS @pytest.mark.asyncio async def test_handle_out_of_hours_leave_message(test_session, mock_max_api, bot): from app.handlers.out_of_hours import handle_out_of_hours_callback FSM.clear_all() user_data = {"user_id": 888, "text": "тестовое обращение"} FSM.set_state(888, BotState.OUT_OF_HOURS) await SettingsCache.refresh(test_session) await handle_out_of_hours_callback(bot, user_data, test_session, mock_max_api, "leave_message") # handle_inquiry completes the flow → ends at CONFIRMATION assert FSM.get_state(888) == BotState.CONFIRMATION @pytest.mark.asyncio async def test_handle_out_of_hours_callback_request(test_session, mock_max_api, bot): from app.handlers.out_of_hours import handle_out_of_hours_callback FSM.clear_all() user_data = {"user_id": 999} FSM.set_state(999, BotState.OUT_OF_HOURS) await SettingsCache.refresh(test_session) await handle_out_of_hours_callback(bot, user_data, test_session, mock_max_api, "callback") mock_max_api.send_message.assert_called_once() assert FSM.get_state(999) == BotState.NAME_REQUEST