from fastapi.testclient import TestClient # Mock settings BEFORE importing the app from unittest.mock import patch with patch("app.core.config.settings.API_KEY", "test-api-key"): from main import app import pytest client = TestClient(app) def test_chat_response(): """Test basic chatbot response and identity.""" response = client.post( "/api/ai/chat", json={"message": "مرحباً، من أنت؟"}, headers={"X-API-Key": "test-api-key"} ) assert response.status_code == 200 data = response.json() assert "عون" in data["response"] # Should identify as Aoun assistant assert "history" in data assert len(data["history"]) >= 2 def test_chat_with_history(): """Test chatbot response with history context.""" history = [ {"role": "user", "content": "اسمي محمد"}, {"role": "model", "content": "أهلاً بك يا محمد في منصة عون، كيف يمكنني مساعدتك؟"} ] response = client.post( "/api/ai/chat", json={ "message": "ما هو اسمي؟", "history": history }, headers={"X-API-Key": "test-api-key"} ) assert response.status_code == 200 data = response.json() assert "محمد" in data["response"] assert len(data["history"]) == 4