#!/usr/bin/env bash
# next/tests/term_codec.sh — Step 3b term codec acceptance test.
#
# Exercises encode/1 + decode/1 for atoms, integers, binaries, tuples,
# lists, nesting, and round-trip equivalence. Built on the substrate-fix
# trio: binary_to_list/list_to_binary (24e3bf53), $X literals (3d80bd8c),
# atom_to_list/integer_to_list charlists (4852cca9).

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

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/term_codec.erl\")) :name)")

;; --- encode produces correct headers ---

;; atom 'ok' -> bytes "a2:ok"
(epoch 10)
(eval "(get (erlang-eval-ast \"term_codec:encode(ok) =:= <<97, 50, 58, 111, 107>>\") :name)")

;; integer 42 -> "i2:42"
(epoch 11)
(eval "(get (erlang-eval-ast \"term_codec:encode(42) =:= <<105, 50, 58, 52, 50>>\") :name)")

;; negative integer -99 -> "i3:-99"
(epoch 12)
(eval "(get (erlang-eval-ast \"term_codec:encode(-99) =:= <<105, 51, 58, 45, 57, 57>>\") :name)")

;; binary <<1,2,3>> -> "b3:" + 1,2,3
(epoch 13)
(eval "(get (erlang-eval-ast \"term_codec:encode(<<1, 2, 3>>) =:= <<98, 51, 58, 1, 2, 3>>\") :name)")

;; empty list -> "l0:"
(epoch 14)
(eval "(get (erlang-eval-ast \"term_codec:encode([]) =:= <<108, 48, 58>>\") :name)")

;; tuple {a, b} -> "t2:" + enc(a) + enc(b) = "t2:a1:aa1:b"
(epoch 15)
(eval "(get (erlang-eval-ast \"term_codec:encode({a, b}) =:= <<116, 50, 58, 97, 49, 58, 97, 97, 49, 58, 98>>\") :name)")

;; --- round-trip: encode then decode returns original term ---

(epoch 20)
(eval "(get (erlang-eval-ast \"{ok, T, _} = term_codec:decode(term_codec:encode(ok)), T =:= ok\") :name)")

(epoch 21)
(eval "(get (erlang-eval-ast \"{ok, T, _} = term_codec:decode(term_codec:encode(42)), T =:= 42\") :name)")

(epoch 22)
(eval "(get (erlang-eval-ast \"{ok, T, _} = term_codec:decode(term_codec:encode(-99)), T =:= -99\") :name)")

(epoch 23)
(eval "(get (erlang-eval-ast \"{ok, T, _} = term_codec:decode(term_codec:encode(<<1, 2, 3, 4, 5>>)), T =:= <<1, 2, 3, 4, 5>>\") :name)")

(epoch 24)
(eval "(get (erlang-eval-ast \"{ok, T, _} = term_codec:decode(term_codec:encode([])), T =:= []\") :name)")

(epoch 25)
(eval "(get (erlang-eval-ast \"{ok, T, _} = term_codec:decode(term_codec:encode({a, b, c})), T =:= {a, b, c}\") :name)")

(epoch 26)
(eval "(get (erlang-eval-ast \"{ok, T, _} = term_codec:decode(term_codec:encode([1, 2, 3])), T =:= [1, 2, 3]\") :name)")

;; --- nested: activity-shaped term (atoms, ints, binaries, nested tuple+list) ---

(epoch 30)
(eval "(get (erlang-eval-ast \"Act = {create, [{id, 1}, {actor, alice}, {payload, <<104, 105>>}]}, {ok, T, _} = term_codec:decode(term_codec:encode(Act)), T =:= Act\") :name)")

;; --- decode returns remainder so multiple frames can be streamed ---

(epoch 31)
(eval "(get (erlang-eval-ast \"E1 = term_codec:encode(foo), E2 = term_codec:encode(42), Both = list_to_binary([E1, E2]), {ok, T1, Rest} = term_codec:decode(Both), {ok, T2, _} = term_codec:decode(Rest), {T1, T2} =:= {foo, 42}\") :name)")

;; --- binary content with embedded zero / newline bytes round-trips ---

(epoch 32)
(eval "(get (erlang-eval-ast \"B = <<0, 10, 0, 10, 0>>, {ok, T, _} = term_codec:decode(term_codec:encode(B)), T =:= B\") :name)")

;; --- bad form returns {error, _} not a crash ---

(epoch 40)
(eval "(get (erlang-eval-ast \"element(1, term_codec:decode(<<122, 122, 122>>))\") :name)")
EPOCHS

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

check() {
  local epoch="$1" desc="$2" expected="$3"
  local actual
  actual=$(echo "$OUTPUT" | grep -A1 "^(ok-len $epoch " | tail -1 || true)
  if echo "$actual" | grep -q "^(ok-len"; then actual=""; fi
  if [ -z "$actual" ]; then
    actual=$(echo "$OUTPUT" | grep "^(ok $epoch " | head -1 || true)
  fi
  if [ -z "$actual" ]; then
    actual=$(echo "$OUTPUT" | grep "^(error $epoch " | head -1 || true)
  fi
  [ -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 2  "module loads"                "term_codec"
check 10 "encode atom"                 "true"
check 11 "encode int"                  "true"
check 12 "encode neg int"              "true"
check 13 "encode binary"               "true"
check 14 "encode []"                   "true"
check 15 "encode tuple"                "true"
check 20 "round-trip atom"             "true"
check 21 "round-trip int"              "true"
check 22 "round-trip neg int"          "true"
check 23 "round-trip binary"           "true"
check 24 "round-trip []"               "true"
check 25 "round-trip tuple"            "true"
check 26 "round-trip list"             "true"
check 30 "round-trip nested activity"  "true"
check 31 "streaming two frames"        "true"
check 32 "binary w/ embedded NUL+LF"   "true"
check 40 "bad form -> error tag"       "error"

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