#!/usr/bin/env bash
#clear
#export PS4='\e[90m+${LINENO} in ${#BASH_SOURCE[@]}>${FUNCNAME[0]}:${BASH_SOURCE[@]##*/} \e[0m'
#set -x

level=$1
if [ -z "$level" ]; then
  level=1
else 
  # remove the level parameter
  shift
fi
echo "starting: ${BASH_SOURCE[@]##*/} <LOG_LEVEL=$1>"

#echo "sourcing init"
source this
source test.suite

log.level $level

competionArray=(once config list file ite)
source oo

test.tronMonitor() 
{
((TEST_COUNTER++))
console.log "


Test 0: tronMonitor \"$*\"
===================================================================="
tronMonitor.start "$@"
console.log "RETURN: $RETURN_VALUE  Result: $RESULT 
===================================================================="
}

test.case - "tronMonitor test start" \
  tronMonitor $*
expect 0 "*" "Test start"

source tronMonitor
source hiveMind

### test.method

# ============================================================================
# D2.3 — Observer wiring tests (team.register/team.remove → tronMonitor)
# ----------------------------------------------------------------------------
# Verifies the trigger CONTRACT in commit 597f93e: hiveMind.team.register/remove
# dispatches `tronMonitor add|remove <session>`. Real tronMonitor side effects
# (screen window, TRON_MONITOR_ENV) are covered by tronMonitor's own tests; here
# we use a function spy so the test runs even when `screen` isn't installed.
# ============================================================================

D23_CALLS=$(mktemp -t tron.calls.XXXXXX 2>/dev/null || mktemp)
D23_RC=0

# Spy: bash function takes precedence over PATH for `command -v`, so the
# trigger in hiveMind.team.register/remove invokes this instead of the script.
tronMonitor() {
  echo "$@" >> "$D23_CALLS"
  return $D23_RC
}

__d23_cleanup() {
  unset -f tronMonitor 2>/dev/null
  if [ -f "$HIVEMIND_TEAMS" ]; then
    grep -v '^__test_tron_' "$HIVEMIND_TEAMS" > "${HIVEMIND_TEAMS}.tmp" 2>/dev/null
    mv "${HIVEMIND_TEAMS}.tmp" "$HIVEMIND_TEAMS"
  fi
  if [ -f "$HIVEMIND_ACTIVE_TEAM_FILE" ] \
     && grep -q '^__test_tron_' "$HIVEMIND_ACTIVE_TEAM_FILE" 2>/dev/null; then
    rm -f "$HIVEMIND_ACTIVE_TEAM_FILE"
  fi
  rm -f "$D23_CALLS"
}
trap __d23_cleanup EXIT

# --- D2.3.1 — team.register fires `tronMonitor add <session>` ---------------
> "$D23_CALLS"
D23_RC=0

test.case $level "D2.3.1: team.register fires tronMonitor add" \
  hiveMind.team.register __test_tron_a "test team a"

if grep -qx 'add __test_tron_a' "$D23_CALLS" \
   && grep -q '^__test_tron_a|' "$HIVEMIND_TEAMS" 2>/dev/null; then
  expect.pass "trigger fired with 'add __test_tron_a' and registry has entry"
else
  expect.fail "calls=[$(tr '\n' ';' < "$D23_CALLS")] entry=[$(grep '^__test_tron_a|' "$HIVEMIND_TEAMS" 2>/dev/null)]"
fi

# --- D2.3.2 — team.remove fires `tronMonitor remove <session>` --------------
> "$D23_CALLS"

test.case $level "D2.3.2: team.remove fires tronMonitor remove" \
  hiveMind.team.remove __test_tron_a

if grep -qx 'remove __test_tron_a' "$D23_CALLS" \
   && ! grep -q '^__test_tron_a|' "$HIVEMIND_TEAMS" 2>/dev/null; then
  expect.pass "trigger fired with 'remove __test_tron_a' and registry entry gone"
else
  expect.fail "calls=[$(tr '\n' ';' < "$D23_CALLS")] entry=[$(grep '^__test_tron_a|' "$HIVEMIND_TEAMS" 2>/dev/null)]"
fi

# --- D2.3.3 — idempotency: register twice → single add, single entry --------
> "$D23_CALLS"

__d23_register_twice() {
  hiveMind.team.register __test_tron_b "first" 2>/dev/null
  hiveMind.team.register __test_tron_b "second" 2>/dev/null
}

test.case $level "D2.3.3a: register twice → single add fired, single entry" \
  __d23_register_twice

d23_adds=$(grep -cx 'add __test_tron_b' "$D23_CALLS" 2>/dev/null)
d23_entries=$(grep -c '^__test_tron_b|' "$HIVEMIND_TEAMS" 2>/dev/null)
if [ "${d23_adds:-0}" -eq 1 ] && [ "${d23_entries:-0}" -eq 1 ]; then
  expect.pass "single add fired, single registry entry (idempotent register)"
else
  expect.fail "expected adds=1 entries=1, got adds=${d23_adds:-0} entries=${d23_entries:-0}"
fi

# --- D2.3.3 — idempotency: remove twice → first removes, second is no-op ----
> "$D23_CALLS"

__d23_remove_twice() {
  hiveMind.team.remove __test_tron_b 2>/dev/null
  hiveMind.team.remove __test_tron_b 2>/dev/null
  return 0  # second call returns 1; we just want both attempted
}

test.case $level "D2.3.3b: remove twice → idempotent, no errors" \
  __d23_remove_twice

d23_removes=$(grep -cx 'remove __test_tron_b' "$D23_CALLS" 2>/dev/null)
d23_entries=$(grep -c '^__test_tron_b|' "$HIVEMIND_TEAMS" 2>/dev/null)
if [ "${d23_removes:-0}" -eq 1 ] && [ "${d23_entries:-0}" -eq 0 ]; then
  expect.pass "single remove fired, registry clean (idempotent remove)"
else
  expect.fail "expected removes=1 entries=0, got removes=${d23_removes:-0} entries=${d23_entries:-0}"
fi

# --- D2.3.4 — soft-fail: tronMonitor returns 1 → register still succeeds ----
> "$D23_CALLS"
D23_RC=1   # simulate "screen not running" / dead session

test.case $level "D2.3.4: register tolerates tronMonitor failure (soft-fail)" \
  hiveMind.team.register __test_tron_c "soft-fail check"

if grep -qx 'add __test_tron_c' "$D23_CALLS" \
   && grep -q '^__test_tron_c|' "$HIVEMIND_TEAMS" 2>/dev/null \
   && [ "$RETURN_VALUE" -eq 0 ]; then
  expect.pass "trigger fired and registration succeeded despite tronMonitor failure"
else
  expect.fail "soft-fail broken: calls=[$(tr '\n' ';' < "$D23_CALLS")] entry=[$(grep '^__test_tron_c|' "$HIVEMIND_TEAMS" 2>/dev/null)] rc=$RETURN_VALUE"
fi

# Cleanup C entry (still in soft-fail mode; remove triggers spy too — fine)
hiveMind.team.remove __test_tron_c >/dev/null 2>&1
D23_RC=0

test.suite.save.results
