#!/usr/bin/env bash
# PR #28662 (fix(web): Diff pane expands unmodified lines again), fixes #27171.
# Claim: already-fixed. #33629 (e6fa2e35c3) fixed the same root cause on main: Pierre dropped loaded file
# contents when the host re-supplied an unchanged diff as a new parsed object. Main's patched
# @pierre/diffs matches pending contents by cacheKey instead of object identity, so PR #28662's
# parse-identity cache in DiffPanel is no longer needed for expansion to work.
# Shows: main's browser regression for exactly that scenario passes on main; #27171 was closed by #33629.
# Needs deps installed (`vp i`), Playwright's chromium headless shell
# (`node apps/web/node_modules/playwright/cli.js install --only-shell chromium`), and `gh` authenticated.
set -euo pipefail

REPO=pingdotgg/t3code
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/28662/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"

PATCH="patches/@pierre__diffs@1.7.0-beta.3.patch"
TEST=apps/web/src/components/diffs/codeViewContextExpansion.browser.test.ts

# 1. Issue and superseding PR.
issue=$(gh api "repos/$REPO/issues/27171" --jq '"\(.state) \(.state_reason)"')
closer=$(gh api graphql -f query='{repository(owner:"pingdotgg",name:"t3code"){issue(number:27171){timelineItems(itemTypes:CLOSED_EVENT,last:1){nodes{...on ClosedEvent{closer{...on PullRequest{number merged}}}}}}}}' \
  --jq '.data.repository.issue.timelineItems.nodes[0].closer | "\(.number) \(.merged)"')
if [ "$issue" = "closed completed" ] && [ "$closer" = "33629 true" ]; then
  pass "issue #27171 (the bug this PR fixes) is closed as completed by merged PR #33629"
else
  fail "issue #27171 state='$issue' closer='$closer'"
fi
sha=$(git log "$MAIN" --format=%h --grep="(#33629)" | head -1)
if [ -n "$sha" ]; then pass "#33629 is on main as $sha ($(git log -1 --format=%s "$sha"))"
else fail "#33629 not found on main"; fi

# 2. Static: main's Pierre patch applies loaded contents to an equal diff, not only the same object.
if git show "$MAIN:$PATCH" | grep -qF '+			if (areDiffTargetsEqual(pendingHydratedDiff.expectedDiff, fileDiff)) {' \
  && git show "$MAIN:pnpm-workspace.yaml" | grep -qF '"@pierre/diffs@1.7.0-beta.3": patches/@pierre__diffs@1.7.0-beta.3.patch'; then
  pass "main patches @pierre/diffs to match pending file contents by cacheKey (areDiffTargetsEqual), replacing the identity check"
else
  fail "main's @pierre/diffs patch does not contain the cacheKey match"
fi
if [ -n "$sha" ] && ! git merge-base --is-ancestor "$sha" "$MB" \
  && ! git grep -qF 'areDiffTargetsEqual(pendingHydratedDiff.expectedDiff' "$MB" -- patches/; then
  pass "the cacheKey match landed after the PR's merge-base ($MB), which still had the identity check the PR works around"
else
  fail "the PR's merge-base already had the cacheKey match (unexpected)"
fi

# 3. Behavioral: re-supplying an unchanged diff as a new parsed object, then expanding, shows hidden lines on main.
git checkout -q --detach "$MAIN"
vp test run --reporter=verbose "$TEST" > "$WORK/main.log" 2>&1 || true
grep -E "^ +(✓|×)|Tests +[0-9]|Executable doesn't exist" "$WORK/main.log" || tail -30 "$WORK/main.log"
if grep -qF "Executable doesn't exist" "$WORK/main.log"; then
  echo "SETUP  Playwright's chromium headless shell is missing; install it with:"
  echo "       node apps/web/node_modules/playwright/cli.js install --only-shell chromium"
  exit 2
fi
if grep -qE "Tests +1 passed" "$WORK/main.log" && ! grep -qE "Tests +.*failed" "$WORK/main.log"; then
  pass "on main, hidden context expands after the host re-supplies an unchanged diff as a new object (the #27171 trigger)"
else
  fail "main's expansion regression did not pass"
fi

if [ "$ok" = 1 ]; then
  echo "VERDICT: CLOSE (already-fixed) - main expands hidden diff context after refreshes via #33629's cacheKey match"
  PROOF_DONE=1; exit 0
fi
echo "VERDICT: NOT PROVEN - at least one check failed"
exit 1
