Files
backend/test/test_ai_proxy.py
2026-02-02 22:22:11 -08:00

63 lines
2.2 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import httpx
import asyncio
import time
# Testing against the running service on localhost
BASE_URL = "http://localhost:8000"
async def test_ai_proxy_integration():
async with httpx.AsyncClient(base_url=BASE_URL, timeout=30.0) as client:
print("--- Starting AI Proxy Integration Test ---")
# 1. Register a new user
username = f"user_{int(time.time())}"
print(f"1. Registering user: {username}")
reg_res = await client.post("/register", json={
"username": username,
"password": "testpassword",
"email": f"user_{int(time.time())}@example.com"
})
if reg_res.status_code != 200:
print(f"Registration failed: {reg_res.text}")
return
# 2. Login to get token
print("2. Logging in...")
login_res = await client.post("/login", json={
"username": username,
"password": "testpassword"
})
if login_res.status_code != 200:
print(f"Login failed: {login_res.text}")
return
token = login_res.json()["access_token"]
# 3. Request AI Proxy
print("3. Sending AI Proxy request...")
headers = {"Authorization": f"Bearer {token}"}
ai_request = {
"messages": [
{"role": "user", "content": "Tell me a joke."}
],
"model": "some-model"
}
try:
response = await client.post("/ai/proxy", json=ai_request, headers=headers)
print(f"Response Status Code: {response.status_code}")
print(f"Response Content: {response.text[:200]}...") # Print first 200 chars
if response.status_code == 200:
print("✅ Success: AI Proxy returned 200 OK")
elif response.status_code in [400, 401]:
print(" Proxy worked, but AI provider returned error (likely invalid/missing API key)")
else:
print(f"❌ Unexpected status code: {response.status_code}")
except Exception as e:
print(f"❌ Request failed: {str(e)}")
print("--- Test Completed ---")
if __name__ == "__main__":
asyncio.run(test_ai_proxy_integration())