#!/usr/bin/env bash
# next/tests/nx_kernel_multi.sh — m2 Step 1a tests.
#
# Pure-functional multi-actor bucket APIs. No gen_server.

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

# Two actors share the same signing-key bytes but have different ids;
# signatures verify because each carries the matching public_keys
# entry. AliceK / BobK distinguish them visually only.
PRELUDE='AliceK = <<1,2,3,4>>, AliceKS = [{key_id,k1},{algorithm,ed25519},{value,AliceK}], AliceAS = [{public_keys,[[{id,k1},{created,0},{value,AliceK}]]}], BobK = <<5,6,7,8>>, BobKS = [{key_id,k1},{algorithm,ed25519},{value,BobK}], BobAS = [{public_keys,[[{id,k1},{created,0},{value,BobK}]]}], Req = [{type,create},{object,nil}],'

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 "(get (erlang-load-module (file-read \"next/kernel/envelope.erl\")) :name)")
(epoch 3)
(eval "(get (erlang-load-module (file-read \"next/kernel/log.erl\")) :name)")
(epoch 4)
(eval "(get (erlang-load-module (file-read \"next/kernel/pipeline.erl\")) :name)")
(epoch 5)
(eval "(get (erlang-load-module (file-read \"next/kernel/outbox.erl\")) :name)")
(epoch 6)
(eval "(get (erlang-load-module (file-read \"next/kernel/nx_kernel.erl\")) :name)")

;; new/0 returns kernel with 0 actors
(epoch 10)
(eval "(erlang-eval-ast \"S = nx_kernel:new(), nx_kernel:actor_count(S)\")")

;; new/0 has next_actor_seq = 1
(epoch 11)
(eval "(erlang-eval-ast \"S = nx_kernel:new(), nx_kernel:next_actor_seq(S)\")")

;; new/0 has actor_id/1 = nil  (legacy accessor returns nil with no actors)
(epoch 12)
(eval "(get (erlang-eval-ast \"S = nx_kernel:new(), nx_kernel:actor_id(S) =:= nil\") :name)")

;; add_actor returns {ok, NewState}
(epoch 13)
(eval "(get (erlang-eval-ast \"${PRELUDE} {ok, _} = nx_kernel:add_actor(alice, AliceKS, AliceAS, nx_kernel:new()), ok\") :name)")

;; has_actor returns true after add
(epoch 14)
(eval "(get (erlang-eval-ast \"${PRELUDE} {ok, S} = nx_kernel:add_actor(alice, AliceKS, AliceAS, nx_kernel:new()), nx_kernel:has_actor(alice, S)\") :name)")

;; actors/1 lists the new actor
(epoch 15)
(eval "(get (erlang-eval-ast \"${PRELUDE} {ok, S} = nx_kernel:add_actor(alice, AliceKS, AliceAS, nx_kernel:new()), nx_kernel:actors(S) =:= [alice]\") :name)")

;; add_actor twice same id -> {error, already_present}
(epoch 16)
(eval "(get (erlang-eval-ast \"${PRELUDE} {ok, S} = nx_kernel:add_actor(alice, AliceKS, AliceAS, nx_kernel:new()), case nx_kernel:add_actor(alice, AliceKS, AliceAS, S) of {error, already_present} -> ok; _ -> bad end\") :name)")

;; add two distinct actors -> both present
(epoch 17)
(eval "(get (erlang-eval-ast \"${PRELUDE} {ok, S1} = nx_kernel:add_actor(alice, AliceKS, AliceAS, nx_kernel:new()), {ok, S2} = nx_kernel:add_actor(bob, BobKS, BobAS, S1), nx_kernel:actors(S2) =:= [alice, bob]\") :name)")

;; next_actor_seq increments per add
(epoch 18)
(eval "(erlang-eval-ast \"${PRELUDE} {ok, S1} = nx_kernel:add_actor(alice, AliceKS, AliceAS, nx_kernel:new()), {ok, S2} = nx_kernel:add_actor(bob, BobKS, BobAS, S1), nx_kernel:next_actor_seq(S2)\")")

;; publish/3 to known actor returns {ok, _, NewState}
(epoch 19)
(eval "(get (erlang-eval-ast \"${PRELUDE} {ok, S1} = nx_kernel:add_actor(alice, AliceKS, AliceAS, nx_kernel:new()), case nx_kernel:publish(alice, Req, S1) of {ok, _, _} -> ok; _ -> bad end\") :name)")

;; publish/3 advances only the named actor's log
(epoch 20)
(eval "(get (erlang-eval-ast \"${PRELUDE} {ok, S1} = nx_kernel:add_actor(alice, AliceKS, AliceAS, nx_kernel:new()), {ok, S2} = nx_kernel:add_actor(bob, BobKS, BobAS, S1), {ok, _, S3} = nx_kernel:publish(alice, Req, S2), AliceTip = nx_kernel:actor_log_tip(alice, S3), BobTip = nx_kernel:actor_log_tip(bob, S3), {AliceTip, BobTip} =:= {1, 0}\") :name)")

;; publish/3 to unknown actor -> {error, no_actor, State}
(epoch 21)
(eval "(get (erlang-eval-ast \"${PRELUDE} S = nx_kernel:new(), case nx_kernel:publish(ghost, Req, S) of {error, no_actor, _} -> ok; _ -> bad end\") :name)")

;; Two actors maintain independent next_published counters
(epoch 22)
(eval "(get (erlang-eval-ast \"${PRELUDE} {ok, S1} = nx_kernel:add_actor(alice, AliceKS, AliceAS, nx_kernel:new()), {ok, S2} = nx_kernel:add_actor(bob, BobKS, BobAS, S1), {ok, _, S3} = nx_kernel:publish(alice, Req, S2), {ok, _, S4} = nx_kernel:publish(alice, Req, S3), {ok, _, S5} = nx_kernel:publish(bob, Req, S4), {ok, AliceN} = nx_kernel:actor_next_published(alice, S5), {ok, BobN} = nx_kernel:actor_next_published(bob, S5), {AliceN, BobN} =:= {3, 2}\") :name)")

;; actor_state/2 returns per-actor AS
(epoch 23)
(eval "(get (erlang-eval-ast \"${PRELUDE} {ok, S1} = nx_kernel:add_actor(alice, AliceKS, AliceAS, nx_kernel:new()), {ok, S2} = nx_kernel:add_actor(bob, BobKS, BobAS, S1), {ok, ASa} = nx_kernel:actor_state(alice, S2), {ok, ASb} = nx_kernel:actor_state(bob, S2), {ASa, ASb} =:= {AliceAS, BobAS}\") :name)")

;; with_actor_projections sets per-actor projection list
(epoch 24)
(eval "(get (erlang-eval-ast \"${PRELUDE} {ok, S1} = nx_kernel:add_actor(alice, AliceKS, AliceAS, nx_kernel:new()), {ok, S2} = nx_kernel:add_actor(bob, BobKS, BobAS, S1), {ok, S3} = nx_kernel:with_actor_projections(alice, [px], S2), {ok, AliceP} = nx_kernel:actor_projections(alice, S3), {ok, BobP} = nx_kernel:actor_projections(bob, S3), {AliceP, BobP} =:= {[px], []}\") :name)")

;; Legacy new/3 + publish/2 still route to the single actor
(epoch 25)
(eval "(get (erlang-eval-ast \"${PRELUDE} S = nx_kernel:new(alice, AliceKS, AliceAS), {ok, _, S1} = nx_kernel:publish(Req, S), nx_kernel:log_tip(S1) =:= 1 andalso nx_kernel:actor_id(S1) =:= alice\") :name)")

;; ── Step 1b: gen_server multi-actor calls ──────────────────────
;; The Erlang-on-SX scheduler doesn't preserve spawned processes
;; across separate erlang-eval-ast invocations, so each gen_server
;; test inlines start_link with operations (same convention as
;; nx_kernel_server.sh).

(epoch 26)
(eval "(er-load-gen-server!)")

;; start_link works, actors/0 lists the single seeded actor
(epoch 30)
(eval "(get (erlang-eval-ast \"${PRELUDE} nx_kernel:start_link(alice, AliceKS, AliceAS), nx_kernel:actors() =:= [alice]\") :name)")

;; add_actor/3 (gen_server) -> :ok, actors/0 reflects both
(epoch 31)
(eval "(get (erlang-eval-ast \"${PRELUDE} nx_kernel:start_link(alice, AliceKS, AliceAS), ok = nx_kernel:add_actor(bob, BobKS, BobAS), nx_kernel:actors() =:= [alice, bob]\") :name)")

;; add_actor/3 duplicate -> {error, already_present}
(epoch 32)
(eval "(get (erlang-eval-ast \"${PRELUDE} nx_kernel:start_link(alice, AliceKS, AliceAS), case nx_kernel:add_actor(alice, AliceKS, AliceAS) of {error, already_present} -> ok; _ -> bad end\") :name)")

;; publish_to/2 advances only the named actor's log
(epoch 33)
(eval "(get (erlang-eval-ast \"${PRELUDE} nx_kernel:start_link(alice, AliceKS, AliceAS), nx_kernel:add_actor(bob, BobKS, BobAS), {ok, _} = nx_kernel:publish_to(alice, Req), AliceTip = nx_kernel:log_tip_for(alice), BobTip = nx_kernel:log_tip_for(bob), {AliceTip, BobTip} =:= {1, 0}\") :name)")

;; Interleaved publishes preserve per-actor counters
(epoch 34)
(eval "(get (erlang-eval-ast \"${PRELUDE} nx_kernel:start_link(alice, AliceKS, AliceAS), nx_kernel:add_actor(bob, BobKS, BobAS), nx_kernel:publish_to(alice, Req), nx_kernel:publish_to(bob, Req), nx_kernel:publish_to(alice, Req), AliceTip = nx_kernel:log_tip_for(alice), BobTip = nx_kernel:log_tip_for(bob), {AliceTip, BobTip} =:= {2, 1}\") :name)")

;; publish_to unknown actor -> {error, no_actor}, no kernel crash
(epoch 35)
(eval "(get (erlang-eval-ast \"${PRELUDE} nx_kernel:start_link(alice, AliceKS, AliceAS), case nx_kernel:publish_to(ghost, Req) of {error, no_actor} -> ok; _ -> bad end\") :name)")

;; state_for/1 returns the per-actor AS
(epoch 36)
(eval "(get (erlang-eval-ast \"${PRELUDE} nx_kernel:start_link(alice, AliceKS, AliceAS), nx_kernel:add_actor(bob, BobKS, BobAS), {ok, ASb} = nx_kernel:state_for(bob), ASb =:= BobAS\") :name)")

;; with_projections_for/2 sets per-actor projections, observable via bucket_for
(epoch 37)
(eval "(get (erlang-eval-ast \"${PRELUDE} nx_kernel:start_link(alice, AliceKS, AliceAS), nx_kernel:add_actor(bob, BobKS, BobAS), nx_kernel:with_projections_for(alice, [px]), {ok, AliceBucket} = nx_kernel:bucket_for(alice), {ok, BobBucket} = nx_kernel:bucket_for(bob), [{projections, AliceP} | _] = lists:filter(fun(P) -> element(1, P) =:= projections end, AliceBucket), [{projections, BobP} | _] = lists:filter(fun(P) -> element(1, P) =:= projections end, BobBucket), {AliceP, BobP} =:= {[px], []}\") :name)")
EPOCHS

OUTPUT=$(timeout 240 "$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  6  "nx_kernel module loaded"            "nx_kernel"
check 10  "new/0 -> 0 actors"                  "0"
check 11  "new/0 -> next_actor_seq = 1"        "1"
check 12  "new/0 actor_id = nil"               "true"
check 13  "add_actor returns {ok, NewState}"   "ok"
check 14  "has_actor true after add"           "true"
check 15  "actors/1 lists added actor"         "true"
check 16  "duplicate add -> already_present"   "ok"
check 17  "two distinct actors both present"   "true"
check 18  "next_actor_seq increments"          "3"
check 19  "publish/3 returns {ok, _, S}"       "ok"
check 20  "publish/3 isolates per actor"       "true"
check 21  "publish/3 unknown -> no_actor"      "ok"
check 22  "independent next_published seqs"    "true"
check 23  "actor_state/2 per-actor"            "true"
check 24  "with_actor_projections per-actor"   "true"
check 25  "legacy new/3 + publish/2 routes"    "true"
check 26  "gen_server loaded"                  "gen_server"
check 30  "start_link seeds bucket 0"          "true"
check 31  "add_actor/3 (srv) -> ok + actors"   "true"
check 32  "add_actor/3 duplicate detected"     "ok"
check 33  "publish_to/2 isolates per actor"    "true"
check 34  "interleaved publishes per actor"    "true"
check 35  "publish_to unknown -> no_actor"     "ok"
check 36  "state_for/1 per-actor AS"           "true"
check 37  "with_projections_for per-actor"     "true"

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