#!/usr/bin/env bash
# Assert node is on PATH in buildbox execution contexts that do not load mise shims:
#   - minimal PATH (cursor seat / non-login ssh)
#   - remote-runner PATH before mise eval
#   - harness seat container image
#
#   modules/buildbox/test/node-runtime.smoke.sh [debian1]
set -uo pipefail

HOST="${1:-debian1}"
SEAT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../harness/seat" && pwd)"
IMAGE="$(node -e 'process.stdout.write(require(process.argv[1]).image)' "$SEAT_DIR/seat-remote.json")"
NODE_MAJOR=24
SSH=(ssh -F /dev/null -i "$HOME/.ssh/id_ed25519_buildbox" -p 2222 "user@$HOST")
PASS=0
FAIL=0
SKIP=0

want() {
  local name="$1" got="$2" expect="$3"
  if [ "$got" = "$expect" ]; then
    PASS=$((PASS + 1))
    echo "PASS $name"
  else
    FAIL=$((FAIL + 1))
    echo "FAIL $name (want=$expect got=$got)"
  fi
}

want_node_major() {
  local name="$1" got="$2"
  case "$got" in
    v${NODE_MAJOR}.*)
      PASS=$((PASS + 1))
      echo "PASS $name ($got)"
      ;;
    *)
      FAIL=$((FAIL + 1))
      echo "FAIL $name (want v${NODE_MAJOR}.x got ${got:-none})"
      ;;
  esac
}

skip() {
  SKIP=$((SKIP + 1))
  echo "SKIP $1"
}

if ! "${SSH[@]}" true >/dev/null 2>&1; then
  skip "$HOST unreachable"
  echo
  echo "PASS=$PASS FAIL=$FAIL SKIP=$SKIP"
  exit 0
fi

REPORT="$("${SSH[@]}" "IMAGE=$(printf %q "$IMAGE") NODE_MAJOR=$NODE_MAJOR bash -s" <<'REMOTE'
set -uo pipefail
export PATH="/usr/local/bin:/usr/bin:/bin"

minimal="$(node --version 2>/dev/null || echo missing)"
minimal_test=missing
tmp_test="$(mktemp /tmp/node-runtime.smoke.XXXXXX.test.js)"
cat >"$tmp_test" <<'JS'
const test = require('node:test');
const assert = require('node:assert/strict');
test('ok', () => {
  assert.ok(true);
});
JS
if node --test "$tmp_test" >/dev/null 2>&1; then
  minimal_test=ok
fi
rm -f "$tmp_test"

for env_file in "$HOME"/.config/environment.d/*.conf; do
  [ -r "$env_file" ] || continue
  set -a
  # shellcheck disable=SC1090
  . "$env_file"
  set +a
done
export PATH="$HOME/.cargo/bin:$HOME/.local/bin:$PATH"
runner="$(node --version 2>/dev/null || echo missing)"

seat=skip
if podman image exists "$IMAGE" 2>/dev/null; then
  seat="$(podman run --rm --entrypoint node "$IMAGE" --version 2>/dev/null || echo missing)"
fi

printf 'minimal=%s\n' "$minimal"
printf 'minimal_test=%s\n' "$minimal_test"
printf 'runner=%s\n' "$runner"
printf 'seat=%s\n' "$seat"
REMOTE
)"

minimal="$(printf '%s\n' "$REPORT" | sed -n 's/^minimal=//p')"
minimal_test="$(printf '%s\n' "$REPORT" | sed -n 's/^minimal_test=//p')"
runner="$(printf '%s\n' "$REPORT" | sed -n 's/^runner=//p')"
seat="$(printf '%s\n' "$REPORT" | sed -n 's/^seat=//p')"

want_node_major "minimal PATH exposes node ${NODE_MAJOR}" "$minimal"
want "minimal PATH runs node --test" "$minimal_test" ok
want_node_major "remote-runner PATH exposes node ${NODE_MAJOR}" "$runner"
case "$seat" in
  skip) skip "seat image $IMAGE not present on $HOST";;
  *) want_node_major "seat image exposes node ${NODE_MAJOR}" "$seat";;
esac

echo
echo "PASS=$PASS FAIL=$FAIL SKIP=$SKIP"
[[ "$FAIL" -eq 0 ]]
