File size: 6,186 Bytes
4b9f7d2
 
 
 
 
 
 
 
bb20239
 
 
 
4b9f7d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import os, requests, time
from log import log

BASE_URL = "http://localhost:7860"
MOCK_BASE = os.getenv("MOCK_BASE_URL")  # Örnek: https://abc123.ngrok.io

test_results = []

def assert_test(name, actual, expected_substring, explanation=None):
    if explanation:
        log(f"🧪 TEST: {name}{explanation}")

    if expected_substring in str(actual):
        log(f"[TEST] {name:<45} ✅")
        test_results.append((name, True))
    else:
        log(f"[TEST] {name:<45} ❌ — Beklenen: {expected_substring}, Gelen: {actual}")
        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 wait_for_intent_training(timeout_sec=60):
    log("⏳ Intent eğitimi tamamlanıyor mu kontrol ediliyor...")
    for _ in range(timeout_sec // 3):
        logs = open("/tmp/logs.txt").read() if os.path.exists("/tmp/logs.txt") else ""
        if "✅ Intent eğitimi tamamlandı" in logs:
            return True
        time.sleep(3)
    return False

def run_all_tests():
    try:
        log("🚀 Test süreci başlatıldı.")
        session_id = requests.post(f"{BASE_URL}/start_chat").json().get("session_id")
        headers = {"X-Session-ID": session_id}

        # 1. LLM fallback testi
        r = requests.post(f"{BASE_URL}/chat", json={"user_input": "lorem ipsum"}, headers=headers)
        assert_test("LLM fallback", r.json(), "Bu konuda maalesef bilgim yok")

        # 2. Intent eğitimi (doviz + yol intentleri)
        intents = {
            "intents": [
                {
                    "name": "doviz-kuru",
                    "examples": ["dolar kuru nedir", "euro kuru nedir"],
                    "variables": ["currency:{dolar} kuru nedir", "currency:{euro} kuru nedir"],
                    "variable_formats": {"currency": "currency_format"},
                    "action": {
                        "url": f"{MOCK_BASE}/doviz",
                        "method": "POST",
                        "headers": [{"key": "Authorization", "value": "Bearer {auth_tokens.doviz-kuru.token}"}],
                        "body": {"currency": "{variables.currency}"},
                        "auth": {
                            "auth_endpoint": f"{MOCK_BASE}/auth",
                            "auth_body": {"username": "user", "password": "pass"},
                            "auth_token_path": "token",
                            "auth_refresh_endpoint": f"{MOCK_BASE}/refresh",
                            "refresh_body": {"refresh_token": "{auth_tokens.doviz-kuru.refresh_token}"}
                        },
                        "response_parser": {"field": "rate"},
                        "reply_template": "{variables.currency} kuru şu an {rate} TL."
                    }
                },
                {
                    "name": "yol-durumu",
                    "examples": ["Ankara'dan İstanbul'a yol durumu"],
                    "variables": ["from_location:{Ankara} to_location:{İstanbul} yol durumu"],
                    "action": {
                        "url": f"{MOCK_BASE}/yol",
                        "method": "POST",
                        "headers": [{"key": "Authorization", "value": "Bearer {auth_tokens.yol-durumu.token}"}],
                        "body": {
                            "from_location": "{variables.from_location}",
                            "to_location": "{variables.to_location}"
                        },
                        "auth": {
                            "auth_endpoint": f"{MOCK_BASE}/auth",
                            "auth_body": {"username": "user", "password": "pass"},
                            "auth_token_path": "token",
                            "auth_refresh_endpoint": f"{MOCK_BASE}/refresh",
                            "refresh_body": {"refresh_token": "{auth_tokens.yol-durumu.refresh_token}"}
                        },
                        "response_parser": {"field": "status"},
                        "reply_template": "{status}"
                    }
                }
            ]
        }
        requests.post(f"{BASE_URL}/train_intents", json=intents)
        if not wait_for_intent_training():
            assert_test("Intent eğitimi zamanında tamamlandı", "False", "True")
            summarize_tests()
            return

        r = requests.post(f"{BASE_URL}/load_intent_model")
        assert_test("Intent modeli yüklendi", r.json(), "ok")

        # 3. Eksik parametre — doviz-kuru
        r = requests.post(f"{BASE_URL}/chat", json={"user_input": "döviz kuru nedir"}, headers=headers)
        assert_test("Eksik parametre — currency", r.json(), "bir döviz cinsi")

        # 4. Parametre tamamlanması
        r = requests.post(f"{BASE_URL}/chat", json={"user_input": "euro"}, headers=headers)
        assert_test("Parametre tamamlandı — euro", r.json(), "euro kuru şu an")

        # 5. Eksik parametre — yol-durumu
        r = requests.post(f"{BASE_URL}/chat", json={"user_input": "Ankara'dan yol durumu"}, headers=headers)
        assert_test("Eksik parametre — to_location", r.json(), "hangi şehir")

        # 6. Parametre tamamlanması — yol
        r = requests.post(f"{BASE_URL}/chat", json={"user_input": "İstanbul"}, headers=headers)
        assert_test("Parametre tamamlandı — yol durumu", r.json(), "trafik açık")

        # 7. Konu değişikliği testi
        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(), "Bu konuda maalesef")

        # 8. Geçersiz parametre — doviz
        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")

        summarize_tests()

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