File size: 4,537 Bytes
a0b9343 |
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 135 136 137 138 139 |
# Test script for Phase III Chat API
# Tests the /api/chat endpoint with sample messages
import requests
import json
from uuid import uuid4
BASE_URL = "http://localhost:8000"
# For testing without JWT, create a test token
# In production, users get this from Phase II /auth/login
TEST_USER_ID = str(uuid4()) # Replace with actual user ID from your database
def get_test_jwt():
"""
Get a test JWT token.
For now, this is a placeholder.
In production, users login via Phase II backend.
"""
# TODO: Implement actual JWT generation or get from login
# For testing, you might need to:
# 1. Create a user in the database
# 2. Call /auth/login to get JWT
# 3. Use that JWT here
return "YOUR_JWT_TOKEN_HERE"
def test_chat_endpoint():
"""Test the chat endpoint with various messages"""
headers = {
"Authorization": f"Bearer {get_test_jwt()}",
"Content-Type": "application/json"
}
test_cases = [
{
"name": "Create task (English)",
"message": "Add a task to buy milk"
},
{
"name": "Create task (Urdu)",
"message": "Doodh lene ka task add karo"
},
{
"name": "Create task with priority",
"message": "Add task 'Finish project' with high priority"
},
{
"name": "Create task with due date",
"message": "Add task 'Submit report' due 2026-02-01"
},
{
"name": "List tasks",
"message": "Show my tasks"
},
{
"name": "List tasks (Urdu)",
"message": "Mere tasks dikhao"
}
]
print("=" * 60)
print("PHASE III CHAT API TEST")
print("=" * 60)
for test in test_cases:
print(f"\n[Test] {test['name']}")
print(f"Message: {test['message']}")
payload = {
"message": test['message']
}
try:
response = requests.post(
f"{BASE_URL}/api/chat/",
headers=headers,
json=payload,
timeout=30
)
if response.status_code == 200:
data = response.json()
print(f"β
Success!")
print(f"Reply: {data.get('reply', 'No reply')[:100]}...")
print(f"Conversation ID: {data.get('conversation_id')}")
if data.get('tool_calls'):
print(f"Tool Calls: {len(data['tool_calls'])} tool(s) executed")
else:
print(f"β Failed: {response.status_code}")
print(f"Error: {response.text}")
except Exception as e:
print(f"β Exception: {str(e)}")
print("\n" + "=" * 60)
print("TEST COMPLETE")
print("=" * 60)
def test_health_endpoints():
"""Test health check endpoints"""
print("\n[Health Check] Testing endpoints...")
endpoints = [
("/", "Root"),
("/health", "Main Health"),
("/api/chat/health", "Chat Health")
]
for endpoint, name in endpoints:
try:
response = requests.get(f"{BASE_URL}{endpoint}")
status = "β
" if response.status_code == 200 else "β"
print(f"{status} {name}: {response.status_code}")
except Exception as e:
print(f"β {name}: {str(e)}")
if __name__ == "__main__":
print("""
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Phase III - AI-Powered Todo Chatbot Test Suite β
β β
β Before running, ensure: β
β 1. Backend server is running (port 8000) β
β 2. You have a valid JWT token from Phase II β
β 3. Update get_test_jwt() with your token β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
""")
# Test health endpoints
test_health_endpoints()
# Test chat endpoint
print("\n" + "β οΈ NOTE: You need a valid JWT token to test /api/chat")
print("Update get_test_jwt() function with your token first!")
print("\nSkipping chat tests... (uncomment after adding JWT)\n")
# test_chat_endpoint()
|