#!/bin/sh
# /rc (/remote-control) in the terminal interface, against the test relay (test/pic/fake_relay.c)
# and a scripted phone (test/pic/fake_phone.c). With no phone paired, /rc goes straight to pairing:
# the pairing QR code, nothing of ssh, and the invite setting named only when pairing needs it.
# With a phone paired, /rc turns pic remote on and is two lines and a line of keys: on or off, and
# the phone page's address for this conversation, already on the clipboard (a stand-in command
# here; OSC 52 over ssh) and whole for the terminal's own selection in a narrow window. The
# phones, the QR code and pairing are behind d, q and p, and the footer shows "/rc" at the right. /rc ssh
# and pic rc --ssh are the ssh path, as text. pic rc prints the phone's address.
# Usage: test/tui/rc.sh [--no-faults]
set -eu
PIC=${PIC:?set PIC to the pic binary}
SERVER=${SERVER:?set SERVER to the fake_server binary}
RELAY=${RELAY:?set RELAY to the fake_relay binary}
PHONE=${PHONE:?set PHONE to the fake_phone binary}
DRIVE=${DRIVE:?set DRIVE to the pty_drive binary}
FAULTS=yes # --no-faults (the release build): the ssh lookups and the paired cases need the dev build's seams
[ "${1:-}" = --no-faults ] && FAULTS=no
S=$(cd "$(dirname "$0")/../pic/streams" && pwd)
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-rc.XXXXXX")
export HOME="$T/home"
export PIC_ORIGIN=person PIC_TEST_RC_TEMP="$T/tmp" # a person's conversations, in workspaces pic remote lists
mkdir -p "$HOME/.pic" "$T/ws" "$T/slog"
export ANTHROPIC_API_KEY=pic-rc-test-key PIC_MODEL_CATALOG=offline PIC_PI_EXPORT=0
export PIC_NO_NATIVE_CLIPBOARD=1 # never the real clipboard
export TERM=xterm-256color # a real terminal always sets it; CI runners do not
unset SSH_CONNECTION SSH_CLIENT SSH_TTY TMUX
export FAKE_RELAY_BEAT_MS=60000 # a quiet relay: connected must not wait for its first comment
unset PIC_TEST_PASSPHRASE PIC_KEY_FD PIC_TEST_KEYCHAIN # a test's HOME: the store's key is a file there, no passphrase
pids=
cleanup() {
exec 2> /dev/null
"$PIC" remote stop > /dev/null 2>&1 || :
for p in $pids; do kill -9 "$p" 2> /dev/null || :; done
[ -n "${KEEP:-}" ] || rm -rf -- "$T"
}
trap cleanup EXIT HUP INT TERM
# and a pic remote this run starts ends by itself (dev builds) should the run be killed before cleanup
export PIC_TEST_RC_DEADLINE_S=1800
fail() {
printf 'FAIL: %s\n' "$*" >&2
[ -s "$HOME/.pic/remote.log" ] && tail -20 "$HOME/.pic/remote.log" >&2
exit 1
}
plain() { perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\e\][^\a\e]*(\a|\e\\)//g; s/\r//g' "$1"; }
await() { # FILE PATTERN: wait up to 30 s for the fixed string PATTERN in FILE
i=0
until grep -qF -- "$2" "$1" 2> /dev/null; do
i=$((i + 1))
[ "$i" -lt 600 ] || fail "timed out waiting for $2 in $1"
sleep 0.05
done
}
"$RELAY" "$T/relay.port" 300 test-invite > "$T/relay.out" 2> "$T/relay.err" &
pids="$pids $!"
"$SERVER" "$T/sport" "$T/slog" 300 "sse:$S/text.sse" &
pids="$pids $!"
i=0
while [ ! -s "$T/relay.port" ] || [ ! -s "$T/sport" ]; do
i=$((i + 1))
[ "$i" -lt 600 ] || fail "relay or model server did not start"
sleep 0.05
done
BASE="http://127.0.0.1:$(cat "$T/relay.port")"
printf '{"providers":{"fake":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$ANTHROPIC_API_KEY","models":[{"id":"m","contextWindow":200000}]}}}' \
"$(cat "$T/sport")" > "$T/models.json"
export PIC_MODELS_FILE="$T/models.json"
pic() { "$PIC" --db "$T/j.sqlite" --workspace "$T/ws" --model fake/m "$@"; }
drive() { # NAME COLS ROWS [pic args]: the terminal interface on the script on stdin
name=$1 cols=$2 rows=$3
shift 3
"$DRIVE" "$T/$name.tx" "$cols" "$rows" "$PIC" --tui-mode "${MODE:-regular}" --db "$T/j.sqlite" --workspace "$T/ws" --model fake/m "$@" ||
fail "$name: $(plain "$T/$name.tx" | grep -v '^\s*$' | tail -12)"
}
"$PIC" settings remote.relay "$BASE" > /dev/null
# A conversation to open on the phone.
pic ask 'first question' > /dev/null 2> "$T/ask.err" || fail "ask: $(cat "$T/ask.err")"
# ---- ssh, as text: /rc ssh and pic rc --ssh -----------------------------------------------------
[ "$FAULTS" = yes ] && export PIC_TEST_RC="-|testmac.local|0"
pic rc --ssh > "$T/ssh.out" || fail "pic rc --ssh: exit $?"
grep -qF 'ssh -t ' "$T/ssh.out" || fail "pic rc --ssh: no command: $(cat "$T/ssh.out")"
grep -qF 'Remote Login' "$T/ssh.out" || fail "pic rc --ssh: no checks: $(cat "$T/ssh.out")"
! grep -qF -e '▀' -e '▄' -e '█' "$T/ssh.out" || fail "pic rc --ssh: a QR code"
drive ssh 100 40 <<'EOF2'
expect pic fake/m
send /rc ssh\r
expect Remote control over ssh
expect then run: cd
expect Remote Login
send c
expect Sent the command to the terminal's clipboard
send \e
wait 300
send \x04\x04
EOF2
grep -aq "$(printf '\033]52;c;')" "$T/ssh.tx" || fail "/rc ssh: c copied nothing"
! grep -q '▀' "$T/ssh.tx" || fail "/rc ssh: a QR code drawn"
unset PIC_TEST_RC
printf 'PASS: /rc ssh and pic rc --ssh: the ssh command and checks as text, no QR code; c copies\n'
# ---- no phone paired: /rc is pairing ------------------------------------------------------------
pic rc > "$T/none.out" || fail "pic rc unpaired: exit $?"
grep -qF 'No phone is paired' "$T/none.out" || fail "pic rc unpaired: $(cat "$T/none.out")"
if pic rc --link > /dev/null 2>&1; then fail "pic rc --link: a link with no phone paired"; fi
# Without the relay's invite, pairing cannot start: then, and only then, the setting is named.
drive noinvite 100 40 <<EOF2
expect pic fake/m
send /rc\r
expect remote.invite
send \e
wait 300
send \x04\x04
EOF2
"$PIC" settings remote.invite test-invite > /dev/null
drive pairing 100 50 <<EOF2
expect pic fake/m
send /rc\r
expect Scan this with the phone's camera
expect /#p1.
expect check its six digits here
send \e
wait 500
send /hotkeys\r
expect Other:
send \x04\x04
EOF2
plain "$T/pairing.tx" > "$T/pairing.txt"
grep -q '▀' "$T/pairing.tx" || fail "pairing: no QR code drawn"
! grep -qE 'ssh://|Remote Login|Tailscale' "$T/pairing.txt" || fail "pairing: ssh on the pairing screen"
! grep -qF 'remote.invite' "$T/pairing.txt" || fail "pairing: the invite named though it is set"
printf 'PASS: /rc with no phone paired goes straight to pairing: a QR code, no ssh; Esc closes\n'
printf 'PASS: /rc names remote.invite only when pairing cannot start without it\n'
if [ "$FAULTS" = no ]; then
exit 0
fi
# ---- a phone paired: /rc turns remote on and shows this conversation's link ---------------------
mkfifo "$T/pair.in"
"$PIC" remote pair --pipe < "$T/pair.in" > "$T/pair.out" 2> "$T/pair.err" &
PP=$!
pids="$pids $PP"
exec 3> "$T/pair.in"
await "$T/pair.out" '"type":"offer"'
URL=$(sed -n 's/.*"url":"\([^"]*\)".*/\1/p' "$T/pair.out" | head -1)
mkdir -p "$T/ph"
"$PHONE" "$T/ph" pair "$URL" "Test Phone" > "$T/ph.out" 2>&1 &
pids="$pids $!"
await "$T/pair.out" '"type":"code"'
echo y >&3
await "$T/ph.out" paired
wait "$PP" || fail "pair: $(cat "$T/pair.out" "$T/pair.err")"
exec 3>&-
SID=$("${SQLITE3:-sqlite3}" "$T/j.sqlite" "SELECT id FROM pic_session ORDER BY updated_ms DESC LIMIT 1")
LINK=$(pic rc --link) || fail "pic rc --link: exit $?"
case "$LINK" in "$BASE/#s=$SID&c="*) ;; *) fail "pic rc --link: $LINK (session $SID)" ;; esac
case "$LINK" in *"$T"*) fail "pic rc --link: the link names a path: $LINK" ;; esac
pic rc > "$T/paired.out"
grep -qF "$LINK" "$T/paired.out" || fail "pic rc: no link: $(cat "$T/paired.out")"
grep -qF -e '▀' -e '▄' -e '█' "$T/paired.out" || fail "pic rc: no QR code"
# The link carries the conversation's id alone, so its code fits a 24-row window with room to spare.
qrows=$(grep -c -e '▀' -e '▄' -e '█' "$T/paired.out")
[ "$qrows" -le 21 ] || fail "pic rc: the QR code takes $qrows rows"
grep -qF 'pic remote is not running' "$T/paired.out" || fail "pic rc: running: $(cat "$T/paired.out")"
printf 'PASS: pic rc prints the phone page address for the conversation and its QR code\n'
# The clipboard here is a command that keeps what it is given.
printf '#!/bin/sh\ncat > "%s/clip"\n' "$T" > "$T/clipcmd"
chmod +x "$T/clipcmd"
export PIC_TEST_CLIPBOARD_CMD="$T/clipcmd" # exported, not set on a call: sh keeps a function call's assignments
sgr() { perl -pe 's/\e\[[0-9;]*m//g' "$1"; } # the colours out, the cursor's moves and line breaks kept
drive paired 120 50 -c <<EOF2
expect pic fake/m
send /rc\r
expect ● Remote control is on · link copied to the clipboard
expect /#s=
expect c copy · q QR code · p pair · d devices · s turn off · /rc ssh · esc close
send d
expect Paired phones
expect Test Phone
expect does not start when you log in
send \e
expect c copy
send q
expect for a phone's camera
send x
expect c copy
send \r
wait 300
send \x04\x04
EOF2
plain "$T/paired.tx" > "$T/paired.txt"
[ "$(cat "$T/clip")" = "$LINK" ] || fail "paired: the clipboard has $(cat "$T/clip"), not $LINK"
grep -q '▀' "$T/paired.tx" || fail "paired: q drew no QR code"
! grep -qE 'ssh://|Remote Login|then run:' "$T/paired.txt" || fail "paired: ssh on the screen"
! grep -qF 'Run pic remote' "$T/paired.txt" || fail "paired: tells to run pic remote while it runs"
! grep -aq "$(printf '\033]52;c;')" "$T/paired.tx" || fail "paired: OSC 52 sent though the clipboard command worked"
"$PIC" remote status > "$T/status.out"
grep -qF 'connected to' "$T/status.out" || fail "status: $(cat "$T/status.out")"
printf 'PASS: /rc with a phone paired: remote on and connected at once, and the link, copied through the clipboard command\n'
printf 'PASS: the phones and the login advice are behind d, the QR code behind q; Esc and any key come back\n'
grep -qE '• .* +/rc *$' "$T/paired.txt" || fail "paired: no /rc at the right of the footer: $(grep -F '•' "$T/paired.txt" | tail -3)"
printf 'PASS: the footer shows /rc at the right while pic remote is connected\n'
# A click on it, with the mouse on, opens /rc.
"$PIC" settings mouse on > /dev/null
(MODE=fullscreen && drive click 100 30 -c) <<'EOF2' || exit 1
expect pic fake/m
wait 800
send \e[<0;99;29M
send \e[<0;99;29m
expect ● Remote control is on
send \e
send \x04\x04
EOF2
"$PIC" settings mouse auto > /dev/null
printf 'PASS: a click on the footer'"'"'s /rc opens /rc\n'
# The two lines alone: no phones, no login advice, no QR code, and it fits a short window. The link
# is wider than the window: no line break is written inside it, the terminal wraps it itself.
drive short 40 12 -c <<EOF2
expect pic fake/m
send /rc\r
expect ● Remote control is on
expect close
send z
expect > z
send \x15
wait 300
send \x04\x04
EOF2
plain "$T/short.tx" > "$T/short.txt"
! grep -qF -e 'Test Phone' -e 'log in' -e 'taller' "$T/short.txt" || fail "short: more than the two lines: $(cat "$T/short.txt")"
! grep -q '▀' "$T/short.tx" || fail "short: a QR code on /rc itself"
[ "${#LINK}" -gt 40 ] || fail "short: the link ($LINK) fits the window, so nothing is tested"
sgr "$T/short.tx" | grep -qF -- "$LINK" || fail "short: the link is broken across lines by pic"
(MODE=fullscreen && drive shortfs 40 12 -c) <<EOF2 || exit 1
expect fake/m
send /rc\r
expect ● Remote control is on
expect close
send z
expect > z
send \x15
wait 300
send \x04\x04
EOF2
sgr "$T/shortfs.tx" | grep -qF -- "$(printf '\033[?7h%s\033[?7l' "$LINK")" ||
fail "fullscreen: the link is not written as one line for the terminal to wrap"
printf 'PASS: /rc is two lines and a line of keys in a 40x12 window; any other key closes it\n'
printf 'PASS: a link wider than the window is one line to the terminal, inline and fullscreen\n'
# Over ssh the clipboard command would fill the wrong machine's clipboard: OSC 52, and said so.
rm -f "$T/clip"
(export SSH_CONNECTION='10.0.0.2 5000 10.0.0.1 22' && drive overssh 120 30 -c) <<EOF2 || exit 1
expect pic fake/m
send /rc\r
expect ● Remote control is on · link sent to the terminal's clipboard
send \e
wait 300
send \x04\x04
EOF2
[ ! -e "$T/clip" ] || fail "over ssh: the far machine's clipboard command ran"
grep -aq "$(printf '\033]52;c;')" "$T/overssh.tx" || fail "over ssh: no OSC 52"
# A clipboard command that fails: the terminal is asked, and /rc does not say copied.
printf '#!/bin/sh\nexit 1\n' > "$T/noclip"
chmod +x "$T/noclip"
(export PIC_TEST_CLIPBOARD_CMD="$T/noclip" && drive noclip 120 30 -c) <<EOF2 || exit 1
expect pic fake/m
send /rc\r
expect link sent to the terminal's clipboard
send c
wait 300
send \e
wait 300
send \x04\x04
EOF2
grep -aq "$(printf '\033]52;c;')" "$T/noclip.tx" || fail "noclip: no OSC 52"
if plain "$T/noclip.tx" | grep -qF 'link copied to the clipboard'; then fail "noclip: said copied though the command failed"; fi
printf 'PASS: over ssh, or with no working clipboard command, the link goes to the terminal by OSC 52 and /rc says so\n'
# /rc devices and /rc qr open those pages directly; /rc off turns it off with no panel.
drive words 120 50 -c <<'EOF2'
expect pic fake/m
send /rc devices\r
expect Paired phones
expect Test Phone
send \e
wait 300
send /rc qr\r
expect for a phone's camera
send \e
wait 300
send /rc nope\r
expect /rc takes nothing, or pair, devices, qr, off or ssh
send \x04\x04
EOF2
grep -q '▀' "$T/words.tx" || fail "/rc qr: no QR code drawn"
[ ! -e "$T/clip" ] || fail "/rc devices or /rc qr copied the link unasked"
printf 'PASS: /rc devices and /rc qr open the phones and the QR code\n'
# Again, with remote on: s turns it off, and /rc off does too.
drive off 120 50 -c <<'EOF2'
expect pic fake/m
send /rc\r
expect ● Remote control is on
send s
expect Turn remote control off? [y/N]
send n
wait 300
send s
expect Turn remote control off? [y/N]
send y
expect ○ Remote control is off
expect s turn on
send \e
wait 300
send \x04\x04
EOF2
"$PIC" remote status > "$T/status2.out"
grep -qF 'not running' "$T/status2.out" || fail "s: $(cat "$T/status2.out")"
printf 'PASS: s on /rc asks, and y turns remote control off\n'
# A key the page does not take closes it and is typed: no first letter lost, no letter that acts.
drive typing 120 50 -c <<'EOF2'
expect pic fake/m
send /rc\r
expect ● Remote control is on
send xyz
expect > xyz
send \x15
wait 300
send \x04\x04
EOF2
printf 'PASS: a key /rc does not take closes it and reaches the editor\n'
drive offword 120 50 -c <<'EOF2'
expect pic fake/m
send /rc\r
expect ● Remote control is on
send \e
wait 300
send /rc off\r
expect Remote control is off
send \x04\x04
EOF2
"$PIC" remote status > "$T/status3.out"
grep -qF 'not running' "$T/status3.out" || fail "/rc off: $(cat "$T/status3.out")"
printf 'PASS: /rc off turns remote control off without the panel\n'