signal-bot/scripts/selftest.sh
Krzysztof kuhy Rudnicki 558dd297a4 release: 0.0.7 «Telesfor» — !papież, and the bot can test itself
Adds !papież / !papiez / !pope: a random pope as a photo, captioned with his
Latin name, reign and pl.wikipedia link. popes.json already carried a Commons
portrait filename for all 267 and nothing had ever consumed it.

The Latin name comes from the Latin column of Poczet papieży, not from the
la.wikipedia sitelink. The sitelink resolved for all 267 and was still wrong:
la.wikipedia numbers several popes differently, so #154 came out as
"Stefan IX — Stephanus X" in a single caption, which reads as an off-by-one.
The table's own column says "Stephanus Nonus" and cannot disagree with the
name beside it. LATIN_OVERRIDES ships empty; it is a tripwire, not a backlog.

Portraits are cached on disk under CacheDirectory=signal-bot, named from the
ordinal so no upstream string ever reaches a path, and bounded at 267 entries
so nothing needs evicting. Verified that CacheDirectory wins against
ProtectHome=read-only before relying on it.

Also adds POST /_selftest and scripts/selftest.sh. Signal never delivers an
account its own sends, so no command has ever been exercised end-to-end
without asking a human to type it. This injects a real frame into the live
dispatcher; it is loopback-only and refuses any group not in SELFTEST_GROUPS,
which is empty by default.

Verified against the real endpoints, not just the fake: 267/267 portraits
resolve on Commons, regeneration is byte-identical on every previously
shipped field, and !papież was driven 7 times through the live dispatcher in
testbot, returning distinct popes.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015d48t4qsQLBJnDwDeo2Nom
2026-08-30 17:06:25 +02:00

65 lines
2.0 KiB
Bash
Executable File

#!/bin/bash
# ============================================================================
# Type a command at the running bot on its own behalf.
#
# Signal never delivers an account its own sends, so this is the only way to
# exercise a command end-to-end without a second Signal account. It posts for
# real, into a real group -- SELFTEST_GROUPS decides which.
#
# scripts/selftest.sh testbot '!papież' 5
# ============================================================================
set -euo pipefail
SCRIPT_NAME="$(basename "$0")"
readonly SCRIPT_NAME
readonly BOT="http://127.0.0.1:${BIND_PORT:-8137}"
GROUP="${1:-}"
TEXT="${2:-}"
TIMES="${3:-1}"
usage() {
echo "Usage: $SCRIPT_NAME <group-name> <text> [times]"
echo " The group must be listed in SELFTEST_GROUPS, or the bot refuses."
exit 0
}
validate_requirements() {
if [[ -z "$GROUP" || -z "$TEXT" ]]; then
echo "Error: group and text are required" >&2
usage
fi
if ! command -v jq >/dev/null; then
echo "Error: jq is required" >&2
exit 1
fi
if ! curl -sf "${BOT}/healthz" >/dev/null; then
echo "Error: no healthy bot at ${BOT}" >&2
exit 1
fi
}
main() {
validate_requirements
local reply status
for ((i = 1; i <= TIMES; i++)); do
reply="$(curl -s -X POST "${BOT}/_selftest" \
-H 'Content-Type: application/json' \
--data "$(jq -nc --arg g "$GROUP" --arg t "$TEXT" '{group:$g,text:$t}')")"
status="$(jq -r '.status' <<<"$reply")"
if [[ "$status" != "delivered" ]]; then
echo " ${i}/${TIMES} ${status}: $(jq -r '.reason // "?"' <<<"$reply")" >&2
exit 1
fi
echo " ${i}/${TIMES} delivered ${TEXT} to ${GROUP}"
# The cooldown is per user per command, and the self-test is always
# the same user, so a tighter loop just proves the cooldown works.
[[ $i -lt $TIMES ]] && sleep "${SELFTEST_INTERVAL:-11}"
done
echo "== done; look at ${GROUP} in Signal"
}
main "$@"