#!/bin/sh
# Saved model endpoints against a fake OpenAI-compatible server: pic endpoints add saves a URL
# and fetches GET {URL}/models; pic models (and RPC's get_available_models) list those models
# as NAME/MODEL, marked as the endpoint's; refresh fetches again, and a failed refresh keeps the
# list and says why; a request for NAME/MODEL goes to the URL's chat completions (with the
# endpoint's key, sealed under its name); the default model and the built-in providers are
# left alone; remove forgets the endpoint and its key; names and URLs are checked; the RPC
# commands do the same. --no-faults (the release build): the key cases, which feed the
# passphrase through PIC_TEST_PASSPHRASE (dev builds only), are skipped.
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}
FAULTS=yes
[ "${1:-}" = --no-faults ] && FAULTS=no
C=$(cd "$(dirname "$0")/chat-streams" && pwd)
A=$(cd "$(dirname "$0")/streams" && pwd)
R=$(cd "$(dirname "$0")/responses-streams" && pwd)
SQLITE3=${SQLITE3:-sqlite3}
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-endpoints.XXXXXX")
T=$(cd "$T" && pwd -P)
spid=
stop() { [ -n "$spid" ] && kill "$spid" 2>/dev/null && wait "$spid" 2>/dev/null || :; spid=; }
cleanup() { stop; 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
unset PIC_TEST_PASSPHRASE PIC_MODELS_FILE
mkdir "$T/home" "$T/ws"
export HOME="$T/home" PIC_MODEL_CATALOG=offline PIC_PI_EXPORT=0 PIC_NO_KEY_CHECKS=1
pic() { "$PIC" --db "$T/j" --workspace "$T/ws" "$@"; }
# serve NAME SPEC...: a fresh fake server, its requests in $T/NAME/k.req, its port in $port
serve() {
stop
name=$1
shift
mkdir "$T/$name"
"$SERVER" "$T/$name.port" "$T/$name" 60 "$@" &
spid=$!
i=0
while [ ! -s "$T/$name.port" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server $name"; sleep 0.05; done
port=$(cat "$T/$name.port")
}
printf '{"object":"list","data":[{"id":"gpt-x","object":"model","owned_by":"openai"},{"id":"claude-y","object":"model"}]}' > "$T/list1.json"
printf '{"object":"list","data":[{"id":"gpt-x","owned_by":"openai"},{"id":"claude-y"},{"id":"gemini-z","owned_by":"google"}]}' > "$T/list2.json"
printf '{"data":[{"type":"model","id":"claude-sonnet-x","display_name":"Claude Sonnet X","created_at":"2026-01-01T00:00:00Z"}],"has_more":false}' > "$T/anth-list.json"
printf '{"error":{"message":"upstream is down","type":"server_error"}}' > "$T/down.json"
# Names, URLs and CLI flags are checked before anything is saved or fetched.
pic endpoints add openai http://127.0.0.1:1/v1 2> "$T/e1" && fail "a built-in provider's name was taken"
grep -q 'openai is a provider pic already has' "$T/e1" || fail "collision: $(cat "$T/e1")"
pic endpoints add 'a/b' http://127.0.0.1:1/v1 2> /dev/null && fail "a name with a slash was saved"
pic endpoints add proxy ftp://127.0.0.1/v1 2> /dev/null && fail "an ftp URL was saved"
pic endpoints add proxy http://example.com/v1 2> "$T/e2" && fail "plain http to another host was saved"
grep -q 'only to this machine' "$T/e2" || fail "http check: $(cat "$T/e2")"
pic endpoints add proxy 'https://u:[email protected]/v1' 2> /dev/null && fail "a URL with a user was saved"
pic endpoints add proxy http://127.0.0.1:1/v1 --api bogus 2> "$T/e3" && fail "an unknown --api was accepted"
grep -q 'openai-completions' "$T/e3" && grep -q 'anthropic-messages' "$T/e3" && grep -q 'openai-responses' "$T/e3" ||
fail "unknown --api error: $(cat "$T/e3")"
pic endpoints add proxy http://127.0.0.1:1/v1 --api '' 2> /dev/null && fail "an empty --api was accepted"
pic endpoints add proxy http://127.0.0.1:1/v1 --api 2> /dev/null && fail "a missing --api value was accepted"
pic endpoints add proxy http://127.0.0.1:1/v1 --api openai-completions --api anthropic-messages 2> /dev/null && fail "duplicate --api was accepted"
pic endpoints add proxy http://127.0.0.1:1/v1 --key --basic 2> /dev/null && fail "conflicting --key --basic was accepted"
pic endpoints add proxy http://127.0.0.1:1/v1 --basic --key 2> /dev/null && fail "conflicting --basic --key was accepted"
pic endpoints add proxy http://127.0.0.1:1/v1 --key --key 2> /dev/null && fail "duplicate --key was accepted"
pic endpoints add proxy http://127.0.0.1:1/v1 --basic --basic 2> /dev/null && fail "duplicate --basic was accepted"
pic endpoints add proxy http://127.0.0.1:1/v1 --api anthropic-messages --key sk-leaked-secret 2> "$T/e4" && fail "--api then --key VALUE accepted"
grep -q -- '--key takes no value' "$T/e4" || fail "--api then --key value: $(cat "$T/e4")"
grep -q 'sk-leaked-secret' "$T/e4" && fail "secret echoed after --api --key"
pic endpoints list | grep -q 'No saved endpoints' || fail "a refused add saved something: $(pic endpoints list)"
printf 'PASS: names, URLs, --api and flags are checked; a provider'"'"'s name is never taken\n'
# Add: saved, its models fetched once; the default model and the providers stay as they were.
pic settings defaultModel anthropic/claude-haiku-5-5
pic models --all > "$T/before"
serve add "status:200:$T/list1.json" "status:200:$T/list2.json" "status:500:$T/down.json" "sse:$C/text.sse"
pic endpoints add proxy "http://127.0.0.1:$port/v1/" > "$T/add.out" 2> "$T/add.err" || fail "add: $(cat "$T/add.err")"
grep -q "Saved the endpoint proxy (http://127.0.0.1:$port/v1)" "$T/add.out" || fail "add said: $(cat "$T/add.out")"
grep -q '2 models, in /model as proxy/MODEL' "$T/add.out" || fail "add count: $(cat "$T/add.out")"
head -1 "$T/add/1.req" | grep -q '^GET /v1/models HTTP/1.1' || fail "fetch: $(head -1 "$T/add/1.req")"
grep -qi '^authorization:' "$T/add/1.req" && fail "a key was sent with no key saved"
pic endpoints list > "$T/list"
grep -q "^proxy *http://127.0.0.1:$port/v1 2 models, fetched " "$T/list" || fail "list: $(cat "$T/list")"
pic models > "$T/models"
grep -q '^proxy/gpt-x *from proxy$' "$T/models" || fail "pic models: $(grep proxy "$T/models")"
grep -q '^proxy/claude-y ' "$T/models" || fail "pic models: claude-y"
pic models --json | grep -q '^{"model":"proxy/gpt-x","provider":"proxy","id":"gpt-x",.*"available":true,.*"endpoint":"proxy"}$' ||
fail "json: $(pic models --json | grep proxy/gpt-x)"
pic models --all | grep -v '^proxy/' > "$T/after"
cmp -s "$T/before" "$T/after" || fail "the providers' models changed: $(diff "$T/before" "$T/after" | head -5)"
[ "$(pic settings defaultModel)" = anthropic/claude-haiku-5-5 ] || fail "default after add: $(pic settings defaultModel)"
[ -z "$(pic settings lastModel || true)" ] || fail "lastModel set by add"
printf 'PASS: add saves the URL and lists its models as proxy/MODEL; nothing else changes\n'
# Refresh fetches again; a failed one keeps the list and says why.
pic endpoints refresh proxy > "$T/r1" 2>&1 || fail "refresh: $(cat "$T/r1")"
grep -q '^proxy: 3 models$' "$T/r1" || fail "refresh said: $(cat "$T/r1")"
pic models | grep -q '^proxy/gemini-z ' || fail "refresh: gemini-z not listed"
pic endpoints refresh > "$T/r2" 2>&1 && fail "a failed refresh succeeded"
grep -q "proxy: http://127.0.0.1:$port/v1/models answered HTTP 500: upstream is down (the list it had is kept)" "$T/r2" ||
fail "failed refresh said: $(cat "$T/r2")"
pic models | grep -q '^proxy/gemini-z ' || fail "failed refresh lost the list"
pic endpoints list > "$T/list"
grep -q '3 models, fetched' "$T/list" || fail "list after failure: $(cat "$T/list")"
grep -q 'the last refresh failed, so this list is older: .*HTTP 500: upstream is down' "$T/list" || fail "list error: $(cat "$T/list")"
printf 'PASS: refresh replaces the list; a failed refresh keeps it and says why\n'
# A request for proxy/gpt-x goes to the URL's chat completions, naming gpt-x; nothing is saved.
pic --model proxy/gpt-x ask 'hi there' > "$T/ask.out" 2> "$T/ask.err" || fail "ask: $(cat "$T/ask.err")"
req=$(grep -l '^POST ' "$T"/add/*.req | head -1)
[ -n "$req" ] || fail "no request reached the endpoint"
head -1 "$req" | grep -q '^POST /v1/chat/completions HTTP/1.1' || fail "request: $(head -1 "$req")"
grep -q '"model":"gpt-x"' "$req" || fail "request model: $(tail -1 "$req" | head -c 300)"
[ "$(pic settings defaultModel)" = anthropic/claude-haiku-5-5 ] || fail "default after ask: $(pic settings defaultModel)"
printf 'PASS: proxy/gpt-x is sent to the endpoint'"'"'s chat completions as gpt-x\n'
# RPC: list, add, refresh, remove; get_available_models marks the endpoint's models and exposes api.
serve rpc "status:200:$T/list1.json" "status:404:-" "status:200:$T/anth-list.json" "status:200:$T/anth-list.json"
cat > "$T/rpc.in" <<EOF
{"id":"1","type":"get_endpoints"}
{"id":"2","type":"add_endpoint","name":"second","url":"http://127.0.0.1:$port/v1"}
{"id":"3","type":"get_available_models"}
{"id":"4","type":"refresh_endpoints","name":"second"}
{"id":"5","type":"remove_endpoint","name":"second"}
{"id":"6","type":"remove_endpoint","name":"second"}
{"id":"7","type":"add_endpoint","name":"badapi","url":"http://127.0.0.1:$port/v1","api":"bogus"}
{"id":"8","type":"add_endpoint","name":"anthrpc","url":"http://127.0.0.1:$port/v1","api":"anthropic-messages"}
{"id":"9","type":"get_endpoints"}
{"id":"10","type":"refresh_endpoints","name":"anthrpc"}
{"id":"11","type":"get_available_models"}
{"id":"12","type":"remove_endpoint","name":"anthrpc"}
EOF
pic --mode rpc < "$T/rpc.in" > "$T/rpc.out" 2> "$T/rpc.err" || fail "rpc: $(cat "$T/rpc.err")"
grep '"id":"1"' "$T/rpc.out" | grep -q '"endpoints":\[{"name":"proxy","url":"http://127.0.0.1:[0-9]*/v1","api":"openai-completions","hasKey":false,"models":\[{"id":"gpt-x","owned_by":"openai"},{"id":"claude-y","owned_by":null},' ||
fail "get_endpoints: $(grep '"id":"1"' "$T/rpc.out")"
grep '"id":"2"' "$T/rpc.out" | grep -q '"success":true.*"data":{"name":"second","api":"openai-completions","count":2}' || fail "add_endpoint: $(grep '"id":"2"' "$T/rpc.out")"
grep '"id":"3"' "$T/rpc.out" | grep -q '"id":"gpt-x",[^}]*"provider":"second".*"endpoint":"second"' ||
fail "get_available_models: $(grep '"id":"3"' "$T/rpc.out" | head -c 600)"
grep '"id":"4"' "$T/rpc.out" | grep -q '"endpoints":\[{"name":"second","api":"openai-completions","count":0,"error":".*HTTP 404"}\]' ||
fail "refresh_endpoints: $(grep '"id":"4"' "$T/rpc.out")"
grep '"id":"5"' "$T/rpc.out" | grep -q '"data":{"removed":true}' || fail "remove_endpoint: $(grep '"id":"5"' "$T/rpc.out")"
grep '"id":"6"' "$T/rpc.out" | grep -q '"data":{"removed":false}' || fail "remove_endpoint again: $(grep '"id":"6"' "$T/rpc.out")"
grep '"id":"7"' "$T/rpc.out" | grep -q '"success":false' || fail "add_endpoint bad api: $(grep '"id":"7"' "$T/rpc.out")"
grep '"id":"8"' "$T/rpc.out" | grep -q '"success":true.*"data":{"name":"anthrpc","api":"anthropic-messages","count":1}' ||
fail "add_endpoint anthropic: $(grep '"id":"8"' "$T/rpc.out")"
grep '"id":"9"' "$T/rpc.out" | grep -q '"name":"anthrpc","url":"http://127.0.0.1:[0-9]*/v1","api":"anthropic-messages"' ||
fail "get_endpoints anthropic: $(grep '"id":"9"' "$T/rpc.out")"
grep '"id":"10"' "$T/rpc.out" | grep -q '"endpoints":\[{"name":"anthrpc","api":"anthropic-messages","count":1,"error":null}\]' ||
fail "refresh_endpoints anthropic: $(grep '"id":"10"' "$T/rpc.out")"
grep '"id":"11"' "$T/rpc.out" | grep -q '"id":"claude-sonnet-x",[^}]*"api":"anthropic-messages",[^}]*"provider":"anthrpc"' ||
fail "get_available_models anthropic: $(grep '"id":"11"' "$T/rpc.out" | head -c 800)"
grep '"id":"12"' "$T/rpc.out" | grep -q '"data":{"removed":true}' || fail "remove_endpoint anthropic: $(grep '"id":"12"' "$T/rpc.out")"
printf 'PASS: the RPC commands list, add, refresh and remove endpoints with api\n'
# Remove: gone from the list and from pic models; its models no longer resolve.
pic endpoints remove proxy | grep -q 'Removed the endpoint proxy' || fail "remove"
pic endpoints list | grep -q 'No saved endpoints' || fail "list after remove: $(pic endpoints list)"
pic models | grep -q '^proxy/' && fail "models after remove"
pic --model proxy/gpt-x ask hi > /dev/null 2> "$T/gone.err" && fail "a removed endpoint still answered"
grep -q 'unknown' "$T/gone.err" || fail "removed endpoint: $(cat "$T/gone.err")"
pic endpoints remove proxy 2> /dev/null && fail "removing twice succeeded"
printf 'PASS: remove forgets the endpoint and its models\n'
# A 401 from an endpoint with no key says how to add one; --key takes no value.
serve nokey "status:200:$T/list1.json" "status:401:-"
pic endpoints add noauth "http://127.0.0.1:$port/v1" > /dev/null 2> "$T/n.err" || fail "add noauth: $(cat "$T/n.err")"
pic --model noauth/claude-y ask hi > /dev/null 2> "$T/n2.err" && fail "a 401 ask succeeded"
grep -q "this endpoint has no key saved: pic endpoints add noauth http://127.0.0.1:$port/v1 --key asks for one" "$T/n2.err" ||
fail "401 with no key: $(cat "$T/n2.err")"
pic endpoints add noauth "http://127.0.0.1:$port/v1" --key sk-typed-on-the-line > /dev/null 2> "$T/n3.err" && fail "--key took a value"
grep -q -- '--key takes no value' "$T/n3.err" || fail "--key value: $(cat "$T/n3.err")"
grep -q 'sk-typed-on-the-line' "$T/n3.err" && fail "the value was echoed"
pic endpoints remove noauth > /dev/null
printf 'PASS: a 401 from an endpoint with no key says how to add one; --key takes no value, and says so\n'
if [ "$FAULTS" = yes ]; then
# A key: sealed under the endpoint's name, sent with the fetch and with requests.
KEY="pic-endpoint-sentinel-$$"
serve keyed "status:200:$T/list1.json" "sse:$C/text.sse" "status:401:-"
printf '%s\n' "$KEY" | PIC_TEST_PASSPHRASE=pass pic endpoints add keyed "http://127.0.0.1:$port/v1" --key > "$T/k.out" 2> "$T/k.err" ||
fail "add with a key: $(cat "$T/k.err")"
grep -q 'with its key, sealed' "$T/k.out" || fail "keyed add said: $(cat "$T/k.out")"
grep -qi "^authorization: Bearer $KEY" "$T/keyed/1.req" || fail "the fetch did not send the key"
pic keys | grep -q 'keyed' || fail "the key is not stored under the endpoint's name: $(pic keys)"
PIC_TEST_PASSPHRASE=pass pic --model keyed/claude-y ask hi > /dev/null 2> "$T/k2.err" || fail "keyed ask: $(cat "$T/k2.err")"
grep -qi "^authorization: Bearer $KEY" "$T/keyed/2.req" || fail "the request did not send the key"
grep -q '"model":"claude-y"' "$T/keyed/2.req" || fail "keyed request model"
grep -c "$KEY" "$T/k.out" "$T/k.err" "$T/k2.err" | grep -qv ':0$' && fail "the key was printed"
PIC_TEST_PASSPHRASE=pass pic --model keyed/claude-y ask hi > /dev/null 2> "$T/k3.err" && fail "a 401 keyed ask succeeded"
grep -q "the saved key was refused: pic endpoints add keyed http://127.0.0.1:$port/v1 --key asks for a new one" "$T/k3.err" ||
fail "401 with a key: $(cat "$T/k3.err")"
pic endpoints remove keyed > /dev/null || fail "remove keyed"
pic keys | grep -q 'keyed' && fail "the key outlived its endpoint"
printf 'PASS: an endpoint'"'"'s key is sealed under its name and sent with its fetch and requests\n'
fi
if [ "$FAULTS" = yes ]; then
# --basic: a user and a password, two lines of stdin, sealed under the endpoint's name and sent
# as HTTP Basic with the fetch and with requests; never printed, never in models.json.
PW="//pic-basic-sentinel-$$" # begins like a path: a password is stored as typed
CR=$(printf '\r')
WANT=$(printf '%s' "alice:$PW" | base64 | tr -d '\n')
serve basic "status:200:$T/list1.json" "sse:$C/text.sse" "status:401:-" "status:401:-"
printf 'alice\n%s\n' "$PW" | PIC_TEST_PASSPHRASE=pass pic endpoints add valet "http://127.0.0.1:$port/v1" --basic > "$T/b.out" 2> "$T/b.err" ||
fail "add --basic: $(cat "$T/b.err")"
grep -q 'with its user and password, sealed' "$T/b.out" || fail "basic add said: $(cat "$T/b.out")"
grep -q "^Authorization: Basic $WANT$CR\{0,1\}$" "$T/basic/1.req" || fail "the fetch did not send Basic: $(grep -i '^authorization' "$T/basic/1.req")"
pic endpoints list > "$T/b.list"
grep -q 'user alice, password set' "$T/b.list" || fail "list: $(cat "$T/b.list")"
pic auth status > "$T/b.status" 2>&1 || :
grep -q 'valet.*user alice, password set' "$T/b.status" || fail "auth status: $(cat "$T/b.status")"
PIC_TEST_PASSPHRASE=pass pic --model valet/claude-y ask hi > /dev/null 2> "$T/b2.err" || fail "basic ask: $(cat "$T/b2.err")"
grep -q "^Authorization: Basic $WANT$CR\{0,1\}$" "$T/basic/2.req" || fail "the request did not send Basic"
head -1 "$T/basic/2.req" | grep -q '^POST /v1/chat/completions' || fail "basic request: $(head -1 "$T/basic/2.req")"
PIC_TEST_PASSPHRASE=pass pic endpoints refresh valet > "$T/b3.out" 2>&1 && fail "a 401 refresh succeeded"
grep -q "pic endpoints add valet http://127.0.0.1:$port/v1 --basic" "$T/b3.out" || fail "401 refresh hint: $(cat "$T/b3.out")"
PIC_TEST_PASSPHRASE=pass pic --model valet/claude-y ask hi > /dev/null 2> "$T/b4.err" && fail "a 401 ask succeeded"
grep -q "pic endpoints add valet http://127.0.0.1:$port/v1 --basic" "$T/b4.err" || fail "401 ask hint: $(cat "$T/b4.err")"
grep -c "$PW" "$T/b.out" "$T/b.err" "$T/b.list" "$T/b.status" "$T/b2.err" "$T/b3.out" "$T/b4.err" "$HOME/.pic/models.json" 2> /dev/null |
grep -qv ':0$' && fail "the password was printed or written to models.json"
printf 'alice\n\n' | PIC_TEST_PASSPHRASE=pass pic endpoints add valet2 "http://127.0.0.1:$port/v1" --basic 2> /dev/null && fail "an empty password was sealed"
printf 'al:ice\npw\n' | PIC_TEST_PASSPHRASE=pass pic endpoints add valet2 "http://127.0.0.1:$port/v1" --basic 2> /dev/null && fail "a user with a colon was sealed"
pic endpoints remove valet > /dev/null || fail "remove valet"
pic keys | grep -q 'valet' && fail "the password outlived its endpoint"
printf 'PASS: --basic seals a user and password and sends HTTP Basic; a 401 says how to fix it\n'
fi
# The terminal interface: /model lists the endpoint's models marked "from NAME", with rows to
# add another endpoint (a small form) and to refresh the saved ones.
serve tui "status:200:$T/list1.json" "status:200:$T/list2.json"
pic endpoints add proxy "http://127.0.0.1:$port/v1" > /dev/null || fail "add for the picker"
screen() { perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$1" | grep -v '^\s*$' | tail -16; }
cat > "$T/tui.in" <<EOF
expect new conversation
send /model\r
expect Select a model
send gpt-x
expect from proxy
send \x1b
wait 300
send /model\r
expect Select a model
send endpoint
expect Add an endpoint
expect Refresh endpoint models
send \r
expect Name (its models are NAME/MODEL):
send third
send \r
expect URL:
send http://127.0.0.1:$port/v1
send \r
expect API key (Enter for none, Tab for a user and password):
send \r
expect Saved the endpoint third: 3 models, in /model as third/MODEL
send /model\r
expect Select a model
send endpoint
expect Refresh endpoint models
send \x1b[B
wait 300
send \r
expect Endpoints refreshed: proxy: 3 models; third: 3 models
send \x1b
wait 300
send \x04\x04
EOF
"$DRIVE" "$T/tui.tx" 140 30 "$PIC" --tui-mode regular --db "$T/j" --workspace "$T/ws" < "$T/tui.in" || fail "picker: $(screen "$T/tui.tx")"
pic endpoints list | grep -q "^third *http://127.0.0.1:$port/v1 3 models" || fail "the form did not save: $(pic endpoints list)"
[ "$(pic settings defaultModel)" = anthropic/claude-haiku-5-5 ] || fail "default after the picker: $(pic settings defaultModel)"
printf 'PASS: /model shows an endpoint'"'"'s models as from it; its rows add an endpoint and refresh them\n'
if [ "$FAULTS" = yes ]; then
# The form's Tab at the key step asks for a user and a password instead, sent as HTTP Basic.
TPW="pic-form-sentinel-$$"
cat > "$T/tui2.in" <<EOF
expect new conversation
send /model\r
expect Select a model
send endpoint
expect Add an endpoint
send \r
expect Name (its models are NAME/MODEL):
send fourth
send \r
expect URL:
send http://127.0.0.1:$port/v1
send \r
expect API key (Enter for none, Tab for a user and password):
send \t
expect User:
send bob
send \r
expect Password:
send $TPW
send \r
expect Passphrase for pic's credential store
send pass
send \r
expect Stop asking for this passphrase
send n
expect Saved the endpoint fourth: 3 models, in /model as fourth/MODEL
send \x04\x04
EOF
PIC_TEST_PASSPHRASE=pass "$DRIVE" "$T/tui2.tx" 140 30 "$PIC" --tui-mode regular --db "$T/j" --workspace "$T/ws" < "$T/tui2.in" ||
fail "basic form: $(screen "$T/tui2.tx")"
WANT2=$(printf '%s' "bob:$TPW" | base64 | tr -d '\n')
grep -l "^Authorization: Basic $WANT2" "$T"/tui/*.req > /dev/null 2>&1 || fail "the form's fetch did not send Basic"
pic endpoints list | grep -q 'user bob, password set' || fail "list after the form: $(pic endpoints list)"
printf 'PASS: the picker form takes a user and password for HTTP Basic\n'
fi
# --api anthropic-messages: works with both host-only and /v1 URLs, sends anthropic-version,
# parses Anthropic's model list shape, and sends POST /v1/messages.
serve anth_host "status:200:$T/anth-list.json" "sse:$A/text.sse"
pic endpoints add anth-host "http://127.0.0.1:$port" --api anthropic-messages > "$T/ah.out" 2> "$T/ah.err" ||
fail "add anth-host: $(cat "$T/ah.err")"
head -1 "$T/anth_host/1.req" | grep -q '^GET /v1/models HTTP/1.1' || fail "anth-host fetch path: $(head -1 "$T/anth_host/1.req")"
grep -qi '^anthropic-version: ' "$T/anth_host/1.req" || fail "anth-host fetch lacked anthropic-version"
pic endpoints list | grep -q "^anth-host .*anthropic-messages" || fail "anth-host list: $(pic endpoints list)"
pic --model anth-host/claude-sonnet-x ask 'hi' > "$T/ah.ask" 2> "$T/ah.ask.err" || fail "anth-host ask: $(cat "$T/ah.ask.err")"
head -1 "$T/anth_host/2.req" | grep -q '^POST /v1/messages HTTP/1.1' || fail "anth-host post path: $(head -1 "$T/anth_host/2.req")"
grep -qi '^anthropic-version: ' "$T/anth_host/2.req" || fail "anth-host post lacked anthropic-version"
grep -q '"model":"claude-sonnet-x"' "$T/anth_host/2.req" || fail "anth-host post model"
grep -q 'Hello, "world"' "$T/ah.ask" || fail "anth-host answer: $(cat "$T/ah.ask")"
serve anth_v1 "status:200:$T/anth-list.json" "sse:$A/text.sse"
pic endpoints add anth-v1 "http://127.0.0.1:$port/v1" --api anthropic-messages > "$T/av.out" 2> "$T/av.err" ||
fail "add anth-v1: $(cat "$T/av.err")"
head -1 "$T/anth_v1/1.req" | grep -q '^GET /v1/models HTTP/1.1' || fail "anth-v1 fetch path: $(head -1 "$T/anth_v1/1.req")"
grep -qi '^anthropic-version: ' "$T/anth_v1/1.req" || fail "anth-v1 fetch lacked anthropic-version"
pic --model anth-v1/claude-sonnet-x ask 'hi' > "$T/av.ask" 2> "$T/av.ask.err" || fail "anth-v1 ask: $(cat "$T/av.ask.err")"
head -1 "$T/anth_v1/2.req" | grep -q '^POST /v1/messages HTTP/1.1' || fail "anth-v1 post path: $(head -1 "$T/anth_v1/2.req")"
grep -qi '^anthropic-version: ' "$T/anth_v1/2.req" || fail "anth-v1 post lacked anthropic-version"
grep -q 'Hello, "world"' "$T/av.ask" || fail "anth-v1 answer: $(cat "$T/av.ask")"
pic endpoints remove anth-host > /dev/null
pic endpoints remove anth-v1 > /dev/null
printf 'PASS: --api anthropic-messages works for host-only and /v1 URLs\n'
# --api openai-responses: sends POST /v1/responses and lists as openai-responses.
serve resp "status:200:$T/list1.json" "sse:$R/reasoning_text.sse"
pic endpoints add oairesp "http://127.0.0.1:$port/v1" --api openai-responses > "$T/rp.out" 2> "$T/rp.err" ||
fail "add oairesp: $(cat "$T/rp.err")"
head -1 "$T/resp/1.req" | grep -q '^GET /v1/models HTTP/1.1' || fail "resp fetch path: $(head -1 "$T/resp/1.req")"
grep -qi '^anthropic-version:' "$T/resp/1.req" && fail "resp fetch sent anthropic-version"
pic endpoints list | grep -q "^oairesp .*openai-responses" || fail "resp list: $(pic endpoints list)"
pic --model oairesp/gpt-x ask 'hi' > "$T/rp.ask" 2> "$T/rp.ask.err" || fail "resp ask: $(cat "$T/rp.ask.err")"
head -1 "$T/resp/2.req" | grep -q '^POST /v1/responses HTTP/1.1' || fail "resp post path: $(head -1 "$T/resp/2.req")"
grep -q '"model":"gpt-x"' "$T/resp/2.req" || fail "resp post model"
grep -q 'Hello, "world"' "$T/rp.ask" || fail "resp answer: $(cat "$T/rp.ask")"
pic endpoints remove oairesp > /dev/null
printf 'PASS: --api openai-responses sends POST /v1/responses\n'
if [ "$FAULTS" = yes ]; then
# Auth + --api in both flag orders: anthropic-messages uses x-api-key with --key and Basic with --basic;
# 401 and 403 hints include --api; openai-responses uses Bearer with --key and Basic with --basic.
AKEY="pic-anth-key-$$"
APW="pic-anth-pw-$$"
AWANT=$(printf '%s' "carol:$APW" | base64 | tr -d '\n')
CR=$(printf '\r')
serve anth_key "status:200:$T/anth-list.json" "sse:$A/text.sse" "status:401:-" "status:403:-"
printf '%s\n' "$AKEY" | PIC_TEST_PASSPHRASE=pass pic endpoints add anthk "http://127.0.0.1:$port/v1" --key --api anthropic-messages > "$T/ak.out" 2> "$T/ak.err" ||
fail "add anthk: $(cat "$T/ak.err")"
grep -qi "^x-api-key: $AKEY" "$T/anth_key/1.req" || fail "anthk fetch lacked x-api-key: $(cat "$T/anth_key/1.req")"
grep -qi '^authorization:' "$T/anth_key/1.req" && fail "anthk fetch sent Authorization instead of x-api-key"
grep -qi '^anthropic-version: ' "$T/anth_key/1.req" || fail "anthk fetch lacked anthropic-version"
PIC_TEST_PASSPHRASE=pass pic --model anthk/claude-sonnet-x ask 'hi' > /dev/null 2> "$T/ak2.err" || fail "anthk ask: $(cat "$T/ak2.err")"
head -1 "$T/anth_key/2.req" | grep -q '^POST /v1/messages HTTP/1.1' || fail "anthk post path"
grep -qi "^x-api-key: $AKEY" "$T/anth_key/2.req" || fail "anthk post lacked x-api-key"
grep -qi '^anthropic-version: ' "$T/anth_key/2.req" || fail "anthk post lacked anthropic-version"
PIC_TEST_PASSPHRASE=pass pic --model anthk/claude-sonnet-x ask 'hi' > /dev/null 2> "$T/ak401.err" && fail "anthk 401 succeeded"
grep -q "pic endpoints add anthk http://127.0.0.1:$port/v1 --api anthropic-messages --key" "$T/ak401.err" ||
fail "anthk 401 hint: $(cat "$T/ak401.err")"
PIC_TEST_PASSPHRASE=pass pic --model anthk/claude-sonnet-x ask 'hi' > /dev/null 2> "$T/ak403.err" && fail "anthk 403 succeeded"
grep -q "pic endpoints add anthk http://127.0.0.1:$port/v1 --api anthropic-messages --key" "$T/ak403.err" ||
fail "anthk 403 hint: $(cat "$T/ak403.err")"
pic endpoints remove anthk > /dev/null
serve anth_basic "status:200:$T/anth-list.json" "sse:$A/text.sse" "status:401:-" "status:403:-"
printf 'carol\n%s\n' "$APW" | PIC_TEST_PASSPHRASE=pass pic endpoints add anthb "http://127.0.0.1:$port" --api anthropic-messages --basic > "$T/ab.out" 2> "$T/ab.err" ||
fail "add anthb: $(cat "$T/ab.err")"
head -1 "$T/anth_basic/1.req" | grep -q '^GET /v1/models HTTP/1.1' || fail "anthb fetch path"
grep -q "^Authorization: Basic $AWANT$CR\{0,1\}$" "$T/anth_basic/1.req" || fail "anthb fetch lacked Basic"
grep -qi '^x-api-key:' "$T/anth_basic/1.req" && fail "anthb fetch sent x-api-key with --basic"
grep -qi '^anthropic-version: ' "$T/anth_basic/1.req" || fail "anthb fetch lacked anthropic-version"
PIC_TEST_PASSPHRASE=pass pic --model anthb/claude-sonnet-x ask 'hi' > /dev/null 2> "$T/ab2.err" || fail "anthb ask: $(cat "$T/ab2.err")"
head -1 "$T/anth_basic/2.req" | grep -q '^POST /v1/messages HTTP/1.1' || fail "anthb post path"
grep -q "^Authorization: Basic $AWANT$CR\{0,1\}$" "$T/anth_basic/2.req" || fail "anthb post lacked Basic"
grep -qi '^x-api-key:' "$T/anth_basic/2.req" && fail "anthb post sent x-api-key with --basic"
grep -qi '^anthropic-version: ' "$T/anth_basic/2.req" || fail "anthb post lacked anthropic-version"
PIC_TEST_PASSPHRASE=pass pic --model anthb/claude-sonnet-x ask 'hi' > /dev/null 2> "$T/ab401.err" && fail "anthb 401 succeeded"
grep -q "pic endpoints add anthb http://127.0.0.1:$port --api anthropic-messages --basic" "$T/ab401.err" ||
fail "anthb 401 hint: $(cat "$T/ab401.err")"
PIC_TEST_PASSPHRASE=pass pic --model anthb/claude-sonnet-x ask 'hi' > /dev/null 2> "$T/ab403.err" && fail "anthb 403 succeeded"
grep -q "pic endpoints add anthb http://127.0.0.1:$port --api anthropic-messages --basic" "$T/ab403.err" ||
fail "anthb 403 hint: $(cat "$T/ab403.err")"
pic endpoints remove anthb > /dev/null
serve resp_auth "status:200:$T/list1.json" "sse:$R/reasoning_text.sse" "status:401:-"
printf '%s\n' "$AKEY" | PIC_TEST_PASSPHRASE=pass pic endpoints add respk "http://127.0.0.1:$port/v1" --api openai-responses --key > /dev/null 2> "$T/rk.err" ||
fail "add respk: $(cat "$T/rk.err")"
grep -qi "^authorization: Bearer $AKEY" "$T/resp_auth/1.req" || fail "respk fetch lacked Bearer"
PIC_TEST_PASSPHRASE=pass pic --model respk/gpt-x ask 'hi' > /dev/null 2> "$T/rk2.err" || fail "respk ask: $(cat "$T/rk2.err")"
head -1 "$T/resp_auth/2.req" | grep -q '^POST /v1/responses HTTP/1.1' || fail "respk post path"
grep -qi "^authorization: Bearer $AKEY" "$T/resp_auth/2.req" || fail "respk post lacked Bearer"
PIC_TEST_PASSPHRASE=pass pic --model respk/gpt-x ask 'hi' > /dev/null 2> "$T/rk401.err" && fail "respk 401 succeeded"
grep -q "pic endpoints add respk http://127.0.0.1:$port/v1 --api openai-responses --key" "$T/rk401.err" ||
fail "respk 401 hint: $(cat "$T/rk401.err")"
pic endpoints remove respk > /dev/null
printf 'PASS: --key and --basic work with --api anthropic-messages and --api openai-responses (401/403 hints too)\n'
fi
# SQLite migration: an older ~/.pic/pic.sqlite without picu_endpoint.api upgrades cleanly to openai-completions.
serve mig "sse:$C/text.sse"
mkdir -p "$T/mig-home/.pic"
"$SQLITE3" "$T/mig-home/.pic/pic.sqlite" <<SQL
CREATE TABLE picu_endpoint(name TEXT PRIMARY KEY, url TEXT NOT NULL, models TEXT NOT NULL, added_ms INTEGER NOT NULL, fetched_ms INTEGER, error TEXT);
INSERT INTO picu_endpoint(name, url, models, added_ms, fetched_ms, error)
VALUES('legacy', 'http://127.0.0.1:$port/v1', '[{"id":"gpt-x","owned_by":"openai"}]', 1000, 1000, NULL);
SQL
HOME="$T/mig-home" "$PIC" --db "$T/mig.j" --workspace "$T/ws" endpoints list > "$T/mig.list" 2> "$T/mig.err" ||
fail "migration list: $(cat "$T/mig.err")"
grep -q "^legacy *http://127.0.0.1:$port/v1 1 model, fetched " "$T/mig.list" || fail "migration list text: $(cat "$T/mig.list")"
[ "$("$SQLITE3" "$T/mig-home/.pic/pic.sqlite" "SELECT api FROM picu_endpoint WHERE name='legacy'")" = "openai-completions" ] ||
fail "migrated row did not default to openai-completions"
HOME="$T/mig-home" "$PIC" --db "$T/mig.j" --workspace "$T/ws" --mode rpc <<'EOF' > "$T/mig.rpc" 2> "$T/mig.rpc.err" || fail "mig rpc: $(cat "$T/mig.rpc.err")"
{"id":"m1","type":"get_endpoints"}
EOF
grep '"id":"m1"' "$T/mig.rpc" | grep -q '"name":"legacy","url":"http://127.0.0.1:[0-9]*/v1","api":"openai-completions"' ||
fail "mig rpc output: $(cat "$T/mig.rpc")"
HOME="$T/mig-home" "$PIC" --db "$T/mig.j" --workspace "$T/ws" --model legacy/gpt-x ask 'hi' > /dev/null 2> "$T/mig.ask.err" ||
fail "mig ask: $(cat "$T/mig.ask.err")"
head -1 "$T/mig/1.req" | grep -q '^POST /v1/chat/completions HTTP/1.1' || fail "mig post path: $(head -1 "$T/mig/1.req")"
printf 'PASS: older pic.sqlite without picu_endpoint.api migrates cleanly to openai-completions\n'