#!/usr/bin/env bash
# PR #34078 "feat(usage): show Muse Code limits, and xAI and Antigravity accounts from a CLIProxyAPI hub"
# Claim: already-fixed. The same change merged to main as #33800 (44ebb2fe57); this takeover branch adds nothing.
# Shows: 44ebb2fe57 is on main; the PR branches from #33800's parent and every file it changes is
# byte-identical to #33800's commit; merging it into main changes nothing; those tests pass on main.
# Needs deps installed (`vp i`). Run from the root of a clean pingdotgg/t3code checkout: bash prove-pr-34078.sh
set -euo pipefail

if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
  echo "Refusing to run: the working tree has uncommitted changes." >&2
  exit 2
fi
ORIG=$(git symbolic-ref -q --short HEAD || git rev-parse HEAD)
restore() { git reset -q --hard; git checkout -q "$ORIG"; }
trap 'rc=$?; restore; [ "${PROOF_DONE:-0}" = 1 ] && exit 0; [ "$rc" -ne 0 ] && exit "$rc"; exit 1' EXIT

git fetch -q origin main

MAIN=$(git rev-parse FETCH_HEAD)
git fetch -q origin pull/34078/head
PR_HEAD=$(git rev-parse FETCH_HEAD)
SUPERSEDING=44ebb2fe57465d32f2bd07c5eb3d8e871bdf5ed5
TESTS=(
  apps/server/src/usage/cliproxyApi.test.ts
  packages/provider-core/src/server/managedProvider.test.ts
  packages/provider-muse/src/server/driver.test.ts
  packages/provider-muse/src/server/usageLimits.test.ts
)
echo "main=$MAIN pr=$PR_HEAD"
fail=0

if git merge-base --is-ancestor "$SUPERSEDING" "$MAIN"; then
  echo "PASS  #33800 ($SUPERSEDING, same title and change) is an ancestor of main"
else
  echo "FAIL  #33800 ($SUPERSEDING) is not on main"; fail=1
fi

MERGED_TREE=$(git merge-tree --write-tree "$MAIN" "$PR_HEAD" | head -1)
if [ "$MERGED_TREE" = "$(git rev-parse "$MAIN^{tree}")" ]; then
  echo "PASS  merging the PR head into main produces main's tree unchanged (tree $MERGED_TREE)"
else
  echo "FAIL  merging the PR head into main changes files:"; git diff --stat "$MAIN" "$MERGED_TREE"; fail=1
fi

MB=$(git merge-base "$MAIN" "$PR_HEAD")
if [ "$MB" = "$(git rev-parse "$SUPERSEDING^")" ]; then
  echo "PASS  the PR branches from the exact parent of #33800's merge commit ($MB)"
else
  echo "FAIL  the PR's merge-base $MB is not #33800's parent"; fail=1
fi
changed=0 differing=0
for f in $(git diff --name-only "$MB" "$PR_HEAD"); do
  changed=$((changed + 1))
  if [ "$(git rev-parse -q --verify "$PR_HEAD:$f" || true)" != "$(git rev-parse -q --verify "$SUPERSEDING:$f" || true)" ]; then
    echo "      differs from #33800: $f"; differing=$((differing + 1))
  fi
done
if [ "$changed" -gt 0 ] && [ "$differing" -eq 0 ]; then
  echo "PASS  all $changed files the PR changes are byte-identical to #33800's merge commit (the PR is a subset of #33800)"
else
  echo "FAIL  $differing of $changed changed files differ from #33800"; fail=1
fi

# main's own copies of these tests: #33800 added them, and they still pass on today's main.
git checkout -q --detach "$MAIN"
if vp test run "${TESTS[@]}"; then
  echo "PASS  main's copies of the PR's test files pass on current main"
else
  echo "FAIL  main's copies of the PR's test files fail on current main"; fail=1
fi

if [ "$fail" -eq 0 ]; then
  echo "VERDICT: CLOSE (already-fixed) - #34078 is fully contained in main via #33800 (44ebb2fe57)"
  PROOF_DONE=1; exit 0
fi
echo "VERDICT: NOT PROVEN - see FAIL lines above"
exit 1
