#!/bin/sh
# Terminal input ergonomics: the second Ctrl-C quits only within a short
# window, a cleared draft comes back with Up.
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}
S=$(cd "$(dirname "$0")/../pic/streams" && pwd)
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-ergo.XXXXXX")
spid=
spid2=
cleanup() { [ -n "$spid" ] && kill "$spid" 2>/dev/null || :; [ -n "$spid2" ] && kill "$spid2" 2>/dev/null || :; rm -rf -- "$T"; }
trap cleanup EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
mkdir "$T/ws" "$T/log" "$T/home"
export HOME="$T/home" ANTHROPIC_API_KEY=pic-tui-test-key PIC_MODELS_FILE="$T/models.json" PIC_MODEL_CATALOG=offline
export T
export PIC_NO_NATIVE_CLIPBOARD=1
unset PIC_TEST_PASSPHRASE OPENAI_API_KEY GROQ_API_KEY
"$SERVER" "$T/port" "$T/log" 120 "sse:$S/text.sse" "sse:$S/text.sse" "sse:$S/text.sse" "sse:$S/text.sse" "sse:$S/text.sse" "sse:$S/text.sse" &
spid=$!
i=0
while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server"; sleep 0.05; done
mkdir "$T/log2"
"$SERVER" "$T/port2" "$T/log2" 120 "hang:$S/text.sse:200" &
spid2=$!
i=0
while [ ! -s "$T/port2" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server 2"; sleep 0.05; done
printf '{"providers":{"fake":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$ANTHROPIC_API_KEY","models":[{"id":"m","contextWindow":200000}]},"slow":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$ANTHROPIC_API_KEY","models":[{"id":"m","contextWindow":200000}]},"nokey":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$NOKEY_API_KEY_UNSET","models":[{"id":"n","contextWindow":200000}]}}}' \
"$(cat "$T/port")" "$(cat "$T/port2")" "$(cat "$T/port")" > "$T/models.json"
drive() { # NAME: run pic with the script on stdin
name=$1
shift
"$DRIVE" "$T/$name.tx" 80 24 "$PIC" --tui-mode "${MODE:-regular}" --db "$T/j" --workspace "$T/ws" --model fake/m "$@" ||
fail "$name: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/$name.tx" | grep -v '^\s*$' | tail -8)"
}
# A Ctrl-C arms quitting for about a second; a later one only arms again.
drive ctrlc <<'EOT'
expect pic fake/m
send \x03
expect press ctrl+c again to quit
wait 1600
send \x03
wait 300
send still here
expect still here
send \x03
wait 100
send \x03
wait 100
send \x03
EOT
printf 'PASS: a second Ctrl-C quits only within the window\n'
# A "!" command is this interface's own: every one after the first shows its
# output once, and no "request from elsewhere" notice.
drive bang <<'EOT'
expect pic fake/m
send !echo out-one\r
expect out-one
wait 600
send !echo out-two\r
expect out-two
wait 1500
send !echo out-three\r
expect out-three
wait 1500
send \x03
wait 100
send \x03
EOT
perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/bang.tx" > "$T/bang.txt"
! grep -q 'from elsewhere' "$T/bang.txt" || fail "bang: a false notice of a request from elsewhere"
n1=$(grep -cFx '$ echo out-one' "$T/bang.txt")
[ "$n1" -ge 1 ] || fail "bang: the first command's block was not seen"
for w in two three; do
[ "$(grep -cFx "\$ echo out-$w" "$T/bang.txt")" = "$n1" ] || fail "bang: out-$w drawn more often than the first command's output"
done
printf 'PASS: a ! command after the first shows no notice and prints once\n'
# A cleared draft comes back with Up; a new conversation's Up brings this folder's earlier prompts.
"$PIC" --db "$T/j" --workspace "$T/ws" --model fake/m ask 'older folder prompt' > /dev/null 2>&1 || fail "ask"
"$PIC" --db "$T/j" --workspace "$T/ws" --model fake/m ask 'newer folder prompt' > /dev/null 2>&1 || fail "ask"
drive draft <<'EOT'
expect pic fake/m
send \e[A
expect > newer folder prompt
send \e[A
expect > older folder prompt
send \x15
send half typed
expect > half typed
send \x03
wait 100
send \e[A
expect > half typed
send \x15
send second draft
send \e
wait 100
send \e
wait 100
send \e[A
expect > second draft
send \x15
send \x04\x04
EOT
printf 'PASS: Ctrl-C and Esc Esc keep the cleared draft one Up away; a new conversation has the prompts of its folder\n'
# A slip in a command or its argument suggests instead of dead-ending.
drive typo <<'EOT'
expect pic fake/m
send /hlep\r
expect unknown command /hlep: did you mean /help? Enter runs it
expect > /help
send \x15
send /clear\r
expect did you mean /new? Enter runs it
expect > /new
send \r
expect new conversation
send /zzzzzz\r
expect unknown command /zzzzzz (/help lists them)
send /login opnrouter\r
expect Log in
expect openrouter
send \e
wait 200
send \x04\x04
EOT
printf 'PASS: an unknown command suggests the nearest, and /login WORD opens the picker on it\n'
# /resume: search, dates, other folders, and a wrong id is refused with the list.
mkdir "$T/ws2"
"$PIC" --db "$T/j2" --workspace "$T/ws2" --model fake/m ask 'elsewhere prompt' > /dev/null 2>&1 || fail "ask elsewhere"
"$PIC" --db "$T/j" --workspace "$T/ws" --model fake/m sessions --all --json | grep -q '"workspace":"[^"]*ws2"' || fail "sessions --all --json: $("$PIC" --db "$T/j" --workspace "$T/ws" sessions --all --json)"
drive resume <<'EOT'
expect pic fake/m
send /resume\r
expect Resume a conversation
expect newer folder prompt
send /
expect search:
send older
expect search: older_
expect older folder prompt
send \e
expect newer folder prompt
send \t
expect Every folder
expect elsewhere prompt
expect in ws2
send /
send elsewhere
expect search: elsewhere_
send \r
expect pic tui --session
send /resume newer\r
expect no conversation is called newer
expect newer folder prompt
send \e
wait 200
send \e
wait 200
send \x04\x04
EOT
plain() { perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$1"; }
plain "$T/resume.tx" | grep -Eq '[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}' || fail "resume: no date in the list"
drive badid tui --session zzzz <<'EOT'
expect no conversation is called zzzz
expect Resume a conversation
send \e
wait 200
send \e
wait 200
send \x04\x04
EOT
! plain "$T/badid.tx" | grep -q 'continuing zzzz' || fail "badid: a conversation that does not exist was opened"
printf 'PASS: /resume searches, shows dates and other folders, and refuses an unknown id with the list\n'
# /help is a short card by intent; /help keys has every key; long output starts at its first line.
drive card <<'EOT'
expect pic fake/m
send /help\r
expect Stop
expect Undo
expect Newline
expect Quit
expect /help keys
send \x04\x04
EOT
plain "$T/card.tx" > "$T/card.txt"
! grep -q 'Defaults that differ from Pi' "$T/card.txt" || fail "card: /help still prints the whole key table"
! grep -q '^/settings ' "$T/card.txt" || fail "card: /help still prints the command table"
[ "$(wc -l < "$T/card.txt")" -lt 60 ] || fail "card: /help is long ($(wc -l < "$T/card.txt") lines)"
(MODE=fullscreen && drive hotkeysfs) <<'EOT'
expect fake/m
send /hotkeys\r
expect Application:
send \x04\x04
EOT
printf 'PASS: /help is a card, /help keys has the table, and long output is shown from its first line\n'
# Small messages: a lone ! is nothing, Stop says where the prompt went, /new says where the last
# conversation is, the key badge follows the model, the credit prompt names /login.
drive lonebang --model slow/m <<'EOT'
expect pic slow/m
send !\r
wait 800
send \x04\x04
EOT
[ ! -e "$T/log2/1.req" ] || fail "a lone ! was sent to the model"
drive small --model slow/m <<'EOT'
expect pic slow/m
send stop me\r
waitfile $T/log2/1.req
send \e
expect Operation aborted. Your prompt is one Up away.
wait 300
send \e[A
expect > stop me
send \x15
send /new\r
expect new conversation (the last one is in /resume)
send /model nokey/n\r
expect nokey/n
wait 300
send \x04\x04
EOT
! plain "$T/small.tx" | grep -q 'nokey/n ยท key env' || fail "small: the footer kept the old model's key badge"
printf 'PASS: a lone ! is nothing; Stop, /new and the key badge say what they should\n'