Files
splinter/tests/test_gateway.sh
lotherk a72df5bb7f gateway: a local file that is not an image goes as a document
sendPhoto and sendDocument are one upload path now: send_upload() takes the
method and the form field, so ![alt](/path/report.md) leaves the chat as a
document with the alt text as caption, in the order of the answer. The photo
branch still checks the bytes (PNG/JPEG/GIF/WebP, 10 MiB), so a text file
cannot ride in as a photo; documents are capped at 50 MiB and have to be a
readable regular file. A missing or unreadable path stays a reference in the
chat.

test_gateway: a local PNG is a photo, a text file is a document, and neither
leaves the markdown in the chat.
2026-11-11 07:17:28 +00:00

208 lines
9.1 KiB
Bash
Executable File

#!/bin/sh
#
# splinter -- the Telegram gateway asks for approval in its own chat.
#
# Before: a tool call that needed consent either was denied (no approval
# stream in a daemon) or waited for an "ok <id>" that only the blocked gateway
# could have read. And a refused sendMessage crashed it (NULL err). Run against
# a fake Bot API and a fake model, once with "ok" and once with "no".
set -u
cd "$(dirname "$0")/.." || exit 1
D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-gw-XXXXXX") || exit 1
trap 'rm -rf "$D"' EXIT
fails=0
wait_fake() { # $1 = port; 0 when something accepts a connection
for i in $(seq 1 100); do
curl -s -o /dev/null "http://127.0.0.1:$1/x" && return 0
sleep 0.2
done
return 1
}
run() { # $1 = answer word; leaves $D/sent.log and maybe $D/out.txt
P=$((20000 + ($$ + ${2:-0}) % 30000))
rm -rf "$D/home" "$D/out.txt" "$D/sent.log"
mkdir -p "$D/home/.splinter/commands"
printf '#!/bin/sh\necho hallo\n' > "$D/home/.splinter/commands/hello"
chmod +x "$D/home/.splinter/commands/hello"
printf 'endpoint = http://127.0.0.1:%s/v1/chat/completions\nmodel = m\ntelegram_allow = 7\nplugin = plugins/focus/focus.so\n' \
"$P" > "$D/home/splinter.conf"
python3 -I tests/fake_telegram.py "$P" "$D/out.txt" "$D/sent.log" "$1" \
> "$D/fake.out" 2>&1 &
S=$!
if ! wait_fake "$P" || ! kill -0 "$S" 2>/dev/null; then
echo "FAIL fake telegram did not start on $P"
cat "$D/fake.out" 2>/dev/null
fails=$((fails + 1))
return 1
fi
HOME="$D/home" SPLINTER_LOCALE=de SPLINTER_TELEGRAM_TOKEN=tok \
SPLINTER_TELEGRAM_API="http://127.0.0.1:$P" \
SPLINTER_ACK_DIR="$D/home/ack" SPLINTER_ACK_TTL=20 \
./splinter --config "$D/home/splinter.conf" --daemon \
> "$D/gw.out" 2>&1 &
G=$!
for i in $(seq 1 120); do
grep -q '^fertig' "$D/sent.log" 2>/dev/null && break
sleep 0.5
done
kill "$G" "$S" 2>/dev/null
wait "$G" "$S" 2>/dev/null
}
check() { # $1 = description, $2.. = command
d=$1; shift
if "$@"; then :; else echo "FAIL $d"; fails=$((fails + 1)); fi
}
run ok 0
check "the command menu is registered" grep -q '^cmd: start' "$D/sent.log"
check "the menu carries plugin commands" grep -q '^cmd: goal' "$D/sent.log"
check "the menu carries command scripts" grep -q '^cmd: hello' "$D/sent.log"
check "approval is asked in the chat, with the tool" \
grep -q '^Zustimmung [0-9]*: write ' "$D/sent.log"
check "approval carries inline buttons" \
grep -q '"callback_data":"ack:' "$D/sent.log"
check "a question meanwhile is told to wait" \
grep -q '^Ich warte noch' "$D/sent.log"
check "the yes is acknowledged" grep -q '^Zustimmung erteilt' "$D/sent.log"
check "the tool ran after the yes" grep -q 'vom gateway' "$D/out.txt"
check "the turn ends with the answer" grep -q '^fertig' "$D/sent.log"
run no 1
check "the no is acknowledged" grep -q '^Abgelehnt' "$D/sent.log"
check "the tool did not run after the no" test ! -e "$D/out.txt"
# a program in commands/ answers in the chat, without a model turn
run_text() { # $1 = first message, $2 = line to wait for
P=$((20000 + ($$ + ${3:-0}) % 30000))
rm -rf "$D/home" "$D/out.txt" "$D/sent.log"
mkdir -p "$D/home/.splinter/commands"
printf '#!/bin/sh\necho "script sagt hallo"\n' \
> "$D/home/.splinter/commands/hello"
chmod +x "$D/home/.splinter/commands/hello"
printf 'endpoint = http://127.0.0.1:%s/v1/chat/completions\nmodel = m\ntelegram_allow = 7\ndelegate = far | test | endpoint=http://127.0.0.1:9/v1/chat/completions | model=other\n' \
"$P" > "$D/home/splinter.conf"
python3 -I tests/fake_telegram.py "$P" "$D/out.txt" "$D/sent.log" ok "$1" \
> "$D/fake.out" 2>&1 &
S=$!
if ! wait_fake "$P" || ! kill -0 "$S" 2>/dev/null; then
echo "FAIL fake telegram did not start on $P"
cat "$D/fake.out" 2>/dev/null
fails=$((fails + 1))
return 1
fi
HOME="$D/home" SPLINTER_LOCALE=de SPLINTER_TELEGRAM_TOKEN=tok \
SPLINTER_TELEGRAM_API="http://127.0.0.1:$P" \
./splinter --config "$D/home/splinter.conf" --daemon \
> "$D/gw.out" 2>&1 &
G=$!
for i in $(seq 1 120); do
grep -q "$2" "$D/sent.log" 2>/dev/null && break
sleep 0.5
done
kill "$G" "$S" 2>/dev/null
wait "$G" "$S" 2>/dev/null
}
run_text "/hello" 'script sagt hallo' 10
check "a command script answers in the chat" \
grep -q 'script sagt hallo' "$D/sent.log"
check "a command script does not start a model turn" test ! -e "$D/out.txt"
run_text "/nosuch" '^unbekanntes Kommando: /nosuch' 20
check "an unknown command is an error" \
grep -q '^unbekanntes Kommando: /nosuch' "$D/sent.log"
check "an unknown command does not start a model turn" test ! -e "$D/out.txt"
# the model's markdown is rendered: Telegram draws HTML, not asterisks
run_text "zeig markdown" '<b>fett</b>' 40
check "an answer is sent with parse_mode=HTML" grep -q '^mode: HTML' "$D/sent.log"
check "bold markdown becomes a tag" grep -q '<b>fett</b>' "$D/sent.log"
check "inline code becomes a tag" grep -q '<code>code</code>' "$D/sent.log"
check "the raw markers do not reach the chat" \
sh -c "! grep -q '\*\*fett\*\*' '$D/sent.log'"
# a table is for the eye: columns in a monospace block, not a row of pipes
run_text "zeig eine tabelle" '<pre>' 50
check "a table is drawn in a monospace block" grep -q '<pre>' "$D/sent.log"
check "a table keeps its columns" grep -q '| a | b |' "$D/sent.log"
# an image in the answer is a photo, not a link, and the text keeps its order
run_text "zeig ein bild" 'Ende' 80
check "the image is sent as a photo" \
grep -q '^photo: https://example.org/b.png caption=Ein Bild' "$D/sent.log"
check "the text before the image arrives" grep -q '^Hier:' "$D/sent.log"
check "the text after the image arrives" grep -q 'Ende' "$D/sent.log"
check "no markdown image reaches the chat" \
sh -c "! grep -q '!\\[Ein Bild\\]' '$D/sent.log'"
run_text "zeig ein codebild" '<code>!\[x\]' 90
check "an image inside code stays text" \
grep -q '<code>!\[x\](https://example.org/x.png)</code>' "$D/sent.log"
check "an image inside code is not a photo" \
sh -c "! grep -q '^photo: https://example.org/x.png' '$D/sent.log'"
# a local image is uploaded as a photo; a non-image stays a reference
run_text "zeig ein lokales bild" 'photo-file: pic.png' 100
check "a local image is uploaded" \
grep -q '^photo-file: pic.png caption=Lokal' "$D/sent.log"
check "the local image does not show up as markdown" \
sh -c "! grep -q '!\\[Lokal\\]' '$D/sent.log'"
run_text "zeig kein bild" 'document-file: notimg.txt' 110
check "a non-image file goes as a document" \
grep -q '^document-file: notimg.txt caption=Kein Bild' "$D/sent.log"
check "it is not sent as a photo" \
sh -c "! grep -q '^photo-file: notimg.txt' '$D/sent.log'"
check "the reference does not stay in the chat" \
sh -c "! grep -q '!\\[Kein Bild\\]' '$D/sent.log'"
# /new starts a fresh conversation and a fresh session file
run_text "/new" 'Neue Konversation: sess-' 45
check "the bot says the new session" grep -q '^Neue Konversation: sess-' "$D/sent.log"
check "a session file was opened" sh -c "ls '$D'/home/.splinter/sess-*.jsonl | tail -1 | grep -q sess-"
# /model offers the configured models and switches on the button
run_text "/model" 'Provider far' 60
check "the model menu lists the configured providers" \
grep -q '"callback_data":"model:far"' "$D/sent.log"
check "the model menu shows the models" grep -q 'far other' "$D/sent.log"
check "the button switches the model" \
grep -q '^Modell: other (Provider far' "$D/sent.log"
# the next turn really uses the new endpoint (the test one is dead: port 9)
check "the next turn goes to the new endpoint" \
grep -q '^openai:' "$D/sent.log"
# /model probe asks the endpoint what it serves and switches to one of them
run_text "/model probe" 'gespeichert' 55
check "the probe lists served models" grep -q '"callback_data":"mm:0"' "$D/sent.log"
check "a probed model is used" grep -q '^Modell: m (Provider main)' "$D/sent.log"
check "the choice is saved for the next start" \
test -f "$D/home/splinter.conf.d/90-model.conf"
check "the saved choice names the model" \
grep -q '^model = m$' "$D/home/splinter.conf.d/90-model.conf"
# /connect writes a provider to the config drop-in
run_text "/connect" 'Provider testprov gespeichert' 70
drop="$D/home/splinter.conf.d/20-connect.conf"
check "connect writes the drop-in" test -f "$drop"
check "the line names the provider" grep -q '^delegate = testprov |' "$drop"
check "the line names the endpoint" \
grep -q "endpoint=http://127.0.0.1:$P/v1/chat/completions" "$drop"
check "the line names the model" grep -q 'model=llama3' "$drop"
check "connect offers to use it now" \
grep -q '"callback_data":"model:testprov"' "$D/sent.log"
check "connect tested the provider" grep -q 'Test bestanden: ok' "$D/sent.log"
# /interview asks the six questions and writes the profile
run_text "/interview" '^Profil geschrieben' 30
prof=$(ls "$D/home/.splinter/profile/"*.md 2>/dev/null | head -1)
check "the interview wrote a profile" test -n "$prof"
check "the first answer is in the profile" grep -q 'antwort-1' "$prof"
check "the last answer is in the profile" grep -q 'antwort-6' "$prof"
check "the profile has the language section" grep -q '## language' "$prof"
if [ "$fails" -eq 0 ]; then echo "test_gateway: ok"; exit 0; fi
cat "$D/gw.out"
exit 1