#!/usr/bin/env bash
# next/tests/smoke_app_pure.sh — Step 9b-pure smoke test.
#
# Mirrors §Step 9b structurally without TCP/curl/JSON. A trigger
# projection (Erlang fun) matches Note activities tagged
# "smoketest", constructs a derived TestEcho activity carrying
# the Note's CID via :echoes, and captures it into projection
# state. Proves the reactive-application mechanism — match-then-
# derive — works end-to-end through nx_kernel's broadcast.
#
# Cascade publication (the trigger actually publishing the
# derived activity back through outbox) is sidestepped to avoid
# gen_server reentrancy; the projection state is the proof point.
# 12 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 — KM/KS/AS, the Match function (Note +
# smoketest tag), the trigger fold body, and various activity
# proplists.
PRELUDE='KM = <<1,2,3,4>>, KS = [{key_id,k1},{algorithm,ed25519},{value,KM}], AS = [{public_keys,[[{id,k1},{created,0},{value,KM}]]}], Match = fun (Act) -> case envelope:get_field(type, Act) of {ok, note} -> case envelope:get_field(object, Act) of {ok, Obj} -> case envelope:get_field(tags, Obj) of {ok, Tags} -> lists:member(smoketest, Tags); _ -> false end; _ -> false end; _ -> false end end, TrigFold = fun (Act, {Captured, Count}) -> case Match(Act) of true -> {ok, Id} = envelope:get_field(id, Act), Derived = [{type, test_echo}, {object, [{echoes, Id}]}], {[Derived | Captured], Count + 1}; false -> {Captured, Count} end end, projection:start_link(trig, {[], 0}, TrigFold), nx_kernel:start_link(alice, KS, AS), nx_kernel:with_projections([trig]), MatchNote = [{type, note}, {object, [{content, hi}, {tags, [smoketest]}]}], NoMatchNote = [{type, note}, {object, [{content, plain}, {tags, [other]}]}],'

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/projection.erl\")) :name)")
(epoch 7)
(eval "(get (erlang-load-module (file-read \"next/kernel/outbox.erl\")) :name)")
(epoch 8)
(eval "(get (erlang-load-module (file-read \"next/kernel/nx_kernel.erl\")) :name)")

;; Initial: no triggers fired
(epoch 10)
(eval "(erlang-eval-ast \"${PRELUDE} {_, Count} = projection:query(trig), Count\")")

;; Matching Note fires the trigger once
(epoch 11)
(eval "(erlang-eval-ast \"${PRELUDE} nx_kernel:publish(MatchNote), {_, Count} = projection:query(trig), Count\")")

;; Non-matching Note does NOT fire trigger
(epoch 12)
(eval "(erlang-eval-ast \"${PRELUDE} nx_kernel:publish(NoMatchNote), {_, Count} = projection:query(trig), Count\")")

;; Mix: one match + one non-match -> trigger fires exactly once
(epoch 13)
(eval "(erlang-eval-ast \"${PRELUDE} nx_kernel:publish(MatchNote), nx_kernel:publish(NoMatchNote), {_, Count} = projection:query(trig), Count\")")

;; Trigger captures the derived TestEcho
(epoch 14)
(eval "(get (erlang-eval-ast \"${PRELUDE} nx_kernel:publish(MatchNote), {[Derived], _} = projection:query(trig), envelope:get_field(type, Derived) =:= {ok, test_echo}\") :name)")

;; Derived TestEcho :echoes points at the Note's :id (CID)
(epoch 15)
(eval "(get (erlang-eval-ast \"${PRELUDE} nx_kernel:publish(MatchNote), {[Derived], _} = projection:query(trig), {ok, Obj} = envelope:get_field(object, Derived), {ok, EchoesId} = envelope:get_field(echoes, Obj), [Logged] = log:entries(nx_kernel:log_state(nx_kernel:query())), {ok, LoggedId} = envelope:get_field(id, Logged), EchoesId =:= LoggedId\") :name)")

;; Two matching Notes -> trigger fires twice, captures both derived
(epoch 16)
(eval "(erlang-eval-ast \"${PRELUDE} nx_kernel:publish(MatchNote), MatchNote2 = [{type, note}, {object, [{content, hello}, {tags, [smoketest]}]}], nx_kernel:publish(MatchNote2), {Captured, Count} = projection:query(trig), {length(Captured), Count}\")")

;; Trigger ignores non-Note activities even if they have :tags
(epoch 17)
(eval "(erlang-eval-ast \"${PRELUDE} OtherType = [{type, pin}, {object, [{tags, [smoketest]}, {path, p}, {cid, c}]}], nx_kernel:publish(OtherType), {_, Count} = projection:query(trig), Count\")")

;; Trigger ignores Note without :tags
(epoch 18)
(eval "(erlang-eval-ast \"${PRELUDE} NoTag = [{type, note}, {object, [{content, hi}]}], nx_kernel:publish(NoTag), {_, Count} = projection:query(trig), Count\")")

;; Multiple tags including smoketest -> matches
(epoch 19)
(eval "(erlang-eval-ast \"${PRELUDE} Many = [{type, note}, {object, [{content, hi}, {tags, [smoketest, foo, bar]}]}], nx_kernel:publish(Many), {_, Count} = projection:query(trig), Count\")")

;; Sig-failed publish doesn't reach the trigger
(epoch 20)
(eval "(erlang-eval-ast \"OtherKM = <<9,9,9,9>>, BadKS = [{key_id,k1},{algorithm,ed25519},{value,OtherKM}], AS = [{public_keys,[[{id,k1},{created,0},{value,<<1,2,3,4>>}]]}], Match = fun (Act) -> case envelope:get_field(type, Act) of {ok, note} -> case envelope:get_field(object, Act) of {ok, Obj} -> case envelope:get_field(tags, Obj) of {ok, Tags} -> lists:member(smoketest, Tags); _ -> false end; _ -> false end; _ -> false end end, TrigFold = fun (Act, {Captured, Count}) -> case Match(Act) of true -> {ok, Id} = envelope:get_field(id, Act), Derived = [{type, test_echo}, {object, [{echoes, Id}]}], {[Derived | Captured], Count + 1}; false -> {Captured, Count} end end, projection:start_link(trig, {[], 0}, TrigFold), nx_kernel:start_link(alice, BadKS, AS), nx_kernel:with_projections([trig]), MatchNote = [{type, note}, {object, [{content, hi}, {tags, [smoketest]}]}], nx_kernel:publish(MatchNote), {_, Count} = projection:query(trig), Count\")")
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  8  "nx_kernel module loaded"           "nx_kernel"
check 10  "initial Count = 0"                 "0"
check 11  "Match fires once"                  "1"
check 12  "Non-match does NOT fire"           "0"
check 13  "Mix: only match fires"             "1"
check 14  "Derived type = test_echo"          "true"
check 15  "Derived :echoes = Note's :id"      "true"
check 16  "Two matches -> 2 derived, count 2" "(2 2)"
check 17  "Non-Note ignored"                  "0"
check 18  "Note without tags ignored"         "0"
check 19  "Multi-tag includes smoketest"      "1"
check 20  "Sig failure -> no trigger"         "0"

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