#!/usr/bin/env bash
# Issue #30060: Cmd + "+" does not zoom in on macOS with a German/Austrian layout.
# Claim: already fixed by #26408 (691b9709b0). On those layouts "+" sits on the US "]" key (code
# BracketRight). The shared shortcut matcher read it as "]", so Cmd + "+" matched the default
# Cmd+] (navigation.forward) and the app swallowed the zoom. Shows: on 691b9709b0^ a Mac Cmd + "+"
# resolves to navigation.forward; on main it resolves to nothing, while US Cmd+] still goes forward.
# Needs deps installed (`vp i`). Run from the root of a clean t3code checkout.
set -euo pipefail

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)
TEST=apps/web/src/prove-issue-30060.test.ts
LOG=$(mktemp)
cleanup() {
  rm -f "$TEST" "$LOG"
  git checkout -q -f "$ORIG"
}
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)
FIX=691b9709b0d73dedd714d38fca8c2ea803595298
BEFORE=$(git rev-parse "$FIX^")
echo "main=$MAIN fix=$FIX before=$BEFORE"

subject=$(git log -1 --format=%s "$FIX")
if git merge-base --is-ancestor "$FIX" "$MAIN" \
  && [ "$subject" = 'fix(web): Ctrl + "+" zooms on keyboard layouts with a dedicated plus key (#26408)' ]; then
  pass "#26408 ($(git rev-parse --short "$FIX")) is on main: $subject"
else
  fail "#26408 is not on main, or $FIX is a different commit ($subject)"
fi

write_test() {
  cat > "$TEST" <<'EOF'
// prove-issue-30060: Cmd + "+" on a German/Austrian Mac layout ("+" on the US "]" key).
import { assert, it } from "vite-plus/test";
import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings";
import { resolveShortcutCommand } from "./keybindings";

const mac = { platform: "MacIntel", context: { terminalFocus: false } };
const key = (k: string) => ({
  key: k, code: "BracketRight", metaKey: true, ctrlKey: false, shiftKey: false, altKey: false,
});

it("prove-issue-30060", () => {
  // A US layout's Cmd+] still goes forward on both commits.
  assert.strictEqual(resolveShortcutCommand(key("]"), DEFAULT_RESOLVED_KEYBINDINGS, mac), "navigation.forward");
  const zoomIn = resolveShortcutCommand(key("+"), DEFAULT_RESOLVED_KEYBINDINGS, mac);
  if (process.env.PROOF_EXPECT === "bug") assert.strictEqual(zoomIn, "navigation.forward");
  else assert.isNull(zoomIn);
});
EOF
}

run_at() {
  git checkout -q --detach "$1"
  write_test
  local rc=0
  PROOF_EXPECT="$2" vp test run "$TEST" > "$LOG" 2>&1 || rc=$?
  rm -f "$TEST"
  if [ "$rc" -eq 0 ] && ! grep -Eq 'Tests +1 passed' "$LOG"; then rc=3; fi
  if [ "$rc" -ne 0 ]; then sed 's#'"$HOME"'#~#g' "$LOG" | tail -25; fi
  return "$rc"
}

if run_at "$BEFORE" bug; then
  pass "before #26408 ($(git rev-parse --short "$BEFORE")): Mac Cmd + \"+\" (code BracketRight) resolves to navigation.forward, so the zoom is swallowed"
else
  fail "before #26408: Mac Cmd + \"+\" did not resolve to navigation.forward"
fi
if run_at "$MAIN" fixed; then
  pass "main ($(git rev-parse --short "$MAIN")): Mac Cmd + \"+\" resolves to no app command, so the app no longer swallows it; US Cmd+] still goes forward"
else
  fail "main: Mac Cmd + \"+\" still resolves to an app command"
fi

if [ "$ok" = 1 ]; then
  echo "VERDICT: #30060 is fixed on main by #26408"
  PROOF_DONE=1; exit 0
fi
echo "VERDICT: not shown"
exit 1
