#!/usr/bin/env bash # e2e_test.sh — the metta_bench use case, end to end against # b3u.dev. # # Source: authored in b3u_use_cases (the benchmarking article's # section 2 second block + section 5, made executable). # # Downloads the use-case code zip, lays out # ~/ccsUseCases/metta_bench, and builds metta_counts against the # SCB C++ library delivered by the metta use case. Ends at a # BUILT executable — running the benchmark on the corpus is yours # (the article's sections 3 and 5-7). # # PRECONDITION: the metta use case's layout exists (run # metta/e2e_test.sh first — this script does not repeat it). # # Environment (same surface as the metta script): # B3U_BASE, B3U_EMAIL, B3U_PASSWORD required # B3U_DOWNLOADS default ~/Downloads # CCSUSECASES_ROOT default ~/ccsUseCases # CF_ACCESS_CLIENT_ID/-_SECRET optional gateway headers set -u : "${B3U_BASE:?set B3U_BASE (e.g. https://b3u.dev)}" : "${B3U_EMAIL:?set B3U_EMAIL}" : "${B3U_PASSWORD:?set B3U_PASSWORD}" DOWNLOADS="${B3U_DOWNLOADS:-$HOME/Downloads}" ROOT_DIR="${CCSUSECASES_ROOT:-$HOME/ccsUseCases}" CURL=(curl -s) if [ -n "${CF_ACCESS_CLIENT_ID:-}" ]; then CURL+=(-H "CF-Access-Client-Id: $CF_ACCESS_CLIENT_ID" -H "CF-Access-Client-Secret: ${CF_ACCESS_CLIENT_SECRET:?}") fi fails=0 pass() { echo " PASS $1"; } fail() { echo " FAIL $1"; fails=$((fails+1)); } mkdir -p "$DOWNLOADS" echo "== 1. login ==" WALL_ALTERNATIVES=" Compile the grammar with a local cppcc if you have one licensed -- this archive ships the .sgr, which is the artifact the service would compile for you." # ⚠ THE LOGIN MUST DIAGNOSE, NOT TRACEBACK. This piped curl straight # into json.load, so ANY non-JSON reply -- a Cloudflare Access redirect, # a 502, a captive portal -- surfaced as a raw Python JSONDecodeError # followed by "check B3U_EMAIL/B3U_PASSWORD", which was both wrong and # unactionable. Found by a user whose credentials were perfectly fine # and who was told to check them anyway. _lb=$(mktemp); _lh=$(mktemp) code=$("${CURL[@]}" -o "$_lb" -D "$_lh" -w '%{http_code}' \ -X POST "$B3U_BASE/login" -H 'Content-Type: application/json' \ -d "{\"email\":\"$B3U_EMAIL\",\"password\":\"$B3U_PASSWORD\"}") token=$(python3 -c 'import json,sys try: print(json.load(open(sys.argv[1])).get("session_token","")) except Exception: print("")' "$_lb") if [ -n "$token" ]; then pass "login -> bearer token" else if grep -qi 'cloudflareaccess\.com' "$_lh" "$_lb" 2>/dev/null; then fail "login -- $B3U_BASE is behind a Cloudflare Access wall (HTTP $code)" echo " The wall answers BEFORE the application does, so no" echo " email or password can reach it. Your credentials are" echo " not the problem." echo echo " b3u.dev itself is open -- so a wall in front of it means" echo " B3U_BASE is pointed at a PRIVATE host (a staging or" echo " preview hostname), not at the public service. Check" echo " B3U_BASE; it should be https://b3u.dev." if [ -n "${WALL_ALTERNATIVES:-}" ]; then echo "$WALL_ALTERNATIVES" fi # (CF_ACCESS_CLIENT_ID / CF_ACCESS_CLIENT_SECRET are honoured above # if you hold a service token for a private host. Reaching the # public service needs no token.) elif [ "$code" = 401 ] || [ "$code" = 403 ]; then fail "login -- rejected (HTTP $code). Check B3U_EMAIL / B3U_PASSWORD." elif [ "$code" = 000 ]; then fail "login -- could not reach $B3U_BASE at all (DNS, TLS or network)." else fail "login -- no session_token in the reply (HTTP $code)" echo " first bytes: $(head -c 160 "$_lb" | tr -d '\r\n')" fi rm -f "$_lb" "$_lh"; exit 1 fi rm -f "$_lb" "$_lh" AUTH=(-H "Authorization: Bearer $token") echo "== 2. download the use-case code zip ==" code=$("${CURL[@]}" "${AUTH[@]}" \ -o "$DOWNLOADS/metta_bench-code.zip" -w '%{http_code}' \ "$B3U_BASE/usecases/metta_bench-code.zip") [ "$code" = 200 ] && pass "GET /usecases/metta_bench-code.zip" \ || { fail "code zip download (got $code)"; exit 1; } echo "== 3. lay out the working directory (the article's section 2) ==" mkdir -p "$ROOT_DIR/metta_bench" cd "$ROOT_DIR/metta_bench" mv "$DOWNLOADS/metta_bench-code.zip" . unzip -o -q metta_bench-code.zip \ && [ -f metta_counts.cc ] && [ -f makefile ] \ && pass "code zip unpacked (metta_counts.cc + makefile)" \ || { fail "unzip / members"; exit 1; } echo "== 4. precondition: the metta delivery layout ==" if [ -d "$ROOT_DIR/metta/scbcpp" ] && [ -d "$ROOT_DIR/metta/cpp" ]; then pass "metta layout present (scbcpp/ + cpp/)" else fail "metta layout missing — run metta/e2e_test.sh first" exit 1 fi echo "== 5. build metta_counts ==" make METTA_HOME="$ROOT_DIR/metta" > make.log 2>&1 \ && pass "make (see make.log)" \ || { fail "make failed (see $ROOT_DIR/metta_bench/make.log)"; exit 1; } [ -x metta_counts ] \ && pass "metta_counts BUILT and executable" \ || fail "metta_counts missing or not executable" echo "== 6. RUN it against a real instance ==" # Goes past "it built", like the obfuscator use case: the walkers # oracle each other, running them is microseconds, and a consumer you # have not run is a consumer you do not have. SAMPLE="$ROOT_DIR/metta/e2e_sample.metta.bfgr" if [ ! -f "$SAMPLE" ]; then SAMPLE="$ROOT_DIR/metta/sample.metta.bfgr" cat > "$ROOT_DIR/metta/sample.metta" <<'SAMPLE_EOF' ; e2e sample (= (greet $who) (say "hello" $who)) (= (add $a $b) (+ $a $b)) (greet &self) (add 1 2) (: greet (-> Symbol Expression)) SAMPLE_EOF ( cd "$ROOT_DIR/metta" && ./ccsmetta -lt sample.metta >/dev/null 2>&1 ) fi if [ -f "$SAMPLE" ]; then out=$(./metta_counts "$SAMPLE" 100 2>&1) echo "$out" | grep -q "walkers agree" \ && pass "both walkers agree (they oracle each other)" \ || { fail "walkers disagreed or the run failed"; echo "$out"; } # the counts are checkable by hand against the sample source, which # is how the nnn-vs-kkk bug in the walker was originally found echo "$out" | grep -q "var=6" && echo "$out" | grep -q "ref=1" \ && pass "counts match the sample by hand (6 \$vars, 1 &ref)" \ || { fail "unexpected counts — walker or grammar drift"; echo "$out"; } else fail "no .metta.bfgr to read — run the metta use case first" fi echo if [ "$fails" -eq 0 ]; then echo "CCS_MeTTa_BENCH E2E: ALL PASS (./metta_counts is ready;" echo " fetch the corpus per the article's section 3 to benchmark)" exit 0 else echo "CCS_MeTTa_BENCH E2E: $fails FAILURE(S)" exit 1 fi