Spaces:
Sleeping
Sleeping
File size: 5,779 Bytes
5590815 | 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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | #!/usr/bin/env python3
"""
Test script for Hiroyuki SLM and API
"""
import requests
import json
import sys
import time
import argparse
import subprocess
API_BASE = "http://localhost:8080"
def test_health():
"""Test health endpoint"""
print("\n=== Testing /health ===")
try:
response = requests.get(f"{API_BASE}/health", timeout=5)
assert response.status_code == 200
data = response.json()
assert data['status'] == 'healthy'
print(f"✓ Health check passed: {data}")
return True
except Exception as e:
print(f"✗ Health check failed: {e}")
return False
def test_models_info():
"""Test models info endpoint"""
print("\n=== Testing /models/info ===")
try:
response = requests.get(f"{API_BASE}/models/info", timeout=5)
assert response.status_code == 200
data = response.json()
print(f"✓ Models info: {data}")
return True
except Exception as e:
print(f"✗ Models info failed: {e}")
return False
def test_exact_match():
"""Test exact match responses"""
print("\n=== Testing Exact Match Responses ===")
test_cases = [
("嘘", "嘘 related"),
("データ", "データ related"),
("学校", "学校 related"),
("Programming", None), # Should use SLM
]
success = True
for message, expected_key in test_cases:
try:
response = requests.post(
f"{API_BASE}/chat",
json={"message": message},
timeout=10
)
assert response.status_code == 200
data = response.json()
print(f" Input: '{message}' -> Response: '{data['response'][:50]}...'")
except Exception as e:
print(f" ✗ Failed for '{message}': {e}")
success = False
return success
def test_slm_generation():
"""Test SLM generation"""
print("\n=== Testing SLM Generation ===")
test_messages = [
"こんにちは",
"どう思いますか?",
"プログラミングについて",
"頭の悪い人について",
"お前は馬鹿なのか?",
]
success = True
for message in test_messages:
try:
response = requests.post(
f"{API_BASE}/chat",
json={"message": message},
timeout=15
)
assert response.status_code == 200
data = response.json()
print(f" Input: '{message}'")
print(f" Output: '{data['response'][:60]}...'")
print()
except Exception as e:
print(f" ✗ Failed for '{message}': {e}")
success = False
return success
def test_error_cases():
"""Test error handling"""
print("\n=== Testing Error Cases ===")
# Missing message
try:
response = requests.post(f"{API_BASE}/chat", json={}, timeout=5)
assert response.status_code == 400
print("✓ Missing message correctly rejected")
except Exception as e:
print(f"✗ Missing message test failed: {e}")
return False
# Invalid message type
try:
response = requests.post(f"{API_BASE}/chat", json={"message": 123}, timeout=5)
assert response.status_code == 400
print("✓ Invalid message type correctly rejected")
except Exception as e:
print(f"✗ Invalid message type test failed: {e}")
return False
return True
def run_all_tests():
"""Run all tests"""
print("=" * 50)
print("Hiroyuki SLM API Test Suite")
print("=" * 50)
# Wait for server to be ready
print("\nWaiting for API server...")
max_retries = 10
for i in range(max_retries):
try:
requests.get(f"{API_BASE}/health", timeout=2)
print("API server is ready!")
break
except:
if i < max_retries - 1:
time.sleep(1)
else:
print("ERROR: API server not responding")
print("Make sure the server is running: python api.py")
sys.exit(1)
# Run tests
results = []
results.append(("Health", test_health()))
results.append(("Models Info", test_models_info()))
results.append(("Exact Match", test_exact_match()))
results.append(("SLM Generation", test_slm_generation()))
results.append(("Error Cases", test_error_cases()))
# Summary
print("\n" + "=" * 50)
print("Test Summary")
print("=" * 50)
passed = sum(1 for _, r in results if r)
total = len(results)
for name, result in results:
status = "✓ PASS" if result else "✗ FAIL"
print(f" {name}: {status}")
print(f"\nTotal: {passed}/{total} tests passed")
return passed == total
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Test script for Hiroyuki SLM and API')
parser.add_argument('--standalone', action='store_true', help='Run API server, test, and shutdown')
parser.add_argument('--api-base', default='http://localhost:8080', help='API base URL')
args = parser.parse_args()
API_BASE = args.api_base
if args.standalone:
print("Starting API server in standalone mode...")
server_process = subprocess.Popen([sys.executable, 'api.py'])
time.sleep(5) # Wait for server to start
try:
success = run_all_tests()
finally:
print("Shutting down API server...")
server_process.terminate()
server_process.wait()
else:
success = run_all_tests()
sys.exit(0 if success else 1)
|