Skip to content
pi-c Artifact [bc46f4d246]
Login

Artifact [bc46f4d246]

Artifact bc46f4d24641dec510a08153299bc678f1d5c3cb491b6b67c15e7a2697bbb8bd:


#!/bin/sh
# Links on a pseudo-terminal against a fake server. A markdown link to a
# relative path is an OSC 8 link to a file:// URL under the workspace and its
# path is not printed; a URL longer than the line opens every piece with one
# id; a path in a tool's title and output that names a file is a link. The
# hyperlinks setting forces them off (the path is printed again) or on; inside
# tmux they are on from tmux 3.4. Fullscreen (dev build): a click on a link
# opens it with the system opener. --no-faults (the release build): no click.
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}
FAULTS=yes
[ "${1:-}" = --no-faults ] && FAULTS=no
V=$(cd "$(dirname "$0")/streams" && pwd)
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-links.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 -p "$T/ws/src" "$T/ws/docs/design" "$T/home" "$T/bin"
: > "$T/ws/src/foo.c"
: > "$T/ws/docs/design/flak.md"
export HOME="$T/home" ANTHROPIC_API_KEY=pic-links-test-key PIC_MODEL_CATALOG=offline PIC_NO_NATIVE_CLIPBOARD=1
export TERM=xterm-256color TERM_PROGRAM=ghostty
unset SSH_CONNECTION SSH_CLIENT SSH_TTY TMUX STY TERM_PROGRAM_VERSION PIC_TEST_BROWSER COLORFGBG
WS=$(printf "%s" "$T/ws" | sed "s#//*#/#g") # as pic writes it: a path, not resolved through symlinks

# 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" 120 "$@" &
    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"
}
# drive NAME MODE [ENV...]: pic in MODE on an 80x24 terminal, the script on stdin
drive() {
    name=$1 mode=$2
    shift 2
    env PIC_MODELS_FILE="$T/$name.json" "$@" "$DRIVE" "$T/$name.tx" 80 24 "$PIC" --tui-mode "$mode" --db "$T/$name.db" \
        --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"; } # NAME BYTES
esc=$(printf '\033')
link_to() { printf ';file://%s%s%s\\' "$WS" "$1" "$esc"; } # PATH: the end of the sequence that opens a link to it
# ask WAIT: send one message, wait for WAIT, quit
ask() {
    printf 'expect pic fake/m\nsend hello\\r\nexpect %s\nsend /name x\\r\nexpect named x\nsend \\x04\\x04\nexpect pic: conversation\n' "$1"
}

serve reply "sse:$V/links_reply.sse"
ask 'The end.' | drive reply regular
has reply "$(link_to /docs/design/flak.md)" || fail "a relative markdown link is not a file:// link under the workspace"
has reply "(docs/design/flak.md)" && fail "the link's path was printed after its text"
ids=$(grep -ao "$esc]8;id=[a-z0-9]*;https://example.com/a/very/long" "$T/reply.tx" | sort | uniq -c)
[ "$(printf '%s\n' "$ids" | wc -l | tr -d ' ')" = 1 ] || fail "the wrapped URL opens with more than one id: $ids"
[ "$(printf '%s\n' "$ids" | awk '{print $1}')" -ge 2 ] || fail "the URL did not wrap into pieces that each open the link: $ids"
printf 'PASS: a relative markdown link opens file://WORKSPACE/PATH; a wrapped URL keeps one id\n'

serve tool "sse:$V/links_tool.sse" "sse:$V/done.sse"
ask 'All done.' | drive tool regular
[ "$(grep -aoF "$(link_to /src/foo.c)" "$T/tool.tx" | wc -l | tr -d ' ')" -ge 2 ] ||
    fail "the path in the bash title and output is not linked twice"
printf 'PASS: a path in a tool title and its output is a link\n'

serve off "sse:$V/links_reply.sse"
"$PIC" settings hyperlinks off > /dev/null
ask 'The end.' | drive off regular
has off "$esc]8;" && fail "hyperlinks off still sent OSC 8"
has off "(docs/design/flak.md)" || fail "hyperlinks off: the link's path is not printed"
"$PIC" settings --unset hyperlinks > /dev/null
printf 'PASS: the hyperlinks setting turns them off\n'

# Apple's Terminal has no OSC 8 (it swallows the sequence, so link text is dead) and opens a URL on Cmd-double-click
# only when the URL itself is on the screen, in one run of text.
serve apple "sse:$V/links_reply.sse"
ask 'The end.' | drive apple regular TERM_PROGRAM=Apple_Terminal
has apple "$esc]8;" && fail "Apple's Terminal got OSC 8"
has apple "(docs/design/flak.md)" || fail "Apple's Terminal: the link's path is not printed"
has apple "https://example.com/a/very/long/path/that/wraps/over/several/lines/of/the/s" ||
    fail "Apple's Terminal: the URL is split by escapes"
printf "PASS: Apple's Terminal gets the addresses as text, with no OSC 8\n"

TM="/tmp/tmux-501/default,1,0"
serve old "sse:$V/links_reply.sse"
ask 'The end.' | drive old regular TMUX="$TM" TERM=tmux-256color TERM_PROGRAM=tmux TERM_PROGRAM_VERSION=3.3a
has old "$esc]8;" && fail "tmux 3.3a got OSC 8"
has old "(docs/design/flak.md)" || fail "tmux 3.3a: the link's path is not printed"
serve new "sse:$V/links_reply.sse"
ask 'The end.' | drive new regular TMUX="$TM" TERM=tmux-256color TERM_PROGRAM=tmux TERM_PROGRAM_VERSION=3.4
has new "$(link_to /docs/design/flak.md)" || fail "tmux 3.4 got no link"
serve forced "sse:$V/links_reply.sse"
"$PIC" settings hyperlinks on > /dev/null
ask 'The end.' | drive forced regular TMUX="$TM" TERM=screen-256color TERM_PROGRAM=tmux TERM_PROGRAM_VERSION=3.2
has forced "$(link_to /docs/design/flak.md)" || fail "hyperlinks on: tmux 3.2 got no link"
"$PIC" settings --unset hyperlinks > /dev/null
printf 'PASS: inside tmux links need tmux 3.4; the setting forces them on\n'

[ "$FAULTS" = yes ] || exit 0
printf '#!/bin/sh\nprintf "%%s\\n" "$1" >> "%s/opened"\n' "$T" > "$T/bin/open"
chmod +x "$T/bin/open"
serve click "sse:$V/links_reply.sse"
{
    printf 'expect pic fake/m\nsend hello\\r\nexpect The end.\nwait 300\n'
    y=1
    while [ "$y" -le 22 ]; do
        printf 'send \\e[<0;6;%sM\nsend \\e[<0;6;%sm\n' "$y" "$y"
        y=$((y + 1))
    done
    printf 'waitfile $T/opened\nsend /name x\\r\nexpect named x\nsend \\x04\\x04\nexpect pic: conversation\n'
} | drive click fullscreen T="$T" PIC_TEST_BROWSER="$T/bin/open"
i=0
while [ ! -s "$T/opened" ]; do i=$((i + 1)); [ "$i" -lt 100 ] || fail "nothing opened"; sleep 0.05; done
sleep 0.3
grep -qxF "file://$WS/docs/design/flak.md" "$T/opened" || fail "a click on the markdown link did not open it: $(cat "$T/opened")"
grep -vxF -e "file://$WS/docs/design/flak.md" -e "https://example.com/a/very/long/path/that/wraps/over/several/lines/of/the/screen/for/sure/yes" \
    "$T/opened" && fail "a click opened something that is not a link on the screen"
printf 'PASS: fullscreen: a click on a link opens it\n'

# The mouse on in Apple's Terminal: pic still knows where the links are, though it draws no OSC 8; a double click
# (Cmd-double-click, as Terminal's own) opens a link once.
rm -f "$T/opened"
serve appleclick "sse:$V/links_reply.sse"
"$PIC" settings mouse on > /dev/null
{
    printf 'expect pic fake/m\nsend hello\\r\nexpect The end.\nwait 300\n'
    y=1
    while [ "$y" -le 22 ]; do
        printf 'send \\e[<0;6;%sM\nsend \\e[<0;6;%sm\nsend \\e[<0;6;%sM\nsend \\e[<0;6;%sm\n' "$y" "$y" "$y" "$y"
        y=$((y + 1))
    done
    printf 'waitfile $T/opened\nwait 300\nsend /name x\\r\nexpect named x\nsend \\x04\\x04\nexpect pic: conversation\n'
} | drive appleclick fullscreen T="$T" PIC_TEST_BROWSER="$T/bin/open" TERM_PROGRAM=Apple_Terminal
"$PIC" settings --unset mouse > /dev/null
has appleclick "$esc]8;" && fail "Apple's Terminal with the mouse on got OSC 8"
[ "$(grep -cxF "file://$WS/docs/design/flak.md" "$T/opened")" = 1 ] ||
    fail "Apple's Terminal: a double click on the markdown link did not open it once: $(cat "$T/opened")"
grep -vxF -e "file://$WS/docs/design/flak.md" -e "https://example.com/a/very/long/path/that/wraps/over/several/lines/of/the/screen/for/sure/yes" \
    "$T/opened" && fail "Apple's Terminal: a click opened something that is not a link on the screen"
printf "PASS: Apple's Terminal with the mouse on: a click opens a link, a double click opens it once\n"