#!/usr/bin/env bash

set -euo pipefail

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

main(){
  setup
  install
  generate_unit_test_reports
  display_location
}

setup(){
    current_directory="$(pwd)"

    cleanup() {
        cd ${current_directory}
    }
    trap cleanup EXIT

    if [[ "$OSTYPE" == "darwin"* ]]; then
        driving_root="$(cd ../../../../ && pwd)"
    else
        driving_root=$(readlink -f $(dirname "${BASH_SOURCE[0]}")/../../../../../)
    fi
    spectrum_root=${driving_root}/spectrum
    firefly_root=${spectrum_root}/apps/firefly
    cd "${firefly_root}/client"
}

install(){
  # 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 ./coverage/lcov-report/ s3://zoox-web/firefly/test-coverage/unit-tests
}

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

main
