#!/bin/sh
# The terminal interface, on a pseudo-terminal against a fake server: a
# reply streams and lands in the transcript, tool calls show a header and
# result, the footer counts tokens, Esc cancels a request, commands work
# (/name, /resume with its picker, unknown ones), pic tui -c shows the
# history, a resize is survived, Ctrl-D quits.
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-tui.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"
# 1 text, 2 tool call, 3 text, 4 hang (cancelled), 5 hang (quit), then text
"$SERVER" "$T/port" "$T/log" 120 "sse:$S/text.sse" "sse:$S/thinking_tool.sse" "sse:$S/text.sse" "hang:$S/text.sse:200" \
    "hang:$S/text.sse:200" "sse:$S/text.sse" &
spid=$!
i=0
while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 100 ] || 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" --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)"
}

drive first <<'EOF'
expect pic fake/m
send hello\r
expect Hello, "world"
expect ↑25 ↓12
send write it\r
expect ✓ write t.txt
expect Successfully wrote to t.txt
expect >
send /name main\r
expect named main
send /bogus\r
expect unknown command /bogus
size 60 18
wait 300
send \x04
expect pic: conversation
EOF
[ -f "$T/ws/t.txt" ] || fail "tool: file not written"
printf 'PASS: a reply streams, a tool call shows its result, commands work, Ctrl-D quits\n'

drive cancel <<'EOF'
expect pic fake/m
send wait for me\r
expect Working
wait 500
send \e
expect request cancelled
send \x04
EOF
"$PIC" --db "$T/j" --workspace "$T/ws" check > /dev/null || fail "cancel: integrity"
printf 'PASS: Esc cancels the running request\n'

drive resume tui -c <<'EOF'
expect continuing
expect > wait for me
send /resume\r
expect Resume a conversation
send \e[B
wait 200
send \r
expect continuing
expect > hello
expect > write it
send \x04
EOF
printf 'PASS: pic tui -c shows the history; /resume picks a conversation\n'

# Quitting while a request is stuck: Ctrl-C twice leaves promptly, the
# request is cancelled and the journal is whole.
drive quit <<'EOF2'
expect pic fake/m
send stuck\r
expect Working
wait 300
send \x03
wait 100
send \x03
expect pic: conversation
EOF2
"$PIC" --db "$T/j" --workspace "$T/ws" log | grep -q '(request cancelled)' || fail "quit: request not cancelled"
"$PIC" --db "$T/j" --workspace "$T/ws" check > /dev/null || fail "quit: integrity"
printf 'PASS: quitting during a stuck request cancels it and exits\n'
