#!/usr/bin/env bash
# next/tests/smoke_pin_pure.sh — Step 9a-pure smoke test.
#
# Mirrors plans/fed-sx-milestone-1.md §Step 9a but without TCP /
# curl / JSON. Exercises Pin-verb extensibility end-to-end:
#   1. define_registry fold projection registers DefineActivity
#   2. A pin-state projection (Erlang fun) folds Pin activities
#   3. Both projections wired into nx_kernel
#   4. Publish Create{DefineActivity{name: pin}} -> registry update
#   5. Publish Pin{path:..., cid:...} -> pin-state update
#
# Proves the meta-projection + verb-fold mechanism is wired
# correctly. The remaining Step 9a deliverable (curl smoke test)
# layers TCP on top — needs Step 8b-start. 14 cases.

set -uo pipefail
cd "$(git rev-parse --show-toplevel)"

SX_SERVER="${SX_SERVER:-hosts/ocaml/_build/default/bin/sx_server.exe}"
if [ ! -x "$SX_SERVER" ]; then
  SX_SERVER="/root/rose-ash/hosts/ocaml/_build/default/bin/sx_server.exe"
fi
if [ ! -x "$SX_SERVER" ]; then
  echo "ERROR: sx_server.exe not found." >&2
  exit 1
fi

VERBOSE="${1:-}"
PASS=0; FAIL=0; ERRORS=""
TMPFILE=$(mktemp); trap "rm -f $TMPFILE" EXIT

# Shared prelude — starts kernel + two projections, wires them in,
# binds DefineAct and PinAct ready to publish.
PRELUDE='KM = <<1,2,3,4>>, KS = [{key_id,k1},{algorithm,ed25519},{value,KM}], AS = [{public_keys,[[{id,k1},{created,0},{value,KM}]]}], PinFold = fun (Act, S) -> case envelope:get_field(type, Act) of {ok, pin} -> case envelope:get_field(object, Act) of {ok, Obj} -> {ok, P} = envelope:get_field(path, Obj), {ok, C} = envelope:get_field(cid, Obj), [{P, C} | S]; _ -> S end; _ -> S end end, projection:start_link(define_reg, registry:new(), define_registry:fold_fn()), projection:start_link(pin_state, [], PinFold), nx_kernel:start_link(alice, KS, AS), nx_kernel:with_projections([define_reg, pin_state]), DefineAct = [{type, create}, {object, [{type, define_activity}, {name, pin}]}], PinAct = [{type, pin}, {object, [{path, docs_intro}, {cid, qm_cid_1}]}],'

cat > "$TMPFILE" <<EPOCHS
(epoch 1)
(load "lib/erlang/tokenizer.sx")
(load "lib/erlang/parser.sx")
(load "lib/erlang/parser-core.sx")
(load "lib/erlang/parser-expr.sx")
(load "lib/erlang/parser-module.sx")
(load "lib/erlang/transpile.sx")
(load "lib/erlang/runtime.sx")
(load "lib/erlang/vm/dispatcher.sx")
(epoch 2)
(eval "(er-load-gen-server!)")
(epoch 3)
(eval "(get (erlang-load-module (file-read \"next/kernel/envelope.erl\")) :name)")
(epoch 4)
(eval "(get (erlang-load-module (file-read \"next/kernel/log.erl\")) :name)")
(epoch 5)
(eval "(get (erlang-load-module (file-read \"next/kernel/pipeline.erl\")) :name)")
(epoch 6)
(eval "(get (erlang-load-module (file-read \"next/kernel/registry.erl\")) :name)")
(epoch 7)
(eval "(get (erlang-load-module (file-read \"next/kernel/projection.erl\")) :name)")
(epoch 8)
(eval "(get (erlang-load-module (file-read \"next/kernel/outbox.erl\")) :name)")
(epoch 9)
(eval "(get (erlang-load-module (file-read \"next/kernel/nx_kernel.erl\")) :name)")
(epoch 10)
(eval "(get (erlang-load-module (file-read \"next/kernel/define_registry.erl\")) :name)")

;; Initial state: pin_state empty
(epoch 20)
(eval "(get (erlang-eval-ast \"${PRELUDE} projection:query(pin_state) =:= []\") :name)")

;; Initial state: pin NOT in registry
(epoch 21)
(eval "(get (erlang-eval-ast \"${PRELUDE} registry:lookup(activity_types, pin, projection:query(define_reg)) =:= not_found\") :name)")

;; Step 1: Publish DefineActivity{pin}, then pin IS in the registry
(epoch 22)
(eval "(get (erlang-eval-ast \"${PRELUDE} nx_kernel:publish(DefineAct), case registry:lookup(activity_types, pin, projection:query(define_reg)) of {ok, _} -> ok; _ -> bad end\") :name)")

;; Define activity does NOT advance pin_state
(epoch 23)
(eval "(get (erlang-eval-ast \"${PRELUDE} nx_kernel:publish(DefineAct), projection:query(pin_state) =:= []\") :name)")

;; Step 2: Publish Pin activity, pin_state has the {path, cid}
(epoch 24)
(eval "(get (erlang-eval-ast \"${PRELUDE} nx_kernel:publish(PinAct), projection:query(pin_state) =:= [{docs_intro, qm_cid_1}]\") :name)")

;; Pin activity does NOT add to the registry
(epoch 25)
(eval "(get (erlang-eval-ast \"${PRELUDE} nx_kernel:publish(PinAct), length(registry:list(activity_types, projection:query(define_reg))) =:= 0\") :name)")

;; Both publishes interleaved — order independent
(epoch 26)
(eval "(get (erlang-eval-ast \"${PRELUDE} nx_kernel:publish(DefineAct), nx_kernel:publish(PinAct), {projection:query(pin_state), case registry:lookup(activity_types, pin, projection:query(define_reg)) of {ok, _} -> registered; _ -> unregistered end} =:= {[{docs_intro, qm_cid_1}], registered}\") :name)")

;; Reverse order: publish Pin FIRST, then DefineActivity — Pin still folds
(epoch 27)
(eval "(get (erlang-eval-ast \"${PRELUDE} nx_kernel:publish(PinAct), nx_kernel:publish(DefineAct), {projection:query(pin_state), case registry:lookup(activity_types, pin, projection:query(define_reg)) of {ok, _} -> registered; _ -> unregistered end} =:= {[{docs_intro, qm_cid_1}], registered}\") :name)")

;; Two Pins -> two entries in pin_state (newest-first)
(epoch 28)
(eval "(get (erlang-eval-ast \"${PRELUDE} PinAct2 = [{type, pin}, {object, [{path, docs_arch}, {cid, qm_cid_2}]}], nx_kernel:publish(PinAct), nx_kernel:publish(PinAct2), projection:query(pin_state) =:= [{docs_arch, qm_cid_2}, {docs_intro, qm_cid_1}]\") :name)")

;; Log tip advances with each publish
(epoch 29)
(eval "(get (erlang-eval-ast \"${PRELUDE} nx_kernel:publish(DefineAct), nx_kernel:publish(PinAct), nx_kernel:log_tip() =:= 2\") :name)")

;; Multiple DefineActivity registrations (different names) accumulate
(epoch 30)
(eval "(get (erlang-eval-ast \"${PRELUDE} Foo = [{type, create}, {object, [{type, define_activity}, {name, foo}]}], nx_kernel:publish(DefineAct), nx_kernel:publish(Foo), length(registry:list(activity_types, projection:query(define_reg))) =:= 2\") :name)")

;; pin_state survives an empty-publish round (non-Pin doesn't disturb)
(epoch 31)
(eval "(get (erlang-eval-ast \"${PRELUDE} Other = [{type, create}, {object, [{type, note}, {content, hi}]}], nx_kernel:publish(PinAct), nx_kernel:publish(Other), projection:query(pin_state) =:= [{docs_intro, qm_cid_1}]\") :name)")
EPOCHS

OUTPUT=$(timeout 300 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)

check() {
  local epoch="$1" desc="$2" expected="$3"
  local actual
  actual=$(echo "$OUTPUT" | awk -v e="$epoch" '
    $0 ~ "^\\(ok-len " e " " { getline; print; exit }
    $0 ~ "^\\(ok " e " "     { print; exit }
    $0 ~ "^\\(error " e " "  { print; exit }
  ')
  [ -z "$actual" ] && actual="<no output for epoch $epoch>"
  if echo "$actual" | grep -qF -- "$expected"; then
    PASS=$((PASS+1))
    [ "$VERBOSE" = "-v" ] && echo "  ok $desc"
  else
    FAIL=$((FAIL+1))
    ERRORS+="  FAIL [$desc] (epoch $epoch) expected: $expected | actual: $actual
"
  fi
}

check 10  "define_registry loaded"            "define_registry"
check 20  "initial pin_state is []"           "true"
check 21  "pin not in registry initially"     "true"
check 22  "DefineActivity registers pin"      "ok"
check 23  "DefineActivity skips pin_state"    "true"
check 24  "Pin advances pin_state"            "true"
check 25  "Pin doesn't register a type"       "true"
check 26  "both publishes: both states ok"    "true"
check 27  "reverse order works too"           "true"
check 28  "two Pins -> two entries"           "true"
check 29  "log tip after two publishes"       "true"
check 30  "two DefineActivities accumulate"   "true"
check 31  "Note doesn't disturb pin_state"    "true"

TOTAL=$((PASS+FAIL))
if [ $FAIL -eq 0 ]; then
  echo "ok $PASS/$TOTAL next/tests/smoke_pin_pure.sh passed"
else
  echo "FAIL $PASS/$TOTAL passed, $FAIL failed:"
  echo "$ERRORS"
fi
[ $FAIL -eq 0 ]
