refactor: fold is_party_alive into party_conn_state

One liveness primitive instead of two. The PID argument was a vestige
(format-checked, never probed, since the kill -0 removal); dropping it
means a roster with a garbage SERVER_PID can no longer mask a live
socket in host/moderation/role resolution.
This commit is contained in:
veg 2026-07-11 12:07:13 +00:00
parent be3fb049d2
commit 6b94255245
5 changed files with 51 additions and 88 deletions

View file

@ -61,7 +61,7 @@ setup() {
}
# Helper: write a $PARTY_TMUX stub that exits with the given code on
# every invocation. Used to test is_party_alive's tmux probe in isolation.
# every invocation. Used to test party_conn_state's probe in isolation.
_stub_tmux() {
cat > "$PARTY_TMP/tmux-stub" <<EOF
#!/bin/sh
@ -71,42 +71,28 @@ EOF
PARTY_TMUX="$PARTY_TMP/tmux-stub"
}
@test "is_party_alive: tmux probe ok → true (regardless of PID liveness)" {
# Liveness is decided by the tmux probe alone; the PID arg is kept for
# caller-API compatibility and numeric-validation, but is not signal-
# probed (POSIX kill(2) returns EPERM cross-user on illumos/BSD/macOS,
# which broke cross-user discovery in earlier revisions). With a stub
# tmux returning 0, both a live and a never-existed PID must pass.
@test "party_conn_state: silent rc-0 probe answers ok" {
_stub_tmux 0
( sleep 30 ) &
pid=$!
is_party_alive "$pid" /dev/null
kill "$pid" 2>/dev/null
wait "$pid" 2>/dev/null || true
# Same PID, now reaped — tmux stub still says yes, so still alive.
is_party_alive "$pid" /dev/null
# And a PID we never owned (init/launchd, root) — likewise alive.
is_party_alive 1 /dev/null
[ "$(party_conn_state /dev/null)" = ok ]
}
@test "is_party_alive: empty/non-numeric PID → false" {
_stub_tmux 0
! is_party_alive "" /dev/null
! is_party_alive "abc" /dev/null
}
@test "is_party_alive: tmux probe fails → false" {
# PID-reuse / planted nc -lU socket scenario: the socket isn't a real
# tmux server, so the tmux handshake fails. is_party_alive rejects
# regardless of PID state (regression seen cross-user on illumos
# native).
@test "party_conn_state: failing probe answers dead" {
# PID-reuse / planted nc -lU socket scenario: nothing at the socket
# speaks the tmux protocol, so the probe fails and the party is dead,
# regardless of any PID recorded in the roster.
_stub_tmux 1
( sleep 30 ) &
pid=$!
! is_party_alive "$pid" /dev/null
kill "$pid" 2>/dev/null
wait "$pid" 2>/dev/null || true
! is_party_alive 1 /dev/null
[ "$(party_conn_state /dev/null)" = dead ]
}
@test "party_conn_state: rc 0 with stderr output answers unauthorized" {
cat > "$PARTY_TMP/tmux-stub" <<'EOF'
#!/bin/sh
echo "access not allowed" >&2
exit 0
EOF
chmod +x "$PARTY_TMP/tmux-stub"
PARTY_TMUX="$PARTY_TMP/tmux-stub"
[ "$(party_conn_state /dev/null)" = unauthorized ]
}
@test "roster_list returns full record paths under PARTY_SOCKET_DIR" {