#!/usr/bin/env bash
# PR #24436 (draft) "fix(web): expanded tool output stays above the composer" (Fixes #21967)
# Claim: already-fixed. #21968 (6700cf6f89, "fix(web): reveal expanded work rows hidden behind the composer")
# fixed the same issue #21967 and is on main.
# Shows: both PRs target #21967; 6700cf6f89 is on main; #21968's browser regression test (expanding a work row
# near the composer, including output that arrives late, ends above the composer with its header visible) passes
# on current main and fails on main just before #21968.
# Needs deps installed (`vp i`) and Playwright Chromium. Run from the root of a clean pingdotgg/t3code checkout.
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/24436/head
PR_HEAD=$(git rev-parse FETCH_HEAD)
SUPERSEDING=6700cf6f894b34cc76bf93ea3bdd33f0573c6855
TEST=apps/web/src/components/chat/MessagesTimeline.browser.test.tsx
NAME="reveals .* output"
echo "main=$MAIN pr=$PR_HEAD"
fail=0
check() { if eval "$2"; then echo "PASS  $1"; else echo "FAIL  $1"; fail=1; fi; }

check "#21968 ($SUPERSEDING) is an ancestor of main" 'git merge-base --is-ancestor "$SUPERSEDING" "$MAIN"'
check "this PR's description targets #21967" \
  'gh pr view 24436 --repo pingdotgg/t3code --json body --jq .body | grep -q "issues/21967"'
check "#21968 is merged and fixes #21967" \
  '[ "$(gh pr view 21968 --repo pingdotgg/t3code --json state --jq .state)" = MERGED ] &&
   gh pr view 21968 --repo pingdotgg/t3code --json body --jq .body | grep -q "Fixes pingdotgg/t3code#21967"'
check "this PR changes only MessagesTimeline.tsx, the file #21968 also fixed" \
  '[ "$(git diff --name-only "$(git merge-base "$MAIN" "$PR_HEAD")" "$PR_HEAD")" = apps/web/src/components/chat/MessagesTimeline.tsx ]'

run_reveal() { vp test run "$TEST" -t "$NAME" 2>&1 || true; }

git checkout -q --detach "$MAIN"
out=$(run_reveal)
echo "$out" | grep -E "Test Files|Tests " || echo "$out" | tail -40
check "main: the 'reveals <mode> output' browser tests pass (inline, at-end, delayed, grouped-delayed, grouped-end)" \
  'grep -qE "Tests +5 passed" <<<"$out" && ! grep -qE "[0-9]+ failed" <<<"$out"'

git checkout -q --detach "$SUPERSEDING^1"
git checkout -q "$SUPERSEDING" -- "$TEST"
out=$(run_reveal)
echo "$out" | grep -E "Test Files|Tests " || echo "$out" | tail -40
check "main before #21968 (with the same test overlaid): the reveal tests fail" \
  'grep -qE "Tests +[1-9][0-9]* failed" <<<"$out"'

if [ "$fail" -eq 0 ]; then
  echo "VERDICT: CLOSE (already-fixed) - #21967 is fixed on main by #21968 ($SUPERSEDING)"
  PROOF_DONE=1; exit 0
fi
echo "VERDICT: NOT PROVEN - see FAIL lines above"
exit 1
