#!/usr/bin/env bash
# The browser commands used for the paired Settings recordings.
# Usage: bash capture-refresh.sh <agent-browser> <session> <output-directory> <before|after>
set -eu
browser=$1
session=$2
output=$3
side=$4
run="$output/$side-$(date -u +%Y%m%dT%H%M%SZ)"
mkdir -p "$run"
ab() { "$browser" --session "$session" "$@"; }
ab wait --text "MiniMax Code" --timeout 60000
ab set viewport 1440 900
# Conceal environment identity; no provider status or error text is changed.
ab eval '(()=>{const style=document.createElement("style");style.textContent="button[aria-label^=\"Environment scope\"],nav[aria-label=\"Threads\"],p:has([role=status]) {visibility:hidden!important}";document.head.append(style)})()'
ab find role button click --name 'Select MiniMax Code'
ab record start "$run/refresh.webm"
ab find role button click --name 'Refresh provider status'
if [ "$side" = before ]; then
  ab wait --text 'The ACP agent could not create a test session.'
else
  ab wait --text 'Not authenticated · Sign in to MiniMax Code'
fi
ab wait --fn '!document.body.innerText.includes("Refreshing providers") && !document.body.innerText.includes("Checking ACP authentication")'
ab mouse move 1350 880
for attempt in 1 2 3 4 5 6 7 8 9 10; do
  ab screenshot "$run/stable-1.png"
  sleep 0.5
  ab screenshot "$run/stable-2.png"
  if cmp -s "$run/stable-1.png" "$run/stable-2.png"; then break; fi
done
cmp "$run/stable-1.png" "$run/stable-2.png"
ab record stop
