#!/bin/sh
# script/test: run the test suite. Uses `timeout` (GNU coreutils) when
# available so the run is hard-capped at 30s; on macOS without
# coreutils the cap is skipped.
set -eu

ROOT="$(cd "$(dirname "$0")/.." && pwd -P)"

rerun_verbose() {
    echo "--- Rerunning with verbose for details ---"
    yarn run vitest run --reporter=verbose
    exit 1
}

main() {
    cd "$ROOT"
    TIMEOUT="$(command -v timeout 2>/dev/null || command -v gtimeout 2>/dev/null || true)"
    if [ -n "$TIMEOUT" ]; then
        "$TIMEOUT" 30s yarn run vitest run --reporter=dot || rerun_verbose
    else
        yarn run vitest run --reporter=dot || rerun_verbose
    fi
}

main "$@"
