#!/usr/bin/env bash
# Run all tests in parallel

set -e

cd "$(dirname "$0")/../.."
FORGE_SCOPE="${FORGE_SCOPE:-full}"

echo "=== 🧪 Running Tests in Parallel ==="

# Create test results directory
mkdir -p test-results

# Run Foundry tests (excluding CCIP integration)
echo "Running Foundry tests..."
bash scripts/forge/scope.sh test "$FORGE_SCOPE" --no-match-path 'test/ccip-integration/*' --json > test-results/foundry.json 2>&1 &
FORGE_PID=$!

# Run Hardhat tests (if any)
if [ -d "test/hardhat" ]; then
    echo "Running Hardhat tests..."
    npx hardhat test > test-results/hardhat.log 2>&1 &
    HARDHAT_PID=$!
else
    HARDHAT_PID=""
fi

# Wait for all tests
wait $FORGE_PID
FORGE_EXIT=$?

if [ -n "$HARDHAT_PID" ]; then
    wait $HARDHAT_PID
    HARDHAT_EXIT=$?
else
    HARDHAT_EXIT=0
fi

# Report results
echo ""
echo "=== Test Results ==="
if [ $FORGE_EXIT -eq 0 ]; then
    echo "✅ Foundry tests: PASSED"
else
    echo "❌ Foundry tests: FAILED"
fi

if [ -n "$HARDHAT_PID" ]; then
    if [ $HARDHAT_EXIT -eq 0 ]; then
        echo "✅ Hardhat tests: PASSED"
    else
        echo "❌ Hardhat tests: FAILED"
    fi
fi

exit $((FORGE_EXIT + HARDHAT_EXIT))
