#!/bin/bash
# =====================================================================
# functional-test.sh - Test the main functionality of the website
# =====================================================================
# This script checks if the main features of the website are working
# =====================================================================

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

echo "=== Testing Website Functionality ==="
echo "Using base URL: $BASE_URL"

# Array to track failures
FAILURES=0

# Function to test a page and check for expected content
test_page() {
    local url="$1"
    local name="$2"
    local expected_title="$3"
    local expected_content="$4"
    
    echo "Testing $name page at $url"
    
    # Check if the page loads
    RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" "$url")
    if [ "$RESPONSE" -eq 200 ]; then
        echo "✅ $name page loads successfully (HTTP $RESPONSE)"
    else
        echo "❌ $name page failed to load (HTTP $RESPONSE)"
        FAILURES=$((FAILURES+1))
        return
    fi
    
    # Check page title
    TITLE=$(curl -s "$url" | grep -o "<title>.*</title>" | sed 's/<title>\(.*\)<\/title>/\1/')
    if [[ "$TITLE" == *"$expected_title"* ]]; then
        echo "✅ Page title matches: $TITLE"
    else
        echo "❌ Page title doesn't match. Expected: $expected_title, Got: $TITLE"
        FAILURES=$((FAILURES+1))
    fi
    
    # Check for expected content
    if [ -n "$expected_content" ]; then
        CONTENT=$(curl -s "$url")
        if echo "$CONTENT" | grep -q "$expected_content"; then
            echo "✅ Page contains expected content: $expected_content"
        else
            echo "❌ Page doesn't contain expected content: $expected_content"
            FAILURES=$((FAILURES+1))
        fi
    fi
    
    echo "---"
}

# Test main page
test_page "$BASE_URL/" "Main" "Colin Knapp - Portfolio" "Colin Knapp"

# Test stories page
test_page "$BASE_URL/stories/" "Stories" "Stories" "Case Studies"

# Test CSV tool
test_page "$BASE_URL/one-pager-tools/csv-tool.html" "CSV Tool" "CSV Viewer" "Paste your CSV data here"

# Check for JavaScript files
echo "Checking for required JavaScript files..."
JS_FILES=("theme.js" "includes.js" "utils.js")
for js_file in "${JS_FILES[@]}"; do
    RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" "$BASE_URL/$js_file")
    if [ "$RESPONSE" -eq 200 ]; then
        echo "✅ $js_file loads successfully (HTTP $RESPONSE)"
    else
        echo "❌ $js_file failed to load (HTTP $RESPONSE)"
        FAILURES=$((FAILURES+1))
    fi
done

# Check for CSS files
echo "Checking for required CSS files..."
CSS_FILES=("styles.css")
for css_file in "${CSS_FILES[@]}"; do
    RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" "$BASE_URL/$css_file")
    if [ "$RESPONSE" -eq 200 ]; then
        echo "✅ $css_file loads successfully (HTTP $RESPONSE)"
    else
        echo "❌ $css_file failed to load (HTTP $RESPONSE)"
        FAILURES=$((FAILURES+1))
    fi
done

# Check for security headers
echo "Checking for security headers..."
HEADERS=$(curl -s -I "$BASE_URL/")
if echo "$HEADERS" | grep -q "Content-Security-Policy"; then
    echo "✅ Content-Security-Policy header found"
else
    echo "❌ Content-Security-Policy header not found"
    FAILURES=$((FAILURES+1))
fi

if echo "$HEADERS" | grep -q "X-Frame-Options"; then
    echo "✅ X-Frame-Options header found"
else
    echo "❌ X-Frame-Options header not found"
    FAILURES=$((FAILURES+1))
fi

# Check if any failures occurred
if [ "$FAILURES" -eq 0 ]; then
    echo "=== All Functionality Tests Passed ==="
    exit 0
else
    echo "=== Functionality Tests Failed: $FAILURES failures ==="
    exit 1
fi
