Skip to content
pi-c Artifact [03077eb4b6]
Login

Artifact [03077eb4b6]

Artifact 03077eb4b6a880c52e946668df5e47f92508e5d4e3a3651a6b5f7c05f152ca0d:


#!/bin/sh
# Fullscreen transcript search (Pi's tui.altScreen.search*) and the Claude Code
# keys Ctrl+R (reverse search through the prompt history) and Ctrl+D (a second
# press within 800 ms to exit), on a pseudo-terminal against a fake server.
# Shift+Ctrl+F and Shift+Enter arrive as the Kitty protocol's CSI u reports.
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-search.XXXXXX")
spids=
cleanup() { [ -n "$spids" ] && kill $spids 2>/dev/null || :; rm -rf -- "$T"; }
trap cleanup EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
mkdir "$T/ws" "$T/home"
export HOME="$T/home" ANTHROPIC_API_KEY=pic-search-test-key PIC_MODEL_CATALOG=offline
export T PIC_NO_NATIVE_CLIPBOARD=1

# serve NAME SPEC...: a fake server whose models file is $T/NAME.json
serve() {
    name=$1
    shift
    mkdir "$T/log-$name"
    "$SERVER" "$T/port-$name" "$T/log-$name" 300 "$@" &
    spids="$spids $!"
    i=0
    while [ ! -s "$T/port-$name" ]; do i=$((i + 1)); [ "$i" -lt 100 ] || fail "server $name"; 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-$name")" > "$T/$name.json"
}
# NAME ROWS MODE [pic args]: pic against server NAME, the script on stdin
drive() {
    name=$1
    rows=$2
    mode=$3
    shift 3
    PIC_MODELS_FILE="$T/$srv.json" "$DRIVE" "$T/$name.tx" 80 "$rows" "$PIC" --tui-mode "$mode" --db "$T/j-$name" \
        --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 reply of 40 lines, one text delta each; "zebra" is on rows 07, 21, 36 and 37.
{
    cat <<'EOF'
event: message_start
data: {"type":"message_start","message":{"id":"msg_long","type":"message","role":"assistant","model":"claude-opus-5-5","content":[],"stop_reason":null,"usage":{"input_tokens":25,"output_tokens":1}}}

event: content_block_start
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}

EOF
    n=1
    while [ "$n" -le 40 ]; do
        word=plain
        case $n in 7 | 21 | 36 | 37) word=Zebra ;; esac
        printf 'event: content_block_delta\ndata: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"row %02d %s\\n"}}\n\n' "$n" "$word"
        n=$((n + 1))
    done
    cat <<'EOF'
event: content_block_stop
data: {"type":"content_block_stop","index":0}

event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"output_tokens":99}}

event: message_stop
data: {"type":"message_stop"}

EOF
} > "$T/long.sse"

# ---- search a long transcript ----------------------------------------------
# Open, type; the first match at or after the view's top is current (3/4: rows
# 36 and 37 are on screen, the other one underlined). Shift+Enter goes back,
# scrolling to row 21 and then row 07; Enter and Ctrl+G go forward and wrap;
# Esc closes and gives the keys back.
serve long "sse:$T/long.sse"
srv=long
drive search 14 fullscreen <<'EOF'
expect pic fake/m
send hello\r
expect row 40
send \e[102;6u
expect ind in transcript
send zebra
expect 3/4
send \e[13;2u
expect row 21
send \e[13;2u
expect row 07
send \r
expect 2/4
send \x07
expect 3/4
send \r
expect 4/4
send \r
expect 1/4
send \x15
send nomatch
expect No matches
send \e
send stay
expect stay
send \x15
send \x04\x04
expect pic: conversation
EOF
perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g' "$T/search.tx" | grep -aq 'No matches' || fail "search: no count for a miss"
grep -aqF "$(printf '\033[1m\033[7m')" "$T/search.tx" || fail "search: the current match is not reversed"
grep -aqF "$(printf '\033[4m')" "$T/search.tx" || fail "search: the other matches are not underlined"
printf 'PASS: fullscreen search opens, counts, steps back and forward through a long transcript, wraps, and closes\n'

# ---- search while a reply streams ------------------------------------------
# The second reply arrives slowly; the search stays open and its count grows
# from 4 to 8 (four more rows say "zebra") as the text lands.
serve slow "sse:$T/long.sse" "slow:$T/long.sse:40"
srv=slow
drive stream 14 fullscreen <<'EOF'
expect pic fake/m
send one\r
expect row 40
expect ↓99
send two\r
waitfile $T/log-slow/2.req
send \e[102;6u
send zebra
expect /8
expect ↓198
send \e
send \x04\x04
expect pic: conversation
EOF
printf 'PASS: fullscreen search follows a reply that is still streaming\n'

# ---- Ctrl+R: reverse search through the prompt history ----------------------
serve text "sse:$S/text.sse"
srv=text
reqs() { ls "$T/log-text" | grep -c '\.req$' || true; }
# the text of the last user message of request N
last() { grep -o '"role":"user","content":\[{"type":"text","text":"[^"]*"' "$T/log-text/$1.req" | tail -1 | sed 's/.*"text":"//; s/"$//'; }
for mode in regular fullscreen; do
    base=$(reqs)
    drive "hist-$mode" 24 "$mode" <<'EOF'
expect pic fake/m
send alpha one\r
expect ↓12
send beta two\r
expect ↓24
send alpha three\r
expect ↓36
send \x12
expect reverse-i-search
send alpha
expect alpha three
send \x12
expect alpha one
send nope
expect failed reverse-i-search
send \x7f\x7f\x7f\x7f
expect alpha three
send \x12
expect alpha one
send \r
wait 200
send \r
expect ↓48
send \x12
send beta
expect beta two
send \e
send zed\r
expect ↓60
send \x04\x04
expect pic: conversation
EOF
    # the fourth request is "alpha one" again (Enter took it into the editor, the next Enter sent it), the fifth
    # "zed"; the cancelled search sent nothing more
    [ "$(last $((base + 4)))" = 'alpha one' ] || fail "ctrl+r $mode: alpha one was not recalled and sent: $(last $((base + 4)))"
    [ "$(last $((base + 5)))" = 'zed' ] || fail "ctrl+r $mode: zed was not sent: $(last $((base + 5)))"
    [ "$(reqs)" = $((base + 5)) ] || fail "ctrl+r $mode: Esc did not cancel the search: $(ls "$T/log-text")"
    printf 'PASS: Ctrl+R finds the newest match, an older one, shows a failure, Enter accepts into the editor, Esc cancels (%s)\n' "$mode"
done

# ---- Ctrl+D: exit only on a second press within 800 ms ----------------------
# (no conversation was started, so there is no "pic: conversation" line: pty_drive checks that pic exits)
drive ctrld 24 fullscreen <<'EOF'
expect pic fake/m
send \x04
expect press ctrl+d again
wait 1100
send \x04
expect press ctrl+d again
send \x0c
send \x04
expect press ctrl+d again
send alive
expect alive
send \x15
send \x04\x04
EOF
printf 'PASS: Ctrl+D exits on a second press within 800 ms, not after a pause or another key\n'

# ---- Ctrl+F: search in fullscreen, cursor right in regular mode -------------
# Mac Terminal sends no Ctrl+Shift+F, so Ctrl+F (Pi's Windows key) opens the
# search too; in regular mode it stays the editor's cursor right.
srv=long
drive ctrlf 14 fullscreen <<'EOF'
expect pic fake/m
send \x06
expect ind in transcript
send \e
send \x04\x04
EOF
printf 'PASS: Ctrl+F opens the fullscreen transcript search\n'
srv=text
base=$(reqs)
drive ctrlf-reg 24 regular <<'EOF'
expect pic fake/m
send ab
send \x01
send \x06
send X
expect aXb
send \r
expect Hello, "world"
send \x04\x04
expect pic: conversation
EOF
[ "$(last $((base + 1)))" = 'aXb' ] || fail "ctrl+f regular: not cursor right: $(last $((base + 1)))"
printf 'PASS: Ctrl+F is cursor right in regular mode\n'