Skip to content
pi-c Artifact [37bded8568]
Login

Artifact [37bded8568]

Artifact 37bded85686f444e23604d6711a3166d831fd5e9a4adbccba53254e2e9da45ae:


#!/bin/sh
# The mouse in fullscreen, on a pseudo-terminal against a fake server: a right
# click hands it to the terminal until a key or a focus-in takes it back; the
# mouse setting off and /mouse (Alt+M) leave it to the terminal with its
# alternate scroll off, and the terminal's own is put back on leaving; a click
# on a link opens it with the system's opener (a fake one here, dev builds),
# and over ssh copies it and hands the next click to the user's terminal;
# TERM=dumb draws inline; Ctrl+Z and exit put every mode back.
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 # --no-faults (the release build): the link case, which needs the dev build's opener seam, is skipped
[ "${1:-}" = --no-faults ] && FAULTS=no
S=$(cd "$(dirname "$0")/../pic/streams" && pwd)
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-mouse.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-mouse-test-key PIC_MODELS_FILE="$T/models.json" PIC_MODEL_CATALOG=offline
export T PIC_NO_NATIVE_CLIPBOARD=1
unset SSH_CONNECTION SSH_CLIENT SSH_TTY TMUX TERM_PROGRAM
TERM=xterm-256color # a real terminal always sets it; CI runners do not
export TERM
"$SERVER" "$T/port" "$T/log" 300 "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"
# NAME ROWS [pic args]: pic fullscreen (the default), the script on stdin
drive() {
    name=$1
    rows=$2
    shift 2
    "$DRIVE" "$T/$name.tx" 80 "$rows" "$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)"
}
pic() { "$PIC" --db "$T/j" --workspace "$T/ws" "$@"; }
# FILE SEQ...: each SEQ (\e is ESC) is written after the one before it; the first missing one is printed
in_order() {
    perl -0777 -e 'my $f = shift; open my $h, "<", $f or die; local $/; my $t = <$h>; my $at = 0;
        for my $s (@ARGV) { (my $e = $s) =~ s/\\e/\e/g; my $i = index($t, $e, $at);
            if ($i < 0) { print "$s\n"; exit 1 } $at = $i + length $e } exit 0' "$@"
}
count() { perl -0777 -ne 'BEGIN { $s = shift; $s =~ s/\\e/\e/g } my $n = () = /\Q$s\E/g; print "$n\n"' "$2" "$1"; }

# A right click releases the mouse (tracking and alternate scroll off) and says so; a key takes it back, and so
# does a focus-in; leaving puts the terminal's alternate scroll (reported on) back.
drive release 24 <<'EOF'
expect pic fake/m
send \e[<2;10;5M
expect mouse released
send \e[C
wait 300
send \e[<2;10;5M
expect mouse released
send \e[I
wait 300
send \x04\x04
EOF
miss=$(in_order "$T/release.tx" '\e[?1007$p' '\e[?1049h' '\e[?1000h\e[?1002h\e[?1006h' \
    '\e[?1006l\e[?1002l\e[?1000l\e[?1007l' 'mouse released' '\e[?1000h' \
    '\e[?1000l' 'mouse released' '\e[?1000h' '\e[?1007h' '\e[?1049l') || fail "release: no $miss in order"
printf 'PASS: a right click hands the mouse to the terminal; a key or a focus-in takes it back\n'

# The setting off: fullscreen with no tracking and no alternate scroll; a right click is not reported anyway.
pic settings mouse off
drive off 24 <<'EOF'
expect pic fake/m
send /settings\r
expect type to filter
send \e
wait 200
send \x04\x04
EOF
pic settings --unset mouse
[ "$(count "$T/off.tx" '\e[?1000h')" -eq 0 ] || fail "mouse off: tracking was turned on"
in_order "$T/off.tx" '\e[?1049h' '\e[?1007l' '\e[?1007h' '\e[?1049l' > /dev/null || fail "mouse off: alternate scroll not off, or not put back"
printf 'PASS: the mouse setting off draws fullscreen without tracking or alternate scroll\n'

# Apple's Terminal never passes Cmd-C to the program, and copies only its own selection: the mouse setting's
# default (auto) leaves the mouse to it there, says so the first time, and /mouse on still takes it; on overrides auto.
(
    export TERM_PROGRAM=Apple_Terminal
    drive apple 24 <<'EOF'
expect pic fake/m
expect Terminal.app: the mouse is left to the terminal
send /settings\r
send mouse
expect Mouse                      auto
send \e
wait 200
send \e
wait 200
send /mouse on\r
expect Mouse on for this session
send \x04\x04
EOF
    [ "$(pic settings mouseNoticeShown)" = true ] || fail "Terminal.app: the notice was not recorded"
    drive apple_again 24 <<'EOF'
expect pic fake/m
send \x04\x04
EOF
    pic settings mouse on
    drive apple_on 24 <<'EOF'
expect pic fake/m
send \x04\x04
EOF
    pic settings --unset mouse
) || exit 1
in_order "$T/apple.tx" '\e[?1049h' '\e[?1007l' 'Terminal.app: the mouse' 'Mouse on' > /dev/null ||
    fail "Terminal.app: the mouse was not left to the terminal"
[ "$(count "$T/apple.tx" '\e[?1000h')" -eq 1 ] || fail "Terminal.app: tracking was on before /mouse on"
in_order "$T/apple.tx" 'Terminal.app: the mouse' '\e[?1000h' 'Mouse on' > /dev/null ||
    fail "Terminal.app: /mouse on did not take the mouse"
in_order "$T/apple_on.tx" '\e[?1049h' '\e[?1000h\e[?1002h\e[?1006h' > /dev/null || fail "Terminal.app, mouse on: no tracking"
[ "$(count "$T/apple_again.tx" 'Terminal.app: the mouse')" -eq 0 ] || fail "Terminal.app: the notice was shown twice"
[ "$(count "$T/apple_again.tx" '\e[?1000h')" -eq 0 ] || fail "Terminal.app, second start: tracking was on"
printf 'PASS: in Terminal.app the mouse is left to the terminal by default, with a notice the first time; on takes it\n'

# /mouse and Alt+M toggle it for the session; /mouse on|off sets it.
drive toggle 24 <<'EOF'
expect pic fake/m
send /mouse\r
expect Mouse off for this session
send \em
expect Mouse on for this session
send /mouse off\r
expect Mouse off for this session
send /mouse sideways\r
expect Usage: /mouse [on|off]
send \x04\x04
EOF
in_order "$T/toggle.tx" '\e[?1000h' '\e[?1000l' 'Mouse off' '\e[?1000h' 'Mouse on' '\e[?1000l' 'Mouse off' > /dev/null ||
    fail "toggle: tracking did not follow /mouse and Alt+M"
[ -z "$(pic settings mouse 2>/dev/null)" ] || fail "toggle: /mouse saved the setting"
printf 'PASS: /mouse and Alt+M turn mouse capture off and on for the session\n'

# Ctrl+Z puts every mode back for the shell and sets them again on return (a session leader on a pseudo-terminal
# is not stopped, so it returns at once); /mouse off first, so alternate scroll goes back and off again too.
drive suspend 24 <<'EOF'
expect pic fake/m
send /mouse off\r
expect Mouse off for this session
send \x1a
wait 500
send \x04\x04
EOF
miss=$(in_order "$T/suspend.tx" '\e[?1049h' '\e[?1000h' 'Mouse off' '\e[?1007h' '\e[?1000l' '\e[?7h' '\e[?1049l' \
    '\e[?1004l' '\e[?2004l' '\e[?2004h' '\e[?1049h' '\e[?1007l' '\e[?1007h' '\e[?1049l') || fail "suspend: no $miss in order"
[ "$(count "$T/suspend.tx" '\e[?1000h')" -eq 1 ] || fail "suspend: tracking came back on after Ctrl+Z with the mouse off"
printf 'PASS: Ctrl+Z and exit put the mouse, wrap, alternate screen and alternate scroll modes back\n'

# TERM=dumb has no alternate screen: pic draws inline.
(
    TERM=dumb
    export TERM
    drive dumb 24 <<'EOF'
expect pic fake/m
send \x04\x04
EOF
) || exit 1
[ "$(count "$T/dumb.tx" '\e[?1049h')" -eq 0 ] || fail "TERM=dumb: the alternate screen was entered"
[ "$(count "$T/dumb.tx" '\e[?1000h')" -eq 0 ] || fail "TERM=dumb: mouse tracking was turned on"
printf 'PASS: TERM=dumb draws inline\n'

[ "$FAULTS" = yes ] || exit 0
# A plain click on a link opens it with the system's opener (PIC_TEST_BROWSER, a script here); a drag over it
# selects instead; over ssh the click says how the terminal opens it.
printf '#!/bin/sh\nprintf "%%s\\n" "$1" >> "$T/opened"\n' > "$T/opener"
chmod +x "$T/opener"
export PIC_TEST_BROWSER="$T/opener" TERM_PROGRAM=ghostty
# Where the link lands depends on the header above it: every row is clicked (and dragged over first).
drags= clicks=
for r in 1 2 3 4 5 6 7 8 9 10 11; do
    drags="$drags
send \e[<0;12;${r}M
send \e[<32;20;${r}M
send \e[<0;20;${r}m"
    clicks="$clicks
send \e[<0;12;${r}M
send \e[<0;12;${r}m"
done
drive link 12 <<EOF
expect pic fake/m
send see https://example.org/x please\r
expect Hello, "world"
wait 200$drags
expect Copied!
wait 300$clicks
waitfile \$T/opened
wait 300
send \x04\x04
expect pic: conversation
EOF
[ "$(cat "$T/opened")" = https://example.org/x ] || fail "link: opened $(cat "$T/opened")"
printf 'PASS: a click on a link opens it; a drag over it selects instead\n'

# Over ssh this machine's opener is the wrong computer's: a click on a web link copies it (OSC 52) and releases the
# mouse, so the next click goes to the user's terminal, which opens the OSC 8 link where the user is.
rm -f "$T/opened"
(
    SSH_CONNECTION="10.0.0.1 1 10.0.0.2 22"
    export SSH_CONNECTION
    drive sshlink 12 tui -c <<EOF
expect Hello, "world"
wait 200$clicks
expect open it on this computer (copied)
send \e[C
wait 300
send \x04\x04
expect pic: conversation
EOF
) || exit 1
[ ! -e "$T/opened" ] || fail "ssh link: opened $(cat "$T/opened")"
miss=$(in_order "$T/sshlink.tx" '\e[?1000h' '\e]52;c;aHR0cHM6Ly9leGFtcGxlLm9yZy94' '\e[?1000l' \
    'open it on this computer' '\e[?1000h') || fail "ssh link: no $miss in order"
printf 'PASS: over ssh a click on a link copies it and hands the next click to the terminal; a key takes it back\n'

# Over ssh a file link names a path on this (the remote) machine: no release; the path is copied and the click says so.
mkdir "$T/log2"
# links.sh's reply without its web link, so every click lands on the file link or on nothing
sed 's| and https://example.com/a/very/long/path/that/wraps/over/several/lines/of/the/screen/for/sure/yes||' \
    "$(dirname "$0")/streams/links_reply.sse" > "$T/file_reply.sse"
grep -q 'example.com' "$T/file_reply.sse" && fail "sshfile: the fixture still has its web link"
"$SERVER" "$T/port2" "$T/log2" 300 "sse:$T/file_reply.sse" &
spid="$spid $!"
i=0
while [ ! -s "$T/port2" ]; do i=$((i + 1)); [ "$i" -lt 100 ] || fail "server 2"; sleep 0.05; done
sed "s/$(cat "$T/port")/$(cat "$T/port2")/" "$T/models.json" > "$T/models2.json"
mkdir -p "$T/ws/docs/design"
printf 'x\n' > "$T/ws/docs/design/flak.md"
fclicks=
for r in 1 2 3 4 5 6 7 8 9 10 11; do
    fclicks="$fclicks
send \e[<0;10;${r}M
send \e[<0;10;${r}m"
done
(
    SSH_CONNECTION="10.0.0.1 1 10.0.0.2 22" PIC_MODELS_FILE="$T/models2.json"
    export SSH_CONNECTION PIC_MODELS_FILE
    "$DRIVE" "$T/sshfile.tx" 80 12 "$PIC" --db "$T/j2" --workspace "$T/ws" --model fake/m <<EOF ||
expect pic fake/m
send links please\r
expect The end.
wait 200$fclicks
expect this file is on the remote machine
send \x04\x04
expect pic: conversation
EOF
        fail "sshfile: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/sshfile.tx" | grep -v '^\s*$' | tail -8)"
) || exit 1
[ "$(count "$T/sshfile.tx" '\e[?1000l')" -eq 1 ] || fail "ssh file link: the mouse was released"
copied=$(perl -MMIME::Base64 -0777 -ne 'print decode_base64($1), "\n" while /\e\]52;c;([A-Za-z0-9+\/=]*)\a/g' "$T/sshfile.tx")
printf '%s\n' "$copied" | grep -q '^/.*/ws/docs/design/flak\.md$' || fail "ssh file link: the path was not copied: $copied"
printf 'PASS: over ssh a click on a file link copies its path and says the file is on the remote machine\n'