#!/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 " 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 . "$(dirname "$0")/sh/port.sh" D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-gw-XXXXXX") || exit 1 SPLINTER_SESSION_DIR="$D/home/.splinter"; export SPLINTER_SESSION_DIR trap 'rm -rf "$D"' EXIT fails=0 run() { # $1 = answer word; leaves $D/sent.log and maybe $D/out.txt P=$(free_port) 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_listen "$P" "$S"; 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 '^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 "the menu carries /status" grep -q '^cmd: status' "$D/sent.log" check "the menu carries /menu" grep -q '^cmd: menu' "$D/sent.log" # a mutating tool runs without a question in the chat: splinter does not gate # its own tools, and the gateway has no approval step to put to anyone check "the tool ran without an approval" grep -q 'vom gateway' "$D/out.txt" check "no approval question in the chat" \ sh -c "! grep -q '^Zustimmung [0-9]*: write ' '$D/sent.log'" check "no approval buttons in the chat" \ sh -c "! grep -q '\"callback_data\":\"ack:' '$D/sent.log'" check "the turn ends with the answer" grep -q '^fertig' "$D/sent.log" # a program in commands/ answers in the chat, without a model turn run_text() { # $1 = first message, $2 = line to wait for, $3 = port offset, # $4 = a line that must appear too before the fake is killed P=$(free_port) 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" # $FAKE_ANSWER lets one case reply with something other than "ok" -- # the /cancel case below needs the operator's message to be /cancel. python3 -I tests/fake_telegram.py "$P" "$D/out.txt" "$D/sent.log" \ "${FAKE_ANSWER:-ok}" "$1" \ > "$D/fake.out" 2>&1 & S=$! if ! wait_listen "$P" "$S"; 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=$! # The fake is killed once the marker showed up - so a check that reads a # line sent *after* it has to name that line here, or the kill wins the # race (/model: the switch is confirmed, then the next turn's error is # sent; killing at the confirmation dropped it). for i in $(seq 1 120); do if grep -q "$2" "$D/sent.log" 2>/dev/null; then if [ -z "${4:-}" ] || grep -q "$4" "$D/sent.log" 2>/dev/null; then break fi fi 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" 'fett' 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 'fett' "$D/sent.log" check "inline code becomes a tag" grep -q '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" '
' 50
check "a table is drawn in a monospace block" grep -q '
' "$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" '!\[x\]' 90
check "an image inside code stays text" \
    grep -q '!\[x\](https://example.org/x.png)' "$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/sessions/telegram/sess-*.jsonl | tail -1 | grep -q sess-"

# /model offers the configured models and switches on the button
run_text "/model" 'Provider far' 60 '^openai:'
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"

# /status: the facts plus the same inline menu
run_text "/status" 'Status (' 120
check "status names the session" grep -q 'Sitzung' "$D/sent.log"
check "status names the model" grep -q 'Modell' "$D/sent.log"
check "status names the endpoint" grep -q 'Endpunkt' "$D/sent.log"
check "status carries the inline menu" grep -q '"callback_data":"status"' "$D/sent.log"
check "status carries the model button" grep -q '"callback_data":"modelmenu"' "$D/sent.log"

# /menu: the inline keyboard, and pressing Status goes through the callback
run_text "/menu" 'Status (' 130 'Sitzung'
check "the menu carries the status button" grep -q '"callback_data":"status"' "$D/sent.log"
check "the menu carries the model button" grep -q '"callback_data":"modelmenu"' "$D/sent.log"
check "the status callback answers" grep -q 'Sitzung' "$D/sent.log"

# /model probe asks the endpoint what it serves and switches to one of them
run_text "/model probe" 'Provider local' 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 local' "$D/sent.log"
check "the choice is not saved" \
    test ! -f "$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: the model interviews; the summary before INTERVIEW_COMPLETE
# becomes USER.txt (the fake answers with the marker once the interview
# prompt is in the request)
run_text "/interview" 'Interview gestartet' 30 'Interview in USER.txt gespeichert'
check "the interview saved USER.txt" test -s "$D/home/.splinter/USER.txt"
check "the summary is in USER.txt" grep -q 'mag C99' "$D/home/.splinter/USER.txt"
check "the marker is not in USER.txt" \
    sh -c "! grep -q INTERVIEW_COMPLETE '$D/home/.splinter/USER.txt'"

# the `ask` tool reaches the operator in the chat, and the reply is the
# observation: the gateway runs its turns on a copy of the base config, so a
# callback set only on the engine's turn config never arrived here and the
# model was told "nobody can be asked" while the operator sat in the chat.
run_text "frag den operator" 'antwort war: ok' 105
check "the question reaches the chat" \
    grep -q 'Which colour should the login page be?' "$D/sent.log"
check "the question carries its options" \
    grep -q -- '- green' "$D/sent.log"
check "the operator's answer reaches the model" \
    grep -q '^antwort war: ok' "$D/sent.log"
check "the model is not told nobody is there" \
    sh -c "! grep -q 'nobody can be asked' '$D/sent.log'"

# /cancel while the question is open: the hint under the question promises it,
# so it has to work. Before this it was handed to the model as the operator's
# answer -- the turn went on with "/cancel" as its decision.
FAKE_ANSWER=/cancel run_text "frag den operator" 'abgebrochen' 106
check "the question is asked" \
    grep -q 'Which colour should the login page be?' "$D/sent.log"
check "/cancel is acknowledged in the chat" grep -q 'abgebrochen' "$D/sent.log"
check "/cancel does not reach the model as the answer" \
    sh -c "! grep -q 'antwort war: /cancel' '$D/sent.log'"
check "the turn ended instead of going on" \
    sh -c "! grep -q 'antwort war: ok' '$D/sent.log'"

# The model sends a tool call with arguments that are not JSON. The turn
# corrects it and asks again - and the operator must see that in the chat:
# the engine's note callback wrote to a screen queue that a gateway never
# reads, so five retries meant twenty-five seconds of silence.
P=$(free_port)
rm -rf "$D/home" "$D/out.txt" "$D/sent.log"
mkdir -p "$D/home/.splinter"
printf 'endpoint = http://127.0.0.1:%s/v1/chat/completions\nmodel = m\ntelegram_allow = 7\n' \
    "$P" > "$D/home/splinter.conf"
python3 -I tests/fake_telegram.py "$P" "$D/out.txt" "$D/sent.log" ok "kaputt" \
    > "$D/fake.out" 2>&1 &
S=$!
if ! wait_listen "$P" "$S"; then
	echo "FAIL fake telegram did not start on $P"
	fails=$((fails + 1))
else
	HOME="$D/home" SPLINTER_LOCALE=de SPLINTER_TELEGRAM_TOKEN=tok \
	    SPLINTER_TELEGRAM_API="http://127.0.0.1:$P" \
	    SPLINTER_RETRY_WAIT_MS=200 \
	    ./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 "a broken call is announced in the chat" \
	    grep -q '^kaputter Tool-Call, neuer Versuch in 0 s (1/5)' "$D/sent.log"
	check "the turn still ends with an answer" grep -q '^fertig' "$D/sent.log"
fi

if [ "$fails" -eq 0 ]; then echo "test_gateway: ok"; exit 0; fi
cat "$D/gw.out"
exit 1