test-sprocket-final.sh raw

   1  #!/bin/bash
   2  
   3  # Final Sprocket Integration Test
   4  # This script tests the complete sprocket integration with the relay
   5  
   6  set -e
   7  
   8  echo "๐Ÿงช Final Sprocket Integration Test"
   9  echo "================================="
  10  
  11  # Configuration
  12  RELAY_PORT="3334"
  13  TEST_CONFIG_DIR="$HOME/.config/ORLY_TEST"
  14  
  15  # Clean up any existing test processes
  16  echo "๐Ÿงน Cleaning up existing processes..."
  17  pkill -f "ORLY_TEST" || true
  18  sleep 2
  19  
  20  # Create test configuration directory
  21  echo "๐Ÿ“ Setting up test environment..."
  22  mkdir -p "$TEST_CONFIG_DIR"
  23  
  24  # Copy the Python sprocket script
  25  cp test-sprocket.py "$TEST_CONFIG_DIR/sprocket.py"
  26  
  27  # Create bash wrapper for the Python script
  28  cat > "$TEST_CONFIG_DIR/sprocket.sh" << 'EOF'
  29  #!/bin/bash
  30  python3 "$(dirname "$0")/sprocket.py"
  31  EOF
  32  
  33  chmod +x "$TEST_CONFIG_DIR/sprocket.sh"
  34  
  35  echo "โœ… Sprocket script created at: $TEST_CONFIG_DIR/sprocket.sh"
  36  
  37  # Set environment variables for the relay
  38  export ORLY_APP_NAME="ORLY_TEST"
  39  export ORLY_DATA_DIR="/tmp/orly_test_data"
  40  export ORLY_LISTEN="127.0.0.1"
  41  export ORLY_PORT="$RELAY_PORT"
  42  export ORLY_LOG_LEVEL="info"
  43  export ORLY_SPROCKET_ENABLED="true"
  44  export ORLY_ADMINS=""
  45  export ORLY_OWNERS=""
  46  
  47  # Clean up test data directory
  48  rm -rf "$ORLY_DATA_DIR"
  49  mkdir -p "$ORLY_DATA_DIR"
  50  
  51  # Function to cleanup
  52  cleanup() {
  53      echo "๐Ÿงน Cleaning up..."
  54      pkill -f "ORLY_TEST" || true
  55      sleep 2
  56      rm -rf "$ORLY_DATA_DIR"
  57      echo "โœ… Cleanup complete"
  58  }
  59  
  60  # Set trap for cleanup
  61  trap cleanup EXIT
  62  
  63  # Start the relay
  64  echo "๐Ÿš€ Starting relay with sprocket enabled..."
  65  go run . test > /tmp/orly_test.log 2>&1 &
  66  RELAY_PID=$!
  67  
  68  # Wait for relay to start
  69  echo "โณ Waiting for relay to start..."
  70  sleep 5
  71  
  72  # Check if relay is running
  73  if ! kill -0 $RELAY_PID 2>/dev/null; then
  74      echo "โŒ Relay failed to start"
  75      echo "Log output:"
  76      cat /tmp/orly_test.log
  77      exit 1
  78  fi
  79  
  80  echo "โœ… Relay started successfully (PID: $RELAY_PID)"
  81  
  82  # Check if websocat is available
  83  if ! command -v websocat &> /dev/null; then
  84      echo "โŒ websocat is required for testing"
  85      echo "Install it with: cargo install websocat"
  86      echo "Or use: go install github.com/gorilla/websocket/examples/echo@latest"
  87      exit 1
  88  fi
  89  
  90  # Test sprocket functionality
  91  echo "๐Ÿงช Testing sprocket functionality..."
  92  
  93  # Test 1: Normal event (should be accepted)
  94  echo "๐Ÿ“ค Test 1: Normal event (should be accepted)"
  95  current_time=$(date +%s)
  96  normal_event="{
  97      \"id\": \"test_normal_123\",
  98      \"pubkey\": \"1234567890abcdef1234567890abcdef12345678\",
  99      \"created_at\": $current_time,
 100      \"kind\": 1,
 101      \"content\": \"Hello, world! This is a normal message.\",
 102      \"sig\": \"test_sig_normal\"
 103  }"
 104  
 105  normal_message="[\"EVENT\",$normal_event]"
 106  normal_response=$(echo "$normal_message" | websocat "ws://127.0.0.1:$RELAY_PORT" --text)
 107  echo "Response: $normal_response"
 108  
 109  if echo "$normal_response" | grep -q '"OK","test_normal_123",true'; then
 110      echo "โœ… Test 1 PASSED: Normal event accepted"
 111  else
 112      echo "โŒ Test 1 FAILED: Normal event not accepted"
 113  fi
 114  
 115  # Test 2: Spam content (should be rejected)
 116  echo "๐Ÿ“ค Test 2: Spam content (should be rejected)"
 117  spam_event="{
 118      \"id\": \"test_spam_456\",
 119      \"pubkey\": \"1234567890abcdef1234567890abcdef12345678\",
 120      \"created_at\": $current_time,
 121      \"kind\": 1,
 122      \"content\": \"This message contains spam content\",
 123      \"sig\": \"test_sig_spam\"
 124  }"
 125  
 126  spam_message="[\"EVENT\",$spam_event]"
 127  spam_response=$(echo "$spam_message" | websocat "ws://127.0.0.1:$RELAY_PORT" --text)
 128  echo "Response: $spam_response"
 129  
 130  if echo "$spam_response" | grep -q '"OK","test_spam_456",false'; then
 131      echo "โœ… Test 2 PASSED: Spam content rejected"
 132  else
 133      echo "โŒ Test 2 FAILED: Spam content not rejected"
 134  fi
 135  
 136  # Test 3: Test kind 9999 (should be shadow rejected)
 137  echo "๐Ÿ“ค Test 3: Test kind 9999 (should be shadow rejected)"
 138  kind_event="{
 139      \"id\": \"test_kind_789\",
 140      \"pubkey\": \"1234567890abcdef1234567890abcdef12345678\",
 141      \"created_at\": $current_time,
 142      \"kind\": 9999,
 143      \"content\": \"Test message with special kind\",
 144      \"sig\": \"test_sig_kind\"
 145  }"
 146  
 147  kind_message="[\"EVENT\",$kind_event]"
 148  kind_response=$(echo "$kind_message" | websocat "ws://127.0.0.1:$RELAY_PORT" --text)
 149  echo "Response: $kind_response"
 150  
 151  if echo "$kind_response" | grep -q '"OK","test_kind_789",true'; then
 152      echo "โœ… Test 3 PASSED: Test kind shadow rejected (OK=true but not processed)"
 153  else
 154      echo "โŒ Test 3 FAILED: Test kind not shadow rejected"
 155  fi
 156  
 157  # Test 4: Blocked hashtag (should be rejected)
 158  echo "๐Ÿ“ค Test 4: Blocked hashtag (should be rejected)"
 159  hashtag_event="{
 160      \"id\": \"test_hashtag_101\",
 161      \"pubkey\": \"1234567890abcdef1234567890abcdef12345678\",
 162      \"created_at\": $current_time,
 163      \"kind\": 1,
 164      \"content\": \"Message with blocked hashtag\",
 165      \"tags\": [[\"t\", \"blocked\"]],
 166      \"sig\": \"test_sig_hashtag\"
 167  }"
 168  
 169  hashtag_message="[\"EVENT\",$hashtag_event]"
 170  hashtag_response=$(echo "$hashtag_message" | websocat "ws://127.0.0.1:$RELAY_PORT" --text)
 171  echo "Response: $hashtag_response"
 172  
 173  if echo "$hashtag_response" | grep -q '"OK","test_hashtag_101",false'; then
 174      echo "โœ… Test 4 PASSED: Blocked hashtag rejected"
 175  else
 176      echo "โŒ Test 4 FAILED: Blocked hashtag not rejected"
 177  fi
 178  
 179  echo ""
 180  echo "๐ŸŽ‰ Sprocket integration test completed!"
 181  echo "๐Ÿ“Š Check the results above to verify sprocket functionality"
 182  echo ""
 183  echo "๐Ÿ“ Relay logs are available at: /tmp/orly_test.log"
 184  echo "๐Ÿ’ก To view logs: cat /tmp/orly_test.log"
 185