#!/usr/bin/env bash
# tests/dc-dc-mtu-geneve-budget/run-tests.sh -- offline harness for
# scripts/dc-dc-mtu-geneve-budget.sh (tooling gap register item #7).
# Pure arithmetic, no external tools/fixtures needed -- every case runs for
# real, nothing is a guard-clause-only stub.
# 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-mtu-geneve-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
}

# --- D-101's own worked example: 1500 -> 1444, exactly -----------------------
run 0 'tenant_mtu=1444' "T1 worked example 1500 -> 1444 reproduces exactly" --underlay-mtu 1500
run 0 'MTU-BUDGET: underlay=1500 jumbo=no tenant_mtu=1444 verdict=PASS' "T2 summary line matches exactly" --underlay-mtu 1500

# --- jumbo input: tenant MTU stays 1500, no reduction ------------------------
run 0 'tenant_mtu=1500' "T3 jumbo (9000) -> tenant MTU stays 1500" --underlay-mtu 9000
run 0 'jumbo=yes' "T4 jumbo case labeled jumbo=yes" --underlay-mtu 9000
run 0 'tenant_mtu=1500' "T5 above-jumbo (12000) also stays 1500" --underlay-mtu 12000

# --- boundary: exactly at jumbo threshold counts as jumbo (>=) --------------
run 0 'jumbo=yes' "T6 exactly-9000 counts as jumbo (>=)" --underlay-mtu 9000
run 0 'jumbo=no' "T7 8999 (one below threshold) is NOT jumbo" --underlay-mtu 8999

# --- missing required args fail loud, non-zero exit, clear message ---------
run 2 'REQUIRED' "T8 missing --underlay-mtu FAILS loud (rc 2)"
run 2 'REQUIRED' "T9 --help alone with no other arg still requires --underlay-mtu" --jumbo-threshold 9000
run 2 'positive integer' "T10 non-numeric --underlay-mtu FAILS loud (rc 2)" --underlay-mtu abc
run 2 'positive integer' "T11 zero --underlay-mtu FAILS loud (rc 2)" --underlay-mtu 0
run 2 'positive integer' "T12 negative --underlay-mtu FAILS loud (rc 2)" --underlay-mtu -5

# --- IPv6-minimum-MTU sanity floor: below 1280 after overhead -> FAIL -------
run 1 'below IPv6 min link MTU' "T13 1300 underlay -> 1244 tenant, below 1280 -> FAIL (rc 1)" --underlay-mtu 1300
run 1 'tenant_mtu=1224' "T13b 1280 underlay -> 1224 tenant, also below floor -> FAIL (rc 1)" --underlay-mtu 1280

# --- --help works, exits 0, mentions D-101 arithmetic -----------------------
run 0 'D-101' "T14 --help exits 0 and cites D-101" --help
run 0 '56 bytes' "T15 --help documents the 56-byte overhead" --help

# --- --jumbo-threshold override behaves consistently ------------------------
run 0 'jumbo=yes' "T16 custom --jumbo-threshold honored (1500 with threshold=1500)" --underlay-mtu 1500 --jumbo-threshold 1500
run 2 'positive integer' "T17 bad --jumbo-threshold FAILS loud (rc 2)" --underlay-mtu 1500 --jumbo-threshold nope

# --- unknown argument fails loud --------------------------------------------
run 2 'unknown argument' "T18 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
