#!/usr/bin/env bash
# tests/dc-dc-ceph-disk-budget/run-tests.sh -- offline harness for
# scripts/dc-dc-ceph-disk-budget.sh (tooling gap register item #7). Pure
# arithmetic, no external tools/fixtures needed -- every case runs for real.
# Exit: 0 all pass | 1 any case failed. ASCII + LF.
set -uo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPT="$HERE/../../scripts/dc-dc-ceph-disk-budget.sh"
PASS=0; FAIL=0

run() { # run <want_rc> <regex> <label> -- args...
  local want="$1" rx="$2" label="$3"; shift 3
  local out rc
  out="$(bash "$SCRIPT" "$@" 2>&1)"; rc=$?
  if [[ "$rc" == "$want" ]] && grep -qE "$rx" <<<"$out"; then
    echo "  PASS  $label"; PASS=$((PASS+1))
  else
    echo "  FAIL  $label (rc=$rc want=$want)"; echo "$out" | sed 's/^/        /'; FAIL=$((FAIL+1))
  fi
}

FITS_ARGS=(--total-disk 10Ti --vr1-dc0-nodes 3 --dc1-per-node-osd 500G \
           --vr1-dc1-nodes 3 --dc2-per-node-osd 500G --backup-overhead-fraction 0.3)
SHORT_ARGS=(--total-disk 2Ti --vr1-dc0-nodes 3 --dc1-per-node-osd 500G \
            --vr1-dc1-nodes 3 --dc2-per-node-osd 500G --backup-overhead-fraction 0.3)

# --- constructed numeric example: size=3 + overhead FITS --------------------
# DC1 = 3*500G = 1.46TiB, DC2 same = 1.46TiB, combined ~2.92TiB, +30% overhead
# ~= 3.80TiB required, against a 10TiB measured total -- fits with margin.
run 0 'verdict=PASS' "T1 size=3+overhead fits within measured total -> PASS" "${FITS_ARGS[@]}"
run 0 'CEPH-BUDGET: total=10.00TiB required\(size3\+overhead\)=3.80TiB verdict=PASS' \
  "T2 exact numeric example reproduces (10TiB total, 3.80TiB required)" "${FITS_ARGS[@]}"

# --- constructed numeric example: size=3 + overhead does NOT fit -----------
# Same Ceph footprint (~3.80TiB required) against a 2TiB measured total ->
# shortfall, size=2 fallback named, NOT silently applied.
run 1 'verdict=FAIL' "T3 size=3+overhead exceeds measured total -> FAIL" "${SHORT_ARGS[@]}"
run 1 'size=2 fallback' "T4 FAIL branch explicitly names size=2 as the fallback" "${SHORT_ARGS[@]}"
run 1 'operator must LOG|explicit.*logged operator decision|EXPLICIT, logged' \
  "T5 FAIL branch states size=2 requires an explicit LOGGED operator decision, not silent" "${SHORT_ARGS[@]}"
run 1 'CEPH-BUDGET: total=2.00TiB required\(size3\+overhead\)=3.80TiB verdict=FAIL shortfall=1.80TiB' \
  "T6 exact numeric shortfall example reproduces" "${SHORT_ARGS[@]}"

# --- missing required args fail loud, non-zero exit, clear message ---------
run 2 'REQUIRED' "T7 all args missing FAILS loud (rc 2)"
run 2 '\-\-backup-overhead-fraction is REQUIRED' \
  "T8 backup-overhead-fraction has NO default -- missing it alone FAILS loud" \
  --total-disk 10Ti --vr1-dc0-nodes 3 --dc1-per-node-osd 500G --vr1-dc1-nodes 3 --dc2-per-node-osd 500G
run 2 '\-\-total-disk is REQUIRED' "T9 missing --total-disk alone FAILS loud" \
  --vr1-dc0-nodes 3 --dc1-per-node-osd 500G --vr1-dc1-nodes 3 --dc2-per-node-osd 500G --backup-overhead-fraction 0.3

# --- bad value formats fail loud --------------------------------------------
run 2 'positive integer' "T10 non-numeric --vr1-dc0-nodes FAILS loud (rc 2)" \
  --total-disk 10Ti --vr1-dc0-nodes abc --dc1-per-node-osd 500G --vr1-dc1-nodes 3 --dc2-per-node-osd 500G --backup-overhead-fraction 0.3
run 2 'non-negative decimal' "T11 non-numeric --backup-overhead-fraction FAILS loud (rc 2)" \
  --total-disk 10Ti --vr1-dc0-nodes 3 --dc1-per-node-osd 500G --vr1-dc1-nodes 3 --dc2-per-node-osd 500G --backup-overhead-fraction abc
run 2 'integer byte count or Ki/Mi/Gi/Ti' "T12 bad size suffix on --total-disk FAILS loud (rc 2)" \
  --total-disk 10XB --vr1-dc0-nodes 3 --dc1-per-node-osd 500G --vr1-dc1-nodes 3 --dc2-per-node-osd 500G --backup-overhead-fraction 0.3

# --- size parsing: plain bytes and 1024-based suffixes agree ----------------
run 0 'verdict=PASS' "T13 plain-byte total-disk equivalent to 10Ti still PASSes" \
  --total-disk 10995116277760 --vr1-dc0-nodes 3 --dc1-per-node-osd 500G --vr1-dc1-nodes 3 --dc2-per-node-osd 500G --backup-overhead-fraction 0.3

# --- --help works, exits 0, cites Section 3 / D-101 and the no-invented-fraction rule ---
run 0 'Section 3' "T14 --help exits 0 and cites buildout-design Section 3" --help
run 0 'NO[[:space:]]*$|NO DEFAULT' "T15 --help documents backup-overhead-fraction has no default" --help

# --- unknown argument fails loud --------------------------------------------
run 2 'unknown argument' "T16 unknown flag FAILS loud (rc 2)" --bogus-flag 5

echo
echo "RESULT: PASS=$PASS FAIL=$FAIL"
[[ "$FAIL" -eq 0 ]] && { echo "ALL PASS"; exit 0; } || exit 1
