#!/bin/sh
# Ctrl+V in the terminal interface, on a pseudo-terminal against a fake
# server and a fake clipboard (the PIC_CLIPBOARD_*_FILE overrides: the real
# clipboard is never touched). An image on the clipboard becomes "[image #N]"
# in the editor, goes to the server with the message and is journaled; deleting
# the marker drops it; copied files go in as paths; text is pasted; a
# bracketed paste of text is unchanged.
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)
V=$(cd "$(dirname "$0")/streams" && pwd)
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-paste.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-paste-test-key PIC_MODELS_FILE="$T/models.json" PIC_MODEL_CATALOG=offline
export PIC_NO_NATIVE_CLIPBOARD=1
mkdir "$T/tmp"
export TMPDIR="$T/tmp" # nothing of the clipboard may be left in it
"$PIC" settings cacheRetention none > /dev/null
PNG_B64='iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGP4z8DwHwAFAAH/iZk9HQAAAABJRU5ErkJggg=='
printf '%s' "$PNG_B64" | base64 -d > "$T/dot.png" 2>/dev/null || printf '%s' "$PNG_B64" | base64 -D > "$T/dot.png"
[ "$(wc -c < "$T/dot.png" | tr -d ' ')" = 70 ] || fail "fixture: dot.png"
printf 'this is not an image' > "$T/fake.png"
printf 'text from the clipboard' > "$T/clip.txt"
printf '%s\n%s\n' "$T/ws/a.txt" "$T/ws/b.txt" > "$T/clip.files"
: > "$T/none"
"$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/bash_hold.sse" "sse:$V/done.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,"input":["text","image"]}]}}}' \
"$(cat "$T/port")" > "$T/models.json"
drive() { # NAME MODE: run pic with the script on stdin (a fresh conversation in the one journal)
name=$1
mode=$2
"$DRIVE" "$T/$name.tx" 80 24 "$PIC" --tui-mode "$mode" --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)"
}
IMG_BLOCK='{"type":"image","source":{"type":"base64","media_type":"image/png","data":"'$PNG_B64'"}}'
has() { grep -qF -- "$2" "$1" || fail "$3: $(grep -o '"messages".*' "$1" | cut -c1-500)"; }
hasnt() { ! grep -qF -- "$2" "$1" || fail "$3"; }
images() { sqlite3 "$T/j" 'SELECT count(*) FROM pic_image'; }
# Ctrl+V with an image: the marker in the editor, then the image with the message.
for mode in regular fullscreen; do
export PIC_CLIPBOARD_IMAGE_FILE="$T/dot.png"
drive "img-$mode" "$mode" <<'EOS'
expect pic fake/m
send what is \x16
expect [image #1]
send this\r
expect Hello, "world"
send \x04\x04
expect pic: conversation
EOS
done
[ -f "$T/log/1.req" ] && [ -f "$T/log/2.req" ] || fail "no requests"
for k in 1 2; do
has "$T/log/$k.req" '"role":"user","content":[{"type":"text","text":"what is [image #1]this"},'"$IMG_BLOCK"']' "request $k: text with its marker, then the image"
done
[ "$(images)" = 1 ] || fail "the image is journaled once (same bytes): $(images)"
printf 'PASS: Ctrl+V attaches a clipboard image, shown as [image #1], sent with the message and journaled (regular and fullscreen)\n'
# Deleting the marker drops the attachment.
drive del regular <<'EOS'
expect pic fake/m
send \x16
expect [image #1]
send \x7f
send no picture\r
expect Hello, "world"
send \x04\x04
expect pic: conversation
EOS
has "$T/log/3.req" '"content":"no picture"' "deleted marker: the text"
hasnt "$T/log/3.req" '"type":"image"' "deleted marker: no image goes out"
printf 'PASS: deleting the marker removes the attachment\n'
# Two images are numbered; Pi's bracketed paste of text is unchanged.
drive two regular <<'EOS'
expect pic fake/m
send \x16
send \x16
expect [image #1][image #2]
send \e[200~pasted text\e[201~\r
expect Hello, "world"
send \x04\x04
expect pic: conversation
EOS
has "$T/log/4.req" '"text":"[image #1][image #2]pasted text"' "two markers and a bracketed paste"
[ "$(grep -o '"type":"image"' "$T/log/4.req" | wc -l | tr -d ' ')" = 2 ] || fail "two images in the request"
printf 'PASS: images are numbered, and a bracketed paste of text is unchanged\n'
# Not an image, copied files and text.
unset PIC_CLIPBOARD_IMAGE_FILE
export PIC_CLIPBOARD_IMAGE_FILE="$T/fake.png"
drive bad regular <<'EOS'
expect pic fake/m
send \x16
expect Failed to paste from clipboard
send \x04\x04
EOS
unset PIC_CLIPBOARD_IMAGE_FILE
export PIC_CLIPBOARD_FILES_FILE="$T/clip.files"
export PIC_CLIPBOARD_IMAGE_FILE="$T/dot.png"
drive files regular <<'EOS'
expect pic fake/m
send see\x16
expect ws/b.txt
send \r
expect Hello, "world"
send \x04\x04
expect pic: conversation
EOS
has "$T/log/5.req" "$T/ws/a.txt" "copied files: their paths"
hasnt "$T/log/5.req" '"type":"image"' "copied files win over an image"
unset PIC_CLIPBOARD_FILES_FILE PIC_CLIPBOARD_IMAGE_FILE
export PIC_CLIPBOARD_TEXT_FILE="$T/clip.txt"
drive text regular <<'EOS'
expect pic fake/m
send \x16
expect text from the clipboard
send \r
expect Hello, "world"
send \x04\x04
expect pic: conversation
EOS
has "$T/log/6.req" '"content":"text from the clipboard"' "text: pasted and sent"
printf 'PASS: a non-image is refused, copied files go in as paths, text is pasted\n'
# Enter while a reply runs steers it: the image goes with the steer, over the command pipe.
export PIC_CLIPBOARD_IMAGE_FILE="$T/dot.png"
unset PIC_CLIPBOARD_TEXT_FILE
(
n=0
until grep -aqF "Steering: look" "$T/steer.tx" 2>/dev/null; do
n=$((n + 1))
[ "$n" -lt 600 ] || exit 0
sleep 0.05
done
: > "$T/ws/release"
) &
rpid=$!
drive steer regular <<'EOS'
expect pic fake/m
send go\r
expect $ touch started
send look \x16
expect [image #1]
send \r
expect Steering: look
expect All done.
send \x04\x04
EOS
wait "$rpid" 2>/dev/null || :
has "$T/log/8.req" "$IMG_BLOCK" "steer: the image reached the model"
has "$T/log/8.req" 'look [image #1]' "steer: the text with its marker"
hasnt "$T/log/7.req" 'look' "steer: not with the first request"
printf 'PASS: an image goes with a steer to a running reply\n'
[ -z "$(ls -A "$T/tmp")" ] || fail "temporary files left behind: $(ls -A "$T/tmp")"
"$PIC" --db "$T/j" --workspace "$T/ws" check > /dev/null || fail "integrity"