#!/bin/bash

# Simple test to see if our debug message appears
set -e

echo "=== Debug Test ==="

# Kill existing processes
pkill -f chromium || true
pkill -f cremotedaemon || true
sleep 2

# Start Chrome
chromium --remote-debugging-port=9222 --user-data-dir=/tmp/chromium-debug --no-sandbox --disable-dev-shm-usage --headless &
CHROME_PID=$!
sleep 5

# Start daemon with debug output
echo "Starting daemon with debug..."
./cremotedaemon --debug &
DAEMON_PID=$!
sleep 3

cleanup() {
    echo "Cleaning up..."
    if [ ! -z "$DAEMON_PID" ]; then
        kill $DAEMON_PID 2>/dev/null || true
    fi
    if [ ! -z "$CHROME_PID" ]; then
        kill $CHROME_PID 2>/dev/null || true
    fi
}
trap cleanup EXIT

# Test our new command and look for debug output
echo "Testing check-element command..."
curl -s -X POST http://localhost:8989/command \
  -H "Content-Type: application/json" \
  -d '{"action": "check-element", "params": {"selector": "body", "type": "exists"}}' &

# Wait a moment for the request to process
sleep 2

echo "Test completed - check daemon output above for debug messages"
