#!/bin/bash

# Test script for cache management functionality
# This script demonstrates the new cache management features in cremote

echo "🧪 Testing Cremote Cache Management Features"
echo "============================================="

# Check if daemon is running
echo "📡 Checking daemon status..."
if ! ./cremote status > /dev/null 2>&1; then
    echo "❌ Daemon is not running. Please start it with: cremotedaemon"
    exit 1
fi
echo "✅ Daemon is running"

# Open a new tab
echo "🌐 Opening new tab..."
TAB_ID=$(./cremote open-tab)
if [ $? -ne 0 ]; then
    echo "❌ Failed to open tab"
    exit 1
fi
echo "✅ Tab opened: $TAB_ID"

# Load a test page
echo "📄 Loading test page..."
./cremote load-url --url="https://httpbin.org/cache/60" --timeout=10
if [ $? -ne 0 ]; then
    echo "❌ Failed to load page"
    exit 1
fi
echo "✅ Page loaded"

# Test 1: Disable cache
echo ""
echo "🚫 Test 1: Disabling cache..."
./cremote disable-cache --timeout=10
if [ $? -eq 0 ]; then
    echo "✅ Cache disabled successfully"
else
    echo "❌ Failed to disable cache"
fi

# Test 2: Enable cache
echo ""
echo "✅ Test 2: Enabling cache..."
./cremote enable-cache --timeout=10
if [ $? -eq 0 ]; then
    echo "✅ Cache enabled successfully"
else
    echo "❌ Failed to enable cache"
fi

# Test 3: Clear cache
echo ""
echo "🧹 Test 3: Clearing cache..."
./cremote clear-cache --timeout=10
if [ $? -eq 0 ]; then
    echo "✅ Cache cleared successfully"
else
    echo "❌ Failed to clear cache"
fi

# Test 4: Test with specific tab ID
echo ""
echo "🎯 Test 4: Testing with specific tab ID..."
./cremote disable-cache --tab="$TAB_ID" --timeout=10
if [ $? -eq 0 ]; then
    echo "✅ Cache disabled for specific tab successfully"
else
    echo "❌ Failed to disable cache for specific tab"
fi

# Clean up
echo ""
echo "🧹 Cleaning up..."
./cremote close-tab --tab="$TAB_ID"
echo "✅ Tab closed"

echo ""
# Test 5: Clear all site data
echo ""
echo "🧹 Test 5: Clearing all site data..."
./cremote clear-all-site-data --timeout=15
if [ $? -eq 0 ]; then
    echo "✅ All site data cleared successfully"
else
    echo "❌ Failed to clear all site data"
fi

# Test 6: Clear cookies only
echo ""
echo "🍪 Test 6: Clearing cookies..."
./cremote clear-cookies --timeout=10
if [ $? -eq 0 ]; then
    echo "✅ Cookies cleared successfully"
else
    echo "❌ Failed to clear cookies"
fi

# Test 7: Clear storage only
echo ""
echo "💾 Test 7: Clearing storage..."
./cremote clear-storage --timeout=10
if [ $? -eq 0 ]; then
    echo "✅ Storage cleared successfully"
else
    echo "❌ Failed to clear storage"
fi

echo "🎉 Cache and site data management tests completed!"
echo ""
echo "📋 Summary of new features:"
echo "  • disable-cache:        Disables browser cache for testing"
echo "  • enable-cache:         Re-enables browser cache"
echo "  • clear-cache:          Clears existing cached resources"
echo "  • clear-all-site-data:  Clears ALL site data (cookies, storage, cache, etc.)"
echo "  • clear-cookies:        Clears cookies only"
echo "  • clear-storage:        Clears web storage only (localStorage, sessionStorage, etc.)"
echo ""
echo "💡 Use cases:"
echo "  • Testing: Ensure fresh page loads without cache"
echo "  • Performance: Test cold load performance"
echo "  • Debugging: Resolve cache-related issues"
echo "  • Development: See changes immediately"
echo "  • Authentication: Clear cookies to test login/logout flows"
echo "  • Privacy: Clear all site data for clean state testing"
echo "  • Storage: Clear web storage to test application state"
