#!/usr/bin/env bash
# PR #37119 (fix(server): Antigravity ask_question asks each choice once with its free-form answer)
# Claim: already fixed on main by two merged PRs: #43233 (824474d7a3) folds agy's `qN_other` field into
# its `qN` choice, and #43258 (744ff6c8cb) shows each field's title as the question with a numbered header.
# Demonstrates: this PR's own regression test (appended unchanged to the test file) fails on 824474d7a3^
# (4 steps) and on 744ff6c8cb^ (form message as the question), and passes on 744ff6c8cb 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=apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.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

FOLD_FIX=824474d7a34d558ba551ee031d9c910fd3cac5aa   # #43233
BODY_FIX=744ff6c8cb06bc9adc9fdecf604efed3df2406a3   # #43258
PR_COMMIT=407e45bde96712bb5e511f6848cc79eea3f5daf2  # #37119 head when verified
git fetch -q origin main
MAIN=$(git rev-parse FETCH_HEAD)
git fetch -q origin pull/37119/head
PR_HEAD=$(git rev-parse FETCH_HEAD)
git cat-file -e "$FOLD_FIX^{commit}"
git cat-file -e "$BODY_FIX^{commit}"
git cat-file -e "$PR_COMMIT^{commit}"
FOLD_PARENT=$(git rev-parse "$FOLD_FIX^")
BODY_PARENT=$(git rev-parse "$BODY_FIX^")
echo "main=$MAIN pr_head=$PR_HEAD"

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

if [ "$PR_HEAD" = "$PR_COMMIT" ]; then
  pass "the PR head is still $PR_COMMIT"
else
  fail "the PR head moved to $PR_HEAD; re-verify"
fi

for c in "$FOLD_FIX" "$BODY_FIX"; do
  if git merge-base --is-ancestor "$c" "$MAIN"; then
    pass "$(git log -1 --format='%h "%s"' "$c") is on main"
  else
    fail "$c is not on main"
  fi
done
if git merge-base --is-ancestor "$FOLD_FIX" "$BODY_PARENT"; then
  pass "#43233 landed before #43258, so $BODY_PARENT (#43258's parent) already folds the Other field"
else
  fail "#43233 is not an ancestor of #43258's parent"
fi

ISSUE=$(gh api graphql -f query='query{repository(owner:"pingdotgg",name:"t3code"){issue(number:37111){state stateReason timelineItems(itemTypes:[CLOSED_EVENT],last:1){nodes{... on ClosedEvent{closer{... on PullRequest{number}}}}}}}}' \
  --jq '.data.repository.issue | "\(.state) \(.stateReason) closer=#\(.timelineItems.nodes[0].closer.number // "none")"')
if [ "$ISSUE" = "CLOSED COMPLETED closer=#43233" ]; then
  pass "issue #37111 (this PR's issue) is $ISSUE"
else
  fail "issue #37111 is not closed as completed by #43233: $ISSUE"
fi

# The PR's regression test is a new describe block at the end of the test file. Take exactly the
# lines the PR adds and append them to the file at each commit, so nothing else in the file changes.
git diff "$PR_COMMIT^^" "$PR_COMMIT" -- "$TEST" | sed -n '/^+++ /,$p' | sed '1d' | grep '^+' | sed 's/^+//' > "$WORK/block.ts"
if grep -q '^describe("AntigravityAdapterV2 questions"' "$WORK/block.ts" && [ "$(grep -c '' "$WORK/block.ts")" -gt 100 ]; then
  pass "extracted the PR's added test block ($(grep -c '' "$WORK/block.ts") lines) from $PR_COMMIT"
else
  fail "could not extract the PR's test block"
fi

NAME='AntigravityAdapterV2 questions > asks each ask_question choice once, with its `_other` field as the custom answer'
result_of() {
  if grep -F "✓ " "$1" | grep -qF "$NAME"; then echo pass
  elif grep -F "× " "$1" | grep -qF "$NAME"; then echo fail
  else echo missing; fi
}

# run_at <label> <commit> <expect: pass|fail>
run_at() {
  local label=$1 commit=$2 expect=$3 log result
  log="$WORK/$label.log"
  git checkout -q -f --detach "$commit"
  cat "$WORK/block.ts" >> "$TEST"
  vp test run --reporter=verbose "$TEST" -t "asks each ask_question choice once" > "$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 $([ "$expect" = pass ] && echo passes || echo fails) at $label ($commit)"
    if [ "$expect" = fail ]; then
      grep -E "AssertionError" "$log" | head -1 | sed 's/^ */      /' || true
      grep -E '^\+ +\[$|^[-+] +"' "$log" | head -8 | sed 's/^/      /' || true
    fi
  else
    fail "the PR's test expected $expect at $label ($commit), got $result"
    grep -E "AssertionError|Error" "$log" | head -5 || true
  fi
}

run_at "43233-parent" "$FOLD_PARENT" fail
run_at "43258-parent" "$BODY_PARENT" fail
run_at "43258" "$BODY_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 - #43233 and #43258 on main ask each agy ask_question choice once, with the question as its body and typed text sent to qN_other; #37119's own test passes on main without its source changes"
PROOF_DONE=1; exit 0
