#!/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)