#!/usr/bin/env bash
# tests/phase-00-carve/run-tests.sh -- offline regression for phase-00-maas-carve.sh.
set -euo pipefail
IFS=$'\n\t'
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPTS="$(cd "$HERE/../../scripts" && pwd)"
TARGET="$SCRIPTS/phase-00-maas-carve.sh"
BIN="$HERE/fakebin"
command -v jq >/dev/null 2>&1 || { echo "FAIL: jq required" >&2; exit 1; }
[ -f "$TARGET" ] || { echo "FAIL: target missing" >&2; exit 1; }
chmod +x "$BIN"/* 2>/dev/null || true
WORK="$(mktemp -d)"; trap 'rm -rf "$WORK"' EXIT

# --- fixtures ---
SUB_BOTH="$WORK/sub_both.json";  printf '[{"id":1,"cidr":"10.12.4.0/22"},{"id":2,"cidr":"10.12.8.0/22"}]\n' > "$SUB_BOTH"
SUB_NOPROV="$WORK/sub_noprov.json"; printf '[{"id":2,"cidr":"10.12.8.0/22"}]\n' > "$SUB_NOPROV"
R_EMPTY="$WORK/r_empty.json";    printf '[]\n' > "$R_EMPTY"
R_ALL="$WORK/r_all.json"; cat > "$R_ALL" <<'JSON'
[ {"id":1,"type":"reserved","start_ip":"10.12.4.2","end_ip":"10.12.4.100","subnet":{"id":1},"comment":"prov vip"},
  {"id":2,"type":"reserved","start_ip":"10.12.8.2","end_ip":"10.12.8.100","subnet":{"id":2},"comment":"metal vip"},
  {"id":3,"type":"reserved","start_ip":"10.12.5.0","end_ip":"10.12.7.254","subnet":{"id":1},"comment":"fip pool"} ]
JSON
R_DRIFT="$WORK/r_drift.json"; cat > "$R_DRIFT" <<'JSON'
[ {"id":1,"type":"reserved","start_ip":"10.12.4.2","end_ip":"10.12.4.63","subnet":{"id":1},"comment":"prov vip (do-doc width)"},
  {"id":2,"type":"reserved","start_ip":"10.12.8.2","end_ip":"10.12.8.100","subnet":{"id":2},"comment":"metal vip"},
  {"id":3,"type":"reserved","start_ip":"10.12.5.0","end_ip":"10.12.7.254","subnet":{"id":1},"comment":"fip pool"} ]
JSON
R_STALE="$WORK/r_stale.json"; cat > "$R_STALE" <<'JSON'
[ {"id":1,"type":"reserved","start_ip":"10.12.4.2","end_ip":"10.12.4.100","subnet":{"id":1},"comment":"prov vip"},
  {"id":2,"type":"reserved","start_ip":"10.12.8.2","end_ip":"10.12.8.100","subnet":{"id":2},"comment":"metal vip"},
  {"id":3,"type":"reserved","start_ip":"10.12.5.0","end_ip":"10.12.7.254","subnet":{"id":1},"comment":"fip pool"},
  {"id":9,"type":"reserved","start_ip":"10.12.8.224","end_ip":"10.12.8.254","subnet":{"id":2},"comment":"STALE old scheme"} ]
JSON

rc_all=0
run() {
  local want="$1" re="$2" label="$3" subs="$4" ranges="$5"; shift 5
  local rc
  set +e
  PATH="$BIN:$PATH" FIX_SUBNETS_FILE="$subs" FIX_RANGES_FILE="$ranges" \
    env "$@" bash "$TARGET" >"$WORK/out" 2>&1
  rc=$?; set -e
  if [ "$rc" -eq "$want" ] && grep -qE "$re" "$WORK/out"; then
    printf '  [OK]  %-40s exit %s\n' "$label" "$rc"
  else
    printf '  [XX]  %-40s exit %s (want %s; /%s/)\n' "$label" "$rc" "$want" "$re"
    sed 's/^/        /' "$WORK/out"; rc_all=1
  fi
}
echo "=== phase-00-maas-carve.sh (fake maas + real jq) ==="
run 0 'reserved 10.12.4.2-10.12.4.100 on 10.12.4.0/22' "fresh: create provider VIP"   "$SUB_BOTH" "$R_EMPTY"
run 0 'reserved 10.12.5.0-10.12.7.254 on 10.12.4.0/22' "fresh: create FIP pool"       "$SUB_BOTH" "$R_EMPTY"
run 0 'stale range absent'                             "fresh: stale absent"          "$SUB_BOTH" "$R_EMPTY"
run 0 'starting 10.12.4.2 already present .10.12.4.2-10.12.4.100.' "idempotent: skip all" "$SUB_BOTH" "$R_ALL"
run 0 'starting 10.12.4.2 already present .10.12.4.2-10.12.4.63.'  "width drift .2-.63 tolerated" "$SUB_BOTH" "$R_DRIFT"
run 0 'REPORT. stale range present: id=9'              "stale present: report-only"   "$SUB_BOTH" "$R_STALE"
run 0 'stale range id=9 deleted'                       "stale present: DELETE_STALE=1" "$SUB_BOTH" "$R_STALE" DELETE_STALE=1
run 1 'no MAAS subnet for cidr 10.12.4.0/22'           "provider subnet missing -> fail" "$SUB_NOPROV" "$R_EMPTY"
echo
[ "$rc_all" -eq 0 ] && echo "ALL PASS" || echo "SOME FAILED"
exit "$rc_all"
