File size: 5,151 Bytes
ebda3d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python3
"""
Post-deployment verification script for Hugging Face Space.
Run this after deployment to verify all endpoints are working correctly.
"""

import requests
import json
import time
import argparse
from typing import Dict, Any

def test_endpoint(url: str, method: str = 'GET', data: Dict[Any, Any] = None, timeout: int = 30) -> Dict[str, Any]:
    """Test a single endpoint and return results."""
    try:
        if method.upper() == 'GET':
            response = requests.get(url, timeout=timeout)
        elif method.upper() == 'POST':
            response = requests.post(url, json=data, timeout=timeout)
        else:
            return {"success": False, "error": f"Unsupported method: {method}"}
        
        return {
            "success": True,
            "status_code": response.status_code,
            "response_time": response.elapsed.total_seconds(),
            "response_data": response.json() if response.headers.get('content-type', '').startswith('application/json') else response.text[:200]
        }
    except requests.exceptions.Timeout:
        return {"success": False, "error": "Request timed out"}
    except requests.exceptions.ConnectionError:
        return {"success": False, "error": "Connection error"}
    except Exception as e:
        return {"success": False, "error": str(e)}

def verify_deployment(base_url: str):
    """Run comprehensive deployment verification."""
    print(f"πŸš€ Starting deployment verification for: {base_url}")
    print("=" * 60)
    
    tests = [
        {
            "name": "Root Endpoint",
            "url": f"{base_url}/",
            "method": "GET",
            "expected_status": 200
        },
        {
            "name": "Health Check",
            "url": f"{base_url}/api/v1/health", 
            "method": "GET",
            "expected_status": 200
        },
        {
            "name": "Static Files Mount (Should return 404, not 500)",
            "url": f"{base_url}/static/test.txt",
            "method": "GET",
            "expected_status": 404
        },
        {
            "name": "Generate Horoscope",
            "url": f"{base_url}/api/v1/generate-horoscope",
            "method": "POST",
            "data": {
                "terms": ["Test", "Deployment", "Success", "Working", "Happy"],
                "date_of_birth": "1990-01-01"
            },
            "expected_status": 200
        }
    ]
    
    results = []
    
    for test in tests:
        print(f"\nπŸ” Testing: {test['name']}")
        print(f"   URL: {test['url']}")
        
        result = test_endpoint(
            url=test['url'],
            method=test['method'],
            data=test.get('data'),
            timeout=30
        )
        
        if result['success']:
            status_code = result['status_code']
            expected = test['expected_status']
            
            if status_code == expected:
                print(f"   βœ… PASS - Status: {status_code}, Time: {result['response_time']:.2f}s")
                if 'response_data' in result:
                    print(f"   πŸ“ Response: {result['response_data']}")
            else:
                print(f"   ❌ FAIL - Expected: {expected}, Got: {status_code}")
                print(f"   πŸ“ Response: {result['response_data']}")
        else:
            print(f"   ❌ ERROR - {result['error']}")
        
        results.append({
            "test": test['name'],
            "result": result,
            "expected_status": test['expected_status']
        })
        
        # Small delay between tests
        time.sleep(1)
    
    # Summary
    print("\n" + "=" * 60)
    print("πŸ“Š DEPLOYMENT VERIFICATION SUMMARY")
    print("=" * 60)
    
    passed = 0
    failed = 0
    
    for result in results:
        test_name = result['test']
        test_result = result['result']
        expected = result['expected_status']
        
        if test_result['success'] and test_result['status_code'] == expected:
            print(f"βœ… {test_name}")
            passed += 1
        else:
            print(f"❌ {test_name}")
            failed += 1
    
    print(f"\nπŸ“ˆ Results: {passed} passed, {failed} failed")
    
    if failed == 0:
        print("πŸŽ‰ All tests passed! Deployment is successful.")
        return True
    else:
        print("πŸ’₯ Some tests failed. Check the errors above.")
        return False

def main():
    parser = argparse.ArgumentParser(description='Verify Hugging Face Space deployment')
    parser.add_argument(
        '--url', 
        default='https://huggingface.co/spaces/ch404/cardserver',
        help='Base URL of the deployed application'
    )
    parser.add_argument(
        '--wait', 
        type=int, 
        default=0,
        help='Wait N seconds before starting tests (useful for fresh deployments)'
    )
    
    args = parser.parse_args()
    
    if args.wait > 0:
        print(f"⏰ Waiting {args.wait} seconds for deployment to fully start...")
        time.sleep(args.wait)
    
    success = verify_deployment(args.url)
    exit(0 if success else 1)

if __name__ == "__main__":
    main()