#!/usr/bin/env bash
# PR #38830 (fix(acp-registry): drop inherited npm prefix casings for npx installs)
# Claim: already-fixed. #38839 (528687d226) fixed the same Windows NPM_CONFIG_PREFIX collision
# (issue #38697), and the PR branch's own latest merge resolved to main's version, so its head
# now changes nothing relative to main.
# Shows: the PR head tree equals its merge-base with main; #38839's regression test passes at
# 528687d226 and on current main, and fails at its parent. Needs deps installed (`vp i`) and `gh` authenticated.
set -euo pipefail

REPO=pingdotgg/t3code
FIX=528687d2260ca0c132c6d634bcb15fc94c4604c6
TEST=packages/provider-acp-registry/src/server/AcpRegistrySupport.test.ts
TEST_NAME="gives Windows npm commands only the managed prefix spelling"
ok=1
pass() { echo "PASS  $*"; }
fail() { echo "FAIL  $*"; ok=0; }

if [ -n "$(git status --porcelain)" ]; then
  echo "refusing to run on a dirty tree"
  exit 2
fi
ORIG=$(git symbolic-ref -q --short HEAD || git rev-parse HEAD)
WORK=$(mktemp -d)
cleanup() {
  git checkout -q -f -- . 2>/dev/null || true
  git checkout -q -f "$ORIG"
  rm -rf "$WORK"
}
trap 'rc=$?; cleanup; [ "${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/38830/head
PR_HEAD=$(git rev-parse FETCH_HEAD)
MB=$(git merge-base "$MAIN" "$PR_HEAD")
echo "main=$MAIN pr_head=$PR_HEAD merge_base=$MB"

# 1. #38839 is merged and on main.
if git merge-base --is-ancestor "$FIX" "$MAIN"; then
  pass "#38839 is on main as ${FIX:0:10} ($(git log -1 --format=%s "$FIX"))"
else
  fail "${FIX:0:10} is not on main"
fi
merged=$(gh pr view 38839 --repo "$REPO" --json state,mergeCommit --jq '"\(.state) \(.mergeCommit.oid)"')
if [ "$merged" = "MERGED $FIX" ]; then
  pass "gh: #38839 is MERGED with merge commit ${FIX:0:10}"
else
  fail "gh: #38839 state/merge commit is '$merged'"
fi

# 2. The PR head adds nothing: its tree equals its merge-base with main, which already contains #38839.
if git merge-base --is-ancestor "$FIX" "$MB"; then
  pass "the PR's merge-base ${MB:0:10} already contains #38839"
else
  fail "the PR's merge-base does not contain #38839"
fi
changed=$(git diff --name-only "$MB" "$PR_HEAD")
if [ -z "$changed" ]; then
  pass "git diff merge-base..PR head is empty: the PR changes no files"
else
  fail "the PR head still changes: $changed"
fi
merged_tree=$(git merge-tree --write-tree "$MAIN" "$PR_HEAD")
if [ "$merged_tree" = "$(git rev-parse "$MAIN^{tree}")" ]; then
  pass "merging the PR head into main yields main's tree unchanged"
else
  fail "merging the PR head into main changes the tree"
fi

# 3. Behavior: #38839's regression test passes at the fix and fails at its parent.
run_test() {
  vp test run "$TEST" -t "$TEST_NAME" > "$1" 2>&1 || true
  grep -E "Tests +[0-9]" "$1" || true
}
git checkout -q --detach "$FIX"
run_test "$WORK/fix.log"
if grep -qE "Tests +1 passed" "$WORK/fix.log" && ! grep -qE "Tests +.*[0-9]+ failed" "$WORK/fix.log"; then
  pass "at ${FIX:0:10}, a win32 npx install with an inherited NPM_CONFIG_PREFIX gives npm only the managed npm_config_prefix"
else
  fail "the regression test does not pass at ${FIX:0:10}"
fi

git checkout -q --detach "$MAIN"
run_test "$WORK/main.log"
if grep -qE "Tests +1 passed" "$WORK/main.log" && ! grep -qE "Tests +.*[0-9]+ failed" "$WORK/main.log"; then
  pass "on main ${MAIN:0:10}, the same test passes"
else
  fail "the regression test does not pass on main ${MAIN:0:10}"
fi

git checkout -q --detach "$FIX^"
git checkout -q "$FIX" -- "$TEST"
run_test "$WORK/parent.log"
grep -E "AssertionError|expected \[" "$WORK/parent.log" | head -3 || true
if grep -qE "Tests +1 failed" "$WORK/parent.log"; then
  pass "at the fix's parent ${FIX:0:10}^, the same test fails: npm receives both prefix spellings"
else
  fail "the regression test does not fail at the fix's parent"
fi
git checkout -q -f -- .

if [ "$ok" = 1 ]; then
  echo "VERDICT: CLOSE (already-fixed) - #38839 fixed issue #38697 and this PR's head no longer changes anything"
  PROOF_DONE=1; exit 0
fi
echo "VERDICT: claim not supported"
exit 1
