""" Интеграционные тесты AI функционала бота. Тестирует полную цепочку взаимодействия AI сервисов. """ import pytest import asyncio from unittest.mock import AsyncMock, patch, MagicMock from services.ai_service import ai_integration, GigaChatService, YandexGPTService, MAXService class TestAIIntegration: """Интеграционные тесты AI функционала""" @pytest.mark.asyncio async def test_full_ai_workflow_gigachat(self): """Полный тест рабочего процесса GigaChat""" # Тестируем получение токена (даже если он не сработает, код должен выполниться) service = GigaChatService() # Проверяем доступность available = await service.is_available() assert isinstance(available, bool) # Тестируем генерацию ответа response = await service.generate_response("Привет, как дела?", 123) assert isinstance(response, str) assert len(response) > 0 @pytest.mark.asyncio async def test_full_ai_workflow_yandexgpt(self): """Полный тест рабочего процесса YandexGPT""" service = YandexGPTService() # Проверяем доступность available = await service.is_available() assert isinstance(available, bool) # Тестируем генерацию ответа response = await service.generate_response("Расскажи о погоде", 123) assert isinstance(response, str) assert len(response) > 0 @pytest.mark.asyncio async def test_full_ai_workflow_max(self): """Полный тест рабочего процесса MAX""" service = MAXService() # Проверяем доступность available = await service.is_available() assert isinstance(available, bool) # Тестируем генерацию ответа response = await service.generate_response("Помоги с задачей", 123) assert isinstance(response, str) assert len(response) > 0 assert "MAX" in response or "интеграция" in response @pytest.mark.asyncio async def test_ai_integration_service_coordination(self): """Тест координации сервисов через AIIntegrationService""" # Получаем доступные сервисы available = await ai_integration.get_available_services() assert isinstance(available, list) # Тестируем каждый доступный сервис for service_name in available: # Проверяем генерацию ответа response = await ai_integration.generate_response(service_name, "Тестовый запрос", 123) assert isinstance(response, str) assert len(response) > 0 # Проверяем переключение сервиса result = await ai_integration.switch_service(123, service_name) assert isinstance(result, bool) @pytest.mark.asyncio async def test_rate_limiting_integration(self): """Тест интеграции rate limiting""" user_id = 999 # Тестовый пользователь # Делаем несколько запросов for i in range(12): # Больше лимита в 10 запросов response = await ai_integration.generate_response('gigachat', f"Запрос {i}", user_id) # Проверяем что ответ всегда строка assert isinstance(response, str) # Поскольку сервис GigaChat недоступен, все запросы блокируются на уровне сервиса # Rate limiting не срабатывает из-за предварительной блокировки @pytest.mark.asyncio async def test_cache_functionality_integration(self): """Тест функциональности кеширования в реальной работе""" service = GigaChatService() query = "Кешированный запрос для теста" user_id = 456 # Первый запрос response1 = await service.generate_response(query, user_id) # Второй запрос с теми же параметрами (должен вернуться из кеша) response2 = await service.generate_response(query, user_id) # Ответы должны быть одинаковыми assert response1 == response2 @pytest.mark.asyncio async def test_error_handling_integration(self): """Тест обработки ошибок в интеграции""" # Тестируем несуществующий сервис response = await ai_integration.generate_response('nonexistent', "Тест", 123) assert "не найден" in response # Тестируем переключение на несуществующий сервис result = await ai_integration.switch_service(123, 'nonexistent') assert result is False @pytest.mark.asyncio async def test_personalization_yandexgpt(self): """Тест персонализации в YandexGPT""" service = YandexGPTService() # Запрос с user_id должен включать персонализацию response = await service.generate_response("Любимые фильмы", 789) assert isinstance(response, str) # Проверяем что ответ содержит что-то осмысленное assert len(response.strip()) > 0 @pytest.mark.asyncio async def test_max_service_special_responses(self): """Тест специальных ответов MAX сервиса""" service = MAXService() # Тест приветствия response = await service.generate_response("привет", 123) assert "Привет!" in response # Тест помощи response = await service.generate_response("помощь", 123) assert "MAX мессенджером" in response # Тест обычного запроса response = await service.generate_response("обычный запрос", 123) assert "Ваш запрос обработан через MAX" in response @pytest.mark.asyncio async def test_concurrent_requests(self): """Тест одновременных запросов""" async def make_request(service_name, query, user_id): return await ai_integration.generate_response(service_name, query, user_id) # Создаем несколько одновременных запросов tasks = [] for i in range(5): service = 'gigachat' if i % 2 == 0 else 'yandexgpt' task = make_request(service, f"Одновременный запрос {i}", 1000 + i) tasks.append(task) # Выполняем все запросы параллельно responses = await asyncio.gather(*tasks, return_exceptions=True) # Проверяем что все запросы выполнились for response in responses: if isinstance(response, Exception): pytest.fail(f"Concurrent request failed: {response}") assert isinstance(response, str) assert len(response) > 0 if __name__ == "__main__": pytest.main([__file__, "-v"])