#!/usr/bin/env bash
# Re-proves the model-retention default flip in a scratch checkout of pingdotgg/t3code.
#   repro.sh <scratch-worktree> [fix-ref]
# B1: the fix's driver table, spliced into latest main and the #34993 head, then the fix as-is.
# B2: fx runtime transcript (real status builder + merge; only the fx CLI's answers are faked)
#     on latest main and on the fix.
# B3: the other suites that exercise the registry merge, on the fix.
# B4: the fix's fx status test (an `fx models` answer without ids is a failed listing) on main and on the fix.
set -u
W=${1:?scratch worktree}
FIX=${2:-fix/provider-model-retention-default}
HERE=$(cd "$(dirname "$0")" && pwd)
T=apps/server/src/provider/ProviderRegistry.test.ts
cd "$W"
git fetch -q origin main && MAIN=$(git rev-parse --short origin/main)
git fetch -q origin pull/34993/head && PR=$(git rev-parse --short FETCH_HEAD)
FIXSHA=$(git rev-parse --short "$FIX")
table() {
  (cd apps/server && vp test run src/provider/ProviderRegistry.test.ts --reporter=verbose 2>&1 |
    grep -E '(✓|×) .*(completed listing no longer returns|ready probe can omit)|Tests ' |
    sed -E 's/^ +//; s/ [0-9]+ms$//; s/^(✓|×) src\/provider\/ProviderRegistry\.test\.ts > /\1 /')
}
transcript() {
  cp "$HERE/FxRetention.proof.test.ts" apps/server/src/provider/
  (cd apps/server && vp test run src/provider/FxRetention.proof.test.ts --silent=false 2>&1 |
    grep -E '^(probe|merged)')
  rm apps/server/src/provider/FxRetention.proof.test.ts
}
for ref in "main:$MAIN" "pr34993:$PR"; do
  git checkout -q --detach "${ref#*:}" && python3 -I "$HERE/splice-table.py" "$FIX"
  echo "## B1 ${ref%%:*} ($(git rev-parse --short HEAD)) + fix's driver table"
  table
  git checkout -q -- "$T"
done
git checkout -q --detach "$FIXSHA"
echo "## B1 fix ($FIXSHA)"
table
for ref in "main:$MAIN" "fix:$FIXSHA"; do
  git checkout -q --detach "${ref#*:}"
  echo "## B2 ${ref%%:*} ($(git rev-parse --short HEAD)) fx runtime transcript"
  transcript
done
echo "## B3 fix ($FIXSHA) other suites that run the registry merge"
(cd apps/server && vp test run src/secrets/SecretCheck.test.ts src/provider/providerCompatibility.test.ts 2>&1 | grep -E 'Tests ')
FXT=packages/provider-fx/src/server/status.test.ts
for ref in "main:$MAIN" "fix:$FIXSHA"; do
  git checkout -q --detach "${ref#*:}" && git show "$FIXSHA:$FXT" > "$FXT"
  echo "## B4 ${ref%%:*} ($(git rev-parse --short HEAD)) + fix's fx status test"
  (cd packages/provider-fx && vp test run src/server/status.test.ts --reporter=verbose 2>&1 |
    grep -E '(✓|×) .*without ids|Tests ' | sed -E 's/^ +//; s/ [0-9]+ms$//')
  git checkout -q -- "$FXT"
done
git checkout -q --detach "$MAIN"
