#!/usr/bin/env bash

set -euo pipefail

bright_green='\u001b[32;1m'
reset_color='\033[0m'

main(){
  install
  generate_unit_test_reports
  generate_e2e_test_reports
  display_location
}

install(){
  ./install_yarn.sh

  # Install dependencies.
  yarn install --frozen-lockfile
  # Lint the entire project.
  yarn lint --no-fix --max-warnings=0
}

generate_unit_test_reports(){
  # Run unit tests with coverage.
  # NOTE(@price) - instanbul code instrumentation is non-deterministic and shows up in snapshots.
  # The implication is that we cannot commit instrumented test results, so we have to generate
  # coverage and then revert the snapshots.
  yarn test:unit --coverage --updateSnapshot
  # Here we restore the old snapshots.
  yarn test:unit --updateSnapshot
  aws s3 sync ./tests/unit/coverage/lcov-report/ s3://zoox-web/fleetdash/test-coverage/unit-tests
}

generate_e2e_test_reports(){
  source ../test_environment/setup.sh
  cd ${current_directory}/../
  docker-compose up  --abort-on-container-exit --exit-code-from=cypress
  cd frontend
  aws s3 sync ./tests/e2e/coverage/lcov-report/ s3://zoox-web/fleetdash/test-coverage/e2e-tests
}

current_directory="$(pwd)"
cleanup() {
  cd ${current_directory}
}

trap cleanup EXIT INT

display_location(){
  echo -e "${bright_green}
  View e2e report on http://fleetdash.web.zooxlabs.com/test-coverage/e2e-tests/index.html
  ${reset_color}"

  echo -e "${bright_green}
  View unit report on http://fleetdash.web.zooxlabs.com/test-coverage/unit-tests/index.html
  ${reset_color}"
}

main
