#!/usr/bin/env bash # The analytics page in a real Chrome, over a deployment of 490 accounts. # # Starts crates/didbot-serve/examples/analytics_fixture.rs on a free loopback # port, waits while it seeds, then runs # crates/didbot-serve/tests/browser/analytics.mjs against it with Playwright # driving the Chrome on this machine. Screenshots of each tab, at desktop and # phone widths, are left in DIDBOT_ANALYTICS_SHOTS. # # Opt-in, not a commit hook: seeding takes a minute in a debug build and it # wants a browser. scripts/ci.sh runs it when DIDBOT_E2E=1. # # Usage: scripts/test-analytics-browser.sh set -euo pipefail cd "$(dirname "$0")/.." if [ "$#" -ne 0 ]; then echo "usage: $0" >&2 exit 2 fi CHROME="${CHROME:-$(command -v google-chrome || command -v chromium || true)}" if [ -z "$CHROME" ]; then echo "test-analytics-browser: no Chrome on PATH; set CHROME to run it" >&2 exit 0 fi SHOTS="${DIDBOT_ANALYTICS_SHOTS:-${TMPDIR:-/tmp}/didbot-analytics-shots}" WORK="$(mktemp -d "${TMPDIR:-/tmp}/didbot-analytics-browser.XXXXXX")" PID="" cleanup() { local status=$? if [ -n "$PID" ]; then kill -TERM "$PID" 2>/dev/null || true wait "$PID" 2>/dev/null || true fi if [ "$status" -ne 0 ]; then echo "test-analytics-browser: failed; the fixture's log is in $WORK" >&2 else rm -rf "$WORK" fi exit "$status" } trap cleanup EXIT INT TERM PORT="$(python3 -c 'import socket; s = socket.socket(); s.bind(("127.0.0.1", 0)); print(s.getsockname()[1])')" export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-$PWD/target}" cargo build --quiet -p didbot-serve --example analytics_fixture "$CARGO_TARGET_DIR/debug/examples/analytics_fixture" "$PORT" 10 >"$WORK/fixture.log" 2>&1 & PID=$! waited=0 until grep -q "^operator cookie:" "$WORK/fixture.log"; do if ! kill -0 "$PID" 2>/dev/null || [ "$waited" -gt 600 ]; then echo "test-analytics-browser: the fixture never started" >&2 cat "$WORK/fixture.log" >&2 exit 1 fi waited=$((waited + 1)) sleep 0.5 done (cd crates/didbot-serve/tests/browser && npm ci --silent --no-audit --no-fund) ANALYTICS_URL="http://127.0.0.1:$PORT" \ ANALYTICS_COOKIE="$(sed -n 's/^operator cookie: //p' "$WORK/fixture.log")" \ ANALYTICS_SHOTS="$SHOTS" \ CHROME="$CHROME" \ node crates/didbot-serve/tests/browser/analytics.mjs echo "screenshots are in $SHOTS" >&2