#!/usr/bin/env bash # Usage: scripts/e2e.sh [--tracks js,scala,fsharp] [--base-url ] [-- ] # End-to-end tests (e2e/, Playwright) of every track's four pages against the pure JS model. Copies the repo to a temp # dir, replaces each selected track's src/ with its solutions/, runs build-web.sh for Scala and F#, serves the copy with # scripts/serve.mjs on a free port, runs the tests against it and cleans up. The exit code is Playwright's. # --tracks the tracks to test (default: all three) # --base-url test an already served copy instead (no copy, no build); its /src must hold the code to test # -- ... passed to `playwright test`, e.g. -- --project=chromium, -- -g "snake", -- --repeat-each=3 # The first run installs @playwright/test into e2e/node_modules (npm, needs the network once). Browsers are never # downloaded: a missing one is skipped with a message (see e2e/playwright.config.mjs). set -euo pipefail repo="$(cd "$(dirname "$0")/.." && pwd)" tracks="js,scala,fsharp" base_url="" pw_args=() while [ $# -gt 0 ]; do case "$1" in --tracks) tracks="${2:?--tracks needs a value}"; shift 2 ;; --base-url) base_url="${2:?--base-url needs a value}"; shift 2 ;; --) shift; pw_args=("$@"); break ;; *) echo "usage: scripts/e2e.sh [--tracks js,scala,fsharp] [--base-url ] [-- ]" >&2; exit 2 ;; esac done IFS=',' read -r -a selected <<< "$tracks" for t in "${selected[@]}"; do case "$t" in js|scala|fsharp) ;; *) echo "unknown track: $t" >&2; exit 2 ;; esac done if [ ! -d "$repo/e2e/node_modules/@playwright/test" ]; then echo "e2e: installing @playwright/test into e2e/node_modules" >&2 (cd "$repo/e2e" && npm ci --no-audit --no-fund) || { echo "e2e: npm ci failed (no network?)" >&2; exit 2; } fi tmp="" server="" cleanup() { if [ -n "$server" ]; then kill "$server" 2>/dev/null || true; fi if [ -n "$tmp" ]; then rm -rf "$tmp"; fi } trap cleanup EXIT if [ -z "$base_url" ]; then tmp="$(mktemp -d)" # The copy leaves out builds lying around in the tree (*/web/dist), so a page is only testable once built here. tar -C "$repo" --exclude=.git --exclude=node_modules --exclude=bin --exclude=obj --exclude=.scala-build \ --exclude=.bsp --exclude=.superpowers --exclude=./e2e --exclude='./*/web/dist' -cf - . | tar -C "$tmp" -xf - for t in "${selected[@]}"; do rm -rf "$tmp/$t/src" cp -r "$tmp/$t/solutions" "$tmp/$t/src" if [ "$t" != js ]; then echo "e2e: building the $t pages (build-web.sh)" >&2 (cd "$tmp/$t" && ./build-web.sh > "$tmp/build-$t.log" 2>&1) || { cat "$tmp/build-$t.log" >&2; echo "e2e: $t build failed" >&2; exit 2; } fi done port="$(node -e "const s=require('net').createServer();s.listen(0,'127.0.0.1',()=>{console.log(s.address().port);s.close()})")" node "$tmp/scripts/serve.mjs" "$port" > "$tmp/serve.log" 2>&1 & server=$! base_url="http://127.0.0.1:$port" for _ in $(seq 1 50); do if node -e "fetch('$base_url/js/index.html').then(r=>process.exit(r.ok?0:1),()=>process.exit(1))"; then break; fi sleep 0.1 done fi echo "e2e: testing $tracks at $base_url" >&2 set +e (cd "$repo/e2e" && E2E_BASE_URL="$base_url" E2E_TRACKS="$tracks" npx --no-install playwright test ${pw_args[@]+"${pw_args[@]}"}) status=$? set -e exit $status