test-sprocket-working.sh raw
1 #!/bin/bash
2
3 # Working Sprocket Test
4 # This script tests sprocket functionality with properly formatted messages
5
6 set -e
7
8 echo "๐งช Working Sprocket Test"
9 echo "======================="
10
11 # Configuration
12 RELAY_PORT="3335" # Use different port to avoid conflicts
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 # Test sprocket functionality with a simple Python WebSocket client
83 echo "๐งช Testing sprocket functionality..."
84
85 # Create a simple Python WebSocket test client
86 cat > /tmp/test_client.py << 'EOF'
87 #!/usr/bin/env python3
88 import asyncio
89 import websockets
90 import json
91 import time
92
93 async def test_sprocket():
94 uri = "ws://127.0.0.1:3335"
95
96 try:
97 async with websockets.connect(uri) as websocket:
98 print("โ
Connected to relay")
99
100 # Test 1: Normal event (should be accepted)
101 print("๐ค Test 1: Normal event (should be accepted)")
102 current_time = int(time.time())
103 normal_event = {
104 "id": "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
105 "pubkey": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
106 "created_at": current_time,
107 "kind": 1,
108 "content": "Hello, world! This is a normal message.",
109 "sig": "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
110 }
111
112 normal_message = ["EVENT", normal_event]
113 await websocket.send(json.dumps(normal_message))
114
115 response = await websocket.recv()
116 print(f"Response: {response}")
117
118 if '"OK","0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",true' in response:
119 print("โ
Test 1 PASSED: Normal event accepted")
120 else:
121 print("โ Test 1 FAILED: Normal event not accepted")
122
123 # Test 2: Spam content (should be rejected)
124 print("๐ค Test 2: Spam content (should be rejected)")
125 spam_event = {
126 "id": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
127 "pubkey": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
128 "created_at": current_time,
129 "kind": 1,
130 "content": "This message contains spam content",
131 "sig": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"
132 }
133
134 spam_message = ["EVENT", spam_event]
135 await websocket.send(json.dumps(spam_message))
136
137 response = await websocket.recv()
138 print(f"Response: {response}")
139
140 if '"OK","1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",false' in response:
141 print("โ
Test 2 PASSED: Spam content rejected")
142 else:
143 print("โ Test 2 FAILED: Spam content not rejected")
144
145 # Test 3: Test kind 9999 (should be shadow rejected)
146 print("๐ค Test 3: Test kind 9999 (should be shadow rejected)")
147 kind_event = {
148 "id": "2345678901bcdef01234567890abcdef01234567890abcdef01234567890abcdef",
149 "pubkey": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
150 "created_at": current_time,
151 "kind": 9999,
152 "content": "Test message with special kind",
153 "sig": "2345678901bcdef01234567890abcdef01234567890abcdef01234567890abcdef2345678901bcdef01234567890abcdef01234567890abcdef01234567890abcdef"
154 }
155
156 kind_message = ["EVENT", kind_event]
157 await websocket.send(json.dumps(kind_message))
158
159 response = await websocket.recv()
160 print(f"Response: {response}")
161
162 if '"OK","2345678901bcdef01234567890abcdef01234567890abcdef01234567890abcdef",true' in response:
163 print("โ
Test 3 PASSED: Test kind shadow rejected (OK=true but not processed)")
164 else:
165 print("โ Test 3 FAILED: Test kind not shadow rejected")
166
167 # Test 4: Blocked hashtag (should be rejected)
168 print("๐ค Test 4: Blocked hashtag (should be rejected)")
169 hashtag_event = {
170 "id": "3456789012cdef0123456789012cdef0123456789012cdef0123456789012cdef",
171 "pubkey": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
172 "created_at": current_time,
173 "kind": 1,
174 "content": "Message with blocked hashtag",
175 "tags": [["t", "blocked"]],
176 "sig": "3456789012cdef0123456789012cdef0123456789012cdef0123456789012cdef3456789012cdef0123456789012cdef0123456789012cdef0123456789012cdef"
177 }
178
179 hashtag_message = ["EVENT", hashtag_event]
180 await websocket.send(json.dumps(hashtag_message))
181
182 response = await websocket.recv()
183 print(f"Response: {response}")
184
185 if '"OK","3456789012cdef0123456789012cdef0123456789012cdef0123456789012cdef",false' in response:
186 print("โ
Test 4 PASSED: Blocked hashtag rejected")
187 else:
188 print("โ Test 4 FAILED: Blocked hashtag not rejected")
189
190 except Exception as e:
191 print(f"โ Error: {e}")
192
193 if __name__ == "__main__":
194 asyncio.run(test_sprocket())
195 EOF
196
197 # Check if websockets is available
198 if ! python3 -c "import websockets" 2>/dev/null; then
199 echo "๐ฆ Installing websockets library..."
200 pip3 install websockets
201 fi
202
203 # Run the test
204 python3 /tmp/test_client.py
205
206 echo ""
207 echo "๐ Sprocket integration test completed!"
208 echo "๐ Relay logs are available at: /tmp/orly_test.log"
209 echo "๐ก To view logs: cat /tmp/orly_test.log"
210