#!/usr/bin/env bash
set -euo pipefail
identity=$1
out=$2
session=$3
mkdir -p "$out"
link=$(node -e 'const x=require(process.argv[1]); process.stdout.write(`t3code-dev://issues/${x.environmentId}/${x.projectId}/github/pingdotgg%2Ft3code/46214`)' "$identity")
agent-device record start "$out/selection.mp4" --hide-touches --session "$session"
for mode in explicit-host hostless; do
  target=$link
  if [[ $mode == explicit-host ]]; then target="$link?host=github.com"; fi
  agent-device open com.t3tools.t3code.dev "$target" --session "$session" >/dev/null
  agent-device snapshot -i --session "$session" > "$out/$mode.txt"
  for attempt in 1 2 3; do
    agent-device screenshot "$out/$mode-a.png" --session "$session" >/dev/null
    python3 -c 'import time; time.sleep(0.5)'
    agent-device screenshot "$out/$mode.png" --session "$session" >/dev/null
    changed=$(compare -metric AE "$out/$mode-a.png" "$out/$mode.png" null: 2>&1 || true)
    if [[ $changed == 0 ]]; then break; fi
    if [[ $attempt == 3 ]]; then echo "Screenshots did not settle: $changed pixels" >&2; exit 1; fi
  done
  printf '%s: ' "$mode"
  rg 'Issue #46214,' "$out/$mode.txt"
done
agent-device record stop --session "$session"
