#!/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=
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; }
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" &
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":"$ANTHROPIC_API_KEY","models":[{"id":"m","contextWindow":200000}]}}}' \
"$(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 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 -c '^\$ echo out-one$' "$T/bang.txt")
for w in two three; do
[ "$(grep -c "^\$ 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 folder's prompts\n'