#!/usr/bin/env bash
# PR #42918 (fix(client-runtime): a composer send right after Stop queues instead of failing to steer)
# Claim: already on main. The PR's only commit was squash-merged as 9e385083d6 "... (#42918)",
# but GitHub left the PR open. The squash carries the same patch, and merging the PR head into it changes nothing.
# Demonstrates: the PR's own regression case ("a run Stop has reached") fails on 9e385083d6^ and
# passes on 9e385083d6 and on current main.
# Needs deps installed (`vp i`) and `gh` authenticated. Run from the root of a clean t3code checkout.
set -euo pipefail

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)
TEST=packages/client-runtime/src/state/threadWorkflows.test.ts
cleanup() {
  git reset -q --hard 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

FIX=9e385083d6255d4f4a94153ed25533f309f0da16
ORIGINAL_PR_COMMIT=a9fbde880a466faaecbed3955f39323d67a3116c
git fetch -q origin main
MAIN=$(git rev-parse FETCH_HEAD)
git fetch -q origin pull/42918/head
PR_HEAD=$(git rev-parse FETCH_HEAD)
git cat-file -e "$FIX^{commit}"
git cat-file -e "$ORIGINAL_PR_COMMIT^{commit}"
FIX_PARENT=$(git rev-parse "$FIX^")
echo "main=$MAIN pr_head=$PR_HEAD"

FAILED=0
pass() { echo "PASS  $*"; }
fail() { echo "FAIL  $*"; FAILED=1; }

if git merge-base --is-ancestor "$FIX" "$MAIN"; then
  pass "$FIX is an ancestor of main"
else
  fail "$FIX is not on main"
fi

SUBJECT=$(git log -1 --format=%s "$FIX")
if [ "$SUBJECT" = "fix(client-runtime): a composer send right after Stop queues instead of failing to steer (#42918)" ]; then
  pass "$FIX is the squash of #42918: \"$SUBJECT\""
else
  fail "unexpected subject on $FIX: $SUBJECT"
fi

if [ "$(git diff "$ORIGINAL_PR_COMMIT^" "$ORIGINAL_PR_COMMIT")" = "$(git diff "$FIX_PARENT" "$FIX")" ]; then
  pass "the PR's commit $ORIGINAL_PR_COMMIT and $FIX change the same 3 files with an identical patch"
else
  fail "the patch of $FIX differs from the PR's commit $ORIGINAL_PR_COMMIT"
fi

MERGED_TREE=$(git merge-tree --write-tree "$FIX" "$PR_HEAD" | head -1)
if [ "$MERGED_TREE" = "$(git rev-parse "$FIX^{tree}")" ]; then
  pass "merging the current PR head $PR_HEAD into $FIX yields $FIX's own tree: the PR adds nothing more"
else
  fail "merging the PR head into $FIX changes the tree (the PR carries more than $FIX)"
fi

PR_STATE=$(gh pr view 42918 --repo pingdotgg/t3code --json state,mergeCommit --jq '"\(.state) mergeCommit=\(.mergeCommit.oid // "none")"')
echo "INFO  GitHub reports #42918 as: $PR_STATE"

NAME="thread workflows > gates composer steering for a run Stop has reached"
# result_of <log> -> pass|fail|missing (verbose lines read "✓ |project| src/... > name")
result_of() {
  local needle="threadWorkflows.test.ts > $NAME"
  if grep -F "✓ " "$1" | grep -qF "$needle"; then echo pass
  elif grep -F "× " "$1" | grep -qF "$needle"; then echo fail
  else echo missing; fi
}

# run_at <label> <commit> <expect: pass|fail>: the PR head's test file over the commit's source.
run_at() {
  local label=$1 commit=$2 expect=$3 log result
  log="$WORK/$label.log"
  git checkout -q -f --detach "$commit"
  git checkout -q "$PR_HEAD" -- "$TEST"
  vp test run --reporter=verbose "$TEST" > "$log" 2>&1 || true
  git reset -q --hard
  result=$(result_of "$log")
  echo "INFO  $label: $(grep -E '^ +Tests +' "$log" | head -1 | sed 's/^ *//')"
  if [ "$result" = "$expect" ]; then
    pass "the PR's test '$NAME' $([ "$expect" = pass ] && echo passes || echo fails) at $label ($commit)"
    if [ "$expect" = fail ]; then grep -E "AssertionError" "$log" | head -1 | sed 's/^ */      /' || true; fi
  else
    fail "the PR's test '$NAME' expected $expect at $label ($commit), got $result"
    grep -E "AssertionError|expected|Error" "$log" | head -5 || true
  fi
}

run_at "fix-parent" "$FIX_PARENT" fail
run_at "fix" "$FIX" pass
run_at "main" "$MAIN" pass

if [ "$FAILED" -ne 0 ]; then
  echo "VERDICT: NOT PROVEN - at least one check failed"
  exit 1
fi
echo "VERDICT: ALREADY FIXED - #42918's own commit is on main as $FIX; a composer send after Stop no longer steers on main, and the PR adds nothing main lacks"
PROOF_DONE=1; exit 0
