#!/bin/bash

trap 'exit' INT

PLATFORM=${1:-}

case $PLATFORM in
  ios | android )
    ;;

  *)
    echo "Error! You must pass either 'android' or 'ios'"
    echo ""
    exit 1
    ;;
esac

# NOTE: This script is intended to be run from the root of the project (CI)
if [ "$PLATFORM" == "ios" ]; then
  testFiles=$(ls apps/mobile/tests/*.yml apps/mobile/tests/ios-only/*.yml)
else
  testFiles=$(ls apps/mobile/tests/*.yml apps/mobile/tests/android-only/*.yml)
fi


# Run onboarding first
onboardingFile="apps/mobile/tests/onboarding.yml"
if ! maestro test "$onboardingFile"
then
  echo "Onboarding test failed. Retrying in 30 seconds..."
  sleep 30
  if ! maestro test "$onboardingFile"
  then
    echo "Onboarding test failed again. Retrying for the last time in 120 seconds..."
    sleep 120
    if ! maestro test "$onboardingFile"
    then
      echo "Onboarding test failed again. Exiting..."
      exit 1
    fi
  fi
fi

failedTests=()

# Run the rest of the files
for file in $testFiles
do
  # Skip onboarding.yml since it has already been run
  if [ "$file" == "$onboardingFile" ]
  then
    continue
  fi

  if ! maestro test "$file"
  then
    echo "Test ${file} failed. Retrying in 30 seconds..."
    sleep 30
    if ! maestro test "$file"
    then
      echo "Test ${file} failed again. Retrying for the last time in 120 seconds..."
      sleep 120
      if ! maestro test "$file"
      then
        failedTests+=("$file")
      fi
    fi
  fi
done

if [ ${#failedTests[@]} -eq 0 ]; then
    exit 0
else
    echo "These tests failed:"
    printf '%s\n' "${failedTests[@]}"
    exit 1
fi