#!/bin/sh
# /auth in the terminal interface, on a pseudo-terminal: every key of every provider in one list with
# what pic knows of each and which one the model uses; Enter fixes the selected row (makes a healthy
# key the provider's, opens the login for a refused one); d removes a stored key after y; a opens the
# login's providers, as /login does; m the model picker; /key and /logout with no provider open it; a model
# whose provider has no key gets a row to add one; the screen names no Alt or F-key and no key text.
# In the box over the transcript (fullscreen) and docked above the footer (regular). --no-faults
# (the release build): skipped, since the keys are stored with PIC_TEST_PASSPHRASE, which only the
# dev build reads.
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}
SQLITE3=${SQLITE3:-sqlite3}
if [ "${1:-}" = --no-faults ]; then
printf 'SKIP: auth needs the dev build (PIC_TEST_PASSPHRASE)\n'
exit 0
fi
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-tui-auth.XXXXXX")
T=$(cd "$T" && 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; }
mkdir "$T/ws" "$T/log" "$T/home"
for v in $(env | sed -nE 's/^([A-Za-z0-9_]*_(API_KEY|TOKEN))=.*/\1/p'); do unset "$v"; done
PASS="pic-tui-auth-pass-$$"
K1="pic-tui-auth-one-$$"
K2="pic-tui-auth-two-$$"
export HOME="$T/home" PIC_MODELS_FILE="$T/models.json" PIC_MODEL_CATALOG=offline PIC_PI_EXPORT=0 T PIC_TEST_PASSPHRASE="$PASS"
export PIC_NO_NATIVE_CLIPBOARD=1 PIC_NO_KEY_CHECKS=1
printf '{"type":"error","error":{"type":"billing_error","message":"Your credit balance is too low to access the Anthropic API."}}' > "$T/billing.json"
"$SERVER" "$T/port" "$T/log" 120 "status:402:$T/billing.json" &
spid=$!
i=0
while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server"; sleep 0.05; done
cat > "$T/models.json" <<EOM
{"providers":{
"fake":{"baseUrl":"http://127.0.0.1:$(cat "$T/port")","api":"anthropic-messages","apiKey":"\$FAKE_API_KEY","models":[{"id":"m","contextWindow":200000},{"id":"n","contextWindow":200000}]},
"bare":{"baseUrl":"http://127.0.0.1:$(cat "$T/port")","api":"anthropic-messages","apiKey":"\$BARE_API_KEY","models":[{"id":"b","contextWindow":200000}]}}}
EOM
printf '%s\n' "$K1" | "$PIC" --db "$T/j" --workspace "$T/ws" login fake > /dev/null 2> "$T/login.err" || fail "login: $(cat "$T/login.err")"
printf '%s\n' "$K2" | "$PIC" --db "$T/j" --workspace "$T/ws" auth login fake --api-key --as second > /dev/null 2> "$T/login.err" ||
fail "login --as: $(cat "$T/login.err")"
screen() { perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\e\][^\a\e]*(\a|\e\\)//g; s/\r//g' "$1" | grep -v '^\s*$' | tail -20; }
drive() { # NAME MODE ARGS...: pic on a pseudo-terminal, the script on stdin
name=$1
mode=$2
shift 2
"$DRIVE" "$T/$name.tx" 100 30 "$PIC" --tui-mode "$mode" --db "$T/j" --workspace "$T/ws" "$@" || fail "$name: $(screen "$T/$name.tx")"
}
active() { "$SQLITE3" "$HOME/.pic/pic.sqlite" "SELECT coalesce((SELECT value FROM picu_setting WHERE key = 'activeKey:fake'), '-')"; }
# The list, the model's key marked, and Enter on another healthy key makes it the provider's.
drive list regular --model fake/m <<'EOF'
expect pic fake/m
send /auth\r
expect Auth
expect model fake/m · key default · store not opened yet
expect default* ← this model
expect second
expect enter: fix · a: add · b: models for a key · d: remove
send \e[B
expect enter: use this key
send \r
expect fake's requests use "second" from the next one
send \e
wait 200
send \x04\x04
EOF
[ "$(active)" = second ] || fail "enter: not the active key: $(active)"
printf 'PASS: /auth lists every key, marks the model'"'"'s, and Enter makes another the provider'"'"'s\n'
# b gives a key a family of models: the rule is saved, the row says so, and the model picker's key
# column names the key each model uses.
drive bind regular --model fake/m <<'EOF'
expect pic fake/m
send /auth\r
expect Auth
send \e[A
expect enter: use this key · b: models for this key
send b
expect Models for "default": fake/_
send n*
expect fake/n*_
send \r
expect For fake/n* by rule
expect fake/n* uses "default" from the next request
send \e
wait 200
send /model\r
expect by rule
send \e
wait 200
send \x04\x04
EOF
[ "$("$SQLITE3" "$HOME/.pic/pic.sqlite" "SELECT value FROM picu_setting WHERE key = 'keyFor:fake/n*'")" = default ] ||
fail "bind: the rule was not saved"
grep -aq 'default by rule' "$T/bind.tx" || fail "bind: the picker's key column: $(screen "$T/bind.tx")"
printf 'PASS: b gives a key a family of models, and the model picker names each model'"'"'s key\n'
# A refused key: red, its reason whole, and Enter opens its login under its label.
"$SQLITE3" "$HOME/.pic/pic.sqlite" "INSERT OR REPLACE INTO picu_credential_health(name, state, reason, status, checked_ms)
VALUES('fake:second', 'credit', 'Your credit balance is too low to access the Anthropic API.', 402, $(date +%s)000)"
drive refused fullscreen --model fake/m <<'EOF'
expect pic fake/m
send /auth\r
expect Auth
expect ! out of credit
expect Your credit balance is too low
expect enter: replace this key
send \r
expect API key "second" for fake:
send \e
wait 200
send \x04\x04
EOF
printf 'PASS: a refused key says why, and Enter opens its login under its label\n'
# d then y removes a stored key; any other key keeps it.
drive remove regular --model fake/m <<'EOF'
expect pic fake/m
send /key\r
expect Auth
send \e[A
send d
expect Remove fake's "default"? y: remove · any other key: keep
send n
wait 200
send d
expect Remove fake's "default"?
send y
expect Removed fake's "default".
send \e
wait 200
send \x04\x04
EOF
n=$("$SQLITE3" "$HOME/.pic/pic.sqlite" "SELECT count(*) FROM picu_credential WHERE name = 'fake'")
[ "$n" = 0 ] || fail "remove: the key is still stored"
printf 'PASS: d asks, y removes a stored key, any other key keeps it\n'
# A model whose provider has no key: a row to add one; a, m, /login and /logout open what they say.
drive nokey regular --model bare/b <<'EOF'
expect pic bare/b
send /auth\r
expect Auth
expect none ← this model
expect enter: add a key or sign in
send \r
expect API key for bare:
send \e
wait 300
send /logout\r
expect d removes the selected key or sign-in
send m
expect Select a model
send \e
wait 200
send /auth\r
expect Auth
send a
expect Log in
send \e
wait 200
send /login\r
expect Log in
send \e
wait 200
send \x04\x04
EOF
printf 'PASS: a keyless provider has a row to add one; a, m, /login and /logout open what they say\n'
for f in "$T"/*.tx; do
! grep -aqiE 'alt\+|f2' "$f" || fail "$(basename "$f"): the screen names an Alt or F-key: $(grep -aioE '.{20}(alt\+|f2).{20}' "$f" | head -2)"
! grep -aqF -e "$K1" -e "$K2" "$f" || fail "$(basename "$f"): a key reached the screen"
done
printf 'PASS: no Alt or F-key and no key text on any screen\n'