#!/bin/sh
# /share to the artifact publisher, on a pseudo-terminal against a fake publisher (fake_server; the
# real one and a real token are never used): the overlay names the destination and sends nothing
# before the yes, the request is a POST of the page with the token as a bearer, a large page goes up
# as a blob first, a refused token is explained without the token, and auto falls back to the
# gist unless a token is stored and artifactsUrl is set.
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}
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-share.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/home" "$T/bin"
export HOME="$T/home" ANTHROPIC_API_KEY=pic-share-test-key PIC_MODELS_FILE="$T/models.json" PIC_MODEL_CATALOG=offline
export T PIC_NO_NATIVE_CLIPBOARD=1
unset PIC_TEST_PASSPHRASE HTML_PUBLISHER_TOKEN HTML_PUBLISHER_URL
TOKEN="pubtok-$$-7c1e9"
cat > "$T/bin/gh" <<'GH'
#!/bin/sh
printf '%s\n' "$*" >> "$GH_LOG.args"
cat > "$GH_LOG.stdin"
echo https://gist.example/secret123
GH
chmod +x "$T/bin/gh"
export GH_LOG="$T/gh" PATH="$T/bin:$PATH"
printf '{"id":"abc123","tip":"t","viewerUrl":"https://pub.example/v/abc123"}' > "$T/pub.json"
printf '{"error":"Unauthorized"}' > "$T/denied.json"
serve() { # SPEC...: a fresh fake publisher, its port in $PORT, requests in $T/log
[ -n "$spid" ] && kill "$spid" 2>/dev/null && wait "$spid" 2>/dev/null || :
rm -rf "$T/log" "$T/port"
mkdir "$T/log"
"$SERVER" "$T/port" "$T/log" 120 "$@" &
spid=$!
i=0
while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 200 ] || fail "server"; sleep 0.05; done
PORT=$(cat "$T/port")
}
serve "status:201:$T/pub.json"
printf '{"providers":{"fake":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$ANTHROPIC_API_KEY","models":[{"id":"m","contextWindow":200000}]}}}' \
"$PORT" > "$T/models.json"
pic() { "$PIC" --db "$T/j" --workspace "$T/ws" "$@"; }
drive() { # NAME: run pic's terminal with the script on stdin
name=$1
shift
"$DRIVE" "$T/$name.tx" 100 24 "$PIC" --tui-mode regular --db "$T/j$name" --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)"
}
cat > "$T/pi.jsonl" <<'JSONL'
{"type":"session","version":3,"id":"abc","timestamp":"2026-01-01T00:00:00.000Z","cwd":"/x"}
{"type":"message","id":"m1","parentId":null,"message":{"role":"user","content":"What is 2 + 2?"}}
{"type":"message","id":"m2","parentId":"m1","message":{"role":"assistant","content":[{"type":"text","text":"It is 4, said the answer."}]}}
JSONL
# a page over 256 KB: one answer of about 490 KB
awk 'BEGIN {
s = "0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 "
for (i = 0; i < 12; i++) s = s s
printf "{\"type\":\"session\",\"version\":3,\"id\":\"big\",\"timestamp\":\"2026-01-01T00:00:00.000Z\",\"cwd\":\"/x\"}\n"
printf "{\"type\":\"message\",\"id\":\"m1\",\"parentId\":null,\"message\":{\"role\":\"user\",\"content\":\"hi\"}}\n"
printf "{\"type\":\"message\",\"id\":\"m2\",\"parentId\":\"m1\",\"message\":{\"role\":\"assistant\",\"content\":[{\"type\":\"text\",\"text\":\"%s\"}]}}\n", s
}' > "$T/big.jsonl"
body() { # FILE: the index.html a request carried, decoded
sed -n 's/.*"index.html":"\([^"]*\)".*/\1/p' "$1" | { base64 -d 2> /dev/null || base64 -D; }
}
# The token is added the way a user adds it.
printf '%s\n' "$TOKEN" | "$PIC" login artifacts > "$T/login.out" 2>&1 || { cat "$T/login.out" >&2; fail "login artifacts"; }
! grep -qF "$TOKEN" "$T/login.out" || fail "login printed the token"
pic settings artifactsUrl "http://127.0.0.1:$PORT" > /dev/null
# 1. Auto with a token and an address: the overlay names the publisher; no sends nothing.
drive no <<EOF
expect pic fake/m
send /import $T/pi.jsonl\r
expect imported
send /share\r
expect Share this conversation to the artifact publisher
expect leaves this machine for the artifact publisher
wait 200
send n
expect not shared
send \x04\x04
EOF
[ ! -e "$T/log/1.req" ] || fail "no: a request went out before the yes"
[ ! -e "$T/gh.args" ] || fail "no: gh ran"
printf 'PASS: /share names the artifact publisher and sends nothing without a yes\n'
# 2. Yes: one POST of the page, the token as a bearer.
drive yes <<EOF
expect pic fake/m
send /import $T/pi.jsonl\r
expect imported
send /share\r
expect Share this conversation to the artifact publisher
wait 200
send y
expect shared to the artifact publisher
expect https://pub.example/v/abc123
send \x04\x04
EOF
[ -s "$T/log/1.req" ] || fail "yes: no request"
[ ! -e "$T/log/2.req" ] || fail "yes: more than one request"
head -1 "$T/log/1.req" | grep -q '^POST /api/artifacts HTTP' || fail "yes: $(head -1 "$T/log/1.req")"
grep -qi "^authorization: Bearer $TOKEN" "$T/log/1.req" || fail "yes: no bearer token"
body "$T/log/1.req" | grep -q 'It is 4, said the answer' || fail "yes: the page lacks the answer"
body "$T/log/1.req" | grep -qi '<!doctype html>' || fail "yes: not an HTML page"
[ ! -e "$T/gh.args" ] || fail "yes: gh ran"
! grep -qF "$TOKEN" "$T/yes.tx" || fail "yes: the token reached the screen"
printf 'PASS: /share publishes the page with the stored token, and not through gh\n'
# 3. A page over 256 KB goes up as a blob, then its pointer is committed.
serve "status:200:-" "status:201:$T/pub.json"
pic settings artifactsUrl "http://127.0.0.1:$PORT" > /dev/null
drive blob <<EOF
expect pic fake/m
send /import $T/big.jsonl\r
expect imported
send /share\r
expect Share this conversation to the artifact publisher
wait 200
send y
expect shared to the artifact publisher
send \x04\x04
EOF
head -1 "$T/log/1.req" | grep -q '^PUT /api/blobs/[0-9a-f]\{64\} HTTP' || fail "blob: $(head -1 "$T/log/1.req")"
head -1 "$T/log/2.req" | grep -q '^POST /api/artifacts HTTP' || fail "blob: second was $(head -1 "$T/log/2.req")"
body "$T/log/2.req" | grep -q '^artifact-blob 1' || fail "blob: no pointer"
sha=$(head -1 "$T/log/1.req" | sed 's/.*blobs\/\([0-9a-f]*\) .*/\1/')
body "$T/log/2.req" | grep -q "^sha256 $sha" || fail "blob: the pointer names another blob"
printf 'PASS: a page over 256 KB goes up as a blob and the artifact holds its pointer\n'
# 4. A refused token is explained, without the token.
serve "status:401:$T/denied.json"
pic settings artifactsUrl "http://127.0.0.1:$PORT" > /dev/null
drive denied <<EOF
expect pic fake/m
send /import $T/pi.jsonl\r
expect imported
send /share\r
expect Share this conversation to the artifact publisher
wait 200
send y
expect refused the token
send \x04\x04
EOF
! grep -qF "$TOKEN" "$T/denied.tx" || fail "denied: the token reached the screen"
printf 'PASS: a refused token is said so and never shown\n'
# 5. Auto without an address, and gist chosen with a token: the gist, as before.
pic settings --unset artifactsUrl > /dev/null
drive nourl <<EOF
expect pic fake/m
send /import $T/pi.jsonl\r
expect imported
send /share\r
expect Share this conversation as a secret GitHub gist
send n
expect not shared
send \x04\x04
EOF
pic settings artifactsUrl "http://127.0.0.1:$PORT" > /dev/null
pic settings sessionShareDestination gist > /dev/null
drive gist <<EOF
expect pic fake/m
send /import $T/pi.jsonl\r
expect imported
send /share\r
expect Share this conversation as a secret GitHub gist
wait 200
send y
expect shared as a secret gist
send \x04\x04
EOF
[ "$(cat "$T/gh.args")" = "gist create -" ] || fail "gist: gh got $(cat "$T/gh.args" 2> /dev/null)"
printf 'PASS: auto needs a token and an address; gist keeps the gist\n'
# 6. The variable counts as a token for auto: with none stored, the publisher is still named.
pic settings --unset sessionShareDestination > /dev/null
pic logout artifacts > /dev/null
(
export HTML_PUBLISHER_TOKEN="envtok-$$"
drive envtok <<EOF
expect pic fake/m
send /import $T/pi.jsonl\r
expect imported
send /share\r
expect Share this conversation to the artifact publisher
send n
expect not shared
send \x04\x04
EOF
)
printf 'PASS: HTML_PUBLISHER_TOKEN is enough for auto to choose the publisher\n'
# 7. The command /share runs: where it goes, and a page on stdin.
serve "status:201:$T/pub.json"
pic settings artifactsUrl "http://127.0.0.1:$PORT" > /dev/null
[ "$(pic share --where)" = "gist" ] || fail "share --where: $(pic share --where)"
printf '%s\n' "$TOKEN" | "$PIC" login artifacts > /dev/null 2>&1
[ "$(pic share --where)" = "artifacts http://127.0.0.1:$PORT" ] || fail "share --where with a token: $(pic share --where)"
[ "$(printf '<!doctype html><p>cli page</p>' | pic share)" = "https://pub.example/v/abc123" ] || fail "share: no link"
body "$T/log/1.req" | grep -q 'cli page' || fail "share: the page was not sent"
pic settings artifactsUrl "http://127.0.0.1:1" > /dev/null
if printf '<!doctype html>' | pic share > "$T/out" 2> "$T/err"; then fail "share: an unreachable publisher succeeded"; fi
grep -q 'could not reach the publisher' "$T/err" || fail "share: $(cat "$T/err")"
! grep -qF "$TOKEN" "$T/err" || fail "share: the token reached stderr"
printf 'PASS: pic share says where /share goes, publishes stdin and reports a publisher it cannot reach\n'