#!/usr/bin/env bash
# tests/phase-06-capi-stack/run-tests.sh -- offline regression for
# phase-06-capi-stack.sh. Fake ssh; real bash.
# Key assertions: (1) sub-steps run a,b,c,d,e,f in order; (2) ORC (c) precedes
# clusterctl init (d); (3) if ORC fails, init must NOT run (the hardened order
# exists precisely to stop CAPO crash-looping on a missing ORC CRD).
set -euo pipefail
IFS=$'\n\t'
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPTS="$(cd "$HERE/../../scripts" && pwd)"
TARGET="$SCRIPTS/phase-06-capi-stack.sh"
BIN="$HERE/fakebin"
[ -f "$TARGET" ] || { echo "FAIL: $TARGET missing" >&2; exit 1; }
chmod +x "$BIN"/* 2>/dev/null || true
WORK="$(mktemp -d)"; trap 'rm -rf "$WORK"' EXIT
rc_all=0

mkenv() { printf 'MGMT_FIP=%s\n' '10.12.7.222' > "$WORK/net.env"; }
: > "$WORK/id_key"
ORDER="$WORK/order"

run() {  # want_rc  out_regex  want_order(comma or -)  label  [extra env...]
  local want="$1" re="$2" order_want="$3" label="$4"; shift 4
  local rc order_got
  : > "$ORDER"
  set +e
  PATH="$BIN:$PATH" HOME="$WORK" ENVFILE="$WORK/net.env" SSH_KEY="$WORK/id_key" \
    ORDER_FILE="$ORDER" env "$@" bash "$TARGET" >"$WORK/out" 2>&1
  rc=$?
  set -e
  order_got=$(paste -sd, "$ORDER" 2>/dev/null || true)
  local ok=1
  [ "$rc" -eq "$want" ] || ok=0
  grep -qE "$re" "$WORK/out" || ok=0
  if [ "$order_want" != '-' ] && [ "$order_got" != "$order_want" ]; then ok=0; fi
  if [ "$ok" = 1 ]; then
    printf '  [OK]  %-46s exit %s  order=[%s]\n' "$label" "$rc" "$order_got"
  else
    printf '  [XX]  %-46s exit %s (want %s; /%s/; order [%s] want [%s])\n' \
      "$label" "$rc" "$want" "$re" "$order_got" "$order_want"
    sed 's/^/        /' "$WORK/out"; rc_all=1
  fi
}

echo "=== phase-06-capi-stack.sh ==="
mkenv
run 0 'Phase-06 complete'   'a,b,c,d,e,f' "happy path -> full stack, ordered"
run 1 '6.6a'                'a'           "6.6a pin fail -> stop at a"          A_FAIL=1
run 1 '6.6b'                'a,b'         "6.6b cert-manager fail -> stop at b" B_FAIL=1
run 1 '6.6c'                'a,b,c'       "6.6c ORC fail -> init (d) NOT run"   C_FAIL=1
run 1 '6.6d'                'a,b,c,d'     "6.6d init fail -> stop at d"         D_FAIL=1
run 1 '6.6e'                'a,b,c,d,e'   "6.6e CAAPH/janitor fail -> stop at e" E_FAIL=1
run 1 '6.6f'                'a,b,c,d,e,f' "6.6f verify fail -> stop at f"       F_FAIL=1

# preconditions
run 2 'not found' '-' "precondition: no ENVFILE -> exit 2" ENVFILE="$WORK/nope.env"
: > "$WORK/net.env"
run 2 'MGMT_FIP unset' '-' "precondition: MGMT_FIP unset -> exit 2"
mkenv

echo "=== assert: ORC (c) strictly precedes clusterctl init (d) on happy path ==="
: > "$ORDER"
PATH="$BIN:$PATH" HOME="$WORK" ENVFILE="$WORK/net.env" SSH_KEY="$WORK/id_key" \
  ORDER_FILE="$ORDER" bash "$TARGET" >/dev/null 2>&1 || true
ci=$(grep -n '^c$' "$ORDER" | head -1 | cut -d: -f1)
di=$(grep -n '^d$' "$ORDER" | head -1 | cut -d: -f1)
if [ -n "$ci" ] && [ -n "$di" ] && [ "$ci" -lt "$di" ]; then
  echo "  [OK] ORC at step $ci precedes clusterctl init at step $di"
else
  echo "  [XX] ORC/init ordering wrong (c=$ci d=$di)"; rc_all=1
fi

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