. "$BST_LIB"
require expect expect/present

# expect drives a real pty (spawn/send/expect/wait) — coverage shell cannot do.
# On musl/wasm32-hwjs expect starts but execve-EINVALs in spawn (BRINTOS-37):
# present-but-broken -> `not ok`, never `skip`. On the static glibc arm expect
# embeds the Tcl interpreter, so it SIGILLs at interp init (getpwuid -> glibc NSS
# dlopen -> `unreachable` at __nss_module_load.resume; rc>=128, no output) BEFORE
# reaching the pty path — a distinct blocker, brintos#92 / BRINTOS-77.
BLOCK92="blocked: glibc NSS __nss_module_load SIGILL on static wasm32-hwjs - brintos#92 / BRINTOS-77"
out=$(expect "$BST_DATA/selftest_expect.exp" 2>&1); rc=$?
eseen=$(printf '%s\n' "$out" | grep -c '^E-')
blocked=0
[ "$rc" -ge 128 ] && [ "$eseen" -eq 0 ] && blocked=1

case "$out" in *ALL-EXPECT-OK*) all=1 ;; *) all=0 ;; esac
if [ "$rc" -eq 0 ] && [ "$all" -eq 1 ]; then bst_ok expect/all-ok
elif [ "$blocked" -eq 1 ]; then bst_notok expect/all-ok "$BLOCK92"
else bst_notok expect/all-ok "rc=$rc $(_bst_1line "$out")"; fi

if [ "$blocked" -eq 1 ]; then
  # expect never started; record the E1-E4 cases against the blocker.
  for tag in E1ver E2spawn E3regexp E4wait; do bst_notok "expect/$tag" "$BLOCK92"; done
else
  for tag in E1ver E2spawn E3regexp E4wait; do
    case "$out" in
      *"E-$tag OK"*) bst_ok "expect/$tag" ;;
      *)             bst_notok "expect/$tag" "not OK in output (rc=$rc)" ;;
    esac
  done
fi
