Files
c-relay/tests/1_nip_test.sh
2025-09-03 20:39:06 -04:00

112 lines
3.1 KiB
Bash
Executable File

#!/bin/bash
# Simple C-Relay Test - Create type 1 event and upload to relay
# Uses nak to generate and publish a single event
set -e # Exit on any error
# Color constants
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
BOLD='\033[1m'
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() {
echo -e "${BLUE}${BOLD}=== $1 ===${RESET}"
}
print_step() {
echo -e "${YELLOW}[STEP]${RESET} $1"
}
print_success() {
echo -e "${GREEN}${RESET} $1"
}
print_error() {
echo -e "${RED}${RESET} $1"
}
print_info() {
echo -e "${BLUE}[INFO]${RESET} $1"
}
# Main test function
run_test() {
print_header "C-Relay Simple Test"
# Check if nak is available
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"
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"
return 1
else
print_success "Event uploaded to relay"
return 0
fi
}
# Run the test
if run_test; then
echo
print_success "Test completed successfully"
exit 0
else
echo
print_error "Test failed"
exit 1
fi