#!/bin/sh
# runs the suite. asks to start the stack if it isn't up, and rebuilds the web when web/ changed
# pnpm e2e the whole suite
# pnpm e2e specs/star.spec.ts one spec
# pnpm e2e --report=report.html also write an html report
# pnpm e2e --har=run.har also write the requests as a har
# pnpm e2e --login=ui every new account logs in through the login page
# pnpm e2e --web-modules=DIR the web node_modules the report loads, default web/node_modules
# pnpm e2e --sweep clean up what a crashed run left
# every other argument goes to playwright
set -eu
here="$(dirname "$0")"
sweep=
n=$#
while [ "$n" -gt 0 ]; do
arg="$1"
shift
n=$((n - 1))
case $arg in
--report=*) export E2E_REPORT="${arg#*=}" ;;
--har=*) export E2E_REPORT_HAR="${arg#*=}" ;;
--login=*) export E2E_LOGIN="${arg#*=}" ;;
--web-modules=*) export E2E_WEB_MODULES="${arg#*=}" ;;
--sweep) sweep=1 ;;
*) set -- "$@" "$arg" ;;
esac
done
E2E_STACK="$("$here/up" --stack)"
export E2E_STACK
if [ "$E2E_STACK" = pc ]; then
root="$(cd "$here/.." && pwd)"
E2E_KNOT_SCAN="${E2E_KNOT_SCAN:-$(here="$root/localinfra/pc" && . "$here/bin/env.sh" && echo "$TNGL_STATE/data/knot2/repositories")}"
export E2E_KNOT_SCAN
E2E_PC_STATE="${E2E_PC_STATE:-$(here="$root/localinfra/pc" && . "$here/bin/env.sh" && echo "$TNGL_STATE")}"
export E2E_PC_STATE
# playwright's own chromium download won't run here, so the nix app supplies one
if [ -z "${E2E_CHROMIUM:-}" ] && command -v nix >/dev/null 2>&1; then
E2E_CHROMIUM=$(cd "$root" && nix run .#localinfra -- --exec printenv E2E_CHROMIUM 2>/dev/null) || E2E_CHROMIUM=
export E2E_CHROMIUM
fi
fi
status=0
"$here/up" --check || status=$?
case $status in
0) ;;
2)
# the sweep only removes accounts, it doesn't look at the web
if [ -z "$sweep" ]; then
echo "web/ changed since the web was built, rebuilding it, this takes a few minutes" >&2
"$here/up"
fi
;;
*)
if [ ! -t 0 ]; then
echo "the stack isn't up, start it with pnpm e2e:up" >&2
exit 1
fi
printf "the e2e stack isn't up, start it? the web build takes a few minutes [y/N] " >&2
read -r answer
case $answer in
[yY]*) "$here/up" ;;
*) exit 1 ;;
esac
;;
esac
cd "$here"
[ -d node_modules ] || pnpm install
[ -n "${E2E_CHROMIUM:-}" ] || pnpm exec playwright install chromium
if [ -n "$sweep" ]; then
export E2E_SWEEP=1
set -- --project=sweep "$@"
fi
exec pnpm exec playwright test "$@"