Spaces:
Paused
Paused
import os | |
import requests | |
from log import log | |
BASE_URL = os.environ.get("BASE_URL", "http://localhost:7860") | |
test_results = [] | |
def assert_test(name, actual, expected_substring, explanation=None): | |
if explanation: | |
log(f"🧪 TEST: {name} → {explanation}") | |
actual_str = str(actual) | |
if expected_substring in actual_str: | |
log(f"[TEST] {name:<45} ✅") | |
test_results.append((name, True)) | |
else: | |
log(f"[TEST] {name:<45} ❌ — Beklenen: {expected_substring}, Gelen: {actual_str[:100]}...") | |
test_results.append((name, False)) | |
def summarize_tests(): | |
total = len(test_results) | |
success = sum(1 for _, ok in test_results if ok) | |
fail = total - success | |
log("🧾 TEST SONUCU ÖZETİ") | |
log(f"🔢 Toplam Test : {total}") | |
log(f"✅ Başarılı : {success}") | |
log(f"❌ Başarısız : {fail}") | |
def run_all_tests(): | |
try: | |
log("🚀 Test süreci başlatıldı.") | |
response = requests.post(f"{BASE_URL}/start_chat?project_name=project1") | |
if response.status_code != 200: | |
raise Exception(f"Start chat başarısız: {response.status_code}, {response.text}") | |
session_id = response.json().get("session_id") | |
if not session_id: | |
raise Exception("Session ID alınamadı.") | |
headers = {"X-Session-ID": session_id} | |
r = requests.post(f"{BASE_URL}/chat", json={"user_input": "bilinmeyen bir soru"}, headers=headers) | |
assert_test("LLM fallback", r.json(), "maalesef") | |
r = requests.post(f"{BASE_URL}/chat", json={"user_input": "döviz kuru nedir"}, headers=headers) | |
assert_test("Eksik parametre — currency", r.json(), "Lütfen currency") | |
r = requests.post(f"{BASE_URL}/chat", json={"user_input": "dolar"}, headers=headers) | |
assert_test("Parametre tamamlandı — dolar", r.json(), "dolar kuru şu an") | |
r = requests.post(f"{BASE_URL}/chat", json={"user_input": "yenidolar kuru nedir"}, headers=headers) | |
assert_test("Geçersiz parametre — currency", r.json(), "geçerli bir döviz") | |
r = requests.post(f"{BASE_URL}/chat", json={"user_input": "hava nasıl"}, headers=headers) | |
assert_test("Konu değişikliği sonrası fallback", r.json(), "maalesef") | |
r = requests.post(f"{BASE_URL}/chat", json={"user_input": "yol durumu"}, headers=headers) | |
assert_test("Eksik parametre — from_location", r.json(), "Lütfen from_location") | |
r = requests.post(f"{BASE_URL}/chat", json={"user_input": "Ankara"}, headers=headers) | |
assert_test("Eksik parametre — to_location", r.json(), "Lütfen to_location") | |
r = requests.post(f"{BASE_URL}/chat", json={"user_input": "İstanbul"}, headers=headers) | |
assert_test("Parametre tamamlandı — yol durumu", r.json(), "trafik açık") | |
summarize_tests() | |
except Exception as e: | |
log(f"❌ run_all_tests sırasında hata oluştu: {e}") |