File size: 2,404 Bytes
ba86059
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import os
import time

BASE_URL = "http://localhost:8000"
TEST_USER = "tester_01"
TEST_PASS = "password123"
SESSION_ID = "session_test_99"

def test_backend():
    print("TEST: Starting Local Backend Test...")
    
    # 1. Check Root
    try:
        res = requests.get(f"{BASE_URL}/")
        print(f"OK: Root Health: {res.json()}")
    except:
        print("FAIL: Server is NOT running. Please start it with uvicorn first!")
        return

    # 2. Register
    requests.post(f"{BASE_URL}/register", json={"username": TEST_USER, "password": TEST_PASS})
    
    # 3. Login
    login_res = requests.post(f"{BASE_URL}/login", json={"username": TEST_USER, "password": TEST_PASS})
    if login_res.status_code != 200:
        print("FAIL: Login failed")
        return
    token = login_res.json()["access_token"]
    headers = {"Authorization": f"Bearer {token}"}
    print("OK: Login successful, token received.")

    # 4. Upload (Tạo một file text giả lập PDF hoặc gửi text)
    # Ở đây tôi giả định bạn có 1 file PDF test. Nếu không, tôi sẽ bỏ qua bước này 
    # và test bằng cách hỏi Wiki (System scope)
    
    # 5. Chat Test (Sử dụng mode Ultrafast để test tốc độ)
    print(f"LOG: Testing Chat (Mode: Ultrafast, Session: {SESSION_ID})...")
    chat_payload = {
        "message": "What is TurboQuant?",
        "mode": "ultrafast",
        "scope": "both",
        "session_id": SESSION_ID
    }
    
    start = time.time()
    chat_res = requests.post(f"{BASE_URL}/chat", json=chat_payload, headers=headers)
    end = time.time()
    
    if chat_res.status_code == 200:
        print(f"OK: Chat response started in {end-start:.2f}s")
        print("--- STREAMING RESPONSE ---")
        full_text = ""
        for line in chat_res.iter_lines():
            if line:
                decoded_line = line.decode('utf-8')
                if "--META_END--" in decoded_line:
                    meta_part = decoded_line.split("--META_END--")[0]
                    print(f"METADATA: {meta_part}")
                else:
                    print(decoded_line, end="", flush=True)
                    full_text += decoded_line
        print("\n--------------------------")
        print(f"OK: Full answer received.")
    else:
        print(f"FAIL: Chat failed: {chat_res.text}")

if __name__ == "__main__":
    test_backend()