#!/bin/sh
# A click on the footer's model, thinking level or key, with the mouse on in fullscreen, on a
# pseudo-terminal against a fake server: the model opens the picker, the level the thinking selector
# and the key /auth's list, each as its slash command does, also while a reply streams; a click on
# the footer's left end does nothing; with the mouse off nothing is reported, so nothing happens.
# --no-faults (the release build): skipped, since the keys are stored with PIC_TEST_PASSPHRASE,
# which only the dev build reads.
set -eu
PIC=${PIC:?set PIC to the pic binary}
SERVER=${SERVER:?set SERVER to the fake_server binary}
DRIVE=${DRIVE:?set DRIVE to the pty_drive binary}
if [ "${1:-}" = --no-faults ]; then
printf 'SKIP: footerclick needs the dev build (PIC_TEST_PASSPHRASE)\n'
exit 0
fi
S=$(cd "$(dirname "$0")/../pic/streams" && pwd)
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-tui-footerclick.XXXXXX")
T=$(cd "$T" && pwd -P)
spid=
cleanup() { [ -n "$spid" ] && kill "$spid" 2>/dev/null || :; rm -rf -- "$T"; }
trap cleanup EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
unset SSH_CONNECTION SSH_CLIENT SSH_TTY TMUX TERM_PROGRAM
TERM=xterm-256color # a real terminal always sets it; CI runners do not
export TERM
for v in $(env | sed -nE 's/^([A-Za-z0-9_]*_(API_KEY|TOKEN))=.*/\1/p'); do unset "$v"; done
mkdir "$T/ws" "$T/log" "$T/home"
PASS="pic-tui-fclick-pass-$$"
export HOME="$T/home" PIC_MODELS_FILE="$T/models.json" PIC_MODEL_CATALOG=offline PIC_PI_EXPORT=0 T PIC_TEST_PASSPHRASE="$PASS"
export PIC_NO_NATIVE_CLIPBOARD=1 PIC_NO_KEY_CHECKS=1
"$SERVER" "$T/port" "$T/log" 120 "hang:$S/text.sse:600" &
spid=$!
i=0
while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server"; sleep 0.05; done
printf '{"providers":{"fake":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$FAKE_API_KEY","models":[{"id":"r","contextWindow":200000,"reasoning":true}]}}}' \
"$(cat "$T/port")" > "$T/models.json"
printf 'k-one-%s\n' $$ | "$PIC" --db "$T/j" --workspace "$T/ws" login fake > /dev/null 2> "$T/login.err" || fail "login: $(cat "$T/login.err")"
printf 'k-two-%s\n' $$ | "$PIC" --db "$T/j" --workspace "$T/ws" auth login fake --api-key --as second > /dev/null 2> "$T/login.err" ||
fail "login --as: $(cat "$T/login.err")"
screen() { perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\e\][^\a\e]*(\a|\e\\)//g; s/\r//g' "$1" | grep -v '^\s*$' | tail -12; }
drive() { # NAME: fullscreen pic, 100 x 30, the script on stdin
name=$1
"$DRIVE" "$T/$name.tx" 100 30 "$PIC" --tui-mode fullscreen --db "$T/j" --workspace "$T/ws" --model fake/r || fail "$name: $(screen "$T/$name.tx")"
}
"$PIC" --db "$T/j" --workspace "$T/ws" settings mouse on > /dev/null
# The footer's right end is "fake/r • LEVEL · key NAME", flush with column 100 on row 30. Where each
# part is comes from the footer as drawn: read it from a first run.
drive look <<'EOF'
expect key default
send \x04\x04
EOF
line=$(screen "$T/look.tx" | grep -a 'fake/r' | tail -1)
tail=$(printf '%s' "$line" | sed -E 's/.*(fake\/r.*)$/\1/')
level=$(printf '%s' "$tail" | sed -E 's/^fake\/r • ([a-z]+) .*$/\1/')
[ "$(printf '%s' "$tail" | sed -E 's/ *$//')" = "fake/r • $level · key default" ] || fail "footer: [$tail]"
width=$(printf '%s' "$tail" | perl -CS -Mutf8 -ne 'chomp; s/\s+$//; print length')
first=$((100 - width)) # 0-based column of "fake/r"
mx=$((first + 3)) # 1-based column inside the model name
lx=$((first + 6 + 3 + 2)) # inside the level
kx=$((100 - 3)) # inside "key default"
row=30
# Each part opens its panel, at rest and while a reply streams.
drive clicks <<EOF
expect key default
send \e[<0;$mx;${row}M
send \e[<0;$mx;${row}m
expect Select a model
send \e
send \e[<0;$lx;${row}M
send \e[<0;$lx;${row}m
expect Thinking level
send \e
send \e[<0;$kx;${row}M
send \e[<0;$kx;${row}m
expect enter: fix
send \e
send \e[<0;5;${row}M
send \e[<0;5;${row}m
wait 300
send go\r
expect Passphrase
send $PASS\r
expect Stop asking for this passphrase
send n
expect Hello
send \e[<0;$mx;${row}M
send \e[<0;$mx;${row}m
expect Select a model
send \e
send \e[<0;$lx;${row}M
send \e[<0;$lx;${row}m
expect Thinking level
send \e
send \e[<0;$kx;${row}M
send \e[<0;$kx;${row}m
expect enter: fix
send \e
send \x04\x04
EOF
! grep -aq 'Follow-up' "$T/clicks.tx" || fail "clicks: something was queued"
printf 'PASS: a click on the footer'"'"'s model, thinking level and key opens the picker, the selector and /auth, also while a reply streams\n'
# With the mouse off pic never asks the terminal for clicks, so nothing on the footer can open.
"$PIC" --db "$T/j" --workspace "$T/ws" settings mouse off > /dev/null
drive off <<'EOF'
expect key default
send \x04\x04
EOF
on=$(printf '\033[?1000h')
! grep -aqF "$on" "$T/off.tx" || fail "off: the mouse was taken"
grep -aqF "$on" "$T/look.tx" || fail "look: the mouse was not taken, so the clicks above proved nothing"
printf 'PASS: with the mouse off pic does not ask for clicks\n'