#!/usr/bin/env bash
# flow-on-sx conformance runner — runs all flow test suites in one sx_server process.
#
# Usage:
#   bash lib/flow/conformance.sh        # run all suites
#   bash lib/flow/conformance.sh -v     # verbose (list each suite)

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:-}"

# Suites: NAME RUNNER-FN PATH
SUITES=(
  "basic       flow-basic-tests-run!     lib/flow/tests/basic.sx"
  "control     flow-ctl-tests-run!       lib/flow/tests/control.sx"
  "suspend     flow-sus-tests-run!       lib/flow/tests/suspend.sx"
  "recovery    flow-rec-tests-run!       lib/flow/tests/recovery.sx"
  "distributed flow-dist-tests-run!      lib/flow/tests/distributed.sx"
  "api         flow-api-tests-run!       lib/flow/tests/api.sx"
  "combinators flow-cmb-tests-run!       lib/flow/tests/combinators.sx"
  "railway     flow-rail-tests-run!      lib/flow/tests/railway.sx"
  "integration flow-int-tests-run!       lib/flow/tests/integration.sx"
  "hygiene     flow-hyg-tests-run!       lib/flow/tests/hygiene.sx"
  "host        flow-hst-tests-run!       lib/flow/tests/host.sx"
)

TMPFILE=$(mktemp); trap "rm -f $TMPFILE" EXIT
EPOCH=1

emit_load () { echo "(epoch $EPOCH)"; echo "(load \"$1\")"; EPOCH=$((EPOCH+1)); }
emit_eval () { echo "(epoch $EPOCH)"; echo "(eval \"$1\")"; EPOCH=$((EPOCH+1)); }

{
  emit_load "lib/guest/lex.sx"
  emit_load "lib/guest/reflective/env.sx"
  emit_load "lib/guest/reflective/quoting.sx"
  emit_load "lib/scheme/parser.sx"
  emit_load "lib/scheme/eval.sx"
  emit_load "lib/scheme/runtime.sx"
  emit_load "lib/flow/spec.sx"
  emit_load "lib/flow/store.sx"
  emit_load "lib/flow/remote.sx"
  emit_load "lib/flow/host.sx"
  emit_load "lib/flow/api.sx"
  for SUITE in "${SUITES[@]}"; do
    read -r _NAME _RUNNER FILE <<< "$SUITE"
    emit_load "$FILE"
    emit_eval "($_RUNNER)"
  done
} > "$TMPFILE"

OUTPUT=$(timeout 540 "$SX_SERVER" < "$TMPFILE" 2>&1 || true)

TOTAL_PASS=0
TOTAL_FAIL=0
FAILED_SUITES=()

LAST_DICT_LINES=$(echo "$OUTPUT" | grep -E '^\{:' || true)

I=0
while read -r LINE; do
  [ -z "$LINE" ] && continue
  P=$(echo "$LINE" | grep -oE ':passed [0-9]+' | awk '{print $2}')
  F=$(echo "$LINE" | grep -oE ':failed [0-9]+' | awk '{print $2}')
  [ -z "$P" ] && P=0
  [ -z "$F" ] && F=0
  SUITE_INFO="${SUITES[$I]}"
  SUITE_NAME=$(echo "$SUITE_INFO" | awk '{print $1}')
  TOTAL_PASS=$((TOTAL_PASS + P))
  TOTAL_FAIL=$((TOTAL_FAIL + F))
  if [ "$F" -gt 0 ]; then
    FAILED_SUITES+=("$SUITE_NAME: $P/$((P+F))")
    printf 'X %-12s %d/%d\n' "$SUITE_NAME" "$P" "$((P+F))"
    echo "$LINE" | grep -oE ':name "[^"]*"' | sed 's/:name /    fail: /'
  elif [ "$VERBOSE" = "-v" ]; then
    printf 'ok %-12s %d passed\n' "$SUITE_NAME" "$P"
  fi
  I=$((I+1))
done <<< "$LAST_DICT_LINES"

TOTAL=$((TOTAL_PASS + TOTAL_FAIL))
if [ "$TOTAL" -eq 0 ]; then
  echo "ERROR: no suite results parsed. Raw output:" >&2
  echo "$OUTPUT" >&2
  exit 1
fi
if [ $TOTAL_FAIL -eq 0 ]; then
  echo "ok $TOTAL_PASS/$TOTAL flow-on-sx tests passed (${#SUITES[@]} suites)"
else
  echo "FAIL $TOTAL_PASS/$TOTAL passed, $TOTAL_FAIL failed:"
  for S in "${FAILED_SUITES[@]}"; do echo "  $S"; done
  exit 1
fi
