Basic relay functionality completed
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Simple C-Relay Test - Create type 1 event and upload to relay
|
||||
# Uses nak to generate and publish a single event
|
||||
# Comprehensive C-Relay Test - Test event types and subscriptions
|
||||
# Uses nak to generate and publish various event types, then tests subscriptions
|
||||
|
||||
set -e # Exit on any error
|
||||
|
||||
@@ -16,7 +16,6 @@ RESET='\033[0m'
|
||||
# Test configuration
|
||||
RELAY_URL="ws://127.0.0.1:8888"
|
||||
TEST_PRIVATE_KEY="nsec1j4c6269y9w0q2er2xjw8sv2ehyrtfxq3jwgdlxj6qfn8z4gjsq5qfvfk99"
|
||||
TEST_CONTENT="Hello from C-Relay test!"
|
||||
|
||||
# Print functions
|
||||
print_header() {
|
||||
@@ -39,74 +38,244 @@ print_info() {
|
||||
echo -e "${BLUE}[INFO]${RESET} $1"
|
||||
}
|
||||
|
||||
# Main test function
|
||||
run_test() {
|
||||
print_header "C-Relay Simple Test"
|
||||
print_warning() {
|
||||
echo -e "${YELLOW}[WARNING]${RESET} $1"
|
||||
}
|
||||
|
||||
# Global arrays to store event IDs for subscription tests
|
||||
declare -a REGULAR_EVENT_IDS=()
|
||||
declare -a REPLACEABLE_EVENT_IDS=()
|
||||
declare -a EPHEMERAL_EVENT_IDS=()
|
||||
declare -a ADDRESSABLE_EVENT_IDS=()
|
||||
|
||||
# Helper function to publish event and extract ID
|
||||
publish_event() {
|
||||
local event_json="$1"
|
||||
local event_type="$2"
|
||||
local description="$3"
|
||||
|
||||
# Check if nak is available
|
||||
# Extract event ID
|
||||
local event_id=$(echo "$event_json" | jq -r '.id' 2>/dev/null)
|
||||
if [[ "$event_id" == "null" || -z "$event_id" ]]; then
|
||||
print_error "Could not extract event ID from $description"
|
||||
return 1
|
||||
fi
|
||||
|
||||
print_info "Publishing $description..."
|
||||
|
||||
# Create EVENT message in Nostr format
|
||||
local event_message="[\"EVENT\",$event_json]"
|
||||
|
||||
# Publish to relay
|
||||
local response=""
|
||||
if command -v websocat &> /dev/null; then
|
||||
response=$(echo "$event_message" | timeout 5s websocat "$RELAY_URL" 2>&1 || echo "Connection failed")
|
||||
else
|
||||
print_error "websocat not found - required for testing"
|
||||
return 1
|
||||
fi
|
||||
|
||||
|
||||
# Check response
|
||||
if [[ "$response" == *"Connection failed"* ]]; then
|
||||
print_error "Failed to connect to relay for $description"
|
||||
return 1
|
||||
elif [[ "$response" == *"true"* ]]; then
|
||||
print_success "$description uploaded (ID: ${event_id:0:16}...)"
|
||||
|
||||
# Store event ID in appropriate array
|
||||
case "$event_type" in
|
||||
"regular") REGULAR_EVENT_IDS+=("$event_id") ;;
|
||||
"replaceable") REPLACEABLE_EVENT_IDS+=("$event_id") ;;
|
||||
"ephemeral") EPHEMERAL_EVENT_IDS+=("$event_id") ;;
|
||||
"addressable") ADDRESSABLE_EVENT_IDS+=("$event_id") ;;
|
||||
esac
|
||||
echo # Add blank line for readability
|
||||
return 0
|
||||
else
|
||||
print_warning "$description might have failed: $response"
|
||||
echo # Add blank line for readability
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Test subscription with filters
|
||||
test_subscription() {
|
||||
local sub_id="$1"
|
||||
local filter="$2"
|
||||
local description="$3"
|
||||
local expected_count="$4"
|
||||
|
||||
print_step "Testing subscription: $description"
|
||||
|
||||
# Create REQ message
|
||||
local req_message="[\"REQ\",\"$sub_id\",$filter]"
|
||||
|
||||
print_info "Testing filter: $filter"
|
||||
|
||||
# Send subscription and collect events
|
||||
local response=""
|
||||
if command -v websocat &> /dev/null; then
|
||||
response=$(echo -e "$req_message\n[\"CLOSE\",\"$sub_id\"]" | timeout 3s websocat "$RELAY_URL" 2>/dev/null || echo "")
|
||||
fi
|
||||
|
||||
|
||||
# Count EVENT responses (lines containing ["EVENT","sub_id",...])
|
||||
local event_count=0
|
||||
if [[ -n "$response" ]]; then
|
||||
event_count=$(echo "$response" | grep -c "\"EVENT\"" 2>/dev/null || echo "0")
|
||||
fi
|
||||
|
||||
if [[ "$expected_count" == "any" ]]; then
|
||||
if [[ $event_count -gt 0 ]]; then
|
||||
print_success "$description - Found $event_count events"
|
||||
else
|
||||
print_warning "$description - No events found"
|
||||
fi
|
||||
elif [[ $event_count -eq $expected_count ]]; then
|
||||
print_success "$description - Found expected $event_count events"
|
||||
else
|
||||
print_warning "$description - Expected $expected_count events, found $event_count"
|
||||
fi
|
||||
|
||||
# Show a few sample events for verification (first 2)
|
||||
if [[ $event_count -gt 0 && "$description" == "All events" ]]; then
|
||||
print_info "Sample events (first 2):"
|
||||
echo "$response" | grep "\"EVENT\"" | head -2 | while IFS= read -r line; do
|
||||
local event_content=$(echo "$line" | jq -r '.[2].content' 2>/dev/null || echo "N/A")
|
||||
local event_kind=$(echo "$line" | jq -r '.[2].kind' 2>/dev/null || echo "N/A")
|
||||
local event_id=$(echo "$line" | jq -r '.[2].id' 2>/dev/null || echo "N/A")
|
||||
echo " - ID: ${event_id:0:16}... Kind: $event_kind Content: ${event_content:0:30}..."
|
||||
done
|
||||
fi
|
||||
|
||||
echo # Add blank line for readability
|
||||
return 0
|
||||
}
|
||||
|
||||
# Main test function
|
||||
run_comprehensive_test() {
|
||||
print_header "C-Relay Comprehensive Test"
|
||||
|
||||
# Check dependencies
|
||||
print_step "Checking dependencies..."
|
||||
if ! command -v nak &> /dev/null; then
|
||||
print_error "nak command not found"
|
||||
print_info "Please install nak: go install github.com/fiatjaf/nak@latest"
|
||||
return 1
|
||||
fi
|
||||
print_success "nak found"
|
||||
|
||||
# Step 1: Create type 1 event with nak including tags
|
||||
print_step "Creating type 1 event with nak and tags..."
|
||||
|
||||
local event_json
|
||||
if ! event_json=$(nak event --sec "$TEST_PRIVATE_KEY" -c "$TEST_CONTENT" -k 1 --ts $(date +%s) -e "test_event_id" -p "test_pubkey" -t "subject=Test Event" 2>/dev/null); then
|
||||
print_error "Failed to generate event with nak"
|
||||
if ! command -v websocat &> /dev/null; then
|
||||
print_error "websocat command not found"
|
||||
print_info "Please install websocat for testing"
|
||||
return 1
|
||||
fi
|
||||
|
||||
print_success "Event created successfully"
|
||||
print_header "FULL EVENT JSON"
|
||||
echo "$event_json" | jq . 2>/dev/null || echo "$event_json"
|
||||
echo
|
||||
|
||||
# Step 2: Upload to C-Relay
|
||||
print_step "Uploading event to C-Relay at $RELAY_URL..."
|
||||
|
||||
# Create EVENT message in Nostr format
|
||||
local event_message="[\"EVENT\",$event_json]"
|
||||
|
||||
# Use websocat or wscat to send to relay if available
|
||||
local response=""
|
||||
if command -v websocat &> /dev/null; then
|
||||
print_info "Using websocat to connect to relay..."
|
||||
response=$(echo "$event_message" | timeout 5s websocat "$RELAY_URL" 2>&1 || echo "Connection failed")
|
||||
elif command -v wscat &> /dev/null; then
|
||||
print_info "Using wscat to connect to relay..."
|
||||
response=$(echo "$event_message" | timeout 5s wscat -c "$RELAY_URL" 2>&1 || echo "Connection failed")
|
||||
else
|
||||
# Fallback: use nak publish
|
||||
print_info "Using nak to publish event..."
|
||||
response=$(echo "$event_json" | nak event --relay "$RELAY_URL" 2>&1 || echo "Publish failed")
|
||||
fi
|
||||
|
||||
print_header "FULL RELAY RESPONSE"
|
||||
echo "$response"
|
||||
echo
|
||||
|
||||
if [[ "$response" == *"Connection failed"* ]] || [[ "$response" == *"Publish failed"* ]]; then
|
||||
print_error "Failed to connect to relay or publish event"
|
||||
print_info "Make sure the relay is running: ./make_and_restart_relay.sh"
|
||||
if ! command -v jq &> /dev/null; then
|
||||
print_error "jq command not found"
|
||||
print_info "Please install jq for JSON processing"
|
||||
return 1
|
||||
else
|
||||
print_success "Event uploaded to relay"
|
||||
return 0
|
||||
fi
|
||||
print_success "All dependencies found"
|
||||
|
||||
print_header "PHASE 1: Publishing Various Event Types"
|
||||
|
||||
# Test 1: Regular Events (kind 1)
|
||||
print_step "Creating regular events (kind 1)..."
|
||||
local regular1=$(nak event --sec "$TEST_PRIVATE_KEY" -c "Regular event #1" -k 1 --ts $(($(date +%s) - 100)) -t "type=regular" -t "test=phase1" 2>/dev/null)
|
||||
local regular2=$(nak event --sec "$TEST_PRIVATE_KEY" -c "Regular event #2 with tags" -k 1 --ts $(($(date +%s) - 90)) -e "previous_event_id" -p "test_pubkey" -t "type=regular" -t "test=phase1" 2>/dev/null)
|
||||
|
||||
publish_event "$regular1" "regular" "Regular event #1"
|
||||
publish_event "$regular2" "regular" "Regular event #2"
|
||||
|
||||
# Test 2: Replaceable Events (kind 0 - metadata)
|
||||
print_step "Creating replaceable events (kind 0)..."
|
||||
local replaceable1=$(nak event --sec "$TEST_PRIVATE_KEY" -c '{"name":"Test User","about":"Testing C-Relay"}' -k 0 --ts $(($(date +%s) - 80)) -t "type=replaceable" 2>/dev/null)
|
||||
local replaceable2=$(nak event --sec "$TEST_PRIVATE_KEY" -c '{"name":"Test User Updated","about":"Updated profile"}' -k 0 --ts $(($(date +%s) - 70)) -t "type=replaceable" 2>/dev/null)
|
||||
|
||||
publish_event "$replaceable1" "replaceable" "Replaceable event #1 (metadata)"
|
||||
publish_event "$replaceable2" "replaceable" "Replaceable event #2 (metadata update)"
|
||||
|
||||
# Test 3: Ephemeral Events (kind 20000+)
|
||||
print_step "Creating ephemeral events (kind 20001)..."
|
||||
local ephemeral1=$(nak event --sec "$TEST_PRIVATE_KEY" -c "Ephemeral event - should not be stored permanently" -k 20001 --ts $(date +%s) -t "type=ephemeral" 2>/dev/null)
|
||||
|
||||
publish_event "$ephemeral1" "ephemeral" "Ephemeral event"
|
||||
|
||||
# Test 4: Addressable Events (kind 30000+)
|
||||
print_step "Creating addressable events (kind 30001)..."
|
||||
local addressable1=$(nak event --sec "$TEST_PRIVATE_KEY" -c "Addressable event with d-tag" -k 30001 --ts $(($(date +%s) - 50)) -t "d=test-article" -t "type=addressable" 2>/dev/null)
|
||||
local addressable2=$(nak event --sec "$TEST_PRIVATE_KEY" -c "Updated addressable event" -k 30001 --ts $(($(date +%s) - 40)) -t "d=test-article" -t "type=addressable" -t "updated=true" 2>/dev/null)
|
||||
|
||||
publish_event "$addressable1" "addressable" "Addressable event #1"
|
||||
publish_event "$addressable2" "addressable" "Addressable event #2 (update)"
|
||||
|
||||
# Brief pause to let events settle
|
||||
sleep 2
|
||||
|
||||
print_header "PHASE 2: Testing Subscriptions and Filters"
|
||||
|
||||
# Test subscription filters
|
||||
print_step "Testing various subscription filters..."
|
||||
|
||||
# Test 1: Get all events
|
||||
test_subscription "test_all" '{}' "All events" "any"
|
||||
|
||||
# Test 2: Get events by kind
|
||||
test_subscription "test_kind1" '{"kinds":[1]}' "Kind 1 events only" "2"
|
||||
test_subscription "test_kind0" '{"kinds":[0]}' "Kind 0 events only" "any"
|
||||
|
||||
# Test 3: Get events by author (pubkey)
|
||||
local test_pubkey=$(echo "$regular1" | jq -r '.pubkey' 2>/dev/null)
|
||||
test_subscription "test_author" "{\"authors\":[\"$test_pubkey\"]}" "Events by specific author" "any"
|
||||
|
||||
# Test 4: Get recent events (time-based)
|
||||
local recent_timestamp=$(($(date +%s) - 200))
|
||||
test_subscription "test_recent" "{\"since\":$recent_timestamp}" "Recent events" "any"
|
||||
|
||||
# Test 5: Get events with specific tags
|
||||
test_subscription "test_tag_type" '{"#type":["regular"]}' "Events with type=regular tag" "any"
|
||||
|
||||
# Test 6: Multiple kinds
|
||||
test_subscription "test_multi_kinds" '{"kinds":[0,1]}' "Multiple kinds (0,1)" "any"
|
||||
|
||||
# Test 7: Limit results
|
||||
test_subscription "test_limit" '{"kinds":[1],"limit":1}' "Limited to 1 event" "1"
|
||||
|
||||
print_header "PHASE 3: Database Verification"
|
||||
|
||||
# Check what's actually stored in the database
|
||||
print_step "Verifying database contents..."
|
||||
|
||||
if command -v sqlite3 &> /dev/null; then
|
||||
print_info "Events by type in database:"
|
||||
sqlite3 db/c_nostr_relay.db "SELECT event_type, COUNT(*) as count FROM events GROUP BY event_type;" | while read line; do
|
||||
echo " $line"
|
||||
done
|
||||
|
||||
print_info "Recent events in database:"
|
||||
sqlite3 db/c_nostr_relay.db "SELECT substr(id, 1, 16) || '...' as short_id, event_type, kind, substr(content, 1, 30) || '...' as short_content FROM events ORDER BY created_at DESC LIMIT 5;" | while read line; do
|
||||
echo " $line"
|
||||
done
|
||||
|
||||
print_success "Database verification complete"
|
||||
else
|
||||
print_warning "sqlite3 not available for database verification"
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# Run the test
|
||||
if run_test; then
|
||||
# Run the comprehensive test
|
||||
print_header "Starting C-Relay Comprehensive Test Suite"
|
||||
echo
|
||||
|
||||
if run_comprehensive_test; then
|
||||
echo
|
||||
print_success "All tests completed successfully!"
|
||||
print_info "The C-Relay hybrid schema implementation is working correctly"
|
||||
echo
|
||||
print_success "Test completed successfully"
|
||||
exit 0
|
||||
else
|
||||
echo
|
||||
print_error "Test failed"
|
||||
print_error "Some tests failed"
|
||||
exit 1
|
||||
fi
|
||||
Reference in New Issue
Block a user