""" Unit & Integration tests for FastAPI server endpoints """ import os import sys import pytest if hasattr(sys.stdout, 'reconfigure'): try: sys.stdout.reconfigure(encoding='utf-8') except Exception: pass sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) from fastapi.testclient import TestClient from api.server import app client = TestClient(app) def test_health_endpoint(): res = client.get("/api/v1/health") assert res.status_code == 200 data = res.json() assert data["status"] == "healthy" assert "loaded_checkpoints" in data print(" [✓] Health test passed") def test_models_list(): res = client.get("/api/v1/models") assert res.status_code == 200 data = res.json() assert data["total_models"] >= 4 print(" [✓] Models list test passed") def test_detect_language(): res_ar = client.post("/api/v1/detect-language", json={"text": "هذا مقال باللغة العربية الفصحى للتجربة."}) assert res_ar.status_code == 200 assert res_ar.json()["detected_language"] == "ar" res_en = client.post("/api/v1/detect-language", json={"text": "This is a clean English test paragraph for language detection."}) assert res_en.status_code == 200 assert res_en.json()["detected_language"] == "en" print(" [✓] Language detection test passed") def test_tokenize_endpoint(): res = client.post("/api/v1/tokenize", json={ "text": "الذكاءُ الاصطناعيُّ يُحدِث ثورةً. هل توافق؟", "lang": "ar" }) assert res.status_code == 200 data = res.json() assert data["sentence_count"] == 2 assert len(data["tokens"]) > 0 print(" [✓] Tokenize test passed") def test_summarize_extractive_arabic(): res = client.post("/api/v1/summarize", json={ "text": "شهدت الاسواق العالمية تراجعا حادا في اسعار النفط الخام بعد قرار منظمة اوبك زيادة الانتاج. ادى ذلك الى انخفاض سعر برميل النفط الى ما دون الثمانين دولارا للمرة الاولى منذ عام كامل.", "mode": "extractive", "method": "textrank", "sentences": 1 }) assert res.status_code == 200 data = res.json() assert len(data["summary"]) > 0 assert data["detected_language"] == "ar" print(" [✓] Extractive Arabic summarization test passed") def test_summarize_abstractive_arabic(): res = client.post("/api/v1/summarize", json={ "text": "اعلنت وكالة ناسا الفضائية الامريكية عن خطط طموحة للعودة الى سطح القمر في اطار برنامج ارتيميس حيث ستشمل البعثة اول امراة واول شخص من ذوي البشرة الداكنة.", "mode": "abstractive", "method": "seq2seq", "lang": "ar", "beam_width": 3 }) assert res.status_code == 200 data = res.json() assert len(data["summary"]) > 0 assert data["mode"] == "abstractive" print(" [✓] Abstractive Arabic summarization test passed:", data["summary"]) def test_evaluate_endpoint(): res = client.post("/api/v1/evaluate", json={ "original_text": "Global semiconductor shortages are easing according to the latest industry report with chip inventories returning to normal.", "generated_summary": "Semiconductor shortage eases as chip inventories return to normal levels.", "reference_summary": "Global chip shortage easing with inventories normalizing across industry.", "lang": "en" }) assert res.status_code == 200 data = res.json() assert "rouge_1" in data assert "bleu_cumulative" in data print(" [✓] Evaluate test passed") if __name__ == "__main__": test_health_endpoint() test_models_list() test_detect_language() test_tokenize_endpoint() test_summarize_extractive_arabic() test_summarize_abstractive_arabic() test_evaluate_endpoint() print("\n[🎉] ALL API INTEGRATION TESTS PASSED SUCCESSFULLY!")