File size: 2,731 Bytes
e697769
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import json
import uuid
import urllib.request
import urllib.error
from datetime import datetime, timedelta

def test_cognitive_api():
    url = "http://localhost:8000/api/cognitive/analyze"
    
    # Construct a valid payload
    session_id = f"sess_{uuid.uuid4().hex[:8]}"
    start_time = datetime.utcnow()
    end_time = start_time + timedelta(seconds=60)
    
    payload = {
        "session_id": session_id,
        "patient_id": None,  # Will trigger fallback user logic
        "user_metadata": {
            "userAgent": "TestScript/1.0"
        },
        "tasks": [
            {
                "task_id": "reaction_time_v1",
                "start_time": start_time.isoformat(),
                "end_time": end_time.isoformat(),
                "events": [
                    {
                        "timestamp": 1000,
                        "event_type": "test_start",
                        "payload": {}
                    },
                    {
                        "timestamp": 2000,
                        "event_type": "stimulus_shown",
                        "payload": {"trial": 1}
                    },
                    {
                        "timestamp": 2250,
                        "event_type": "response_received",
                        "payload": {"trial": 1, "rt": 250}
                    },
                    {
                         "timestamp": 3000,
                         "event_type": "test_end",
                         "payload": {}
                    }
                ],
                "metadata": {}
            }
        ]
    }
    
    print(f"Sending request to {url}...")
    
    req = urllib.request.Request(
        url, 
        data=json.dumps(payload).encode('utf-8'), 
        headers={'Content-Type': 'application/json'}
    )
    
    try:
        with urllib.request.urlopen(req) as response:
            status = response.status
            print(f"Status: {status}")
            
            data = json.loads(response.read().decode('utf-8'))
            print("Success!")
            print(f"Risk Assessment: {data.get('risk_assessment')}")
            print(f"Status: {data.get('status')}")
            
            if data.get('status') == 'success':
                print("✅ End-to-end API test passed")
            else:
                 print("⚠️ API returned success but payload status was not 'success'")

    except urllib.error.HTTPError as e:
        print(f"HTTP Error {e.code}: {e.read().decode('utf-8')}")
    except urllib.error.URLError as e:
        print(f"Connection failed: {e.reason}")
        print("Ensure the backend server is running on localhost:8000")

if __name__ == "__main__":
    test_cognitive_api()