← back to Govarbitrage

apps/mobile/device-proof-evidence/GovArbUITest/watch-capture.sh

63 lines

#!/bin/bash
# Background watcher: retries the XCUITest until the iPhone is unlocked, then
# captures the post-tap connection banner and finalizes evidence. Stops on first
# success. HARD GATES unchanged (no ASC/prod writes).
set -uo pipefail
SP="/private/tmp/claude-501/-Volumes-Henry-mac2-offload-govarbitrage-apps-mobile/67a7d3e4-f953-493b-81f3-842d7b14f1d7/scratchpad/GovArbUITest"
P8="/Users/macstudio3/.appstoreconnect/private_keys/AuthKey_72Y2TZT54R.p8"
EV="/Volumes/Henry/mac2-offload/govarbitrage/apps/mobile/device-proof-evidence"
DEV="00008150-001039662E3B401C"
LOG="$SP/watch-capture.log"
: > "$LOG"
MAX=45   # ~45 attempts
for i in $(seq 1 $MAX); do
  echo "[attempt $i/$MAX $(date +%H:%M:%S)] running xcodebuild test..." >> "$LOG"
  rm -rf "$SP/result.xcresult"
  timeout 110 xcodebuild test \
    -project "$SP/GovArbUITest.xcodeproj" -scheme GovArbUITests \
    -destination "platform=iOS,id=$DEV" \
    -derivedDataPath "$SP/DerivedData" -resultBundlePath "$SP/result.xcresult" \
    -allowProvisioningUpdates -authenticationKeyPath "$P8" \
    -authenticationKeyID 72Y2TZT54R -authenticationKeyIssuerID cfbd63ed-301b-465c-aad7-49e94420ad70 \
    >> "$LOG" 2>&1
  rc=$?
  if [ -f "$SP/result.xcresult/Info.plist" ] && grep -q "TEST SUCCEEDED" "$LOG"; then
    echo "[attempt $i] TEST SUCCEEDED — exporting" >> "$LOG"
    OUT="$SP/attachments_final"; rm -rf "$OUT"
    xcrun xcresulttool export attachments --path "$SP/result.xcresult" --output-path "$OUT" >> "$LOG" 2>&1
    # map named attachments -> evidence files
    python3 - "$OUT" "$EV" >> "$LOG" 2>&1 <<'PY'
import json,sys,shutil,datetime,os
out,ev=sys.argv[1],sys.argv[2]
iso=datetime.datetime.now().strftime("%Y-%m-%dT%H-%M-%S")
m=json.load(open(os.path.join(out,"manifest.json")))
name_map={
 "cold-launch-opportunities":"device-journey-1-cold-launch-opportunities",
 "detail-maxbid-valuation":"device-journey-2-detail-maxbid-valuation",
 "settings":"device-journey-3-settings",
 "settings-connection-test":"device-journey-4-settings-connection-test",
 "siwa-available":"device-journey-5-siwa-available",
}
copied=[]
for t in m:
  for a in t.get("attachments",[]):
    nm=(a.get("suggestedHumanReadableName") or "").split("_")[0]
    fn=a.get("exportedFileName")
    if nm in name_map and fn and fn.endswith(".png"):
      dst=os.path.join(ev,f"{name_map[nm]}-{iso}.png")
      shutil.copy(os.path.join(out,fn),dst)
      copied.append(os.path.basename(dst))
print("COPIED:",copied)
PY
    echo "[done] finalized post-tap capture $(date +%H:%M:%S)" >> "$LOG"
    export TK_AGENT=govarb-xcuitest
    tk log TK-10279 "watch-capture: phone unlocked on attempt $i — post-tap connection banner captured + all 5 journey shots re-exported to device-proof-evidence/" >/dev/null 2>&1
    exit 0
  fi
  echo "[attempt $i] not ready (rc=$rc, likely locked) — sleeping 20s" >> "$LOG"
  pkill -9 xcodebuild >/dev/null 2>&1; pkill -9 -f GovArbUITests >/dev/null 2>&1
  sleep 20
done
echo "[giveup] $MAX attempts exhausted, phone never unlocked" >> "$LOG"
exit 1