#!/usr/bin/env bash
# misc_panic_parse-entry — `NUC_PANIC_AT=parse-entry` fires the
# trigger at the top of `cli::parse::run`, AFTER clap parses and
# AFTER `install_panic_hook(debug=true)` ran. So we expect:
#   - exit != 0
#   - stderr has `panicked at` + a `src/` location (line-tables)
#   - stderr has the full `=== nucleos diagnostic context ===`
#     block (the -d hook appended it)
#   - stderr names the captured `argv` line (proves
#     format_diagnostic_context ran)
set -euo pipefail

cat > "$NUCTEST_TMPDIR/x.dzn" <<'EOF'
interface I { in void e(); behavior { on e: {} } }
component C { provides I p; behavior { on p.e(): {} } }
EOF

set +e
NUC_PANIC_AT=parse-entry "$NUCLEOS" -d parse "$NUCTEST_TMPDIR/x.dzn" \
    > "$NUCTEST_TMPDIR/out.txt" 2> "$NUCTEST_TMPDIR/err.txt"
exit_code=$?
set -e

if [ "$exit_code" = 0 ]; then
    echo "FAIL: expected panic exit, got 0" >&2
    exit 1
fi
for needle in \
    "panicked at" \
    "src/" \
    "=== nucleos diagnostic context ===" \
    "argv    :"; do
    if ! grep -qF "$needle" "$NUCTEST_TMPDIR/err.txt"; then
        echo "FAIL: stderr missing '$needle'" >&2
        cat "$NUCTEST_TMPDIR/err.txt" >&2
        exit 1
    fi
done
