#!/bin/zsh
# Run selected Drape Maestro flows against the Release build on the booted simulator; collect screenshots per flow.
set -u
S=/private/tmp/claude-501/-Users-weshuber/a8022c74-f7ae-438c-882b-8ac437e617c1/scratchpad/audit
FRONT=/Users/weshuber/Documents/Drape-AI/Drape-/frontend
OUT=/Users/weshuber/Documents/Drape-AI/audits/2026-11-03/raw/mobile-app
export JAVA_HOME=/opt/homebrew/opt/openjdk@21/libexec/openjdk.jdk/Contents/Home
export PATH="$JAVA_HOME/bin:/Users/weshuber/Documents/Drape-AI/tools/maestro-download/maestro/bin:$PATH"
set -a; source ${AUDIT_ACCOUNT_ENV:?set AUDIT_ACCOUNT_ENV to a file with DRAPE_TEST_EMAIL/DRAPE_TEST_PASSWORD}; set +a
APP_ID=com.kcb1099.drape; BACKEND_URL=https://dev-api.drape.to
DEVICE=$(xcrun simctl list devices booted | grep -oE '[0-9A-F-]{36}' | head -1)
flows=("$@")
for f in "${flows[@]}"; do
  name=$(basename "$f" .yaml); dir="$OUT/$name"; mkdir -p "$dir"; cd "$dir"
  echo "=== $name ==="
  maestro --device "$DEVICE" test --env APP_ID=$APP_ID --env DRAPE_APP_ID=$APP_ID --env BACKEND_URL=$BACKEND_URL \
    --env DRAPE_TEST_EMAIL="$DRAPE_TEST_EMAIL" --env DRAPE_TEST_PASSWORD="$DRAPE_TEST_PASSWORD" \
    --test-output-dir "$dir/debug" "$( [[ "$f" = /* ]] && echo "$f" || echo "$FRONT/$f" )" > "$dir/maestro.log" 2>&1
  rc=$?; find "$dir/debug" -path "*/takeScreenshot/*.png" -exec cp {} "$dir/" \; 2>/dev/null; for f in $(find "$dir/debug" -path "*/screenshots/*.png" 2>/dev/null); do cp "$f" "$dir/zz-failure-$(basename "$f")"; done
  echo "exit=$rc shots=$(find "$dir" -maxdepth 1 -name "*.png" | wc -l | tr -d ' ')"; grep -E "✅|❌|Passed|Failed|FAILED|PASSED" "$dir/maestro.log" | tail -3
done
