#!/usr/bin/env bash
# Behavior regression for phase-00-maas-standup.sh (D-052/D-053 six-plane).
# Fake `maas` + real jq. Drives DRY-RUN and asserts WOULD/SKIP/DRIFT/refuse
# behaviour across scenarios. Rewritten 2026-07-03 from the retired D-058
# expectations (harness must travel with its script's scheme).
set -uo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPT="$(cd "$HERE/../../scripts" && pwd)/phase-00-maas-standup.sh"
BIN="$HERE/fakebin"; FIX="$HERE/fix"
chmod +x "$BIN"/* 2>/dev/null || true   # GitHub Desktop lands files mode 100644
command -v jq >/dev/null || { echo "FAIL: jq required"; exit 1; }
python3 "$HERE/make_fixtures.py" >/dev/null
rc_all=0; OUT="$(mktemp)"

run() {  # <want_exit> <label> <scenario>
  local want="$1" label="$2" s="$3"
  PATH="$BIN:$PATH" \
    FIX_SUBNETS="$FIX/${s}_subnets.json" FIX_SPACES="$FIX/${s}_spaces.json" \
    FIX_VLANS="$FIX/${s}_vlans.json" FIX_IPRANGES="$FIX/${s}_ipranges.json" \
    FIX_FABRICS="$FIX/${s}_fabrics.json" \
    bash "$SCRIPT" >"$OUT" 2>&1
  local rc=$?
  if [ "$rc" -ne "$want" ]; then
    printf '  [XX] %-44s exit %s (want %s)\n' "$label" "$rc" "$want"; sed 's/^/        /' "$OUT"; rc_all=1; return 1
  fi
  printf '  [ok] %-44s exit %s\n' "$label" "$rc"; return 0
}
has()    { grep -qE "$1" "$OUT" || { printf '       MISS expected /%s/\n' "$1"; rc_all=1; }; }
absent() { grep -qE "$1" "$OUT" && { printf '       LEAK forbidden /%s/\n' "$1"; rc_all=1; } || true; }
count()  { local n; n=$(grep -cE "$1" "$OUT"); [ "$n" -eq "$2" ] || { printf '       COUNT /%s/ = %s (want %s)\n' "$1" "$n" "$2"; rc_all=1; }; }

echo "=== phase-00-maas-standup.sh -- D-052/D-053 six-plane stand-up (fake maas + real jq) ==="

run 0 "fresh MAAS: full six-plane create plan" fresh
has 'no drift'
has 'WOULD: create fabric provider-public'
has 'WOULD: create space provider-public'
has 'WOULD: create space metal-internal'
has 'WOULD: create VLAN vid=103 name=metal-internal mtu=1500'   # fresh: parent mtu unknown -> 1500
has 'WOULD: create subnet 10\.12\.8\.0/22'
has 'WOULD: create subnet 10\.12\.12\.0/22'
has 'WOULD: create subnet 10\.12\.36\.0/22'
has 'WOULD: subnet 10\.12\.4\.0/22 -> gateway_ip=10\.12\.4\.1'
has 'WOULD: subnet 10\.12\.8\.0/22 -> gateway_ip=10\.12\.8\.1'
has 'WOULD: create reserved range 10\.12\.5\.0-10\.12\.7\.254'        # FIP pool (D-003)
has 'WOULD: create reserved range 10\.12\.4\.2-10\.12\.4\.100'         # provider API VIP band
has 'WOULD: create reserved range 10\.12\.12\.2-10\.12\.12\.100'       # metal-internal API VIP band
absent 'DRIFT'
absent 'provider-vip'                                                  # the retired plane must never reappear

run 0 "six-plane done: all SKIP, zero WOULD" done
count 'WOULD:' 0
has 'no drift'
has "space metal-internal exists -- SKIP"
has 'VID 103 on fabric 2 exists -- SKIP'
has 'subnet 10\.12\.16\.0/22 exists -- SKIP'
has 'gateway_ip already 10\.12\.8\.1 -- SKIP'
has 'reserved range starting 10\.12\.5\.0 exists -- SKIP'
has 'reserved range starting 10\.12\.12\.2 exists -- SKIP'
has 'OK \(dryrun\) -- topology consistent with D-052/D-053'

run 1 "wrong plane on a target CIDR: DRIFT + refuse" drift
count 'DRIFT:' 1
has "DRIFT: 10\.12\.16\.0/22 is space 'storage' but the D-052/D-053 scheme assigns it to 'data-tenant'"
has '10\.12\.16\.0/22 occupied by the wrong plane'
has 'need a gated re-CIDR/migration'
has 'subnet 10\.12\.4\.0/22 exists -- SKIP'            # untouched planes still SKIP cleanly
has 'plane replication \(10\.12\.36'                   # loop reaches the last plane
absent 'parse error'

run 1 "wrong VID on metal-internal subnet -> drift(vid)" wrongvid
has "DRIFT: 10\.12\.12\.0/22 space ok \('metal-internal'\) but VID '99' != target 103"

echo
[ "$rc_all" -eq 0 ] && echo "ALL PASS" || echo "SOME FAILED"
rm -f "$OUT"; exit "$rc_all"
