223 lines
6.9 KiB
Python
223 lines
6.9 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test script for Nexus API Client integration.
|
|
|
|
Tests:
|
|
1. NexusAPI singleton creation
|
|
2. Search methods (items, mobs, all)
|
|
3. PluginAPI integration
|
|
4. Plugin access via self.api.nexus_search()
|
|
"""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# Add project root to path
|
|
project_root = Path(__file__).parent.parent
|
|
if str(project_root) not in sys.path:
|
|
sys.path.insert(0, str(project_root))
|
|
|
|
from core.nexus_api import get_nexus_api, NexusAPI, search_items, search_mobs
|
|
from core.plugin_api import get_api, PluginAPI
|
|
|
|
|
|
def test_nexus_singleton():
|
|
"""Test that NexusAPI is a singleton."""
|
|
print("\n[Test 1] NexusAPI Singleton...")
|
|
api1 = get_nexus_api()
|
|
api2 = get_nexus_api()
|
|
|
|
assert api1 is api2, "NexusAPI should be singleton"
|
|
assert isinstance(api1, NexusAPI), "Should return NexusAPI instance"
|
|
print(" ✓ NexusAPI singleton works correctly")
|
|
return True
|
|
|
|
|
|
def test_plugin_api_nexus_methods():
|
|
"""Test PluginAPI has Nexus methods."""
|
|
print("\n[Test 2] PluginAPI Nexus Methods...")
|
|
plugin_api = get_api()
|
|
|
|
# Check methods exist
|
|
assert hasattr(plugin_api, 'register_nexus_service'), "Missing register_nexus_service"
|
|
assert hasattr(plugin_api, 'nexus_search'), "Missing nexus_search"
|
|
assert hasattr(plugin_api, 'nexus_get_item_details'), "Missing nexus_get_item_details"
|
|
assert hasattr(plugin_api, 'nexus_get_market_data'), "Missing nexus_get_market_data"
|
|
|
|
print(" ✓ PluginAPI has all Nexus methods")
|
|
return True
|
|
|
|
|
|
def test_nexus_service_registration():
|
|
"""Test Nexus service registration."""
|
|
print("\n[Test 3] Nexus Service Registration...")
|
|
plugin_api = get_api()
|
|
nexus = get_nexus_api()
|
|
|
|
# Register service
|
|
plugin_api.register_nexus_service(nexus)
|
|
|
|
# Verify it's in services
|
|
assert 'nexus' in plugin_api.services, "Nexus service not registered"
|
|
assert plugin_api.services['nexus'] is nexus, "Wrong service instance"
|
|
|
|
print(" ✓ Nexus service registered correctly")
|
|
return True
|
|
|
|
|
|
def test_nexus_search_via_plugin_api():
|
|
"""Test nexus_search through PluginAPI."""
|
|
print("\n[Test 4] Nexus Search via PluginAPI...")
|
|
plugin_api = get_api()
|
|
|
|
# This will return [] because we don't have actual API access in test
|
|
# but it shouldn't raise an exception
|
|
try:
|
|
results = plugin_api.nexus_search("ArMatrix", "items")
|
|
assert isinstance(results, list), "Should return list"
|
|
print(f" ✓ nexus_search returns list (got {len(results)} results)")
|
|
return True
|
|
except Exception as e:
|
|
print(f" ✗ nexus_search failed: {e}")
|
|
return False
|
|
|
|
|
|
def test_plugin_base_nexus_methods():
|
|
"""Test that BasePlugin would have nexus_search method."""
|
|
print("\n[Test 5] BasePlugin Nexus Access...")
|
|
|
|
# Import base plugin
|
|
from plugins.base_plugin import BasePlugin
|
|
|
|
# Check method exists
|
|
assert hasattr(BasePlugin, 'nexus_search'), "BasePlugin missing nexus_search"
|
|
assert hasattr(BasePlugin, 'nexus_get_item_details'), "BasePlugin missing nexus_get_item_details"
|
|
assert hasattr(BasePlugin, 'nexus_get_market_data'), "BasePlugin missing nexus_get_market_data"
|
|
|
|
print(" ✓ BasePlugin has all Nexus convenience methods")
|
|
return True
|
|
|
|
|
|
def test_nexus_api_configuration():
|
|
"""Test NexusAPI configuration."""
|
|
print("\n[Test 6] NexusAPI Configuration...")
|
|
nexus = get_nexus_api()
|
|
|
|
# Check configuration constants
|
|
assert nexus.BASE_URL == "https://api.entropianexus.com", "Wrong base URL"
|
|
assert nexus.API_VERSION == "v1", "Wrong API version"
|
|
assert nexus.MAX_REQUESTS_PER_SECOND == 5, "Wrong rate limit"
|
|
assert nexus.MAX_RETRIES == 3, "Wrong retry count"
|
|
|
|
print(f" ✓ Configuration correct:")
|
|
print(f" - Base URL: {nexus.BASE_URL}")
|
|
print(f" - Version: {nexus.API_VERSION}")
|
|
print(f" - Rate limit: {nexus.MAX_REQUESTS_PER_SECOND} req/sec")
|
|
print(f" - Max retries: {nexus.MAX_RETRIES}")
|
|
return True
|
|
|
|
|
|
def test_nexus_api_methods_exist():
|
|
"""Test all NexusAPI methods exist."""
|
|
print("\n[Test 7] NexusAPI Methods...")
|
|
nexus = get_nexus_api()
|
|
|
|
required_methods = [
|
|
'search_items',
|
|
'search_mobs',
|
|
'search_all',
|
|
'get_item_details',
|
|
'get_market_data',
|
|
'clear_cache',
|
|
'is_available'
|
|
]
|
|
|
|
for method in required_methods:
|
|
assert hasattr(nexus, method), f"Missing method: {method}"
|
|
assert callable(getattr(nexus, method)), f"Not callable: {method}"
|
|
|
|
print(f" ✓ All {len(required_methods)} required methods present")
|
|
return True
|
|
|
|
|
|
def simulate_plugin_usage():
|
|
"""Simulate how a plugin would use the Nexus API."""
|
|
print("\n[Test 8] Simulating Plugin Usage...")
|
|
|
|
# Create a mock plugin class
|
|
class MockPlugin:
|
|
def __init__(self):
|
|
self.api = get_api()
|
|
|
|
def search_items(self, query):
|
|
"""Plugin calls nexus_search via API."""
|
|
return self.api.nexus_search(query, "items")
|
|
|
|
def get_item_info(self, item_id):
|
|
"""Plugin gets item details."""
|
|
return self.api.nexus_get_item_details(item_id)
|
|
|
|
# Create plugin and test
|
|
plugin = MockPlugin()
|
|
|
|
# These should work without errors (returning empty/None since no API key)
|
|
try:
|
|
results = plugin.search_items("ArMatrix")
|
|
print(f" ✓ Plugin can call nexus_search (returned {len(results)} results)")
|
|
|
|
details = plugin.get_item_info("test_item")
|
|
print(f" ✓ Plugin can call nexus_get_item_details (returned {details})")
|
|
|
|
# Test the exact call from requirements
|
|
results2 = plugin.api.nexus_search("ArMatrix", "items")
|
|
print(f" ✓ Direct API call works: self.api.nexus_search('ArMatrix', 'items')")
|
|
|
|
return True
|
|
except Exception as e:
|
|
print(f" ✗ Plugin usage failed: {e}")
|
|
return False
|
|
|
|
|
|
def main():
|
|
"""Run all tests."""
|
|
print("=" * 60)
|
|
print("Nexus API Client Integration Tests")
|
|
print("=" * 60)
|
|
|
|
tests = [
|
|
test_nexus_singleton,
|
|
test_plugin_api_nexus_methods,
|
|
test_nexus_service_registration,
|
|
test_nexus_search_via_plugin_api,
|
|
test_plugin_base_nexus_methods,
|
|
test_nexus_api_configuration,
|
|
test_nexus_api_methods_exist,
|
|
simulate_plugin_usage
|
|
]
|
|
|
|
passed = 0
|
|
failed = 0
|
|
|
|
for test in tests:
|
|
try:
|
|
if test():
|
|
passed += 1
|
|
else:
|
|
failed += 1
|
|
except Exception as e:
|
|
print(f" ✗ Test failed with exception: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
failed += 1
|
|
|
|
print("\n" + "=" * 60)
|
|
print(f"Results: {passed} passed, {failed} failed")
|
|
print("=" * 60)
|
|
|
|
return failed == 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
success = main()
|
|
sys.exit(0 if success else 1)
|