#!/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);
# 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
"$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 Mouse
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'
# /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 the opener here is the wrong machine's: the click says how the terminal opens it.
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 over ssh: Shift+
send \x04\x04
expect pic: conversation
EOF
) || exit 1
[ ! -e "$T/opened" ] || fail "ssh link: opened $(cat "$T/opened")"
printf 'PASS: over ssh a click on a link says Shift+click instead of opening it here\n'