#!/usr/bin/env bash
# Run from a disposable checkout of current main after `vp i`.
# Place middle-click-primary.e2e.ts in scripts/scenarios first.
set -euo pipefail
scratch=$(mktemp -d)
Xvfb :181 -screen 0 1280x900x24 -nolisten tcp >"$scratch/xvfb.log" 2>&1 &
xvfb_pid=$!
chrome_pid=''
cleanup() {
  if [ -n "$chrome_pid" ]; then kill "$chrome_pid" 2>/dev/null || true; fi
  kill "$xvfb_pid" 2>/dev/null || true
}
trap cleanup EXIT
DISPLAY=:181 google-chrome --no-sandbox --disable-gpu --no-first-run \
  --no-default-browser-check --disable-dev-shm-usage --remote-debugging-port=9181 \
  --user-data-dir="$scratch/chrome" --ozone-platform=x11 about:blank >"$scratch/chrome.log" 2>&1 &
chrome_pid=$!
python3 - <<'PY'
p='scripts/e2e.config.ts'
s=open(p).read().replace(
 'engine: web({ viewport: { width: 1280, height: 800 } }),',
 'engine: web({ viewport: { width: 1280, height: 800 }, connect: { cdpEndpoint: () => "http://127.0.0.1:9181" } }),')
open(p,'w').write(s)
PY
T3CODE_DEV_SEED=0 T3_SCENARIO_INHERIT_ENV=T3CODE_DEV_SEED \
vp run scenarios:prove scripts/scenarios/middle-click-primary.e2e.ts \
  --base 5f95653f25ae0f5655145fa83d94d618c2951432 \
  --head fba987f45dee16c224cc3a82bbd10ff0672723b4 \
  --upload-command 'ghdrop upload'
