Skip to content
pi-c Artifact [bb45b9d016]
Login

Artifact [bb45b9d016]

Artifact bb45b9d01699f7062c882a5adccdde55267ac6c8c52d40bb797e4ba6d33b932d:


#!/bin/sh
# The web_search tool, against the fake server (no network): Brave's web search endpoint as
# results text, the key sealed in the credential store or taken from BRAVE_API_KEY and sent only
# as the token header, count and size caps, control characters dropped, errors that name the cause
# and never the key, and offline. PIC_TEST_WEB_SEARCH_URL (the endpoint, dev builds only) points
# the tool at the fake server; --no-faults (the release build) checks the real endpoint is the
# only one and that a missing key is explained.
set -eu
PIC=${PIC:?set PIC to the pic binary}
SERVER=${SERVER:?set SERVER to the fake_server binary}
FAULTS=yes
[ "${1:-}" = --no-faults ] && FAULTS=no
D=$(cd "$(dirname "$0")/web" && pwd)
T=$(cd "$(mktemp -d "${TMPDIR:-/tmp}/pic-websearch.XXXXXX")" && pwd -P)
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; }
for v in $(env | sed -nE 's/^([A-Za-z0-9_]*_(API_KEY|TOKEN))=.*/\1/p'); do unset "$v"; done
export HOME="$T/home" PIC_PI_EXPORT=0 PIC_MODEL_CATALOG=offline
mkdir -p "$HOME/.pic" "$T/ws"
"$PIC" settings loops.tool false > /dev/null
KEY="BSA-test-key-$$-9f3"

n=0
fresh() { n=$((n + 1)); DB="$T/$n.sqlite"; WS="$T/ws$n"; mkdir "$WS"; }
pic() { "$PIC" --db "$DB" --workspace "$WS" "$@"; }
serve() {
    [ -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 100 ] || fail "server"; sleep 0.05; done
    PORT=$(cat "$T/port")
}
# search INPUT_JSON: a fresh conversation whose one call is web_search; its result in $T/out.
search() {
    fresh
    printf 'tool raw web_search %s\ntext done\n' "$1" > "$T/script"
    pic submit job search > /dev/null
    env ${SENV:-} PIC_TEST_WEB_SEARCH_URL="http://127.0.0.1:${PORT:-1}/res/v1/web/search" "$PIC" --db "$DB" --workspace "$WS" run --script "$T/script" > /dev/null 2>&1 || :
    pic dump | awk '/^entry [0-9]+ [0-9]+ tool_result/ { on = 1; sub(/^[^|]*\| ?/, ""); print; next }
        /^(entry|effect|generation|submission) / { on = 0 } on { print }' > "$T/out"
}
has() { grep -qF -- "$1" "$T/out" || { cat "$T/out" >&2; fail "$2: no '$1'"; }; }
lacks() { ! grep -qF -- "$1" "$T/out" || { cat "$T/out" >&2; fail "$2: has '$1'"; }; }

if [ "$FAULTS" = no ]; then
    PORT=
    search '{"query":"x"}'
    has "pic login brave-search" "release: no key"
    printf 'PASS: release builds explain a missing key\n'
    exit 0
fi

# No key anywhere: the tool says how to add one, and no request is made.
serve "head:200:$D/brave.json:Content-Type: application/json"
search '{"query":"c language"}'
has "no Brave Search API key" "no key"
has "pic login brave-search" "no key"
has "BRAVE_API_KEY" "no key"
has "https://brave.com/search/api/" "no key"
[ ! -e "$T/log/1.req" ] || fail "no key: the server was reached"
printf 'PASS: a missing key is explained\n'

# A key from the environment: results as numbered title, URL and snippet; the token goes in the
# header and nowhere else.
serve "head:200:$D/brave.json:Content-Type: application/json"
SENV="BRAVE_API_KEY=$KEY" search '{"query":"c language & more","count":3}'
has "1. The C Programming Language & more" env
has "   https://example.org/c" env
has "   A book about C — classic." env
has "2. ISO C" env
has "   https://example.net/iso?a=1&b=2" env
has "3. Third" env
lacks "Fourth" "count"
lacks "<strong>" env
! LC_ALL=C grep -q "$(printf '[\001-\010\013-\037\177]')" "$T/out" || fail "a control byte reached the output"
! LC_ALL=C grep -q "$(printf '\302[\200-\237]')" "$T/out" || fail "a C1 control reached the output"
lacks "$KEY" "key in the result"
grep -q '^GET /res/v1/web/search?q=c%20language%20%26%20more&count=3 HTTP/1.1' "$T/log/1.req" || fail "request line: $(head -1 "$T/log/1.req")"
grep -qi "^X-Subscription-Token: $KEY" "$T/log/1.req" || fail "the token header was not sent"
grep -qi "^Accept: application/json" "$T/log/1.req" || fail "no Accept header"
! pic dump | grep -qF "$KEY" || fail "the key is journaled"
printf 'PASS: results come back as text, the key only in the header\n'

# The default count is 5 and a count over 10 is 10; the results asked for are the ones shown.
serve "head:200:$D/brave.json:Content-Type: application/json"
SENV="BRAVE_API_KEY=$KEY" search '{"query":"x"}'
grep -q 'count=5 ' "$T/log/1.req" || fail "default count"
has "4. Fourth" default
serve "head:200:$D/brave.json:Content-Type: application/json"
SENV="BRAVE_API_KEY=$KEY" search '{"query":"x","count":99}'
grep -q 'count=10 ' "$T/log/1.req" || fail "count clamp"
printf 'PASS: the result count is bounded\n'

# A key sealed in the credential store by pic login brave-search.
export PIC_TEST_PASSPHRASE="pic-ws-passphrase-$$"
printf '%s\n' "$KEY" | "$PIC" login brave-search > "$T/login.out" 2>&1 || { cat "$T/login.out" >&2; fail "login brave-search"; }
! grep -qF "$KEY" "$T/login.out" || fail "login printed the key"
serve "head:200:$D/brave.json:Content-Type: application/json"
search '{"query":"sealed"}'
has "1. The C Programming Language & more" sealed
grep -qi "^X-Subscription-Token: $KEY" "$T/log/1.req" || fail "the sealed key was not sent"
"$PIC" logout brave-search > /dev/null 2>&1 || "$PIC" auth logout brave-search > /dev/null 2>&1 || fail "logout"
printf 'PASS: a key sealed by pic login brave-search is used\n'
unset PIC_TEST_PASSPHRASE

# Errors name the cause and never the key.
for c in "401|key" "403|key" "429|rate" "500|HTTP 500"; do
    code=${c%%|*} want=${c#*|}
    printf '{"error":{"detail":"echo %s"}}' "$KEY" > "$T/err.json"
    serve "status:$code:$T/err.json"
    SENV="BRAVE_API_KEY=$KEY" search '{"query":"x"}'
    has "web_search:" "http $code"
    has "HTTP $code" "http $code"
    has "$want" "http $code"
    lacks "$KEY" "http $code"
done
printf 'PASS: failures are explained without the key\n'

# Bad bodies: not JSON, no results, too large.
printf 'not json at all' > "$T/bad.json"
serve "head:200:$T/bad.json:Content-Type: application/json"
SENV="BRAVE_API_KEY=$KEY" search '{"query":"x"}'
has "not understood" "not json"
printf '{"web":{"results":[]}}' > "$T/none.json"
serve "head:200:$T/none.json:Content-Type: application/json"
SENV="BRAVE_API_KEY=$KEY" search '{"query":"nothing"}'
has "no results" "empty"
printf '{"x":"%s"}' "$(head -c 3000000 /dev/zero | tr '\0' a)" > "$T/huge.json"
serve "head:200:$T/huge.json:Content-Type: application/json"
SENV="BRAVE_API_KEY=$KEY" search '{"query":"x"}'
has "too large" "huge"
printf 'PASS: odd replies are explained\n'

# A long snippet is cut.
printf '{"web":{"results":[{"title":"T","url":"https://e.org/","description":"%s"}]}}' "$(head -c 5000 /dev/zero | tr '\0' s)" > "$T/long.json"
serve "head:200:$T/long.json:Content-Type: application/json"
SENV="BRAVE_API_KEY=$KEY" search '{"query":"x"}'
[ "$(wc -c < "$T/out")" -lt 1500 ] || fail "snippet not cut: $(wc -c < "$T/out") bytes"
printf 'PASS: snippets are cut\n'

# Bad input.
for inp in '{}' '{"query":""}' '{"query":5}' '{"query":"x","count":0}' '{"query":"x","count":"a"}'; do
    serve "head:200:$D/brave.json:Content-Type: application/json"
    SENV="BRAVE_API_KEY=$KEY" search "$inp"
    has "web_search:" "bad input $inp"
    [ ! -e "$T/log/1.req" ] || fail "bad input $inp: the server was reached"
done
long=$(head -c 500 /dev/zero | tr '\0' q)
serve "head:200:$D/brave.json:Content-Type: application/json"
SENV="BRAVE_API_KEY=$KEY" search "{\"query\":\"$long\"}"
has "too long" "long query"
printf 'PASS: bad input is refused\n'

# Offline refuses, and nothing is sent.
serve "head:200:$D/brave.json:Content-Type: application/json"
"$PIC" settings offline true > /dev/null
SENV="BRAVE_API_KEY=$KEY" search '{"query":"x"}'
has "offline" offline
[ ! -e "$T/log/1.req" ] || fail "offline: the server was reached"
"$PIC" settings --unset offline > /dev/null
printf 'PASS: offline refuses\n'

# A permission mode counts it as a change: read-only refuses it and nothing is sent.
serve "head:200:$D/brave.json:Content-Type: application/json"
fresh
printf 'tool raw web_search {"query":"x"}\ntext done\n' > "$T/script"
pic --permission-mode read-only submit job ro > /dev/null
SENV="BRAVE_API_KEY=$KEY"
env BRAVE_API_KEY="$KEY" PIC_TEST_WEB_SEARCH_URL="http://127.0.0.1:$PORT/res/v1/web/search" "$PIC" --db "$DB" --workspace "$WS" --permission-mode read-only run --script "$T/script" > /dev/null 2>&1 || :
pic dump | grep -q 'read-only.*web_search was not run' || fail "read-only did not refuse: $(pic dump)"
[ ! -e "$T/log/1.req" ] || fail "read-only: the server was reached"
printf 'PASS: read-only refuses web_search\n'

# On by default, replay-safe, left out by the flags.
SQL=$(dirname "$SERVER")/sqlite3
fresh
pic submit job x > /dev/null
printf 'text done\n' > "$T/script"
pic run --script "$T/script" > /dev/null
"$SQL" "$DB" "SELECT group_concat(json_extract(value,'\$.name'),' ') FROM pic_session, json_each(tools)" | grep -qw web_search || fail "not declared by default"
fresh
pic --exclude-tools web_search submit job x > /dev/null
pic --exclude-tools web_search run --script "$T/script" > /dev/null
! "$SQL" "$DB" "SELECT group_concat(json_extract(value,'\$.name'),' ') FROM pic_session, json_each(tools)" | grep -qw web_search || fail "--exclude-tools web_search still declared it"
serve "head:200:$D/brave.json:Content-Type: application/json"
SENV="BRAVE_API_KEY=$KEY" search '{"query":"x"}'
[ "$("$SQL" "$DB" "SELECT replay_safe FROM pic_effect")" = 1 ] || fail "not replay-safe"
printf 'PASS: on by default, replay-safe, chosen by the flags\n'