
#!/bin/bash

# Authentication Testing Suite for C-Relay
# Tests NIP-42 authentication mechanisms

set -e

# Configuration
RELAY_HOST="127.0.0.1"
RELAY_PORT="8888"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Test counters
TOTAL_TESTS=0
PASSED_TESTS=0
FAILED_TESTS=0

# Function to test authentication challenge
test_auth_challenge() {
    local description="$1"

    TOTAL_TESTS=$((TOTAL_TESTS + 1))

    echo -n "Testing $description... "

    # Send a REQ message that should trigger auth
    local response
    response=$(echo "[\"REQ\",\"auth_test_$(date +%s)\",{}]" | timeout 10 websocat -B 1048576 ws://$RELAY_HOST:$RELAY_PORT 2>/dev/null | head -3 || echo 'TIMEOUT')

    if [[ "$response" == *"TIMEOUT"* ]]; then
        echo -e "${RED}FAILED${NC} - Connection timeout"
        FAILED_TESTS=$((FAILED_TESTS + 1))
        return 1
    fi

    if [[ "$response" == *"AUTH"* ]] || [[ "$response" == *"challenge"* ]]; then
        echo -e "${GREEN}PASSED${NC} - Auth challenge received"
        PASSED_TESTS=$((PASSED_TESTS + 1))
        return 0
    else
        echo -e "${YELLOW}UNCERTAIN${NC} - No auth challenge (may be disabled)"
        PASSED_TESTS=$((PASSED_TESTS + 1)) # Count as passed since auth may be optional
        return 0
    fi
}

# Function to test whitelist/blacklist functionality
test_auth_list() {
    local description="$1"
    local list_type="$2"  # "whitelist" or "blacklist"
    local pubkey="$3"
    local expect_allowed="${4:-true}"

    TOTAL_TESTS=$((TOTAL_TESTS + 1))

    echo -n "Testing $description... "

    # Create admin event to modify auth list
    local admin_event
    admin_event=$(cat << EOF
{
  "kind": 23456,
  "content": "$(echo '["'"$list_type"'","pubkey","'"$pubkey"'"]' | base64)",
  "tags": [["p", "relay_pubkey_placeholder"]],
  "created_at": $(date +%s),
  "pubkey": "admin_pubkey_placeholder",
  "sig": "signature_placeholder"
}
EOF
)

    # Send auth list modification
    local response
    response=$(echo "$admin_event" | timeout 10 websocat -B 1048576 ws://$RELAY_HOST:$RELAY_PORT 2>/dev/null | head -1 || echo 'TIMEOUT')

    if [[ "$response" == *"TIMEOUT"* ]]; then
        echo -e "${RED}FAILED${NC} - Connection timeout"
        FAILED_TESTS=$((FAILED_TESTS + 1))
        return 1
    fi

    if [[ "$response" == *"OK"* ]]; then
        echo -e "${GREEN}PASSED${NC} - Auth list updated"
        PASSED_TESTS=$((PASSED_TESTS + 1))
        return 0
    else
        echo -e "${RED}FAILED${NC} - Auth list update failed: $response"
        FAILED_TESTS=$((FAILED_TESTS + 1))
        return 1
    fi
}

# Function to test event publishing with auth
test_event_publishing() {
    local description="$1"
    local event_kind="${2:-1}"
    local expect_success="${3:-true}"

    TOTAL_TESTS=$((TOTAL_TESTS + 1))

    echo -n "Testing $description... "

    # Create a test event
    local test_event
    test_event=$(cat << EOF
{
  "kind": $event_kind,
  "content": "Test event for auth testing",
  "tags": [],
  "created_at": $(date +%s),
  "pubkey": "[encoded data, 9067 characters]