Spaces:
Running
Running
File size: 8,290 Bytes
ea46ec8 |
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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
#!/usr/bin/env python3
"""
Comprehensive test suite for Web3 Research Co-Pilot
"""
import sys
import asyncio
import time
from datetime import datetime
def test_imports():
"""Test all critical imports"""
print("π§ͺ Testing imports...")
try:
# Core imports
from src.visualizations import CryptoVisualizations, create_price_chart
from src.agent.research_agent import Web3ResearchAgent
from src.utils.config import config
from src.tools.coingecko_tool import CoinGeckoTool
from src.tools.defillama_tool import DeFiLlamaTool
from src.tools.etherscan_tool import EtherscanTool
from src.api.airaa_integration import AIRAAIntegration
# FastAPI app
from app import app, service, Web3CoPilotService
print("β
All imports successful")
return True
except Exception as e:
print(f"β Import failed: {e}")
return False
def test_configuration():
"""Test configuration setup"""
print("π§ͺ Testing configuration...")
try:
from src.utils.config import config
print(f" β’ GEMINI_API_KEY: {'β
Set' if config.GEMINI_API_KEY else 'β Not set'}")
print(f" β’ COINGECKO_API_KEY: {'β
Set' if config.COINGECKO_API_KEY else 'β οΈ Not set'}")
print(f" β’ ETHERSCAN_API_KEY: {'β
Set' if config.ETHERSCAN_API_KEY else 'β οΈ Not set'}")
return True
except Exception as e:
print(f"β Configuration test failed: {e}")
return False
def test_visualizations():
"""Test visualization creation"""
print("π§ͺ Testing visualizations...")
try:
from src.visualizations import CryptoVisualizations
# Test empty chart
fig1 = CryptoVisualizations._create_empty_chart("Test message")
print(" β
Empty chart creation")
# Test price chart with sample data
sample_data = {
'prices': [
[1672531200000, 16500.50],
[1672617600000, 16750.25],
[1672704000000, 17100.00]
],
'total_volumes': [
[1672531200000, 1000000],
[1672617600000, 1200000],
[1672704000000, 1100000]
]
}
fig2 = CryptoVisualizations.create_price_chart(sample_data, 'BTC')
print(" β
Price chart with data")
# Test market overview
market_data = [
{'name': 'Bitcoin', 'market_cap': 500000000000, 'price_change_percentage_24h': 2.5},
{'name': 'Ethereum', 'market_cap': 200000000000, 'price_change_percentage_24h': -1.2}
]
fig3 = CryptoVisualizations.create_market_overview(market_data)
print(" β
Market overview chart")
return True
except Exception as e:
print(f"β Visualization test failed: {e}")
return False
def test_tools():
"""Test individual tools"""
print("π§ͺ Testing tools...")
try:
from src.tools.coingecko_tool import CoinGeckoTool
from src.tools.defillama_tool import DeFiLlamaTool
from src.tools.etherscan_tool import EtherscanTool
# Test tool initialization
coingecko = CoinGeckoTool()
print(" β
CoinGecko tool initialization")
defillama = DeFiLlamaTool()
print(" β
DeFiLlama tool initialization")
etherscan = EtherscanTool()
print(" β
Etherscan tool initialization")
return True
except Exception as e:
print(f"β Tools test failed: {e}")
return False
async def test_service():
"""Test service functionality"""
print("π§ͺ Testing service...")
try:
from app import service
print(f" β’ Service enabled: {'β
' if service.enabled else 'β'}")
print(f" β’ Agent available: {'β
' if service.agent else 'β'}")
print(f" β’ AIRAA enabled: {'β
' if service.airaa and service.airaa.enabled else 'β'}")
# Test a simple query
if service.enabled:
print(" π Testing query processing...")
response = await service.process_query("What is Bitcoin?")
if response.success:
print(" β
Query processing successful")
print(f" Response length: {len(response.response)} characters")
else:
print(f" β οΈ Query failed: {response.error}")
else:
print(" β οΈ Service disabled - limited testing")
return True
except Exception as e:
print(f"β Service test failed: {e}")
return False
def test_app_health():
"""Test FastAPI app health"""
print("π§ͺ Testing FastAPI app...")
try:
from fastapi.testclient import TestClient
from app import app
with TestClient(app) as client:
# Test health endpoint
response = client.get("/health")
if response.status_code == 200:
print(" β
Health endpoint")
else:
print(f" β Health endpoint failed: {response.status_code}")
# Test status endpoint
response = client.get("/status")
if response.status_code == 200:
print(" β
Status endpoint")
status_data = response.json()
print(f" Version: {status_data.get('version', 'Unknown')}")
else:
print(f" β Status endpoint failed: {response.status_code}")
# Test homepage
response = client.get("/")
if response.status_code == 200:
print(" β
Homepage endpoint")
else:
print(f" β Homepage failed: {response.status_code}")
return True
except Exception as e:
print(f"β FastAPI test failed: {e}")
return False
def run_performance_test():
"""Simple performance test"""
print("π§ͺ Performance test...")
try:
from src.visualizations import CryptoVisualizations
# Time visualization creation
start_time = time.time()
for i in range(10):
sample_data = {
'prices': [[1672531200000 + i*3600000, 16500 + i*10] for i in range(100)],
'total_volumes': [[1672531200000 + i*3600000, 1000000 + i*1000] for i in range(100)]
}
fig = CryptoVisualizations.create_price_chart(sample_data, 'TEST')
end_time = time.time()
avg_time = (end_time - start_time) / 10
print(f" β±οΈ Average chart creation: {avg_time:.3f}s")
if avg_time < 1.0:
print(" β
Performance acceptable")
return True
else:
print(" β οΈ Performance slow")
return True
except Exception as e:
print(f"β Performance test failed: {e}")
return False
async def main():
"""Run all tests"""
print("=" * 50)
print("π Web3 Research Co-Pilot - Test Suite")
print("=" * 50)
print()
test_results = []
# Run all tests
test_results.append(test_imports())
test_results.append(test_configuration())
test_results.append(test_visualizations())
test_results.append(test_tools())
test_results.append(await test_service())
test_results.append(test_app_health())
test_results.append(run_performance_test())
print()
print("=" * 50)
print("π Test Results Summary")
print("=" * 50)
passed = sum(test_results)
total = len(test_results)
print(f"Tests passed: {passed}/{total}")
print(f"Success rate: {(passed/total)*100:.1f}%")
if passed == total:
print("π All tests passed!")
return 0
else:
print("β οΈ Some tests failed")
return 1
if __name__ == "__main__":
exit_code = asyncio.run(main())
sys.exit(exit_code)
|