Spaces:
Paused
Paused
import os | |
import json | |
import requests | |
from log import log | |
from parse_llm_blocks import parse_llm_blocks | |
BASE_URL = os.environ.get("BASE_URL", "http://localhost:7860") | |
SERVICE_CONFIG_PATH = os.environ.get("SERVICE_CONFIG_PATH", "service_config.json") | |
with open(SERVICE_CONFIG_PATH, "r", encoding="utf-8") as f: | |
service_config = json.load(f) | |
project_config = service_config["projects"]["project1"]["llm"] | |
intents = project_config["intents"] | |
test_results = [] | |
def assert_test(name, actual, expected_substring, explanation=None): | |
if explanation: | |
log(f"🧪 TEST: {name} → {explanation}") | |
actual_str = str(actual) | |
passed = expected_substring in actual_str | |
if passed: | |
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} | |
# === Test 1: Intent yok | |
r = requests.post(f"{BASE_URL}/chat", json={"user_input": "Selam, nasılsın?"}, headers=headers) | |
parsed = parse_llm_blocks(r.json().get("response", "")) | |
assert_test("Intent yok — selam", parsed["intent"], "NONE") | |
# === Test 2: Döviz kuru intent | |
r = requests.post(f"{BASE_URL}/chat", json={"user_input": "Dolar kuru nedir?"}, headers=headers) | |
parsed = parse_llm_blocks(r.json().get("response", "")) | |
assert_test("Döviz kuru intent", parsed["intent"], "doviz-kuru-intent") | |
# === Test 3: Yol durumu intent | |
r = requests.post(f"{BASE_URL}/chat", json={"user_input": "Ankara'dan İstanbul'a yol durumu nasıl?"}, headers=headers) | |
parsed = parse_llm_blocks(r.json().get("response", "")) | |
assert_test("Yol durumu intent", parsed["intent"], "yol-durumu-intent") | |
# === Test 4: Hava durumu intent | |
r = requests.post(f"{BASE_URL}/chat", json={"user_input": "İzmir'de hava nasıl?"}, headers=headers) | |
parsed = parse_llm_blocks(r.json().get("response", "")) | |
assert_test("Hava durumu intent", parsed["intent"], "hava-durumu-intent") | |
summarize_tests() | |
except Exception as e: | |
log(f"❌ run_all_tests sırasında hata oluştu: {e}") | |