#!/usr/bin/env bash
set -euo pipefail
# Real-app proof for PR 49560. No fabricated provider records or database copies.
# Preparation performed for this proof:
# 1. In a fresh scratch working directory, ran the real CLI:
#    claude -p 'Reply with exactly: Usage formatting proof.' --model haiku --tools '' \
#      --session-id 49560000-0000-4000-8000-000000000002
# 2. Copied ONLY that run's original JSONL into an isolated Claude config directory.
#    Both servers inherited CLAUDE_CONFIG_DIR pointing at that same directory.
# 3. Each checkout: vp i; T3CODE_DEV_SEED=0 vp run dev --host 0.0.0.0
#    Each mobile checkout: REACT_NATIVE_PACKAGER_HOSTNAME=<reachable-host> \
#      vp run dev:client --port <own-port>
# 4. Leased Pixel 9 (Android API 36), installed via mobile:install android.
#    Head was captured first; app storage was cleared before loading base Metro.
#    Paired exactly one environment on each side using pair-client.sh.
# 5. Same UI sequence: Settings > General > Language > Deutsch; Nutzung;
#    disable Codex, leave Claude enabled, Erlauben; Kosten; 30 T. (default).
#    Never enabled an environment filter. Light theme, default text size.
#
# Usage:
#   bash usage-proof-driver.sh seed <original-real-transcript.jsonl> <isolated-claude-dir>
#   bash usage-proof-driver.sh capture <session> <new-output-directory> <scroll-pixels>
# Actual scroll-pixels: head 400, base 398. This compensates native gesture
# rounding; the resulting app view has identical pixel positions on both sides.
case "$1" in
  seed)
    mkdir -p "$3/projects/proof"
    cp "$2" "$3/projects/proof/transcript.jsonl"
    ;;
  capture)
    session="$2"; out="$3"; distance="$4"
    mkdir -p "$out"
    agent-device scroll bottom --session "$session"
    agent-device scroll up --pixels "$distance" --duration-ms 1500 --session "$session"
    agent-device screenshot "$out/stable-a.png" --session "$session"
    sleep 0.5
    agent-device screenshot "$out/final.png" --session "$session"
    python3 - "$out" <<'PY'
from PIL import Image, ImageChops
import sys
from pathlib import Path
p = Path(sys.argv[1])
a = Image.open(p / 'stable-a.png').convert('RGB')
b = Image.open(p / 'final.png').convert('RGB')
assert ImageChops.difference(a, b).getbbox() is None, 'Frame is not stable; capture again.'
print('Stable frames: pixel identical.')
PY
    ;;
  *) exit 2 ;;
esac
