#!/usr/bin/env python3
"""Test script to verify dual-mode operation of Opinion.trade MCP server."""
import os
import sys
from pathlib import Path
# Add package to path
sys.path.insert(0, str(Path(__file__).parent / "src"))
from mcp_opinion import OpinionConfig, create_opinion_server
from mcp_opinion.config import OpinionConfig
def test_read_only_mode():
"""Test read-only mode (API key only)."""
print("=" * 60)
print("TEST 1: Read-Only Mode (No Private Key)")
print("=" * 60)
# Create config without private key
config = OpinionConfig(
api_key="test_api_key_12345",
private_key=None
)
print(f"✓ Config created: {config.get_mode_description()}")
print(f" - API Key: {config.api_key[:20]}...")
print(f" - Private Key: {config.private_key}")
print(f" - Trading Enabled: {config.enable_trading}")
print(f" - Chain ID: {config.chain_id}")
print(f" - API Host: {config.api_host}")
# Create server
server = create_opinion_server(config)
print(f"✓ Server created: {server.name}")
# Check that public client exists
if hasattr(server, '_public_client'):
print("✓ Public client initialized")
else:
print("✗ Public client missing")
return False
# Check that trading client does NOT exist
if hasattr(server, '_trading_client') and server._trading_client is None:
print("✓ Trading client correctly disabled (None)")
else:
print("✗ Trading client should be None in read-only mode")
return False
print("\n✅ Read-only mode test PASSED\n")
return True
def test_trading_mode():
"""Test trading mode (API key + private key)."""
print("=" * 60)
print("TEST 2: Trading Mode (With Private Key)")
print("=" * 60)
# Create config with private key (test key, not real)
test_private_key = "0x" + "a" * 64 # Fake 32-byte hex private key
config = OpinionConfig(
api_key="test_api_key_12345",
private_key=test_private_key
)
print(f"✓ Config created: {config.get_mode_description()}")
print(f" - API Key: {config.api_key[:20]}...")
print(f" - Private Key: {config.private_key[:20]}...")
print(f" - Trading Enabled: {config.enable_trading}")
print(f" - Chain ID: {config.chain_id}")
# Create server (trading client init may fail without real SDK, that's OK)
server = create_opinion_server(config)
print(f"✓ Server created: {server.name}")
# Check that public client exists
if hasattr(server, '_public_client'):
print("✓ Public client initialized")
else:
print("✗ Public client missing")
return False
# Trading client may be None if SDK init failed, but that's expected in test
if hasattr(server, '_trading_client'):
if server._trading_client:
print("✓ Trading client initialized (SDK available)")
else:
print("⚠ Trading client is None (SDK init failed - expected in test environment)")
else:
print("✗ Trading client attribute missing")
return False
print("\n✅ Trading mode test PASSED\n")
return True
def test_config_validation():
"""Test configuration validation."""
print("=" * 60)
print("TEST 3: Configuration Validation")
print("=" * 60)
# Test missing API key
try:
config = OpinionConfig(api_key="")
print("✗ Empty API key should fail validation")
return False
except ValueError as e:
print(f"✓ Empty API key rejected: {e}")
# Test invalid chain ID
try:
config = OpinionConfig(api_key="test", chain_id=-1)
print("✗ Invalid chain ID should fail validation")
return False
except ValueError as e:
print(f"✓ Invalid chain ID rejected: {e}")
# Test valid config
config = OpinionConfig(api_key="valid_key", chain_id=56)
print(f"✓ Valid config accepted: Chain ID {config.chain_id}")
print("\n✅ Configuration validation test PASSED\n")
return True
def main():
"""Run all tests."""
print("\n" + "=" * 60)
print("Opinion.trade MCP Server - Dual-Mode Testing")
print("=" * 60 + "\n")
results = []
# Run tests
results.append(("Read-Only Mode", test_read_only_mode()))
results.append(("Trading Mode", test_trading_mode()))
results.append(("Config Validation", test_config_validation()))
# Print summary
print("=" * 60)
print("TEST SUMMARY")
print("=" * 60)
for test_name, passed in results:
status = "✅ PASSED" if passed else "❌ FAILED"
print(f"{test_name:.<40} {status}")
all_passed = all(result[1] for result in results)
if all_passed:
print("\n🎉 ALL TESTS PASSED!")
return 0
else:
print("\n❌ SOME TESTS FAILED")
return 1
if __name__ == "__main__":
sys.exit(main())