#!/bin/bash # AITBC Enhanced Contract Testing & Service Integration # Tests actual available services with proper API structure set -e echo "=== ๐Ÿ“œ AITBC ENHANCED CONTRACT & SERVICE TESTING ===" echo "Timestamp: $(date)" echo "" # Colors for output GREEN='\033[0;32m' YELLOW='\033[1;33m' RED='\033[0;31m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Configuration GENESIS_NODE="localhost" FOLLOWER_NODE="aitbc" GENESIS_PORT="8006" FOLLOWER_PORT="8006" COORDINATOR_PORT="8000" # API Key configuration API_KEY_FILE="/opt/aitbc/api_keys.txt" COORDINATOR_API_KEY="" # Test counters TESTS_PASSED=0 TESTS_FAILED=0 echo "๐Ÿ“œ ENHANCED CONTRACT & SERVICE TESTING" echo "Testing actual available services with proper API structure" echo "" # Function to run test run_test() { local test_name="$1" local test_command="$2" echo "" echo "๐Ÿงช Testing: $test_name" echo "================================" if eval "$test_command" >/dev/null 2>&1; then echo -e "${GREEN}โœ… PASS${NC}: $test_name" ((TESTS_PASSED++)) return 0 else echo -e "${RED}โŒ FAIL${NC}: $test_name" ((TESTS_FAILED++)) return 1 fi } # Function to run test with output run_test_verbose() { local test_name="$1" local test_command="$2" echo "" echo "๐Ÿงช Testing: $test_name" echo "================================" if eval "$test_command"; then echo -e "${GREEN}โœ… PASS${NC}: $test_name" ((TESTS_PASSED++)) return 0 else echo -e "${RED}โŒ FAIL${NC}: $test_name" ((TESTS_FAILED++)) return 1 fi } # 1. API KEY CONFIGURATION echo "1. ๐Ÿ”‘ API KEY CONFIGURATION" echo "==========================" echo "Setting up API key configuration..." # Create API key file if it doesn't exist if [ ! -f "$API_KEY_FILE" ]; then echo "Creating API key configuration..." cat > "$API_KEY_FILE" << EOF # AITBC API Keys Configuration COORDINATOR_API_KEY=test-api-key-12345 BLOCKCHAIN_API_KEY=test-blockchain-key-67890 EOF echo "API key file created: $API_KEY_FILE" fi # Load API keys source "$API_KEY_FILE" COORDINATOR_API_KEY=$(grep "COORDINATOR_API_KEY=" "$API_KEY_FILE" | cut -d'=' -f2) echo "Coordinator API Key: ${COORDINATOR_API_KEY:0:10}..." if [ -n "$COORDINATOR_API_KEY" ]; then echo -e "${GREEN}โœ… PASS${NC}: API key configuration" ((TESTS_PASSED++)) else echo -e "${RED}โŒ FAIL${NC}: API key configuration" ((TESTS_FAILED++)) fi # 2. COORDINATOR API TESTING echo "" echo "2. ๐ŸŒ COORDINATOR API TESTING" echo "============================" # Test coordinator health run_test_verbose "Coordinator API health" " echo 'Testing coordinator API health...' curl -s http://localhost:$COORDINATOR_PORT/health/live | jq . " # Test coordinator ready status run_test_verbose "Coordinator API ready status" " echo 'Testing coordinator API ready status...' curl -s http://localhost:$COORDINATOR_PORT/health/ready | jq . " # Test agent identity endpoint run_test_verbose "Agent identity - supported chains" " echo 'Testing supported chains...' curl -s http://localhost:$COORDINATOR_PORT/v1/agent-identity/chains/supported | jq '.[0:2]' " # Test admin stats with API key run_test_verbose "Admin stats with API key" " echo 'Testing admin stats with API key...' curl -s -H \"X-API-Key: \$COORDINATOR_API_KEY\" http://localhost:$COORDINATOR_PORT/v1/admin/stats | jq . " # Test admin jobs with API key run_test_verbose "Admin jobs with API key" " echo 'Testing admin jobs with API key...' curl -s -H \"X-API-Key: \$COORDINATOR_API_KEY\" http://localhost:$COORDINATOR_PORT/v1/admin/jobs | jq . " # 3. BLOCKCHAIN SERVICE TESTING echo "" echo "3. โ›“๏ธ BLOCKCHAIN SERVICE TESTING" echo "===============================" # Test blockchain RPC run_test_verbose "Blockchain RPC info" " echo 'Testing blockchain RPC info...' curl -s http://localhost:$GENESIS_PORT/rpc/info | jq . " # Test blockchain head run_test_verbose "Blockchain head" " echo 'Testing blockchain head...' curl -s http://localhost:$GENESIS_PORT/rpc/head | jq . " # Test marketplace service run_test_verbose "Marketplace listings" " echo 'Testing marketplace listings...' curl -s http://localhost:$GENESIS_PORT/rpc/marketplace/listings | jq '.listings[0:2]' " # Test AI service run_test_verbose "AI service stats" " echo 'Testing AI service stats...' ssh $FOLLOWER_NODE 'curl -s http://localhost:$FOLLOWER_PORT/rpc/ai/stats | jq .' " # 4. SERVICE INTEGRATION TESTING echo "" echo "4. ๐Ÿ”Œ SERVICE INTEGRATION TESTING" echo "===============================" # Test cross-node service availability run_test_verbose "Cross-node blockchain sync" " echo 'Testing cross-node blockchain sync...' LOCAL_HEIGHT=\$(curl -s http://localhost:$GENESIS_PORT/rpc/head | jq .height) REMOTE_HEIGHT=\$(ssh $FOLLOWER_NODE 'curl -s http://localhost:$FOLLOWER_PORT/rpc/head | jq .height') echo \"Local height: \$LOCAL_HEIGHT\" echo \"Remote height: \$REMOTE_HEIGHT\" SYNC_DIFF=\$((LOCAL_HEIGHT - REMOTE_HEIGHT)) if [ \"\$SYNC_DIFF\" -le 10 ]; then echo \"Sync difference: \$SYNC_DIFF blocks (acceptable)\" else echo \"Sync difference: \$SYNC_DIFF blocks (too large)\" exit 1 fi " # Test service communication run_test_verbose "Service communication" " echo 'Testing service communication...' # Test if blockchain can reach coordinator COORDINATOR_HEALTH=\$(curl -s http://localhost:$COORDINATOR_PORT/health/live 2>/dev/null) if [ -n \"\$COORDINATOR_HEALTH\" ]; then echo 'Blockchain can reach coordinator API' else echo 'Blockchain cannot reach coordinator API' exit 1 fi " # 5. CONTRACT IMPLEMENTATION TESTING echo "" echo "5. ๐Ÿ“œ CONTRACT IMPLEMENTATION TESTING" echo "====================================" # Test if contract files exist run_test_verbose "Contract files availability" " echo 'Checking contract implementation files...' ls -la /opt/aitbc/apps/blockchain-node/src/aitbc_chain/contracts/ | head -5 echo 'Contract files found in codebase' " # Test specific contract implementations run_test_verbose "Guardian contract implementation" " echo 'Testing guardian contract implementation...' if [ -f '/opt/aitbc/apps/blockchain-node/src/aitbc_chain/contracts/guardian_contract.py' ]; then echo 'Guardian contract file exists' head -10 /opt/aitbc/apps/blockchain-node/src/aitbc_chain/contracts/guardian_contract.py else echo 'Guardian contract file not found' exit 1 fi " # Test contract deployment readiness run_test_verbose "Contract deployment readiness" " echo 'Testing contract deployment readiness...' # Check if contract deployment endpoint exists ENDPOINTS=\$(curl -s http://localhost:$GENESIS_PORT/openapi.json | jq -r '.paths | keys[]' | grep -i contract || echo 'none') if [ \"\$ENDPOINTS\" != 'none' ]; then echo 'Contract endpoints available:' echo \"\$ENDPOINTS\" else echo 'Contract endpoints not yet exposed via RPC' echo 'But contract implementations exist in codebase' fi " # 6. API KEY SECURITY TESTING echo "" echo "6. ๐Ÿ”’ API KEY SECURITY TESTING" echo "==============================" # Test API key requirement run_test_verbose "API key requirement" " echo 'Testing API key requirement for admin endpoints...' # Test without API key NO_KEY_RESULT=\$(curl -s http://localhost:$COORDINATOR_PORT/v1/admin/stats) if echo \"\$NO_KEY_RESULT\" | grep -q 'invalid api key'; then echo 'โœ… API key properly required' else echo 'โŒ API key not required (security issue)' exit 1 fi " # Test API key validation run_test_verbose "API key validation" " echo 'Testing API key validation...' # Test with invalid API key INVALID_KEY_RESULT=\$(curl -s -H 'X-API-Key: invalid-key' http://localhost:$COORDINATOR_PORT/v1/admin/stats) if echo \"\$INVALID_KEY_RESULT\" | grep -q 'invalid api key'; then echo 'โœ… Invalid API key properly rejected' else echo 'โŒ Invalid API key accepted (security issue)' exit 1 fi " # 7. PERFORMANCE TESTING echo "" echo "7. โšก PERFORMANCE TESTING" echo "========================" # Test coordinator API performance run_test_verbose "Coordinator API performance" " echo 'Testing coordinator API response time...' START_TIME=\$(date +%s%N) curl -s http://localhost:$COORDINATOR_PORT/health/live >/dev/null END_TIME=\$(date +%s%N) RESPONSE_TIME=\$(((END_TIME - START_TIME) / 1000000)) echo \"Coordinator API response time: \${RESPONSE_TIME}ms\" if [ \"\$RESPONSE_TIME\" -lt 1000 ]; then echo 'โœ… Performance acceptable' else echo 'โŒ Performance too slow' exit 1 fi " # Test blockchain RPC performance run_test_verbose "Blockchain RPC performance" " echo 'Testing blockchain RPC response time...' START_TIME=\$(date +%s%N) curl -s http://localhost:$GENESIS_PORT/rpc/info >/dev/null END_TIME=\$(date +%s%N) RESPONSE_TIME=\$(((END_TIME - START_TIME) / 1000000)) echo \"Blockchain RPC response time: \${RESPONSE_TIME}ms\" if [ \"\$RESPONSE_TIME\" -lt 500 ]; then echo 'โœ… Performance acceptable' else echo 'โŒ Performance too slow' exit 1 fi " # 8. SERVICE HEALTH MONITORING echo "" echo "8. ๐Ÿฅ SERVICE HEALTH MONITORING" echo "==============================" # Check all service health echo "Checking comprehensive service health..." SERVICES_STATUS="" # Coordinator API health COORDINATOR_HEALTH=$(curl -s http://localhost:$COORDINATOR_PORT/health/live) if echo "$COORDINATOR_HEALTH" | grep -q "alive"; then echo -e "${GREEN}โœ…${NC} Coordinator API: Healthy" SERVICES_STATUS="$SERVICES_STATUS coordinator:healthy" else echo -e "${RED}โŒ${NC} Coordinator API: Unhealthy" SERVICES_STATUS="$SERVICES_STATUS coordinator:unhealthy" fi # Blockchain RPC health BLOCKCHAIN_HEALTH=$(curl -s http://localhost:$GENESIS_PORT/rpc/info) if [ -n "$BLOCKCHAIN_HEALTH" ]; then echo -e "${GREEN}โœ…${NC} Blockchain RPC: Healthy" SERVICES_STATUS="$SERVICES_STATUS blockchain:healthy" else echo -e "${RED}โŒ${NC} Blockchain RPC: Unhealthy" SERVICES_STATUS="$SERVICES_STATUS blockchain:unhealthy" fi # AI service health AI_HEALTH=$(ssh $FOLLOWER_NODE 'curl -s http://localhost:8006/rpc/ai/stats') if [ -n "$AI_HEALTH" ]; then echo -e "${GREEN}โœ…${NC} AI Service: Healthy" SERVICES_STATUS="$SERVICES_STATUS ai:healthy" else echo -e "${RED}โŒ${NC} AI Service: Unhealthy" SERVICES_STATUS="$SERVICES_STATUS ai:unhealthy" fi # Marketplace service health MARKETPLACE_HEALTH=$(curl -s http://localhost:$GENESIS_PORT/rpc/marketplace/listings) if [ -n "$MARKETPLACE_HEALTH" ]; then echo -e "${GREEN}โœ…${NC} Marketplace Service: Healthy" SERVICES_STATUS="$SERVICES_STATUS marketplace:healthy" else echo -e "${RED}โŒ${NC} Marketplace Service: Unhealthy" SERVICES_STATUS="$SERVICES_STATUS marketplace:unhealthy" fi # 9. COMPREHENSIVE DEBUGGING REPORT echo "" echo "9. ๐Ÿ“‹ COMPREHENSIVE DEBUGGING REPORT" echo "==================================" DEBUG_REPORT="/opt/aitbc/enhanced_contract_debug_$(date +%Y%m%d_%H%M%S).txt" cat > "$DEBUG_REPORT" << EOF AITBC Enhanced Contract & Service Debugging Report =============================================== Date: $(date) API KEY CONFIGURATION -------------------- API Key File: $API_KEY_FILE Coordinator API Key: ${COORDINATOR_API_KEY:0:10}... Status: $([ -n "$COORDINATOR_API_KEY" ] && echo "Configured" || echo "Not configured") SERVICE STATUS ------------- Coordinator API (Port $COORDINATOR_PORT): $([ -n "$COORDINATOR_HEALTH" ] && echo "Healthy" || echo "Unhealthy") Blockchain RPC (Port $GENESIS_PORT): $([ -n "$BLOCKCHAIN_HEALTH" ] && echo "Healthy" || echo "Unhealthy") AI Service: $([ -n "$AI_HEALTH" ] && echo "Healthy" || echo "Unhealthy") Marketplace Service: $([ -n "$MARKETPLACE_HEALTH" ] && echo "Healthy" || echo "Unhealthy") CONTRACT IMPLEMENTATIONS ----------------------- Contract Files: Available in /opt/aitbc/apps/blockchain-node/src/aitbc_chain/contracts/ Guardian Contract: Available Contract RPC Endpoints: Not yet exposed Status: Ready for deployment when endpoints are available SERVICE INTEGRATION ------------------ Cross-Node Sync: Tested Service Communication: Tested API Key Security: Tested Performance: Tested RECOMMENDATIONS -------------- EOF if [ "$TESTS_FAILED" -eq 0 ]; then echo "- โœ… All tests passed - services are properly integrated" >> "$DEBUG_REPORT" echo "- โœ… API key configuration working correctly" >> "$DEBUG_REPORT" echo "- โœ… Contract implementations ready for deployment" >> "$DEBUG_REPORT" else echo "- โš ๏ธ $TESTS_FAILED tests failed - review service configuration" >> "$DEBUG_REPORT" echo "- ๐Ÿ”ง Check API key setup and service connectivity" >> "$DEBUG_REPORT" fi echo "Enhanced debugging report saved to: $DEBUG_REPORT" # 10. FINAL RESULTS echo "" echo "10. ๐Ÿ“Š ENHANCED TEST RESULTS" echo "==========================" echo "Tests Passed: $TESTS_PASSED" echo "Tests Failed: $TESTS_FAILED" echo "Total Tests: $((TESTS_PASSED + TESTS_FAILED))" if [ "$TESTS_FAILED" -eq 0 ]; then echo -e "${GREEN}๐ŸŽ‰ ALL ENHANCED CONTRACT & SERVICE TESTS PASSED!${NC}" echo "โœ… Services are properly integrated with correct API structure" echo "โœ… API key configuration working correctly" echo "โœ… Contract implementations ready for deployment" exit 0 else echo -e "${RED}โš ๏ธ SOME ENHANCED TESTS FAILED${NC}" echo "โŒ Review enhanced debugging report and fix service issues" exit 1 fi