#!/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" < {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)") 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="" 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" 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 ]