Skip to content
pi-c Artifact [f2029373a4]
Login

Artifact [f2029373a4]

Artifact f2029373a4851f7906508330a98bffe4adcb952141d9bac8f599585ef7f3095a:


#!/bin/sh
# The line mode (pic chat; plain pic in a terminal): messages continue one
# conversation with the reply streamed, slash commands manage it, and the
# journal is free for other pic commands while the prompt waits.
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-chat.XXXXXX")
spid=
cpid=
cleanup() {
    exec 3>&- 2>/dev/null || :
    [ -n "$spid" ] && kill "$spid" 2>/dev/null
    [ -n "$cpid" ] && kill -9 "$cpid" 2>/dev/null
    rm -rf -- "$T"
}
trap cleanup EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
export ANTHROPIC_API_KEY=pic-chat-test-key
export HOME="$T/home" # each request writes a Pi session file under HOME
mkdir -p "$HOME"
WS="$T/ws"
DB="$T/j.sqlite"
mkdir "$WS" "$T/log"
"$SERVER" "$T/port" "$T/log" 120 "sse:$S/text.sse" &
spid=$!
i=0
while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 100 ] || 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"
await() { # PATTERN: wait up to 10s for it in the chat's output
    i=0
    until grep -q "$1" "$T/out" 2>/dev/null; do
        i=$((i + 1))
        [ "$i" -lt 200 ] || fail "timed out waiting for $1: $(cat "$T/out")"
        sleep 0.05
    done
}
mkfifo "$T/in"
"$PIC" --db "$DB" --workspace "$WS" --model fake/m chat < "$T/in" > "$T/out" 2> "$T/err" &
cpid=$!
exec 3> "$T/in"
await '^pic: fake/m in '
printf 'first\n' >&3
await 'Hello, "world"'
# Idle at the prompt: the journal is free for another pic command.
"$PIC" --db "$DB" --workspace "$WS" sessions > "$T/sessions" 2>&1 || fail "idle: journal held: $(cat "$T/sessions")"
printf '/name main\nsecond \\\nline two\n' >&3
i=0
until [ "$(ls "$T/log" | wc -l | tr -d ' ')" = 2 ]; do i=$((i + 1)); [ "$i" -lt 200 ] || fail "second message"; sleep 0.05; done
await 'Hello, "world".*' 
sleep 0.3
grep -q '"messages":\[{"role":"user","content":"first"},{"role":"assistant",.*{"role":"user","content":"second \\nline two"}\]' "$T/log/2.req" ||
    fail "follow-up: $(grep -o '"messages".*' "$T/log/2.req" | cut -c1-300)"
printf '/sessions\n' >&3
await ' main '
printf '/compact focus\n/new\nthird\n' >&3
i=0
until [ "$(ls "$T/log" | wc -l | tr -d ' ')" = 3 ]; do i=$((i + 1)); [ "$i" -lt 200 ] || fail "third message"; sleep 0.05; done
sleep 0.3
grep -q '"messages":\[{"role":"user","content":"third"}\]' "$T/log/3.req" || fail "/new: not a new conversation"
printf '/bogus\n/quit\n' >&3
exec 3>&-
i=0
while kill -0 "$cpid" 2>/dev/null; do i=$((i + 1)); [ "$i" -lt 200 ] || fail "chat did not quit"; sleep 0.05; done
wait "$cpid" || fail "chat exit status"
cpid=
[ "$(grep -c 'Hello, "world"' "$T/out")" = 3 ] || fail "replies not shown once each: $(cat "$T/out")"
grep -q 'the history is summarized before the next message' "$T/out" || fail "/compact"
grep -q '/bogus' "$T/err" || fail "unknown command not reported"
[ "$("$PIC" --db "$DB" --workspace "$WS" sessions | wc -l | tr -d ' ')" = 2 ] || fail "conversations"
"$PIC" --db "$DB" --workspace "$WS" check > /dev/null || fail "integrity"
[ "$(ls "$HOME"/.pic/sessions/*/*.jsonl | wc -l | tr -d ' ')" = 2 ] || fail "Pi session files: $(ls -R "$HOME/.pic" 2>&1)"
grep -q '"type":"session_info".*"name":"main"' "$HOME"/.pic/sessions/*/*.jsonl || fail "Pi session file: name"
printf 'PASS: pic chat continues a conversation, runs its commands, frees the journal while idle\n'
printf 'PASS: each request leaves its conversation as a Pi session file\n'