#!/bin/sh
# More of the MCP client against the fake MCP server (test/pic/fake_mcp.c): the servers'
# instructions in the system prompt, prompts as slash commands, @server:uri mentions,
# list_changed notifications, --mcp-config and --strict-mcp-config, MAX_MCP_OUTPUT_TOKENS and
# MCP_TOOL_TIMEOUT, pic mcp serve, add-from-claude-desktop, headersHelper and !command values,
# and a UTCP manual through its MCP bridge.
set -eu
PIC=${PIC:?set PIC to the pic binary}
MCP=${MCP:-$(dirname "$PIC")/../fake_mcp}
SQLITE3=${SQLITE3:-sqlite3}
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-mcpmore.XXXXXX")
T=$(cd "$T" && pwd -P)
pids=
stop() { for p in $pids; do kill "$p" 2>/dev/null && wait "$p" 2>/dev/null || :; done; pids=; }
cleanup() { stop; rm -rf -- "$T"; }
trap cleanup EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
unset PIC_TEST_PASSPHRASE MAX_MCP_OUTPUT_TOKENS MCP_TOOL_TIMEOUT
mkdir "$T/home" "$T/ws"
export HOME="$T/home" PIC_MODEL_CATALOG=offline PIC_PI_EXPORT=0 PIC_NO_KEY_CHECKS=1
DB="$T/j"
pic() { "$PIC" --db "$DB" --workspace "$T/ws" "$@"; }
# serve NAME ERA [sse]: a fresh fake MCP server over HTTP, its requests in $T/NAME, its URL in $url
serve() {
    name=$1
    shift
    mkdir "$T/$name"
    "$MCP" "$T/$name.port" "$T/$name" 120 "$@" &
    pids="$pids $!"
    i=0
    while [ ! -s "$T/$name.port" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server $name"; sleep 0.05; done
    url="http://127.0.0.1:$(cat "$T/$name.port")/mcp"
}
fresh() { rm -f "$DB" "$DB-wal" "$DB-shm"; }
system() { "$SQLITE3" "$DB" "SELECT system_prompt FROM pic_session"; }
declared() { "$SQLITE3" "$DB" "SELECT group_concat(json_extract(value,'\$.name'),' ') FROM pic_session, json_each(tools)"; }
result() { "$SQLITE3" "$DB" "SELECT result FROM pic_effect WHERE tool = '$1'"; }
done_script() { printf 'text done\n' > "$T/done"; }
done_script

# ---- the servers' instructions reach the system prompt -------------------------------------------
mkdir "$T/si" "$T/sh"
pic mcp add inst -- "$MCP" --stdio "$T/si" 120 modern > /dev/null 2>&1 || fail "add inst"
pic mcp add quiet --exposure hidden -- "$MCP" --stdio "$T/sh" 120 legacy > /dev/null 2>&1 || fail "add quiet"
fresh
pic submit job 'hello' > /dev/null
pic run --script "$T/done" > /dev/null 2> "$T/e" || fail "run: $(cat "$T/e")"
system > "$T/sp"
grep -q '^# MCP Server Instructions$' "$T/sp" || fail "no MCP section: $(tail -20 "$T/sp")"
grep -q '^The following MCP servers have provided instructions for how to use their tools and resources:$' "$T/sp" ||
    fail "the section's words: $(tail -20 "$T/sp")"
grep -A1 '^## inst$' "$T/sp" | grep -q '^A fake server for tests\.$' || fail "inst's instructions: $(tail -20 "$T/sp")"
grep -q '^## quiet$' "$T/sp" && fail "a hidden server's instructions were sent"
fresh
pic --no-mcp submit job 'hello' > /dev/null
pic --no-mcp run --script "$T/done" > /dev/null 2>&1 || fail "run --no-mcp"
system | grep -q 'MCP Server Instructions' && fail "--no-mcp still sent the servers' instructions"
pic mcp remove quiet > /dev/null
printf 'PASS: the instructions MCP servers give are in the system prompt, unless hidden or --no-mcp\n'
