#!/usr/bin/env bash # run-selftest.sh — prove join-talk.sh derives the seat from its environment and refuses cleanly. # # Runs against the script beside this directory with a fake HOME and a stub `sno` that # records what it was called with. Nothing outside TMPDIR is touched, so a deployed copy # can test itself without registering a real seat. # # Usage: bash run-selftest.sh # Exit: 0 every case passed · 1 a case failed · 2 the suite could not run. set -Eeuo pipefail export LC_ALL=C HERE="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" JOIN="$HERE/../join-talk.sh" [[ -f "$JOIN" ]] || { printf 'selftest: join-talk.sh is missing: %s\n' "$JOIN" >&2; exit 2; } WORK="$(mktemp -d "${TMPDIR:-/tmp}/join-talk-selftest.XXXXXX")" trap 'rm -rf -- "$WORK"' EXIT FAKE_HOME="$WORK/home" LOG="$WORK/calls.log" mkdir -p "$FAKE_HOME/.local/state/sno-reach" "$FAKE_HOME/bin" # A stub `sno` ahead of the real one: it records the argv and succeeds. The real runtime # validates flags itself; the point here is what join-talk.sh decided to pass. cat >"$FAKE_HOME/bin/sno" <<'EOF' #!/usr/bin/env bash printf 'sno %s\n' "$*" >>"$JOIN_SELFTEST_LOG" if [[ "$*" == 'reach terminal-info' && -n "${SNO_REACH_TERMINAL_SOCKET:-}" ]]; then jq -nc --arg handle "relay:$SNO_REACH_TERMINAL_SOCKET" '{handle:$handle,identity:$handle}' fi EOF chmod +x "$FAKE_HOME/bin/sno" export JOIN_SELFTEST_LOG="$LOG" REPO="$WORK/My_Repo.v2" mkdir -p "$REPO/sub" git -C "$REPO" init -q host="$(hostname | tr '[:upper:]' '[:lower:]')" failures=0 fail() { printf 'FAIL %s\n' "$*" >&2; failures=$((failures + 1)); } pass() { printf 'ok %s\n' "$*"; } run_join() { # run_join