""" API endpoint tests using FastAPI TestClient. """ import pytest from unittest.mock import patch, MagicMock from fastapi.testclient import TestClient from app.main import app client = TestClient(app) def test_root(): response = client.get("/") assert response.status_code == 200 data = response.json() assert data["status"] == "ok" assert "timestamp" in data def test_health(): response = client.get("/health") assert response.status_code == 200 data = response.json() assert data["status"] == "ok" @patch("app.api.routes.run_full_crew", return_value="Mock itinerary result") @patch("app.api.routes.load_and_preprocess", side_effect=FileNotFoundError) def test_predict_no_data_file(mock_load, mock_crew): payload = { "from_city": "Cairo", "destination_city": "Luxor", "date_from": "2026/6/19", "date_to": "2026/6/20", "tags": ["historical_site", "restaurant"], "interests": "sightseeing and food", } response = client.post("/predict", json=payload) assert response.status_code == 200 data = response.json() assert data["status"] == "success" assert "itinerary" in data mock_crew.assert_called_once() @patch("app.api.routes.run_full_crew", side_effect=RuntimeError("LLM timeout")) @patch("app.api.routes.load_and_preprocess", side_effect=FileNotFoundError) def test_predict_error_handling(mock_load, mock_crew): payload = { "from_city": "Cairo", "destination_city": "Luxor", "date_from": "2026/6/19", "date_to": "2026/6/20", "tags": [], "interests": "beaches", } response = client.post("/predict", json=payload) assert response.status_code == 500 @patch("app.api.routes.run_full_crew", return_value="Batch result") @patch("app.api.routes.load_and_preprocess", side_effect=FileNotFoundError) def test_batch_predict(mock_load, mock_crew): payload = [ { "from_city": "Cairo", "destination_city": "Luxor", "date_from": "2026/6/19", "date_to": "2026/6/20", "tags": [], "interests": "history", }, { "from_city": "Cairo", "destination_city": "Hurghada", "date_from": "2026/7/1", "date_to": "2026/7/3", "tags": ["beach"], "interests": "beach and relaxation", }, ] response = client.post("/batch_predict", json=payload) assert response.status_code == 200 results = response.json() assert len(results) == 2 assert all(r["status"] == "success" for r in results) @patch("app.api.routes.chat_with_agent") def test_chat(mock_chat): mock_chat.return_value = { "reply": "Sure! Which city in Egypt would you like to visit?", "thread_id": "test_thread", "message_count": 2, } payload = {"message": "I want to plan a trip", "thread_id": "test_thread"} response = client.post("/chat", json=payload) assert response.status_code == 200 data = response.json() assert data["thread_id"] == "test_thread" assert "reply" in data