#!/usr/bin/env bash
# Run keyvaluedb-web's wasm-bindgen integration tests against headless Chrome
# via chromedriver. Resolves chromedriver from $CHROMEDRIVER, system PATH,
# or auto-downloads a Chrome-matched build via the scripts/_chromedriver_helper.sh
# helper.
#
# Usage:
#   ./run_tests.sh                # All tests
#   ./run_tests.sh --no-fail-fast # Continue past the first failure
#   ./run_tests.sh -E 'test(list)' # Filter
#
# Arguments are forwarded to `cargo nextest run`.

set -eo pipefail

SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
WORKSPACE_DIR="$(cd "$SCRIPTDIR/.." && pwd)"

# shellcheck disable=SC1091
source "$WORKSPACE_DIR/scripts/_chromedriver_helper.sh"

CHROMEDRIVER="$(chromedriver_binary_path)" || {
    echo "ERROR: could not resolve a chromedriver binary" >&2
    exit 1
}
export CHROMEDRIVER

pushd "$WORKSPACE_DIR" >/dev/null
# Limit nextest parallelism: each wasm-bindgen-test spawns its own chromedriver
# + Chrome instance, and running many at once tends to make Chrome time out
# during DevTools connection. Two-at-a-time is a stable compromise on both
# laptops and CI runners; override with NEXTEST_TEST_THREADS=N.
WASM_BINDGEN_TEST_TIMEOUT=120 cargo nextest run \
    --target wasm32-unknown-unknown \
    --test-threads="${NEXTEST_TEST_THREADS:-2}" \
    -p keyvaluedb-web \
    "$@"
popd >/dev/null
