File size: 3,648 Bytes
1cf642c
95d826d
1cf642c
 
 
537f2d3
95d826d
 
 
 
 
 
 
 
1cf642c
 
 
 
 
 
 
55a98ca
95d826d
 
1cf642c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
537f2d3
1cf642c
537f2d3
 
 
1cf642c
537f2d3
 
 
1cf642c
 
34fde90
95d826d
1cf642c
55a98ca
3fbf837
34fde90
1cf642c
 
34fde90
1cf642c
 
 
 
95d826d
1cf642c
 
95d826d
1cf642c
 
34fde90
 
 
3fbf837
 
 
1cf642c
 
 
95d826d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
import json
import requests
from log import log

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)

fallback_answers = service_config["projects"]["project1"]["llm"]["fallback_answers"]
currency_options = service_config["config"]["data_formats"]["currency_format"]["valid_options"]
city_options = service_config["config"]["data_formats"]["city_format"]["valid_options"]

test_results = []

def assert_test(name, actual, expected_substring, explanation=None):
    if explanation:
        log(f"🧪 TEST: {name}{explanation}")
    actual_str = str(actual)
    passed = any(ans in actual_str for ans in fallback_answers) if "fallback" in name.lower() else 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}

        r = requests.post(f"{BASE_URL}/chat", json={"user_input": "Mars'a bilet alabilir miyim?"}, headers=headers)
        assert_test("LLM fallback", r.json(), "")

        valid_currency = currency_options[0]
        r = requests.post(f"{BASE_URL}/chat", json={"user_input": f"{valid_currency} kuru nedir"}, headers=headers)
        assert_test("Parametre tamamlandı — dolar", r.json(), f"{valid_currency} 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 cinsi")

        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": city_options[0]}, headers=headers)
        assert_test("Eksik parametre — to_location", r.json(), "Lütfen to_location")

        r = requests.post(f"{BASE_URL}/chat", json={"user_input": city_options[1]}, headers=headers)
        assert_test("Parametre tamamlandı — yol durumu", r.json(), "trafik açık")

        r = requests.post(f"{BASE_URL}/chat", json={"user_input": "hava durumu"}, headers=headers)
        assert_test("Eksik parametre — city", r.json(), "Lütfen city")

        r = requests.post(f"{BASE_URL}/chat", json={"user_input": city_options[0]}, headers=headers)
        assert_test("Parametre tamamlandı — hava durumu", r.json(), f"{city_options[0]} için hava güneşli")

        summarize_tests()

    except Exception as e:
        log(f"❌ run_all_tests sırasında hata oluştu: {e}")