#!/usr/bin/env bash
# Regression tests for od-worktree's checkout registration — the pointer shim-drift-check
# follows from the deploy clone to the checkouts that hold worktrees. Runs with a PATH of
# real system binaries only, so it never spawns a PATH shim.
set -uo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
ODWT="$ROOT/bin/od-worktree"
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}/odwt-reg-XXXX")
trap 'rm -rf "$TMP"' EXIT

REPO="$TMP/repo"
mkdir -p "$REPO"
export PATH=/usr/bin:/bin
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
: >"$REPO/f"; git -C "$REPO" add -A; git -C "$REPO" commit -qm init

REG="$TMP/state/shim-repos"
run() { (cd "$1" && OD_SHIM_REPOS_FILE="$REG" bash "$ODWT" list) >"$TMP/out" 2>"$TMP/err"; }

REAL_PATH=$PATH
mkdir -p "$TMP/bin"
cat >"$TMP/bin/df" <<'STUB'
#!/bin/sh
printf 'Filesystem 1024-blocks Used Available Capacity Mounted on\nmock 20000000 18951424 1048576 95%% /\n'
STUB
chmod +x "$TMP/bin/df"
if (cd "$REPO" && PATH="$TMP/bin:$REAL_PATH" OD_SHIM_REPOS_FILE="$REG" bash "$ODWT" add boundary HEAD) >"$TMP/out" 2>"$TMP/err"; then
  rc=0
else
  rc=$?
fi
[[ $rc -eq 0 && -e "$REPO/.worktrees/boundary" ]] \
  && ok "add admits the exact 1 GiB default boundary" \
  || bad "add admits the exact 1 GiB default boundary" "rc=$rc exists=$([[ -e "$REPO/.worktrees/boundary" ]] && echo yes || echo no) err=$(cat "$TMP/err")"
git -C "$REPO" worktree remove "$REPO/.worktrees/boundary"

cat >"$TMP/bin/df" <<'STUB'
#!/bin/sh
printf 'Filesystem 1024-blocks Used Available Capacity Mounted on\nmock 20000000 18951425 1048575 95%% /\n'
STUB
if (cd "$REPO" && PATH="$TMP/bin:$REAL_PATH" OD_SHIM_REPOS_FILE="$REG" bash "$ODWT" add refused HEAD) >"$TMP/out" 2>"$TMP/err"; then
  rc=0
else
  rc=$?
fi
[[ $rc -eq 75 && ! -e "$REPO/.worktrees/refused" && "$(cat "$TMP/err")" == *"disk admission refused"* ]] \
  && ok "add refuses before mutation below the 1 GiB default floor" \
  || bad "add refuses before mutation below the 1 GiB default floor" "rc=$rc exists=$([[ -e "$REPO/.worktrees/refused" ]] && echo yes || echo no) err=$(cat "$TMP/err")"

cat >"$TMP/bin/df" <<'STUB'
#!/bin/sh
printf 'Filesystem 1024-blocks Used Available Capacity Mounted on\n'
STUB
if (cd "$REPO" && PATH="$TMP/bin:$REAL_PATH" OD_SHIM_REPOS_FILE="$REG" bash "$ODWT" add unknown HEAD) >"$TMP/out" 2>"$TMP/err"; then
  rc=0
else
  rc=$?
fi
[[ $rc -eq 2 && ! -e "$REPO/.worktrees/unknown" && "$(cat "$TMP/err")" == *"disk capacity unknown"* ]] \
  && ok "add fails closed before mutation when capacity is unknown" \
  || bad "add fails closed before mutation when capacity is unknown" "rc=$rc exists=$([[ -e "$REPO/.worktrees/unknown" ]] && echo yes || echo no) err=$(cat "$TMP/err")"

if (cd "$REPO" && PATH="$TMP/bin:$REAL_PATH" OD_WORKTREE_MIN_FREE_KIB=invalid OD_SHIM_REPOS_FILE="$REG" bash "$ODWT" add invalid HEAD) >"$TMP/out" 2>"$TMP/err"; then
  rc=0
else
  rc=$?
fi
[[ $rc -eq 2 && ! -e "$REPO/.worktrees/invalid" && "$(cat "$TMP/err")" == *"must be an integer"* ]] \
  && ok "add rejects a malformed floor before mutation" \
  || bad "add rejects a malformed floor before mutation" "rc=$rc exists=$([[ -e "$REPO/.worktrees/invalid" ]] && echo yes || echo no) err=$(cat "$TMP/err")"

if (cd "$REPO" && PATH="$TMP/bin:$REAL_PATH" OD_WORKTREE_MIN_FREE_KIB=000000000008 OD_SHIM_REPOS_FILE="$REG" bash "$ODWT" list) >"$TMP/out" 2>"$TMP/err"; then
  rc=0
else
  rc=$?
fi
[[ $rc -eq 0 ]] \
  && ok "a decimal floor with leading zeroes is normalized" \
  || bad "a decimal floor with leading zeroes is normalized" "rc=$rc err=$(cat "$TMP/err")"

cat >"$TMP/bin/df" <<'STUB'
#!/bin/sh
printf 'Filesystem 1024-blocks Used Available Capacity Mounted on\nmock 40000000 10000000 30000000 25%% /\n'
STUB
export PATH="$TMP/bin:$REAL_PATH"

run "$REPO"
rc=$?
[[ $rc -eq 0 && "$(cat "$REG" 2>/dev/null)" == "$REPO" ]] \
  && ok "the checkout is registered on first use" \
  || bad "the checkout is registered on first use" "rc=$rc reg=$(cat "$REG" 2>/dev/null) err=$(cat "$TMP/err")"

run "$REPO"; run "$REPO"
[[ "$(wc -l <"$REG")" -eq 1 ]] \
  && ok "repeated use does not duplicate the entry" \
  || bad "repeated use does not duplicate the entry" "reg=$(cat "$REG")"

printf '%s\n' "$TMP/elsewhere" >"$REG"
run "$REPO"
[[ "$(cat "$REG")" == "$TMP/elsewhere
$REPO" ]] \
  && ok "an existing registry is appended to, never replaced" \
  || bad "an existing registry is appended to, never replaced" "reg=$(cat "$REG")"

WT="$REPO/.worktrees/slug"
(cd "$REPO" && OD_WORKTREE_MIN_FREE_KIB=0 OD_SHIM_REPOS_FILE="$REG" bash "$ODWT" add slug HEAD) >/dev/null 2>&1
rm -f "$REG"
(cd "$WT" && OD_SHIM_REPOS_FILE="$REG" bash "$ODWT" list) >/dev/null 2>"$TMP/err"
[[ "$(cat "$REG" 2>/dev/null)" == "$REPO" ]] \
  && ok "use from inside a worktree registers the main checkout" \
  || bad "use from inside a worktree registers the main checkout" "reg=$(cat "$REG" 2>/dev/null) err=$(cat "$TMP/err")"

rm -rf "$TMP/state"
: >"$TMP/state"
run "$REPO"
rc=$?
[[ $rc -eq 0 && "$(cat "$TMP/err")" == *"cannot record"* ]] \
  && ok "an unwritable registry warns and still serves the worktree command" \
  || bad "an unwritable registry warns and still serves the worktree command" "rc=$rc err=$(cat "$TMP/err")"

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