#!/bin/bash
# =====================================================================
# test-csv-tool.sh - Test the CSV tool functionality
# =====================================================================
# This script checks if the CSV tool page loads without CSP errors
# =====================================================================

# Check if base URL is provided
if [ -z "$1" ]; then
  BASE_URL="http://localhost:8080"
else
  BASE_URL="$1"
fi

echo "=== Testing CSV Tool ==="
echo "Using base URL: $BASE_URL"

# Create a test CSV file
echo "Name,Age,City
John,30,New York
Jane,25,San Francisco
Bob,40,Chicago" > /tmp/test.csv

# Check if the page loads properly
echo "Checking if the CSV tool page loads properly..."
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" "$BASE_URL/one-pager-tools/csv-tool.html")

if [ "$RESPONSE" -eq 200 ]; then
    echo "✅ CSV tool page loads successfully (HTTP $RESPONSE)"
else
    echo "❌ CSV tool page failed to load (HTTP $RESPONSE)"
    rm -f /tmp/test.csv
    exit 1
fi

# Check for CSP errors in the response headers
echo "Checking for CSP errors in response headers..."
CSP_HEADER=$(curl -s -I "$BASE_URL/one-pager-tools/csv-tool.html" | grep -i "Content-Security-Policy")

if [ -n "$CSP_HEADER" ]; then
    echo "✅ CSP header found in response"
else
    echo "❌ CSP header not found in response"
    rm -f /tmp/test.csv
    exit 1
fi

# Clean up
rm -f /tmp/test.csv

echo "=== CSV Tool Test Completed Successfully ==="
exit 0
