#!/bin/sh
# Who the user is stays out of what the model is sent: the system prompt (src/context.c), the
# tool list and the messages hold none of the user's email, login name, real name or hostname,
# though every one of them is set in the environment, in git's config and in the account.
# Usage: test/pic/identity.sh [--no-faults]
set -eu
PIC=${PIC:?set PIC to the pic binary}
SERVER=${SERVER:?set SERVER to the fake_server binary}
S=$(cd "$(dirname "$0")/streams" && pwd)
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-identity.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; }
[email protected]
LOGIN=zqlogin7431
REAL='Zebediah Quux7431'
HOST_NAME=$(hostname 2> /dev/null || :)
SHORT=${HOST_NAME%%.*}
ACCOUNT=$(id -un 2> /dev/null || :) # the account running the test, as the system knows it
export HOME="$T/home" ANTHROPIC_API_KEY=pic-identity-test-key PIC_MODEL_CATALOG=offline
export USER=$LOGIN LOGNAME=$LOGIN EMAIL=$EMAIL GIT_AUTHOR_NAME=$REAL GIT_AUTHOR_EMAIL=$EMAIL \
GIT_COMMITTER_NAME=$REAL GIT_COMMITTER_EMAIL=$EMAIL
mkdir -p "$HOME"
printf '[user]\n\tname = %s\n\temail = %s\n' "$REAL" "$EMAIL" > "$HOME/.gitconfig"
mkdir "$T/ws" "$T/log"
"$SERVER" "$T/port" "$T/log" 60 "sse:$S/text.sse" &
spid=$!
i=0
while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server did not start"; sleep 0.05; done
printf '{"providers":{"va":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$ANTHROPIC_API_KEY","models":[{"id":"m"}]}}}' \
"$(cat "$T/port")" > "$T/models.json"
export PIC_MODELS_FILE="$T/models.json"
"$PIC" --db "$T/j.sqlite" --workspace "$T/ws" --model va/m -p 'hello' < /dev/null > "$T/out" 2> "$T/err" ||
fail "-p: $(cat "$T/err")"
req="$T/log/1.req"
[ -s "$req" ] || fail "no request reached the server"
grep -q 'You are a coding assistant running inside pic' "$req" || fail "the request has no system prompt"
absent() { # WHAT VALUE: the request does not hold VALUE (skipped when too short to mean anything)
[ "${#2}" -ge 4 ] || return 0
! grep -qF -- "$2" "$req" || fail "the request holds the $1 ($2)"
}
absent "email" "$EMAIL"
absent "login name" "$LOGIN"
absent "real name" "$REAL"
absent "hostname" "$HOST_NAME"
absent "short hostname" "$SHORT"
absent "account name" "$ACCOUNT"
printf 'PASS: the request holds no email, login name, real name or hostname\n'