File size: 6,573 Bytes
3874cd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
163
164
165
166
167
168
169
#!/usr/bin/env python3
"""
API Testing Script for HF Agents Course Scoring API
Tests connectivity, endpoints, and response formats
"""

import requests
import json
import sys
from datetime import datetime

# API Configuration
API_BASE_URL = "https://agents-course-unit4-scoring.hf.space"
QUESTIONS_URL = f"{API_BASE_URL}/questions"
SUBMIT_URL = f"{API_BASE_URL}/submit"

def test_connectivity():
    """Test basic API connectivity"""
    print("πŸ” Testing API Connectivity...")
    print(f"πŸ“ Base URL: {API_BASE_URL}")
    print("-" * 50)
    
    try:
        response = requests.get(API_BASE_URL, timeout=10)
        print(f"βœ… Base URL Status: {response.status_code}")
        print(f"πŸ“‹ Headers: {dict(response.headers)}")
        if response.text:
            print(f"πŸ“„ Response (first 200 chars): {response.text[:200]}")
        return True
    except requests.exceptions.RequestException as e:
        print(f"❌ Base URL Error: {e}")
        return False

def test_questions_endpoint():
    """Test questions endpoint"""
    print("\nπŸ” Testing Questions Endpoint...")
    print(f"πŸ“ Questions URL: {QUESTIONS_URL}")
    print("-" * 50)
    
    try:
        response = requests.get(QUESTIONS_URL, timeout=15)
        print(f"βœ… Status Code: {response.status_code}")
        print(f"πŸ“‹ Headers: {dict(response.headers)}")
        
        if response.status_code == 200:
            try:
                data = response.json()
                print(f"πŸ“Š Response Type: {type(data)}")
                if isinstance(data, list):
                    print(f"πŸ“ˆ Questions Count: {len(data)}")
                    if data:
                        print(f"πŸ“ Sample Question Keys: {list(data[0].keys()) if data else 'Empty'}")
                        # Show first question safely
                        if data:
                            first_q = data[0]
                            print(f"πŸ“‹ First Question ID: {first_q.get('task_id', 'N/A')}")
                            question_text = first_q.get('question', '')
                            print(f"πŸ“‹ First Question (50 chars): {question_text[:50]}...")
                else:
                    print(f"πŸ“„ Response Content: {data}")
                return True, data
            except json.JSONDecodeError as e:
                print(f"❌ JSON Decode Error: {e}")
                print(f"πŸ“„ Raw Response (first 500 chars): {response.text[:500]}")
                return False, None
        else:
            print(f"❌ HTTP Error: {response.status_code}")
            print(f"πŸ“„ Error Response: {response.text[:500]}")
            return False, None
            
    except requests.exceptions.RequestException as e:
        print(f"❌ Request Error: {e}")
        return False, None

def test_submit_endpoint():
    """Test submit endpoint with minimal payload"""
    print("\nπŸ” Testing Submit Endpoint...")
    print(f"πŸ“ Submit URL: {SUBMIT_URL}")
    print("-" * 50)
    
    # Create minimal test payload
    test_payload = {
        "username": "test_user", 
        "agent_code": "https://github.com/test/repo",
        "answers": [
            {"task_id": "test_task_001", "submitted_answer": "test_answer"}
        ]
    }
    
    try:
        # Note: This might fail with authentication or validation errors, but it tests connectivity
        response = requests.post(SUBMIT_URL, json=test_payload, timeout=30)
        print(f"πŸ“Š Status Code: {response.status_code}")
        print(f"πŸ“‹ Headers: {dict(response.headers)}")
        
        try:
            data = response.json()
            print(f"πŸ“„ Response: {json.dumps(data, indent=2)}")
        except:
            print(f"πŸ“„ Raw Response: {response.text[:500]}")
            
        return response.status_code, response.text
        
    except requests.exceptions.RequestException as e:
        print(f"❌ Submit Error: {e}")
        return None, str(e)

def check_huggingface_space():
    """Check if the HuggingFace Space exists and is accessible"""
    print("\nπŸ” Checking HuggingFace Space...")
    hf_space_url = "https://huggingface.co/spaces/agents-course/unit4-scoring"
    print(f"πŸ“ HF Space URL: {hf_space_url}")
    print("-" * 50)
    
    try:
        response = requests.get(hf_space_url, timeout=10)
        print(f"πŸ“Š HF Space Status: {response.status_code}")
        if response.status_code == 200:
            if "Space" in response.text and "Gradio" in response.text:
                print("βœ… HuggingFace Space appears to be active")
            else:
                print("⚠️  HuggingFace Space exists but might not be a Gradio space")
        else:
            print(f"❌ HuggingFace Space not accessible: {response.status_code}")
    except requests.exceptions.RequestException as e:
        print(f"❌ HF Space Check Error: {e}")

def main():
    """Run all API tests"""
    print("πŸš€ API Testing Suite")
    print(f"⏰ Started at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
    print("=" * 60)
    
    # Test 1: Basic connectivity
    connectivity_ok = test_connectivity()
    
    # Test 2: Questions endpoint
    questions_ok, questions_data = test_questions_endpoint()
    
    # Test 3: Submit endpoint (will likely fail, but shows connectivity)
    submit_status, submit_response = test_submit_endpoint()
    
    # Test 4: Check HuggingFace Space
    check_huggingface_space()
    
    # Summary
    print("\n" + "=" * 60)
    print("πŸ“‹ SUMMARY")
    print("=" * 60)
    print(f"πŸ”— Base URL: {'βœ… OK' if connectivity_ok else '❌ FAILED'}")
    print(f"❓ Questions: {'βœ… OK' if questions_ok else '❌ FAILED'}")
    if questions_ok and questions_data:
        print(f"πŸ“Š Available Questions: {len(questions_data) if isinstance(questions_data, list) else 'Unknown'}")
    print(f"πŸ“€ Submit Endpoint: {'πŸ”— Reachable' if submit_status else '❌ FAILED'}")
    
    if not any([connectivity_ok, questions_ok]):
        print("\n❌ API appears to be completely down or unreachable")
        print("πŸ’‘ Suggestions:")
        print("   - Check your internet connection")
        print("   - Try again later (server might be temporarily down)")
        print("   - Contact course administrators if issue persists")
    elif questions_ok:
        print("\nβœ… Questions endpoint is working - you can fetch questions")
        print("❌ Submit endpoint issue might be temporary server problem")
    
    print(f"\n⏰ Completed at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")

if __name__ == "__main__":
    main()