Skip to content
pi-c Artifact [f60167de30]
Login

Artifact [f60167de30]

Artifact f60167de30d52f454c209cad5f99e328c381b866d7b490658cb63567e202b46b:


#!/bin/sh
# The terminal's answers and the done signal, on a pseudo-terminal against a
# fake server. pty_drive answers DA1 as every terminal does, and OSC 11 when
# told a background: a light answer picks the light theme (ahead of
# COLORFGBG, never over an explicit theme), no answer reaches the editor as
# text, a terminal that answers nothing still starts. When a turn ends with
# the window unfocused pic sends BEL and OSC 9; not while focused, not after a
# short turn with focus unknown, and not when notifyWhenDone is false.
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-replies.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 COLORFGBG COLORTERM VTE_VERSION
export TERM=xterm-256color
"$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" "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 (regular mode, truecolor) with the script on stdin; the rest are pic's arguments
    name=$1
    shift
    env COLORTERM=truecolor "COLORFGBG=${FGBG-}" "$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)"
}
has() { grep -aqF "$2" "$T/$1.tx"; } # TRANSCRIPT BYTES
sgr() { printf '\033[%sm' "$1"; }
dark_bg=$(sgr '48;2;33;59;73')
light_bg=$(sgr '48;2;223;231;236')
signal=$(printf '\007\033]9;pic: reply ready\033\\')
# FIRST... script lines, then one message; "/name" runs once the request is over, so the signal (if any)
# has been written by the time "named" shows.
say() {
    for l in "$@"; do printf '%s\n' "$l"; done
    printf 'expect pic fake/m\nsend hello\\r\nexpect Hello, "world"\nsend /name x\\r\nexpect named x\nsend \\x04\\x04\nexpect pic: conversation\n'
}

say 'bg rgb:ffff/ffff/ffff' | drive light
has light "$light_bg" || fail "a white background answer did not pick the light theme"
has light "$dark_bg" && fail "light: the dark background appeared"
has light "$(printf '\033]11;?\033\\\033[c')" || fail "the probe (OSC 11 then DA1) was not sent"
grep -aq 'rgb' "$T"/log/*.req && fail "the background answer reached the request as typed text"
has light "$signal" && fail "a short turn with focus unknown sent the done signal"
has light "$(printf '\033[?1004h')" || fail "focus reports were not turned on"
has light "$(printf '\033[?1004l')" || fail "focus reports were not turned off on exit"
printf 'PASS: an OSC 11 answer picks the light theme and is consumed, not typed\n'

FGBG='0;15'
say 'bg rgb:1e1e/1e1e/2e2e' | drive darkwins
has darkwins "$dark_bg" || fail "a dark background answer did not win over COLORFGBG=0;15"
FGBG=''
say 'bg rgb:ffff/ffff/ffff' | drive explicit --theme dark
has explicit "$dark_bg" || fail "--theme dark lost to the terminal's answer"
has explicit "$light_bg" && fail "explicit: the light background appeared"
printf 'PASS: the answer wins over COLORFGBG, never over an explicit theme\n'

t0=$(date +%s)
say 'answer off' | drive silent
t1=$(date +%s)
has silent "$dark_bg" || fail "a terminal that answers nothing did not get the default dark theme"
[ $((t1 - t0)) -lt 15 ] || fail "a terminal that answers nothing slowed the run to $((t1 - t0)) s"
printf 'PASS: a terminal that answers nothing still starts, with the default theme\n'

say 'send \e[O' | drive unfocused
has unfocused "$signal" || fail "unfocused: no BEL and OSC 9 when the turn ended"
say 'send \e[O' 'send \e[I' | drive focused
has focused "$signal" && fail "focused: the done signal was sent while the window had focus"
"$PIC" settings notifyWhenDone false > /dev/null
say 'send \e[O' | drive disabled
has disabled "$signal" && fail "notifyWhenDone false: the done signal was still sent"
has disabled ']9;' && fail "notifyWhenDone false: an OSC 9 was still sent"
printf 'PASS: the done signal goes out when unfocused, not when focused or turned off\n'

# Grapheme clusters (mode 2027) and the kitty keyboard protocol: asked in the probe, switched on only when the
# terminal answers, switched off on exit. Without answers neither is written. With the protocol on, Ctrl+D
# arrives as CSI 100;5u and still quits.
printf 'cluster\nkitty\nexpect pic fake/m\nsend hello\\r\nexpect Hello, "world"\nsend \\e[100;5u\nsend \\e[100;5u\nexpect pic: conversation\n' | drive modes
has modes "$(printf '\033[?2027$p')" || fail "the probe did not ask for mode 2027"
has modes "$(printf '\033[?u')" || fail "the probe did not ask for the kitty keyboard flags"
has modes "$(printf '\033[?2027h')" || fail "mode 2027 was not set after the terminal reported it reset"
has modes "$(printf '\033[>1u')" || fail "the kitty keyboard flags were not pushed after the terminal answered"
has modes "$(printf '\033[?2027l')" || fail "mode 2027 was not put back on exit"
has modes "$(printf '\033[<u')" || fail "the kitty keyboard flags were not popped on exit"
say | drive nomodes
has nomodes "$(printf '\033[?2027h')" && fail "mode 2027 was set though the terminal did not answer"
has nomodes "$(printf '\033[>1u')" && fail "the kitty keyboard flags were pushed though the terminal did not answer"
printf 'PASS: mode 2027 and kitty flag 1 are asked for, set only when answered, put back on exit; Ctrl+D as CSI u quits\n'

# The kitty flags are pushed again when Ctrl+Z hands the terminal back, and popped on a crash.
count() { perl -0777 -ne 'BEGIN { $s = shift; $s =~ s/\\e/\e/g } $n = () = /\Q$s\E/g; print $n, "\n"' "$2" "$1"; }
printf 'kitty\nexpect pic fake/m\nsend hello\\r\nexpect Hello, "world"\nsend \\x1a\nwait 800\nsend \\e[100;5u\nsend \\e[100;5u\nexpect pic: conversation\n' | drive suspend
[ "$(count "$T/suspend.tx" '\e[>1u')" = 2 ] || fail "Ctrl+Z and fg: the kitty flags were not pushed again ($(count "$T/suspend.tx" '\e[>1u') pushes)"
[ "$(count "$T/suspend.tx" '\e[<u')" = 2 ] || fail "Ctrl+Z and fg: the kitty flags were not popped for the shell and on exit"
printf 'kitty\nexpect pic fake/m\nsignal 11\n' | drive crash || :
has crash "$(printf '\033[>1u')" || fail "crash: the kitty flags were never pushed"
has crash "$(printf '\033[<u')" || fail "crash: the kitty flags were not popped"
printf 'PASS: kitty flag 1 is pushed again after Ctrl+Z and fg, and popped on a crash\n'

# A mode 2027 answer that comes after the first frame re-wraps and redraws what is on screen.
printf 'clusterlate\nexpect pic fake/m\nsend hello\\r\nexpect Hello, "world"\nsend /name x\\r\nexpect named x\nsend zqzq\nexpect zqzq\nanswer2027\nexpect zqzq\nsend \\x15\\x04\\x04\nexpect pic: conversation\n' | drive late
has late "$(printf '\033[?2027h')" || fail "late answer: mode 2027 was not set"
printf 'PASS: a late mode 2027 answer redraws the screen\n'

# Full OSC 4 + OSC 10 + OSC 11 terminal palette (Gruvbox Dark): generates Pi's OKHSL system theme
# (userMessageBg #253b3c = 37;59;60, not built-in dark 33;59;73), and --theme dark still overrides it.
gruvbox_bg=$(sgr '48;2;37;59;60')
gruvbox_replies() {
    cat <<'EOF2'
fg rgb:ebeb/dbdb/b2b2
bg rgb:2828/2828/2828
pal 0 rgb:28/28/28
pal 1 rgb:cc/24/1d
pal 2 rgb:98/97/1a
pal 3 rgb:d7/99/21
pal 4 rgb:45/85/88
pal 5 rgb:b1/62/86
pal 6 rgb:68/9d/6a
pal 7 rgb:a8/99/84
pal 8 rgb:92/83/74
pal 9 rgb:fb/49/34
pal 10 rgb:b8/bb/26
pal 11 rgb:fa/bd/2f
pal 12 rgb:83/a5/98
pal 13 rgb:d3/86/9b
pal 14 rgb:8e/c0/7c
pal 15 rgb:eb/db/b2
EOF2
}
{ gruvbox_replies; say; } | drive palette
has palette "$gruvbox_bg" || fail "palette: OSC 4+10+11 did not generate the Gruvbox system theme userMessageBg"
has palette "$dark_bg" && fail "palette: built-in dark background appeared instead of generated system theme"
grep -aq 'rgb' "$T"/log/*.req && fail "palette: an OSC palette reply leaked into the request as typed text"
{ gruvbox_replies; say; } | drive palette_explicit --theme dark
has palette_explicit "$dark_bg" || fail "palette: --theme dark lost to the terminal palette"
has palette_explicit "$gruvbox_bg" && fail "palette: generated system theme overrode --theme dark"
printf 'PASS: OSC 4+10+11 generates the terminal-palette system theme, never over an explicit theme\n'