File size: 4,967 Bytes
fb9f2a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
170
"""

Test Script for Avatar-based AI Tutor Engine

"""
import requests
import json

BASE_URL = "http://127.0.0.1:8003"


def test_health():
    """Test health endpoint"""
    print("\n=== Testing Health Endpoint ===")
    response = requests.get(f"{BASE_URL}/health")
    print(f"Status: {response.status_code}")
    print(f"Response: {json.dumps(response.json(), indent=2)}")
    return response.status_code == 200


def test_root():
    """Test root endpoint"""
    print("\n=== Testing Root Endpoint ===")
    response = requests.get(f"{BASE_URL}/")
    print(f"Status: {response.status_code}")
    print(f"Response: {json.dumps(response.json(), indent=2)}")
    return response.status_code == 200


def test_create_avatar_tutor():
    """Test avatar tutor creation"""
    print("\n=== Testing Avatar Tutor Creation ===")
    
    payload = {
        "request_id": "test-001",
        "engine": "avatar-tutor-engine",
        "action": "create_avatar_tutor",
        "actor": {
            "user_id": "test-user",
            "session_id": "test-session"
        },
        "input": {
            "text": "Introduction to Python: Python is a versatile programming language.",
            "items": [
                {
                    "type": "image",
                    "ref": "https://example.com/photo.jpg"
                },
                {
                    "type": "audio",
                    "ref": "https://example.com/voice.mp3"
                }
            ]
        },
        "context": {},
        "options": {
            "lesson_duration": "3 minutes"
        }
    }
    
    print(f"Request: {json.dumps(payload, indent=2)}\n")
    
    try:
        response = requests.post(f"{BASE_URL}/run", json=payload, timeout=60)
        print(f"Status: {response.status_code}")
        print(f"Response: {json.dumps(response.json(), indent=2)}")
        return response.status_code == 200
    except Exception as e:
        print(f"Error: {e}")
        return False


def test_missing_image():
    """Test error handling for missing image"""
    print("\n=== Testing Missing Image Error ===")
    
    payload = {
        "request_id": "test-error-001",
        "engine": "avatar-tutor-engine",
        "action": "create_avatar_tutor",
        "actor": {
            "user_id": "test-user",
            "session_id": None
        },
        "input": {
            "text": "Some content",
            "items": [
                {
                    "type": "audio",
                    "ref": "https://example.com/voice.mp3"
                }
            ]
        },
        "context": {},
        "options": {}
    }
    
    response = requests.post(f"{BASE_URL}/run", json=payload)
    print(f"Status: {response.status_code}")
    result = response.json()
    print(f"Response: {json.dumps(result, indent=2)}")
    
    return not result.get("ok") and result.get("error", {}).get("code") == "MISSING_IMAGE"


def test_invalid_action():
    """Test error handling for invalid action"""
    print("\n=== Testing Invalid Action Error ===")
    
    payload = {
        "request_id": "test-error-002",
        "engine": "avatar-tutor-engine",
        "action": "invalid_action",
        "actor": {
            "user_id": "test-user",
            "session_id": None
        },
        "input": {
            "text": "Some content",
            "items": []
        },
        "context": {},
        "options": {}
    }
    
    response = requests.post(f"{BASE_URL}/run", json=payload)
    print(f"Status: {response.status_code}")
    result = response.json()
    print(f"Response: {json.dumps(result, indent=2)}")
    
    return not result.get("ok") and result.get("error", {}).get("code") == "INVALID_ACTION"


def main():
    """Run all tests"""
    print("=" * 60)
    print("Avatar-based AI Tutor Engine - Test Suite")
    print("=" * 60)
    
    tests = [
        ("Health Check", test_health),
        ("Root Endpoint", test_root),
        ("Create Avatar Tutor", test_create_avatar_tutor),
        ("Missing Image Error", test_missing_image),
        ("Invalid Action Error", test_invalid_action)
    ]
    
    results = []
    for name, test_func in tests:
        try:
            passed = test_func()
            results.append((name, passed))
        except Exception as e:
            print(f"\nTest '{name}' failed with exception: {e}")
            results.append((name, False))
    
    # Summary
    print("\n" + "=" * 60)
    print("Test Summary")
    print("=" * 60)
    for name, passed in results:
        status = "✓ PASSED" if passed else "✗ FAILED"
        print(f"{status}: {name}")
    
    total = len(results)
    passed = sum(1 for _, p in results if p)
    print(f"\nTotal: {passed}/{total} tests passed")


if __name__ == "__main__":
    main()