#!/usr/bin/env bash
# Tcl-on-SX test runner — epoch protocol to sx_server.exe
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"; exit 1; fi

VERBOSE="${1:-}"
TMPFILE=$(mktemp)
HELPER=$(mktemp --suffix=.sx)
trap "rm -f $TMPFILE $HELPER" EXIT

# Helper file: run all test suites and format a parseable summary string
cat > "$HELPER" << 'HELPER_EOF'
(define __pr (tcl-run-parse-tests))
(define __er (tcl-run-eval-tests))
(define __xr (tcl-run-error-tests))
(define __nr (tcl-run-namespace-tests))
(define __cr (tcl-run-coro-tests))
(define __ir (tcl-run-idiom-tests))
(define tcl-test-summary
  (str "PARSE:" (get __pr "passed") ":" (get __pr "failed")
       " EVAL:" (get __er "passed") ":" (get __er "failed")
       " ERROR:" (get __xr "passed") ":" (get __xr "failed")
       " NAMESPACE:" (get __nr "passed") ":" (get __nr "failed")
       " CORO:" (get __cr "passed") ":" (get __cr "failed")
       " IDIOM:" (get __ir "passed") ":" (get __ir "failed")))
HELPER_EOF

cat > "$TMPFILE" << EPOCHS
(epoch 1)
(load "lib/guest/lex.sx")
(load "lib/guest/prefix.sx")
(load "lib/tcl/tokenizer.sx")
(epoch 2)
(load "lib/tcl/parser.sx")
(epoch 3)
(load "lib/tcl/tests/parse.sx")
(epoch 4)
(load "lib/fiber.sx")
(load "lib/guest/reflective/env.sx")
(load "lib/tcl/runtime.sx")
(epoch 5)
(load "lib/tcl/tests/eval.sx")
(epoch 6)
(load "lib/tcl/tests/error.sx")
(epoch 7)
(load "lib/tcl/tests/namespace.sx")
(epoch 8)
(load "lib/tcl/tests/coro.sx")
(epoch 9)
(load "lib/tcl/tests/idioms.sx")
(epoch 10)
(load "$HELPER")
(epoch 11)
(eval "tcl-test-summary")
EPOCHS

OUTPUT=$(timeout 300 "$SX_SERVER" < "$TMPFILE" 2>&1)
[ "$VERBOSE" = "-v" ] && echo "$OUTPUT"

# Extract summary line from epoch 11 output
SUMMARY=$(echo "$OUTPUT" | grep -A1 "^(ok-len 11 " | tail -1 | tr -d '"')

if [ -z "$SUMMARY" ]; then
  echo "ERROR: no summary from test run"
  echo "$OUTPUT" | tail -20
  exit 1
fi

# Parse PARSE:N:M EVAL:N:M ERROR:N:M NAMESPACE:N:M CORO:N:M IDIOM:N:M
PARSE_PART=$(echo "$SUMMARY"     | grep -o 'PARSE:[0-9]*:[0-9]*')
EVAL_PART=$(echo "$SUMMARY"      | grep -o 'EVAL:[0-9]*:[0-9]*')
ERROR_PART=$(echo "$SUMMARY"     | grep -o 'ERROR:[0-9]*:[0-9]*')
NAMESPACE_PART=$(echo "$SUMMARY" | grep -o 'NAMESPACE:[0-9]*:[0-9]*')
CORO_PART=$(echo "$SUMMARY"      | grep -o 'CORO:[0-9]*:[0-9]*')
IDIOM_PART=$(echo "$SUMMARY"     | grep -o 'IDIOM:[0-9]*:[0-9]*')

PARSE_PASSED=$(echo "$PARSE_PART"         | cut -d: -f2)
PARSE_FAILED=$(echo "$PARSE_PART"         | cut -d: -f3)
EVAL_PASSED=$(echo "$EVAL_PART"           | cut -d: -f2)
EVAL_FAILED=$(echo "$EVAL_PART"           | cut -d: -f3)
ERROR_PASSED=$(echo "$ERROR_PART"         | cut -d: -f2)
ERROR_FAILED=$(echo "$ERROR_PART"         | cut -d: -f3)
NAMESPACE_PASSED=$(echo "$NAMESPACE_PART" | cut -d: -f2)
NAMESPACE_FAILED=$(echo "$NAMESPACE_PART" | cut -d: -f3)
CORO_PASSED=$(echo "$CORO_PART"           | cut -d: -f2)
CORO_FAILED=$(echo "$CORO_PART"           | cut -d: -f3)
IDIOM_PASSED=$(echo "$IDIOM_PART"         | cut -d: -f2)
IDIOM_FAILED=$(echo "$IDIOM_PART"         | cut -d: -f3)

PARSE_PASSED=${PARSE_PASSED:-0};         PARSE_FAILED=${PARSE_FAILED:-1}
EVAL_PASSED=${EVAL_PASSED:-0};           EVAL_FAILED=${EVAL_FAILED:-1}
ERROR_PASSED=${ERROR_PASSED:-0};         ERROR_FAILED=${ERROR_FAILED:-1}
NAMESPACE_PASSED=${NAMESPACE_PASSED:-0}; NAMESPACE_FAILED=${NAMESPACE_FAILED:-1}
CORO_PASSED=${CORO_PASSED:-0};           CORO_FAILED=${CORO_FAILED:-1}
IDIOM_PASSED=${IDIOM_PASSED:-0};         IDIOM_FAILED=${IDIOM_FAILED:-1}

TOTAL_PASSED=$((PARSE_PASSED + EVAL_PASSED + ERROR_PASSED + NAMESPACE_PASSED + CORO_PASSED + IDIOM_PASSED))
TOTAL_FAILED=$((PARSE_FAILED + EVAL_FAILED + ERROR_FAILED + NAMESPACE_FAILED + CORO_FAILED + IDIOM_FAILED))
TOTAL=$((TOTAL_PASSED + TOTAL_FAILED))

if [ "$TOTAL_FAILED" = "0" ]; then
  echo "ok $TOTAL_PASSED/$TOTAL tcl tests passed (parse: $PARSE_PASSED, eval: $EVAL_PASSED, error: $ERROR_PASSED, namespace: $NAMESPACE_PASSED, coro: $CORO_PASSED, idiom: $IDIOM_PASSED)"
  exit 0
else
  echo "FAIL $TOTAL_PASSED/$TOTAL passed, $TOTAL_FAILED failed (parse: $PARSE_PASSED/$((PARSE_PASSED+PARSE_FAILED)), eval: $EVAL_PASSED/$((EVAL_PASSED+EVAL_FAILED)), error: $ERROR_PASSED/$((ERROR_PASSED+ERROR_FAILED)), namespace: $NAMESPACE_PASSED/$((NAMESPACE_PASSED+NAMESPACE_FAILED)), coro: $CORO_PASSED/$((CORO_PASSED+CORO_FAILED)), idiom: $IDIOM_PASSED/$((IDIOM_PASSED+IDIOM_FAILED)))"
  if [ -z "$VERBOSE" ]; then
    echo "--- output ---"
    echo "$OUTPUT" | tail -30
  fi
  exit 1
fi
