#!/usr/bin/env bash
#
# Measure lab Core Web Vitals — LCP, INP, CLS, plus FCP and TTFB — for a
# running dragoman, in the Chrome installed on this machine. Installs the two
# packages it needs (playwright-core, web-vitals) on first use; no browser is
# downloaded.
#
# Usage:
#   scripts/web-vitals.sh                          # production, desktop, 3 runs per page
#   scripts/web-vitals.sh --mobile                 # a throttled phone
#   scripts/web-vitals.sh --label 0.5.5 --runs 5
#   scripts/web-vitals.sh --base http://127.0.0.1:3456 --page '/10.1126/science.1102896?noredirect'
#   column -t -s $'\t' bench/web-vitals.tsv
#
# See scripts/web-vitals/run.mjs for every option. Needs node and pnpm.

set -euo pipefail

dir="$(cd "$(dirname "$0")/web-vitals" && pwd)"
# Homebrew's prefix on Apple silicon is not on every non-login shell's PATH.
export PATH="$PATH:/opt/homebrew/bin"

if [[ ! -d "$dir/node_modules/playwright-core" || ! -d "$dir/node_modules/web-vitals" ]]; then
  pnpm --dir "$dir" install --frozen-lockfile --silent
fi
exec node "$dir/run.mjs" "$@"
