#!/bin/sh
# The model catalog: Pi's snapshot is built in; pic models lists the models
# of providers with a key (--all every one); a fresher stored list for a
# provider is laid over the snapshot (newer than it, supported APIs only);
# resolution takes a model's limits from the catalog; --default saves the
# model pic uses when none is given.
set -eu
PIC=${PIC:?set PIC to the pic binary}
SERVER=${SERVER:?set SERVER to the fake_server binary}
here=$(cd "$(dirname "$0")" && pwd)
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-catalog.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; }
mkdir -p "$T/home/.pic" "$T/ws"
export HOME="$T/home" PIC_MODEL_CATALOG=offline
for v in ANTHROPIC_API_KEY OPENAI_API_KEY OPENROUTER_API_KEY DEEPSEEK_API_KEY XAI_API_KEY GROQ_API_KEY CEREBRAS_API_KEY \
    TOGETHER_API_KEY FIREWORKS_API_KEY HF_TOKEN NVIDIA_API_KEY MOONSHOT_API_KEY ZAI_API_KEY; do
    unset "$v"
done
pic() { "$PIC" --db "$T/j" --workspace "$T/ws" "$@"; }

n=$(pic models --all | wc -l | tr -d ' ')
[ "$n" -gt 600 ] || fail "snapshot: $n models"
[ -z "$(pic models)" ] || fail "available without keys: $(pic models | head -2)"
DEEPSEEK_API_KEY=k pic models --json > "$T/ds"
[ "$(wc -l < "$T/ds" | tr -d ' ')" -ge 1 ] || fail "deepseek models with a key"
grep -qv '"provider":"deepseek"' "$T/ds" && fail "other providers listed: $(grep -v deepseek "$T/ds" | head -1)"
grep -q '^{"model":"deepseek/[^"]*","provider":"deepseek","id":"[^"]*","name":"[^"]*","contextWindow":[0-9]*,"reasoning":\(true\|false\),"available":true}$' "$T/ds" ||
    fail "json shape: $(head -1 "$T/ds")"
printf 'PASS: the built-in catalog lists models of providers with a key\n'

# A stored list newer than the snapshot adds and replaces models; one older
# is ignored; unsupported APIs are left out.
printf '{"deepseek":{"models":[{"id":"x-new","name":"New","api":"openai-completions","contextWindow":5000,"maxTokens":100,"type":"chat"},{"id":"img","api":"other-api","type":"chat"}],"checkedAt":1,"lastModified":9999999999999},"groq":{"models":[{"id":"y-old","api":"openai-completions","type":"chat"}],"checkedAt":1,"lastModified":1}}' \
    > "$HOME/.pic/models-store.json"
DEEPSEEK_API_KEY=k pic models | grep -q '^deepseek/x-new ' || fail "overlay: new model missing"
DEEPSEEK_API_KEY=k pic models | grep -q '^deepseek/img ' && fail "overlay: unsupported API listed"
DEEPSEEK_API_KEY=k pic models | grep -q '^deepseek/deepseek-' || fail "overlay: snapshot models lost"
GROQ_API_KEY=k pic models | grep -q 'groq/y-old' && fail "overlay: older list used"
printf 'PASS: a fresher stored list is laid over the snapshot\n'

# Resolution takes the model's limits from the catalog.
mkdir "$T/log"
"$SERVER" "$T/port" "$T/log" 60 "sse:$here/chat-streams/text.sse" &
spid=$!
i=0
while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 100 ] || fail "server"; sleep 0.05; done
pic submit k1 hi > /dev/null
DEEPSEEK_API_KEY=k pic --json --model deepseek/x-new run --model deepseek/x-new \
    --api-url "http://127.0.0.1:$(cat "$T/port")/v1/chat/completions" > "$T/ev" 2> "$T/err" || fail "run: $(cat "$T/err")"
grep -q '"type":"agent_start",.*"model":"deepseek/x-new","contextWindow":5000' "$T/ev" || fail "limits: $(head -1 "$T/ev")"
printf 'PASS: a model'"'"'s limits come from the catalog\n'

# The saved default.
pic models --default nosuch 2> /dev/null && fail "default: unknown model saved"
pic models --default deepseek/x-new > /dev/null || fail "default: not saved"
grep -q '"defaultModel":"deepseek/x-new"' "$HOME/.pic/settings.json" || fail "default: $(cat "$HOME/.pic/settings.json")"
printf 'PASS: pic models --default saves the default model\n'
