codenuk_backend_mine/services/ai-mockup-service/src/test_api.py
2025-10-10 08:56:39 +05:30

189 lines
6.6 KiB
Python

#!/usr/bin/env python3
"""
Test script for the wireframe generation API endpoints
Tests both the universal and device-specific endpoints
"""
import requests
import json
import sys
import os
# Configuration
BASE_URL = "http://localhost:5000"
TEST_PROMPT = "Dashboard with header, left sidebar, 3 stats cards, and footer"
def test_health_endpoint():
"""Test the health check endpoint"""
print("🔍 Testing health endpoint...")
try:
response = requests.get(f"{BASE_URL}/api/health", timeout=10)
if response.status_code == 200:
data = response.json()
print(f"✅ Health check passed: {data}")
return True
else:
print(f"❌ Health check failed: {response.status_code}")
return False
except Exception as e:
print(f"❌ Health check error: {e}")
return False
def test_device_specific_endpoint(device_type):
"""Test device-specific wireframe generation"""
print(f"🔍 Testing {device_type} endpoint...")
try:
response = requests.post(
f"{BASE_URL}/generate-wireframe/{device_type}",
json={"prompt": TEST_PROMPT},
headers={"Content-Type": "application/json"},
timeout=30
)
if response.status_code == 200:
content_type = response.headers.get('content-type', '')
if 'image/svg+xml' in content_type:
print(f"{device_type} endpoint: SVG generated successfully")
print(f" Content-Type: {content_type}")
print(f" Response length: {len(response.text)} characters")
return True
else:
print(f"⚠️ {device_type} endpoint: Unexpected content type: {content_type}")
return False
else:
print(f"{device_type} endpoint failed: {response.status_code}")
try:
error_data = response.json()
print(f" Error: {error_data}")
except:
print(f" Error text: {response.text}")
return False
except Exception as e:
print(f"{device_type} endpoint error: {e}")
return False
def test_universal_endpoint():
"""Test the universal wireframe generation endpoint"""
print("🔍 Testing universal endpoint...")
try:
response = requests.post(
f"{BASE_URL}/generate-wireframe",
json={"prompt": TEST_PROMPT, "device": "desktop"},
headers={"Content-Type": "application/json"},
timeout=30
)
if response.status_code == 200:
content_type = response.headers.get('content-type', '')
if 'image/svg+xml' in content_type:
print(f"✅ Universal endpoint: SVG generated successfully")
print(f" Content-Type: {content_type}")
print(f" Response length: {len(response.text)} characters")
return True
else:
print(f"⚠️ Universal endpoint: Unexpected content type: {content_type}")
return False
else:
print(f"❌ Universal endpoint failed: {response.status_code}")
try:
error_data = response.json()
print(f" Error: {error_data}")
except:
print(f" Error text: {response.text}")
return False
except Exception as e:
print(f"❌ Universal endpoint error: {e}")
return False
def test_all_devices_endpoint():
"""Test the all devices metadata endpoint"""
print("🔍 Testing all devices endpoint...")
try:
response = requests.post(
f"{BASE_URL}/generate-all-devices",
json={"prompt": TEST_PROMPT},
headers={"Content-Type": "application/json"},
timeout=10
)
if response.status_code == 200:
data = response.json()
print(f"✅ All devices endpoint: {data.get('message', 'Success')}")
if 'device_endpoints' in data:
for device, endpoint in data['device_endpoints'].items():
print(f" {device}: {endpoint}")
return True
else:
print(f"❌ All devices endpoint failed: {response.status_code}")
try:
error_data = response.json()
print(f" Error: {error_data}")
except:
print(f" Error text: {response.text}")
return False
except Exception as e:
print(f"❌ All devices endpoint error: {e}")
return False
def main():
"""Run all tests"""
print("🚀 Starting API endpoint tests...")
print(f"📍 Base URL: {BASE_URL}")
print(f"📝 Test Prompt: {TEST_PROMPT}")
print("=" * 60)
# Test health endpoint first
if not test_health_endpoint():
print("❌ Health check failed. Is the backend running?")
sys.exit(1)
print()
# Test device-specific endpoints
devices = ['desktop', 'tablet', 'mobile']
device_results = {}
for device in devices:
device_results[device] = test_device_specific_endpoint(device)
print()
# Test universal endpoint
universal_result = test_universal_endpoint()
print()
# Test all devices endpoint
all_devices_result = test_all_devices_endpoint()
print()
# Summary
print("=" * 60)
print("📊 Test Results Summary:")
print(f" Health Check: {'✅ PASS' if True else '❌ FAIL'}")
for device, result in device_results.items():
status = "✅ PASS" if result else "❌ FAIL"
print(f" {device.capitalize()} Endpoint: {status}")
print(f" Universal Endpoint: {'✅ PASS' if universal_result else '❌ FAIL'}")
print(f" All Devices Endpoint: {'✅ PASS' if all_devices_result else '❌ FAIL'}")
# Overall success
all_passed = all(device_results.values()) and universal_result and all_devices_result
if all_passed:
print("\n🎉 All tests passed! The API is working correctly.")
else:
print("\n⚠️ Some tests failed. Check the output above for details.")
return 0 if all_passed else 1
if __name__ == "__main__":
try:
exit_code = main()
sys.exit(exit_code)
except KeyboardInterrupt:
print("\n\n⏹️ Tests interrupted by user")
sys.exit(1)
except Exception as e:
print(f"\n💥 Unexpected error: {e}")
sys.exit(1)