v0.3.5 - nip42 implemented

This commit is contained in:
Your Name
2025-09-13 08:49:09 -04:00
parent 1690b58c67
commit f3d6afead1
24 changed files with 3705 additions and 532 deletions

View File

@@ -300,75 +300,103 @@ test_expiration_filtering_in_subscriptions() {
return 0
fi
print_info "Setting up test events for subscription filtering..."
print_info "Setting up short-lived events for proper expiration filtering test..."
# First, create a few events with different expiration times
local private_key="91ba716fa9e7ea2fcbad360cf4f8e0d312f73984da63d90f524ad61a6a1e7dbe"
# Event 1: No expiration (should be returned)
# Event 1: No expiration (should always be returned)
local event1=$(nak event --sec "$private_key" -c "Event without expiration for filtering test" --ts $(date +%s))
# Event 2: Future expiration (should be returned)
local future_timestamp=$(($(date +%s) + 1800)) # 30 minutes from now
local event2=$(create_event_with_expiration "Event with future expiration for filtering test" "$future_timestamp")
# Event 3: Past expiration (should NOT be returned if filtering is enabled)
local past_timestamp=$(($(date +%s) - 3600)) # 1 hour ago
local event3=$(create_event_with_expiration "Event with past expiration for filtering test" "$past_timestamp")
# Event 3: SHORT-LIVED EVENT - expires in 3 seconds
local short_expiry=$(($(date +%s) + 3)) # 3 seconds from now
local event3=$(create_event_with_expiration "Short-lived event for filtering test" "$short_expiry")
print_info "Publishing test events..."
print_info "Publishing test events (including one that expires in 3 seconds)..."
# Note: We expect event3 to be rejected on submission in strict mode,
# so we'll create it with a slightly more recent expiration that might get through
local recent_past=$(($(date +%s) - 600)) # 10 minutes ago (outside grace period)
local event3_recent=$(create_event_with_expiration "Recently expired event for filtering test" "$recent_past")
# Submit all events - they should all be accepted initially
local response1=$(echo "[\"EVENT\",$event1]" | timeout 5s websocat "$RELAY_URL" 2>&1)
local response2=$(echo "[\"EVENT\",$event2]" | timeout 5s websocat "$RELAY_URL" 2>&1)
local response3=$(echo "[\"EVENT\",$event3]" | timeout 5s websocat "$RELAY_URL" 2>&1)
# Try to submit all events (some may be rejected)
echo "[\"EVENT\",$event1]" | timeout 3s websocat "$RELAY_URL" >/dev/null 2>&1 || true
echo "[\"EVENT\",$event2]" | timeout 3s websocat "$RELAY_URL" >/dev/null 2>&1 || true
echo "[\"EVENT\",$event3_recent]" | timeout 3s websocat "$RELAY_URL" >/dev/null 2>&1 || true
sleep 2 # Let events settle
print_info "Testing subscription filtering..."
# Create subscription for recent events
local req_message='["REQ","filter_test",{"kinds":[1],"limit":10}]'
local response=$(echo -e "$req_message\n[\"CLOSE\",\"filter_test\"]" | timeout 5s websocat "$RELAY_URL" 2>/dev/null || echo "")
print_info "Subscription response:"
echo "$response"
print_info "Event submission responses:"
echo "Event 1 (no expiry): $response1"
echo "Event 2 (future expiry): $response2"
echo "Event 3 (expires in 3s): $response3"
echo ""
# Count events that contain our test content
# Verify all events were accepted
if [[ "$response1" != *"true"* ]] || [[ "$response2" != *"true"* ]] || [[ "$response3" != *"true"* ]]; then
record_test_result "Expiration Filtering in Subscriptions" "FAIL" "Events not properly accepted during submission"
return 1
fi
print_success "✓ All events accepted during submission"
# Test 1: Query immediately - all events should be present
print_info "Testing immediate subscription (before expiration)..."
local req_message='["REQ","filter_immediate",{"kinds":[1],"limit":10}]'
local immediate_response=$(echo -e "$req_message\n[\"CLOSE\",\"filter_immediate\"]" | timeout 5s websocat "$RELAY_URL" 2>/dev/null || echo "")
local immediate_count=0
if echo "$immediate_response" | grep -q "Event without expiration for filtering test"; then
immediate_count=$((immediate_count + 1))
fi
if echo "$immediate_response" | grep -q "Event with future expiration for filtering test"; then
immediate_count=$((immediate_count + 1))
fi
if echo "$immediate_response" | grep -q "Short-lived event for filtering test"; then
immediate_count=$((immediate_count + 1))
fi
print_info "Immediate response found $immediate_count/3 events"
# Wait for the short-lived event to expire (5 seconds total wait)
print_info "Waiting 5 seconds for short-lived event to expire..."
sleep 5
# Test 2: Query after expiration - short-lived event should be filtered out
print_info "Testing subscription after expiration (short-lived event should be filtered)..."
req_message='["REQ","filter_after_expiry",{"kinds":[1],"limit":10}]'
local expired_response=$(echo -e "$req_message\n[\"CLOSE\",\"filter_after_expiry\"]" | timeout 5s websocat "$RELAY_URL" 2>/dev/null || echo "")
print_info "Post-expiration subscription response:"
echo "$expired_response"
echo ""
# Count events in the expired response
local no_exp_count=0
local future_exp_count=0
local past_exp_count=0
local future_exp_count=0
local expired_event_count=0
if echo "$response" | grep -q "Event without expiration for filtering test"; then
if echo "$expired_response" | grep -q "Event without expiration for filtering test"; then
no_exp_count=1
print_success "✓ Event without expiration found in subscription results"
print_success "✓ Event without expiration found in post-expiration results"
fi
if echo "$response" | grep -q "Event with future expiration for filtering test"; then
if echo "$expired_response" | grep -q "Event with future expiration for filtering test"; then
future_exp_count=1
print_success "✓ Event with future expiration found in subscription results"
print_success "✓ Event with future expiration found in post-expiration results"
fi
if echo "$response" | grep -q "Recently expired event for filtering test"; then
past_exp_count=1
print_warning "✗ Recently expired event found in subscription results (should be filtered)"
if echo "$expired_response" | grep -q "Short-lived event for filtering test"; then
expired_event_count=1
print_error "✗ EXPIRED short-lived event found in subscription results (should be filtered!)"
else
print_success "✓ Recently expired event properly filtered from subscription results"
print_success "✓ Expired short-lived event properly filtered from subscription results"
fi
# Evaluate results
local expected_events=$((no_exp_count + future_exp_count))
if [ $expected_events -ge 1 ] && [ $past_exp_count -eq 0 ]; then
local expected_active_events=$((no_exp_count + future_exp_count))
if [ $expected_active_events -ge 2 ] && [ $expired_event_count -eq 0 ]; then
record_test_result "Expiration Filtering in Subscriptions" "PASS" "Expired events properly filtered from subscriptions"
return 0
else
record_test_result "Expiration Filtering in Subscriptions" "FAIL" "Expiration filtering not working properly in subscriptions"
local details="Found $expected_active_events active events, $expired_event_count expired events (should be 0)"
record_test_result "Expiration Filtering in Subscriptions" "FAIL" "Expiration filtering not working properly in subscriptions - $details"
return 1
fi
}