#!/bin/bash

# Exit on error
set -e

DIR="$(cd "$(dirname "$0")" && pwd)"
ENV_FILE="$DIR/../.env"

if [ -z "$1" ]
  then
    echo "No test file specified."
    exit 1
fi

# Get the test file to run from the first argument
TEST_FILE="$DIR/../tests/$1/index.js"

# Read environment variables from file if the file exists
if [ -f "$ENV_FILE" ]; then
  echo "Reading environment variables from file"
  export $(grep -v '^#' $ENV_FILE | xargs)
fi;

# Exit hook
trap onExit EXIT
onExit() {
  echo "Exiting"

  echo "Killing child processess"
  kill 0
}

# Run the test
echo "Launching tests"
k6 run -e DEV_MODE=1 $TEST_FILE
