#!/usr/bin/env bash

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color

function prism_is_running() {
  curl --silent "http://localhost:4010" >/dev/null 2>&1
}

function is_overriding_api_base_url() {
  [ -n "$TEST_API_BASE_URL" ]
}

if is_overriding_api_base_url ; then
  # If someone is running the tests against the live API, we can trust they know
  # what they're doing and exit early.
  echo -e "${GREEN}✔ Running tests against ${TEST_API_BASE_URL}${NC}"

  exit 0
elif prism_is_running ; then
  echo -e "${GREEN}✔ Mock prism server is running with your OpenAPI spec${NC}"
  echo

  exit 0
else
  echo -e "${RED}ERROR:${NC} The test suite will not run without a mock Prism server"
  echo -e "running against your OpenAPI spec."
  echo
  echo -e "${YELLOW}To fix:${NC}"
  echo
  echo -e "1. Install Prism (requires Node 16+):"
  echo
  echo -e "  With npm:"
  echo -e "    \$ ${YELLOW}npm install -g @stoplight/prism-cli${NC}"
  echo
  echo -e "  With yarn:"
  echo -e "    \$ ${YELLOW}yarn global add @stoplight/prism-cli${NC}"
  echo
  echo -e "2. Run the mock server"
  echo
  echo -e "  To run the server, pass in the path of your OpenAPI"
  echo -e "  spec to the prism command:"
  echo
  echo -e "    \$ ${YELLOW}prism mock path/to/your.openapi.yml${NC}"
  echo

  exit 1
fi
