#!/usr/bin/env bash
# PR #22792 (fix(server): honor custom Claude model options)
# Claim: already-fixed. Main compiles Claude turns against the instance-scoped model catalog since
# #27034 (2d0efd3584) and #31717 (43cf364dd2), which is the whole production change in this PR.
# Behavioral proof: the PR's own 13 added adapter tests pass on current main without its source change,
# and 11 of them fail when the catalog argument is removed from main's compile calls (the original bug).
# Needs deps installed (`vp i`) and `gh` authenticated. Run from the root of a clean t3code checkout.
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/22792/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"

ADAPTER=apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
TEST=apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts

# 1. The superseding commits are on main.
for pr in 27034 31717; do
  sha=$(git log "$MAIN" --format=%h --grep="(#$pr)" | head -1)
  if [ -n "$sha" ]; then pass "#$pr is on main as $sha ($(git log -1 --format=%s "$sha"))"
  else fail "#$pr not found on main"; fi
done
git show "$MAIN:$ADAPTER" > "$WORK/adapter-main.ts"
if grep -q "compileClaudeModelSelection(input.modelSelection, input.modelCatalog)" "$WORK/adapter-main.ts"; then
  pass "main's makeClaudeQueryOptions compiles the selection against the scoped catalog"
else
  fail "main's makeClaudeQueryOptions does not pass a catalog"
fi
issue=$(gh api "repos/$REPO/issues/16672" --jq '"\(.state) \(.state_reason) \(.closed_by.login)"')
if [ "$issue" = "closed completed juliusmarminge" ]; then
  pass "linked issue #16672 closed as completed by juliusmarminge"
else
  fail "issue #16672 state/closer is '$issue'"
fi

# 2. The PR's own tests pass on main without its source change.
git diff "$MB" "$PR_HEAD" -- "$TEST" > "$WORK/tests.diff"
if [ -n "$(git diff --name-only "$MB" "$PR_HEAD" -- ':!'"$ADAPTER" ':!'"$TEST")" ]; then
  fail "PR touches files beyond the adapter and its test"
fi
git checkout -q --detach "$MAIN"
git apply "$WORK/tests.diff"
vp test run --reporter=verbose "$TEST" -t "custom Claude" > "$WORK/main.log" 2>&1 || true
grep -E "^ +(✓|×)|Tests +[0-9]" "$WORK/main.log" || true
if grep -qE "Tests +13 passed" "$WORK/main.log" && ! grep -qE "Tests +.*[0-9]+ failed" "$WORK/main.log"; then
  pass "all 13 tests the PR adds pass on main ($MAIN) with main's adapter"
else
  fail "the PR's tests do not all pass on main"
fi

# 3. Mutation: drop the catalog argument from main's compile calls; the same tests must fail.
perl -0pi -e 's/compileClaudeModelSelection\(\s*([\w.]+),\s*(?:input\.)?modelCatalog,?\s*\)/compileClaudeModelSelection($1)/g' "$ADAPTER"
vp test run "$TEST" -t "custom Claude" > "$WORK/mutant.log" 2>&1 || true
grep -E "Tests +[0-9]" "$WORK/mutant.log" || true
if grep -qE "Tests +11 failed \| 2 passed" "$WORK/mutant.log"; then
  pass "with the catalog removed from main's compile calls, 11 of the 13 tests fail (2 are the PR's controls)"
else
  fail "removing the catalog did not produce the expected 11 failures"
fi

if [ "$ok" = 1 ]; then
  echo "VERDICT: CLOSE (already-fixed) - main already compiles custom Claude selections against the scoped catalog (#27034, #31717)"
  PROOF_DONE=1; exit 0
fi
echo "VERDICT: claim not supported"
exit 1
