#!/bin/sh
# Shared harness helpers for test/pic/remote_*.sh shards.
# Sourced after PIC, SERVER, RELAY, PHONE, SQLITE3, FAULTS, S, T and HOME are set.
: "${PIC:?set PIC to the pic binary}"
: "${SERVER:?set SERVER to the fake_server binary}"
: "${RELAY:?set RELAY to the fake_relay binary}"
: "${PHONE:?set PHONE to the fake_phone binary}"
: "${T:?set T to the temp dir}"
export HOME="${HOME:-$T/home}"
mkdir -p "$HOME/.pic"
unset PIC_KEY_FD OPENROUTER_API_KEY
export ANTHROPIC_API_KEY=pic-remote-test-key PIC_PI_EXPORT=0 PIC_MODEL_CATALOG=offline
export PIC_NO_NATIVE_CLIPBOARD=1
TRIES=2400
export PIC_TEST_SLOW=1
pids=
cleanup() {
exec 2> /dev/null
"$PIC" remote stop > /dev/null 2>&1 || :
for p in $pids; do kill -9 "$p" 2>/dev/null || true; done
rm -rf -- "$T"
}
trap cleanup EXIT HUP INT TERM
export PIC_TEST_RC_DEADLINE_S=1800
fail() {
printf 'FAIL: %s\n' "$*" >&2
for f in "$T"/*.out "$T"/*.err "$T"/*.run "$HOME/.pic/remote.log"; do [ -s "$f" ] && { printf -- '--- %s\n' "$f" >&2; tail -20 "$f" >&2; }; done
exit 1
}
bg() { pids="$pids $1"; }
await() { # FILE PATTERN [TRIES]: wait for the fixed string PATTERN in FILE (50 ms a try, 120 s by default)
i=0
until grep -qF -- "$2" "$1" 2>/dev/null; do
i=$((i + 1))
[ "$i" -lt "${3:-$TRIES}" ] || fail "timed out waiting for $2 in $1"
sleep 0.05
done
}
gone() { # PID: wait for it to exit; its status in rc
rc=0
wait "$1" || rc=$?
}
uq() { "$SQLITE3" -cmd ".timeout 10000" "$HOME/.pic/pic.sqlite" "$1"; }
if [ "$FAULTS" = 0 ]; then
# The release build: pic remote with no store yet says to pair first, and pairs nothing. The
# rest needs PIC_TEST_PASSPHRASE, a dev build's seam.
if "$PIC" remote < /dev/null > "$T/notty.out" 2> "$T/notty.err"; then fail "release: pic remote ran with no terminal"; fi
[ -s "$T/notty.err" ] || fail "release: no message"
printf 'PASS: release: pic remote without a terminal exits with a message\n'
exit 0
fi
export PIC_TEST_PASSPHRASE=remote-test-pass
export PIC_ORIGIN=person PIC_TEST_RC_TEMP="$T/tmp"
export PIC_TEST_RC_PUSH_HTTP=1 PIC_TEST_RC_PUSH_GAP_MS=0
start_relay() {
"$RELAY" "$T/relay.port" 3600 test-invite > "$T/relay.out" 2> "$T/relay.err" &
bg $!
i=0
while [ ! -s "$T/relay.port" ]; do
i=$((i + 1))
[ "$i" -lt $TRIES ] || fail "relay did not start"
sleep 0.05
done
BASE="http://127.0.0.1:$(cat "$T/relay.port")"
"$PIC" settings remote.relay "$BASE" > /dev/null
}
state() { curl -s -X POST "$BASE/_test/state"; }
ctl() { curl -s -X POST "$BASE/_test/$1" > /dev/null; }
np=0
offer() { # start pic remote pair --pipe; its output in $PO, its stdin the fifo $PI (fd 3)
np=$((np + 1))
PO="$T/pair$np.out"
PI="$T/pair$np.in"
mkfifo "$PI"
"$PIC" remote pair --pipe < "$PI" > "$PO" 2> "$T/pair$np.err" &
PP=$!
bg $PP
exec 3> "$PI"
await "$PO" '"type":"offer"'
URL=$(sed -n 's/.*"url":"\([^"]*\)".*/\1/p' "$PO" | head -1)
[ -n "$URL" ] || fail "offer: no address in $(cat "$PO")"
}
phone_pair() { # DIR NAME [ARGS]: the phone pairs in the background; its output in DIR.out
mkdir -p "$1"
"$PHONE" "$1" pair "$URL" "$2" ${3:-} > "$1.out" 2>&1 &
PH=$!
bg $PH
}
term_pair() { # DIR NAME: pair at the terminal, with a yes
offer
phone_pair "$1" "$2"
await "$PO" '"type":"code"'
echo y >&3
gone $PH
[ "$rc" = 0 ] || fail "pair $2: phone exit $rc: $(cat "$1.out")"
gone $PP
exec 3>&-
}
n=0
spid=
serve() { # SPEC...: a fresh scripted model, journal and workspace
[ -n "$spid" ] && kill "$spid" 2>/dev/null || :
n=$((n + 1))
mkdir -p "$T/log$n" "$T/ws$n"
DB="$T/j$n.sqlite"
WS="$T/ws$n"
LOG="$T/log$n"
"$SERVER" "$T/sport$n" "$LOG" 3600 "$@" &
spid=$!
bg $spid
i=0
while [ ! -s "$T/sport$n" ]; do
i=$((i + 1))
[ "$i" -lt $TRIES ] || fail "model server did not start"
sleep 0.05
done
printf '{"providers":{"fake":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$ANTHROPIC_API_KEY","models":[{"id":"m"}]}}}' \
"$(cat "$T/sport$n")" > "$T/models$n.json"
export PIC_MODELS_FILE="$T/models$n.json"
}
pic() { "$PIC" --db "$DB" --workspace "$WS" --model fake/m "$@"; }
q() { "$SQLITE3" -cmd ".timeout 10000" "$DB" "$1"; }
until_sql() { # SQL VALUE: wait up to 120 s for the query to print VALUE
i=0
until [ "$(q "$1")" = "$2" ]; do
i=$((i + 1))
[ "$i" -lt $TRIES ] || fail "timed out waiting for [$1] = $2: $(q "$1")"
sleep 0.05
done
}
nr=0
remote() { # start pic remote; its output in $RO
nr=$((nr + 1))
RO="$T/remote$nr.out"
"$PIC" remote > "$RO" 2> "$T/remote$nr.err" &
RP=$!
bg $RP
await "$RO" 'pic remote: connected'
}
script() { # DIR: run the phone of DIR on the script on stdin, in the background; output in DIR.run
cat > "$1.script"
rm -f "$1"/go* "$1"/at*
"$PHONE" "$1" run < "$1.script" > "$1.run" 2>&1 &
PR=$!
bg $PR
}
done_ok() { # PID WHAT: the phone's script ran to its end
gone "$1"
[ "$rc" = 0 ] || fail "$2: phone exit $rc"
}
hold() { # a request in SESS whose bash call waits for $WS/release
rm -f "$WS/started" "$WS/release"
pic ask --session "$SID" 'hold on' > "$T/hold.out" 2> "$T/hold.err" &
HP=$!
bg $HP
i=0
until [ -e "$WS/started" ]; do i=$((i + 1)); [ "$i" -lt $TRIES ] || fail "hold: did not start: $(cat "$T/hold.err")"; sleep 0.05; done
}
init_paired() { # [COUNT]: start relay, set invite, pair Phone One ($T/ph1) and optionally Phone Away ($T/ph4)
start_relay
"$PIC" settings remote.invite test-invite > /dev/null
term_pair "$T/ph1" "Phone One"
if [ "${1:-1}" -ge 2 ]; then
term_pair "$T/ph4" "Phone Away"
fi
DEV1=$(uq "SELECT lower(hex(id)) FROM picu_remote_device WHERE paired_ms = (SELECT min(paired_ms) FROM picu_remote_device)")
DEVJ=$("$PIC" remote devices --json)
DEV1B=$("$SQLITE3" :memory: "SELECT substr(json_extract(value, '\$.origin'), 8) FROM json_each(json_extract('$DEVJ', '\$.devices'))
WHERE json_extract(value, '\$.id') = '$DEV1'")
[ -n "$DEV1B" ] || fail "devices --json: no origin for $DEV1: $DEVJ"
}