Skip to content
pi-c Artifact [86f0ac5f29]
Login

Artifact [86f0ac5f29]

Artifact 86f0ac5f29c88df9ab76ded1dfdec93748f8fe0dc7956f505e8941e28ba01a13:


#!/bin/sh
# "Update installed" and /restart in the terminal interface (docs/manual/tui.md): a newer pic
# installed over the running one puts a green line in the footer; /restart starts the new file on
# the same conversation. A second case asks for /restart while a reply streams: it says so, waits
# for the reply, and the draft typed meanwhile is in the editor of the pic that starts.
# Usage: test/tui/update.sh [--no-faults]   (the cases need the dev build's PIC_TEST_UPDATE_MS)
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: the update cases need the dev build'"'"'s test seam\n'
    exit 0
fi
S=$(cd "$(dirname "$0")/../pic/streams" && pwd)
V=$(cd "$(dirname "$0")/streams" && pwd)
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-tui-update.XXXXXX")
T=$(cd "$T" && pwd -P)
spid=
waiters=
cleanup() {
    [ -n "$spid" ] && kill "$spid" 2> /dev/null || :
    for w in $waiters; do kill "$w" 2> /dev/null || :; done
    [ -n "${KEEP:-}" ] || rm -rf -- "$T"
}
trap cleanup EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
for v in $(env | sed -nE 's/^([A-Za-z0-9_]*_(API_KEY|TOKEN))=.*/\1/p'); do unset "$v"; done
unset PIC_TEST_PASSPHRASE PIC_KEY_FD
mkdir "$T/ws" "$T/log" "$T/home" "$T/bin1" "$T/bin2"
export HOME="$T/home" PIC_MODELS_FILE="$T/models.json" PIC_MODEL_CATALOG=offline PIC_PI_EXPORT=0 T
export PIC_NO_NATIVE_CLIPBOARD=1 WS="$T/ws" PIC_TEST_UPDATE_MS=100
"$SERVER" "$T/port" "$T/log" 120 "at:/v1/models=status:404:-" "sse:$S/text.sse" "sse:$V/done.sse" "sse:$S/bash_hold.sse" "sse:$V/done.sse" &
spid=$!
i=0
while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server"; sleep 0.05; done
cat > "$T/models.json" <<EOM
{"providers":{"fake":{"baseUrl":"http://127.0.0.1:$(cat "$T/port")","api":"anthropic-messages","apiKey":"\$FAKE_API_KEY","models":[
  {"id":"m","contextWindow":200000}]}}}
EOM
export FAKE_API_KEY=pic-update-fake
"$PIC" settings defaultModel fake/m > /dev/null
"$PIC" settings cacheRetention none > /dev/null
plain() { perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\e\][^\a\e]*(\a|\e\\)//g; s/\r//g' "$1"; }
screen() { plain "$1" | grep -v '^\s*$' | tail -12; }
drive() { # NAME BIN: that copy of pic on a pseudo-terminal, the script on stdin
    name=$1 bin=$2
    "$DRIVE" "$T/$name.tx" 100 30 "$bin" --tui-mode regular --db "$T/j" --workspace "$T/ws" || fail "$name: $(screen "$T/$name.tx")"
}
q() { sqlite3 -cmd ".timeout 10000" "$T/j" "$1"; }
when() { # FILE PATTERN COMMAND...: run COMMAND once PATTERN is in FILE (gives up after 30 s)
    f=$1 pat=$2
    shift 2
    (
        n=0
        until grep -aqF -- "$pat" "$f" 2> /dev/null; do
            n=$((n + 1))
            [ "$n" -lt 600 ] || exit 0
            sleep 0.05
        done
        "$@"
    ) &
    waiters="$waiters $!"
}
install_new() { # a newer pic: a program that notes how it was started, then is pic
    printf '#!/bin/sh\necho "$@" >> "%s/started.log"\nexec "%s" "$@"\n' "$T" "$PIC" > "$T/bin1/new"
    chmod +x "$T/bin1/new"
    mv -f "$T/bin1/new" "$T/bin1/pic" # renamed over the running file, as make install-pic does
}

# ---- an installed update shows in the footer, and /restart goes on in the same conversation ------
cp "$PIC" "$T/bin1/pic"
when "$T/idle.tx" "Hello" install_new
drive idle "$T/bin1/pic" <<'EOF2'
expect pic fake/m
send first question\r
expect Hello
expect Update installed
expect /restart to update
send /restart\r
expect continuing
send second question\r
expect All done.
wait 500
send \x04\x04
EOF2
id=$(q "SELECT id FROM pic_session LIMIT 1")
[ -n "$id" ] || fail "idle: no conversation: $(q '.tables')"
n=$(grep -c -- ' tui --session ' "$T/started.log" || :) # the old pic's own requests also ran the new file
[ "$n" -eq 1 ] || fail "idle: the new pic was restarted $n times: $(cat "$T/started.log")"
grep -qF -- "tui --session $id" "$T/started.log" || fail "idle: started as: $(cat "$T/started.log")"
grep -qF -- "--db $T/j --workspace $T/ws" "$T/started.log" || fail "idle: started as: $(cat "$T/started.log")"
[ "$(ls "$T/log" | wc -l | tr -d ' ')" = 2 ] || fail "idle: the second question did not reach the model"
tail -n 1 "$T/log/2.req" | grep -qF 'second question' || fail "idle: the second request was not the second question"
[ "$(q "SELECT count(*) FROM pic_session")" = 1 ] || fail "idle: a second conversation"
[ "$(plain "$T/idle.tx" | grep -c 'first question')" -ge 1 ] || fail "idle: the first question was not shown after the restart"
printf 'PASS: an installed update shows in the footer and /restart continues the conversation on the new pic\n'

# ---- /restart while a reply streams: it waits, the draft stays ------------------------------------
cp "$PIC" "$T/bin2/pic"
when "$T/busy.tx" "restarting after this reply" touch "$WS/release"
drive busy "$T/bin2/pic" <<'EOF2'
expect pic fake/m
send go\r
waitfile $WS/started
send /restart\r
expect restarting after this reply
send draft after restart
expect All done.
expect continuing
expect draft after restart
send \x15
send \x04\x04
EOF2
[ "$(plain "$T/busy.tx" | grep -c 'pic fake/m')" -ge 2 ] || fail "busy: pic was not started again: $(screen "$T/busy.tx")"
[ "$(q "SELECT count(*) FROM pic_session")" = 2 ] || fail "busy: conversations: $(q 'SELECT id FROM pic_session')"
"$PIC" --db "$T/j" --workspace "$T/ws" check > /dev/null || fail "integrity"
printf 'PASS: /restart during a reply waits for it, then starts pic again with the draft in the editor\n'