# Copyright © Magento, Inc. All rights reserved.
# See COPYING.txt for license details.

set -e

echo "==============================="
echo "   EXECUTE Functional Tests    "
echo "==============================="
bin/mftf build:project

# DevDocs tests hit a live, JS-heavy external site (developer.adobe.com) through an
# aging standalone Selenium/Chrome container. That combination occasionally drops the
# WebDriver connection mid-test ("Connection refused") even though nothing in the test
# or the framework is broken, so retry a few times before treating it as a real failure.
run_test_with_retry() {
    local test_name="$1"
    local max_attempts=3
    local attempt=1

    until bin/mftf run:test "$test_name" -f; do
        if [ "$attempt" -ge "$max_attempts" ]; then
            echo "$test_name failed after $max_attempts attempts"
            return 1
        fi
        attempt=$((attempt + 1))
        echo "$test_name failed, retrying (attempt $attempt/$max_attempts)..."
    done
}

run_test_with_retry DeprecatedDevDocsTest
run_test_with_retry DevDocsTest
run_test_with_retry FormatCurrencyTest
