. "$BST_LIB"
require tclsh tcl/present

out=$(tclsh "$BST_DATA/selftest.tcl" 2>&1); rc=$?

# whole-suite gate
case "$out" in *ALL-TCL-OK*) all=1 ;; *) all=0 ;; esac
if [ "$rc" -eq 0 ] && [ "$all" -eq 1 ]; then bst_ok tcl/all-ok
else bst_notok tcl/all-ok "rc=$rc $(_bst_1line "$out")"; fi

# fine-grained assertions parsed from the payload's R-lines
check_eq tcl/expr  42          "$(printf '%s\n' "$out" | awk '/^R2-expr/{print $2}')"
check_eq tcl/fib15 610         "$(printf '%s\n' "$out" | awk '/^R2-fib15/{print $2}')"
check_eq tcl/list  "1 2 3 10"  "$(printf '%s\n' "$out" | sed -n 's/^R2-list //p')"
check_eq tcl/string "ABC/5"    "$(printf '%s\n' "$out" | awk '/^R2-string/{print $2}')"
check_eq tcl/utf8  1           "$(printf '%s\n' "$out" | awk '/^R3-utf8/{print $2}')"

# encoding count is environment-specific — assert a positive integer, not a literal
encn=$(printf '%s\n' "$out" | awk '/^R1-encN/{print $2}')
if [ -n "$encn" ] && [ "$encn" -gt 0 ] 2>/dev/null; then bst_ok tcl/encodings
else bst_notok tcl/encodings "encN='$encn'"; fi
