test-sprocket-complete.sh raw
1 #!/bin/bash
2
3 # Complete Sprocket Test Suite
4 # This script starts the relay with sprocket enabled and runs tests
5
6 set -e
7
8 echo "๐งช Complete Sprocket Test Suite"
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 # Start the relay with sprocket enabled
38 echo "๐ Starting relay with sprocket enabled..."
39 export ORLY_APP_NAME="ORLY_TEST"
40 export ORLY_DATA_DIR="/tmp/orly_test_data"
41 export ORLY_LISTEN="127.0.0.1"
42 export ORLY_PORT="$RELAY_PORT"
43 export ORLY_LOG_LEVEL="info"
44 export ORLY_SPROCKET_ENABLED="true"
45 export ORLY_ADMINS="npub1test1234567890abcdefghijklmnopqrstuvwxyz1234567890"
46 export ORLY_OWNERS="npub1test1234567890abcdefghijklmnopqrstuvwxyz1234567890"
47
48 # Clean up test data directory
49 rm -rf "$ORLY_DATA_DIR"
50 mkdir -p "$ORLY_DATA_DIR"
51
52 # Start relay in background
53 echo "Starting relay on port $RELAY_PORT..."
54 go run . test > /tmp/orly_test.log 2>&1 &
55 RELAY_PID=$!
56
57 # Wait for relay to start
58 echo "โณ Waiting for relay to start..."
59 sleep 5
60
61 # Check if relay is running
62 if ! kill -0 $RELAY_PID 2>/dev/null; then
63 echo "โ Relay failed to start"
64 echo "Log output:"
65 cat /tmp/orly_test.log
66 exit 1
67 fi
68
69 echo "โ
Relay started successfully (PID: $RELAY_PID)"
70
71 # Function to cleanup
72 cleanup() {
73 echo "๐งน Cleaning up..."
74 kill $RELAY_PID 2>/dev/null || true
75 sleep 2
76 pkill -f "ORLY_TEST" || true
77 rm -rf "$ORLY_DATA_DIR"
78 echo "โ
Cleanup complete"
79 }
80
81 # Set trap for cleanup
82 trap cleanup EXIT
83
84 # Test sprocket functionality
85 echo "๐งช Testing sprocket functionality..."
86
87 # Check if websocat is available
88 if ! command -v websocat &> /dev/null; then
89 echo "โ websocat is required for testing"
90 echo "Install it with: cargo install websocat"
91 echo "Or run: go install github.com/gorilla/websocket/examples/echo@latest"
92 exit 1
93 fi
94
95 # Test 1: Normal event (should be accepted)
96 echo "๐ค Test 1: Normal event (should be accepted)"
97 normal_event='{
98 "id": "test_normal_123",
99 "pubkey": "1234567890abcdef1234567890abcdef12345678",
100 "created_at": '$(date +%s)',
101 "kind": 1,
102 "content": "Hello, world! This is a normal message.",
103 "sig": "test_sig_normal"
104 }'
105
106 normal_message="[\"EVENT\",$normal_event]"
107 normal_response=$(echo "$normal_message" | websocat "ws://127.0.0.1:$RELAY_PORT" --text)
108 echo "Response: $normal_response"
109
110 if echo "$normal_response" | grep -q '"OK","test_normal_123",true'; then
111 echo "โ
Test 1 PASSED: Normal event accepted"
112 else
113 echo "โ Test 1 FAILED: Normal event not accepted"
114 fi
115
116 # Test 2: Spam content (should be rejected)
117 echo "๐ค Test 2: Spam content (should be rejected)"
118 spam_event='{
119 "id": "test_spam_456",
120 "pubkey": "1234567890abcdef1234567890abcdef12345678",
121 "created_at": '$(date +%s)',
122 "kind": 1,
123 "content": "This message contains spam content",
124 "sig": "test_sig_spam"
125 }'
126
127 spam_message="[\"EVENT\",$spam_event]"
128 spam_response=$(echo "$spam_message" | websocat "ws://127.0.0.1:$RELAY_PORT" --text)
129 echo "Response: $spam_response"
130
131 if echo "$spam_response" | grep -q '"OK","test_spam_456",false'; then
132 echo "โ
Test 2 PASSED: Spam content rejected"
133 else
134 echo "โ Test 2 FAILED: Spam content not rejected"
135 fi
136
137 # Test 3: Test kind 9999 (should be shadow rejected)
138 echo "๐ค Test 3: Test kind 9999 (should be shadow rejected)"
139 kind_event='{
140 "id": "test_kind_789",
141 "pubkey": "1234567890abcdef1234567890abcdef12345678",
142 "created_at": '$(date +%s)',
143 "kind": 9999,
144 "content": "Test message with special kind",
145 "sig": "test_sig_kind"
146 }'
147
148 kind_message="[\"EVENT\",$kind_event]"
149 kind_response=$(echo "$kind_message" | websocat "ws://127.0.0.1:$RELAY_PORT" --text)
150 echo "Response: $kind_response"
151
152 if echo "$kind_response" | grep -q '"OK","test_kind_789",true'; then
153 echo "โ
Test 3 PASSED: Test kind shadow rejected (OK=true but not processed)"
154 else
155 echo "โ Test 3 FAILED: Test kind not shadow rejected"
156 fi
157
158 # Test 4: Blocked hashtag (should be rejected)
159 echo "๐ค Test 4: Blocked hashtag (should be rejected)"
160 hashtag_event='{
161 "id": "test_hashtag_101",
162 "pubkey": "1234567890abcdef1234567890abcdef12345678",
163 "created_at": '$(date +%s)',
164 "kind": 1,
165 "content": "Message with blocked hashtag",
166 "tags": [["t", "blocked"]],
167 "sig": "test_sig_hashtag"
168 }'
169
170 hashtag_message="[\"EVENT\",$hashtag_event]"
171 hashtag_response=$(echo "$hashtag_message" | websocat "ws://127.0.0.1:$RELAY_PORT" --text)
172 echo "Response: $hashtag_response"
173
174 if echo "$hashtag_response" | grep -q '"OK","test_hashtag_101",false'; then
175 echo "โ
Test 4 PASSED: Blocked hashtag rejected"
176 else
177 echo "โ Test 4 FAILED: Blocked hashtag not rejected"
178 fi
179
180 # Test 5: Too long content (should be rejected)
181 echo "๐ค Test 5: Too long content (should be rejected)"
182 long_content=$(printf 'a%.0s' {1..1001})
183 long_event="{
184 \"id\": \"test_long_202\",
185 \"pubkey\": \"1234567890abcdef1234567890abcdef12345678\",
186 \"created_at\": $(date +%s),
187 \"kind\": 1,
188 \"content\": \"$long_content\",
189 \"sig\": \"test_sig_long\"
190 }"
191
192 long_message="[\"EVENT\",$long_event]"
193 long_response=$(echo "$long_message" | websocat "ws://127.0.0.1:$RELAY_PORT" --text)
194 echo "Response: $long_response"
195
196 if echo "$long_response" | grep -q '"OK","test_long_202",false'; then
197 echo "โ
Test 5 PASSED: Too long content rejected"
198 else
199 echo "โ Test 5 FAILED: Too long content not rejected"
200 fi
201
202 echo ""
203 echo "๐ Sprocket test suite completed!"
204 echo "๐ Check the results above to verify sprocket functionality"
205 echo ""
206 echo "๐ก To run individual tests, use:"
207 echo " ./test-sprocket-manual.sh"
208 echo ""
209 echo "๐ Relay logs are available at: /tmp/orly_test.log"
210