#!/usr/bin/env bash
# decide() branch coverage for platform-fallback.sh
set -uo pipefail

PLATFORM_FALLBACK_LIB=1
export PLATFORM_FALLBACK_LIB
# shellcheck source=/dev/null
source "$HOME/.claude/bin/platform-fallback.sh"

fail=0
n=0
probe() { # want, reachable, remote_active, local_active, local_busy, streak
  local want="$1"; shift
  local got; got=$(decide "$@")
  n=$((n + 1))
  if [ "$got" != "$want" ]; then
    printf 'FAIL want=%-12s got=%-12s args=[%s]\n' "$want" "$got" "$*"
    fail=$((fail + 1))
  fi
}

#      want           reach remote_act local_act busy streak
probe  hold           1     1          0         0    0   # steady state: debian1 serving
probe  start-remote   1     0          0         0    0   # debian1 up, its runner died -> revive there
probe  recover        1     1          1         0    0   # split brain, laptop idle -> debian1 wins
probe  recover        1     0          1         0    0   # laptop holds session, debian1 back -> hand back
probe  hold           1     0          1         1    0   # laptop mid-job -> never evict, recover next poll
probe  hold           1     1          1         1    0   # same, even with debian1 already up
probe  hold           0     0          0         0    1   # one missed probe is a blip, not an outage
probe  failover       0     0          0         0    2   # sustained: laptop takes the session
probe  failover       0     0          0         0    9
probe  hold           0     0          1         0    3   # already failed over -> nothing to do
probe  hold           0     0          1         1    3   # failed over and working

printf '%d probes, %d failures\n' "$n" "$fail"
exit $((fail > 0))
