#!/usr/bin/env bash
# Resets the test database, creates two accounts, starts the API on :8099 and runs the tests.
#   MYSQL="mysql -uroot" bash tests/run.sh
set -euo pipefail
cd "$(dirname "$0")/.."
MYSQL=${MYSQL:-mysql -uroot}
DB=${UO_TEST_DB:-uni_organiser_test}

$MYSQL -e "DROP DATABASE IF EXISTS $DB; CREATE DATABASE $DB CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
$MYSQL "$DB" < db/sql/001_initial.sql
UO_DB=$DB UO_PASSWORD='correct horse battery' php bin/create-user.php rob@example.com Rob >/dev/null
UO_DB=$DB UO_PASSWORD='another long password' php bin/create-user.php eve@example.com Eve >/dev/null

UO_DB=$DB php -S 127.0.0.1:8099 -t public public/router.php >/tmp/uo-test-server.log 2>&1 &
SERVER=$!
trap 'kill $SERVER' EXIT
sleep 0.7
node --test --test-concurrency=1 tests/api.test.mjs
