#!/usr/bin/env bash
# Regression tests for bin/shim-drift-check — the check that fails when the shims this
# machine executes are not the ones landed on the canonical ref. Fixture-based: it
# never spawns a PATH shim, so it is safe to run anywhere.
set -uo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
CHECK="$ROOT/bin/shim-drift-check"
PASS=0; FAIL=0
ok()  { PASS=$((PASS+1)); printf 'PASS %s\n' "$1"; }
bad() { FAIL=$((FAIL+1)); printf 'FAIL %s\n     %s\n' "$1" "$2"; }

TMP=$(mktemp -d "${TMPDIR:-/tmp}/sdc-test-XXXX")
trap 'rm -rf "$TMP"' EXIT

REPO="$TMP/repo"
BIN="$REPO/modules/workstation/claude/bin"
LIB="$REPO/modules/workstation/claude/lib"
mkdir -p "$BIN" "$LIB"
for f in _git-guard-shim.sh _cpu-guard-shim.sh _tmpjail-shim.sh _kill-guard-shim.sh install-git-guard-real shim-drift-check; do
  printf 'landed %s\n' "$f" > "$BIN/$f"
done
printf 'landed shim-guard\n' > "$LIB/shim-guard.sh"
git -C "$REPO" init -q
git -C "$REPO" config user.email t@t; git -C "$REPO" config user.name t; git -C "$REPO" config commit.gpgsign false
git -C "$REPO" add -A; git -C "$REPO" commit -qm landed
git -C "$REPO" branch -f canonical

LIVE="$TMP/home/.claude/bin"
mkdir -p "$LIVE" "$TMP/home/.claude/lib"
cp "$BIN"/* "$LIVE/"
cp "$LIB/shim-guard.sh" "$TMP/home/.claude/lib/"

run() { OD_SHIM_LIVE_DIR="$LIVE" OD_SHIM_REPO="$REPO" OD_SHIM_REF=canonical bash "$CHECK" >"$TMP/out" 2>"$TMP/err"; }

run
rc=$?
[[ $rc -eq 0 ]] && ok "matching live tree passes" || bad "matching live tree passes" "rc=$rc err=$(cat "$TMP/err")"

printf 'drifted\n' > "$LIVE/_git-guard-shim.sh"
run
rc=$?
[[ $rc -eq 1 && "$(cat "$TMP/err")" == *_git-guard-shim.sh*differs* ]] \
  && ok "content drift in a live shim fails loudly" \
  || bad "content drift in a live shim fails loudly" "rc=$rc err=$(cat "$TMP/err")"
cp "$BIN/_git-guard-shim.sh" "$LIVE/_git-guard-shim.sh"

printf 'drifted lib\n' > "$TMP/home/.claude/lib/shim-guard.sh"
run
rc=$?
[[ $rc -eq 1 && "$(cat "$TMP/err")" == *shim-guard.sh*differs* ]] \
  && ok "content drift in the shared guard lib fails loudly" \
  || bad "content drift in the shared guard lib fails loudly" "rc=$rc err=$(cat "$TMP/err")"
cp "$LIB/shim-guard.sh" "$TMP/home/.claude/lib/shim-guard.sh"

rm -f "$LIVE/_kill-guard-shim.sh"
run
rc=$?
[[ $rc -eq 1 && "$(cat "$TMP/err")" == *"missing on this machine"* ]] \
  && ok "a shim missing from the live tree fails loudly" \
  || bad "a shim missing from the live tree fails loudly" "rc=$rc err=$(cat "$TMP/err")"
cp "$BIN/_kill-guard-shim.sh" "$LIVE/"

git -C "$REPO" worktree add -q -b old "$TMP/oldwt" >/dev/null 2>&1
printf 'pre-fix recursing content\n' > "$TMP/oldwt/modules/workstation/claude/bin/_git-guard-shim.sh"
run
rc=$?
[[ $rc -eq 1 && "$(cat "$TMP/err")" == *"$TMP/oldwt"*differs* ]] \
  && ok "a worktree holding a stale shim copy fails loudly" \
  || bad "a worktree holding a stale shim copy fails loudly" "rc=$rc err=$(cat "$TMP/err")"
git -C "$REPO" worktree remove --force "$TMP/oldwt"

git -C "$REPO" rm -q --cached modules/workstation/claude/bin/_tmpjail-shim.sh
git -C "$REPO" commit -qm unland
git -C "$REPO" branch -f canonical
run
rc=$?
[[ $rc -eq 1 && "$(cat "$TMP/err")" == *"not present in canonical"* ]] \
  && ok "an unlanded live shim fails loudly" \
  || bad "an unlanded live shim fails loudly" "rc=$rc err=$(cat "$TMP/err")"

echo
echo "PASS=$PASS FAIL=$FAIL"
[[ "$FAIL" -eq 0 ]]
