#!/bin/sh
# Prompt templates through the command line: a user template expands in pic ask
# (the request the server receives carries the expansion), a project template
# only once the workspace is trusted, a built-in is never shadowed, unknown
# /names pass through, pic chat expands too, and pic prompts lists them.
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-prompts.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; }
export HOME="$T/home" ANTHROPIC_API_KEY=pic-prompts-test-key PIC_PI_EXPORT=0 PIC_MODEL_CATALOG=offline
mkdir -p "$T/ws/.pic/prompts" "$T/log" "$HOME/.pic/prompts"
# Exact request shapes below: prompt caching (provider.sh) is off here.
"$PIC" settings cacheRetention none > /dev/null
"$SERVER" "$T/port" "$T/log" 120 "sse:$S/text.sse" "sse:$S/text.sse" "sse:$S/text.sse" "sse:$S/text.sse" \
    "sse:$S/text.sse" "sse:$S/text.sse" &
spid=$!
i=0
while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server"; sleep 0.05; done
printf '{"providers":{"fake":{"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() { "$PIC" --db "$T/j" --workspace "$T/ws" --model fake/m "$@"; }
sent() { grep -q "\"content\":\"$2\"" "$T/log/$1.req" || fail "request $1 did not carry: $2 ($(grep -o '"messages".*' "$T/log/$1.req" | cut -c1-200))"; }

cat > "$HOME/.pic/prompts/review.md" <<'MD'
---
description: Review a file
argument-hint: "[focus] <file>"
---
Review $2. Focus: ${1:-correctness}.
MD
printf 'Summarize the notes\nin three bullets\n' > "$HOME/.pic/prompts/notes.md"
printf -- '---\ndescription: from the project\n---\nProject says $@\n' > "$T/ws/.pic/prompts/proj.md"
printf 'user wins\n' > "$HOME/.pic/prompts/both.md"
printf 'project loses\n' > "$T/ws/.pic/prompts/both.md"
printf 'shadow\n' > "$HOME/.pic/prompts/log.md"

pic ask '/review "" src/main.c' > /dev/null 2>&1 || fail "ask review"
sent 1 'Review src\/main.c. Focus: correctness.'
printf 'PASS: a user template expands in pic ask\n'

pic ask '/proj one two' > /dev/null 2>&1 || fail "ask proj (untrusted)"
sent 2 '\/proj one two'
pic trust yes > /dev/null
pic ask '/proj one two' > /dev/null 2>&1 || fail "ask proj (trusted)"
sent 3 'Project says one two'
printf 'PASS: a project template is ignored until the workspace is trusted\n'

pic ask '/nosuch x' > /dev/null 2>&1 || fail "ask nosuch"
sent 4 '\/nosuch x'
printf 'PASS: an unknown /name passes through unchanged\n'

printf '/notes\n/log\n' | pic chat > "$T/chat.out" 2>&1 || fail "chat"
sent 5 'Summarize the notes\\nin three bullets'
[ "$(ls "$T/log" | wc -l | tr -d ' ')" = 5 ] || fail "chat: /log was sent as a template"
grep -q 'Summarize the notes' "$T/chat.out" || fail "chat: /log did not run: $(cat "$T/chat.out")"
printf 'PASS: pic chat expands a template, and a built-in command is not shadowed\n'

pic prompts > "$T/list"
[ "$(wc -l < "$T/list" | tr -d ' ')" = 6 ] || fail "prompts: $(cat "$T/list")"
grep -q "^review	user	\[focus\] <file>	Review a file	$HOME/.pic/prompts/review.md\$" "$T/list" || fail "prompts: review row: $(cat "$T/list")"
grep -q '^notes	user		Summarize the notes	' "$T/list" || fail "prompts: first-line description: $(cat "$T/list")"
grep -q "^proj	project		from the project	$T/ws/.pic/prompts/proj.md\$" "$T/list" || fail "prompts: project row: $(cat "$T/list")"
[ "$(grep -c '^both	' "$T/list")" = 2 ] || fail "prompts: both rows"
pic prompts --json > "$T/list.json"
grep -q '"name":"review","scope":"user","argumentHint":"\[focus\] <file>","description":"Review a file"' "$T/list.json" ||
    fail "prompts --json: $(cat "$T/list.json")"
pic trust no > /dev/null
[ "$(pic prompts | wc -l | tr -d ' ')" = 4 ] || fail "prompts: project rows still listed when untrusted"
printf 'PASS: pic prompts lists name, scope, hint, description and path (the project\x27s only when trusted)\n'

pic ask '/both x' > /dev/null 2>&1 || fail "ask both"
sent 6 'user wins'
printf 'PASS: the user template wins a name both scopes define, as in Pi\n'
