#!/usr/bin/env bash
# disk-admission.sh: dual-filesystem floor, dedupe, parent resolution, override cannot lower floor.
set -euo pipefail

LIB="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/lib"
WORK="$(mktemp -d)"
BIN="$WORK/bin"
mkdir -p "$BIN" "$WORK/root/a" "$WORK/scratch"
# shellcheck source=../lib/disk-admission.sh
. "$LIB/disk-admission.sh"

trap 'rm -rf "$WORK"' EXIT
fail=0
check() { [ "$2" = "$3" ] && printf 'ok   %s\n' "$1" || { printf 'FAIL %s\n  want: %s\n  got:  %s\n' "$1" "$3" "$2"; fail=1; }; }

OK=$((16 * 1024 * 1024 * 1024))
LOW=$((14 * 1024 * 1024 * 1024))

cat >"$BIN/df" <<'STUB'
#!/usr/bin/env bash
target="${@: -1}"
case "$target" in
  /) avail="${ROOT_FREE:?}" ;;
  "$WORK/root"|"$WORK/root"/*) avail="${ROOT_FREE:?}" ;;
  "$WORK/scratch"|"$WORK/scratch"/*) avail="${SCRATCH_FREE:?}" ;;
  "$WORK/missing"|"$WORK/missing"/*) exit 1 ;;
  *) exit 1 ;;
esac
printf 'Avail\n%s\n' "$avail"
STUB
cat >"$BIN/stat" <<'STUB'
#!/usr/bin/env bash
target="${@: -1}"
if [ "${1:-}" = "-c" ] && [ "${2:-}" = "%d" ]; then
  case "$target" in
    "$WORK/scratch"|"$WORK/scratch"/*) printf '2\n'; exit 0 ;;
    *) printf '1\n'; exit 0 ;;
  esac
fi
exec /usr/bin/stat "$@"
STUB
chmod +x "$BIN/df" "$BIN/stat"
export PATH="$BIN:$PATH" WORK ROOT_FREE="$OK" SCRATCH_FREE="$OK"

check "default floor is 15 GiB" "$BUILDBOX_DISK_FLOOR_BYTES" "$((15 * 1024 * 1024 * 1024))"
floor="$(BUILDBOX_DISK_MIN_FREE_BYTES=1 buildbox_disk_floor_bytes)"
check "byte override cannot lower floor" "$floor" "$BUILDBOX_DISK_FLOOR_BYTES"
floor="$(BUILDBOX_DISK_MIN_FREE_GIB=1 buildbox_disk_floor_bytes)"
check "gib override cannot lower floor" "$floor" "$BUILDBOX_DISK_FLOOR_BYTES"
floor="$(BUILDBOX_DISK_MIN_FREE_GIB=20 buildbox_disk_floor_bytes)"
check "gib override may raise floor" "$floor" "$((20 * 1024 * 1024 * 1024))"

resolved="$(buildbox_resolve_probe_path "$WORK/scratch/nested/deep/new")"
check "missing destination resolves nearest parent" "$resolved" "$(realpath "$WORK/scratch")"

paths="$(buildbox_unique_probe_paths "$WORK/root" "$WORK/root/sub")"
count="$(printf '%s\n' "$paths" | sed '/^$/d' | wc -l)"
check "same filesystem deduped to one probe" "$count" "1"

if buildbox_disk_admit testhost / "$WORK/scratch/new" >/dev/null 2>&1; then rc=0; else rc=1; fi
check "both root and scratch safe admits" "$rc" "0"

SCRATCH_FREE="$LOW"
ROOT_FREE="$OK"
if buildbox_disk_admit testhost / "$WORK/scratch/new" >/dev/null 2>&1; then rc=0; else rc=1; fi
check "low scratch refuses" "$rc" "1"

SCRATCH_FREE="$OK"
ROOT_FREE="$LOW"
if buildbox_disk_admit testhost / "$WORK/scratch/new" >/dev/null 2>&1; then rc=0; else rc=1; fi
check "low root refuses" "$rc" "1"

ROOT_FREE="$OK"
SCRATCH_FREE="$OK"
mkdir -p "$WORK/missing"
if buildbox_disk_admit testhost "$WORK/missing/leaf" >/dev/null 2>&1; then rc=0; else rc=1; fi
check "unreadable filesystem refuses as unknown" "$rc" "1"

SCRATCH_FREE="$LOW"
err="$(buildbox_disk_admit testhost / "$WORK/scratch" 2>&1 >/dev/null || true)"
case "$err" in
  *mount=*free=*required=*) ok=1 ;;
  *) ok=0 ;;
esac
check "refusal is actionable" "$ok" "1"

exit "$fail"
