#!/usr/bin/env bats # agent-text — the runtime-neutral Signal phone messenger ("text me"). Reaches # Craig over Signal from any machine on the tailnet: sends directly wherever the # account is registered locally (any machine holding it, as primary or as a # linked device), and otherwise ssh-relays to the first relay host that holds # it. These tests stub ssh/signal-cli/uname on PATH to verify command # construction without a network or a phone. The signal-cli stub answers # `listAccounts` to control which branch the dispatch takes; the uname stub # fixes the machine identity, so the self-relay skip is exercised deterministically # rather than depending on which machine runs the suite. The final test covers # the deprecated agent-page shim. setup() { REPO_ROOT="$(cd "$(dirname "$BATS_TEST_FILENAME")/../.." && pwd)" PAGE="$REPO_ROOT/claude-templates/bin/agent-text" SHIM="$REPO_ROOT/claude-templates/bin/agent-page" STUBS="$(mktemp -d)" LOG="$STUBS/calls.log" cat > "$STUBS/ssh" <> "$LOG" exit 0 EOF # HAS_ACCOUNT controls the listAccounts answer: "1" → the account is # local (send direct), unset/"0" → not local (relay). cat > "$STUBS/signal-cli" <> "$LOG" exit 0 EOF # SELF_HOST fixes what the script sees as this machine. Defaults to a name # in neither relay list, so the plain relay tests reach the first host. cat > "$STUBS/uname" < "$STUBS/ssh" <> "$LOG" case "\$*" in *ratio*) exit 255 ;; esac exit 0 EOF chmod +x "$STUBS/ssh" HAS_ACCOUNT=0 PATH="$STUBS:$PATH" run bash "$PAGE" retry me [ "$status" -eq 0 ] grep -q "^ssh .*ratio" "$LOG" grep -q "^ssh .*velox" "$LOG" } @test "AGENT_TEXT_RELAYS overrides the default relay list" { AGENT_TEXT_RELAYS="spare.example.net" HAS_ACCOUNT=0 PATH="$STUBS:$PATH" \ run bash "$PAGE" custom [ "$status" -eq 0 ] grep -q "^ssh .*spare.example.net" "$LOG" ! grep -q "^ssh .*ratio" "$LOG" } @test "sends directly (no ssh) when the pager account is registered locally" { HAS_ACCOUNT=1 PATH="$STUBS:$PATH" run bash "$PAGE" hello [ "$status" -eq 0 ] grep -q "^signal-cli -a +15045173983 send " "$LOG" ! grep -q "^ssh " "$LOG" } @test "every relay failing reports the desktop fallback and propagates failure" { cat > "$STUBS/ssh" <<'EOF' #!/bin/bash exit 255 EOF chmod +x "$STUBS/ssh" HAS_ACCOUNT=0 PATH="$STUBS:$PATH" run bash "$PAGE" urgent thing [ "$status" -ne 0 ] [[ "$output" == *"notify"* ]] } @test "no reachable relay at all still fails rather than reporting success" { # Every candidate is this machine, so the loop body never runs. rc must stay # at its non-zero seed: a skipped loop is not a delivered message. AGENT_TEXT_RELAYS="ratio.tailf3bb8c.ts.net" SELF_HOST=ratio HAS_ACCOUNT=0 \ PATH="$STUBS:$PATH" run bash "$PAGE" nowhere to go [ "$status" -ne 0 ] [[ "$output" == *"notify"* ]] ! grep -q "^ssh " "$LOG" } @test "a local send failure blames the local send, not the relay list" { # On the machine that holds the account, no relay is ever consulted. A # message naming the relay list sends whoever is debugging it off chasing # the tailnet while the fault is on the box in front of them. cat > "$STUBS/signal-cli" <