Skip to content
pi-c Artifact [10ff71db51]
Login

Artifact [10ff71db51]

Artifact 10ff71db518246651e8688335ded3dbb1b01162be4e2b9098c9f66187b4d5c53:


#!/bin/sh
# The terminal interface, on a pseudo-terminal against a fake server: a
# reply streams and lands in the transcript, tool calls show a header and
# result, the footer counts tokens, Esc cancels a request, commands work
# (/name, /resume with its picker, unknown ones), pic tui -c shows the
# history, a resize is survived, Ctrl-D quits.
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}
S=$(cd "$(dirname "$0")/../pic/streams" && pwd)
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-tui.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 "$T/ws" "$T/log" "$T/home"
export HOME="$T/home" ANTHROPIC_API_KEY=pic-tui-test-key PIC_MODELS_FILE="$T/models.json" PIC_MODEL_CATALOG=offline
# 1 text, 2 tool call, 3 text, 4 hang (cancelled), 5 hang (quit), then text
"$SERVER" "$T/port" "$T/log" 120 "sse:$S/text.sse" "sse:$S/thinking_tool.sse" "sse:$S/text.sse" "hang:$S/text.sse:200" \
    "hang:$S/text.sse:200" "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","contextWindow":200000},{"id":"r","contextWindow":200000,"reasoning":true}]}}}' \
    "$(cat "$T/port")" > "$T/models.json"
drive() { # NAME: run pic with the script on stdin
    name=$1
    shift
    "$DRIVE" "$T/$name.tx" 80 24 "$PIC" --db "$T/j" --workspace "$T/ws" --model fake/m "$@" ||
        fail "$name: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/$name.tx" | grep -v '^\s*$' | tail -8)"
}

drive first <<'EOF'
expect pic fake/m
send hello\r
expect Hello, "world"
expect ↑25 ↓12
send write it\r
expect ✓ write t.txt
expect Successfully wrote to t.txt
expect >
send /name main\r
expect named main
send /bogus\r
expect unknown command /bogus
size 60 18
wait 300
send \x04
expect pic: conversation
EOF
[ -f "$T/ws/t.txt" ] || fail "tool: file not written"
printf 'PASS: a reply streams, a tool call shows its result, commands work, Ctrl-D quits\n'

drive cancel <<'EOF'
expect pic fake/m
send wait for me\r
expect Working
wait 500
send \e
expect request cancelled
send \x04
EOF
"$PIC" --db "$T/j" --workspace "$T/ws" check > /dev/null || fail "cancel: integrity"
printf 'PASS: Esc cancels the running request\n'

drive resume tui -c <<'EOF'
expect continuing
expect > wait for me
send /resume\r
expect Resume a conversation
send \e[B
wait 200
send \r
expect continuing
expect > hello
expect > write it
send \x04
EOF
printf 'PASS: pic tui -c shows the history; /resume picks a conversation\n'

# Quitting while a request is stuck: Ctrl-C twice leaves promptly, the
# request is cancelled and the journal is whole.
drive quit <<'EOF2'
expect pic fake/m
send stuck\r
expect Working
wait 300
send \x03
wait 100
send \x03
expect pic: conversation
EOF2
"$PIC" --db "$T/j" --workspace "$T/ws" log | grep -q '(request cancelled)' || fail "quit: request not cancelled"
"$PIC" --db "$T/j" --workspace "$T/ws" check > /dev/null || fail "quit: integrity"
printf 'PASS: quitting during a stuck request cancels it and exits\n'

# Slash menu and model picker: /mod completes with Tab; /model completes a
# model name; Ctrl+L opens the picker, typing filters, Enter picks, Ctrl+S
# also saves the default; /copy copies the last reply.
drive models <<'EOF2'
expect pic fake/m
send /mod
expect select a model
send \t
send haiku-5-5
expect claude-haiku-5-5  [anthropic]
send \r
wait 200
send \r
expect model anthropic/claude-haiku-5-5
send \x0c
expect Select a model
send fake
expect m [fake]
send \r
expect model fake/m
send \x0c
expect Select a model
send fake
expect ✓ m [fake]
send \x13
expect saved as the default
send /copy\r
expect no reply to copy yet
send \x04
EOF2
[ "$("$PIC" --db "$T/j" --workspace "$T/ws" settings defaultModel)" = fake/m ] || fail "models: default not saved"
printf 'PASS: the slash menu completes commands and models; the picker filters, picks and saves a default\n'

# The commands that keep state in ~/.pic/pic.sqlite. A fake gh stands in for
# GitHub: it records what it is given and prints a URL.
mkdir "$T/bin"
cat > "$T/bin/gh" <<'GH'
#!/bin/sh
printf '%s\n' "$*" >> "$GH_LOG.args"
cat > "$GH_LOG.stdin"
echo https://gist.example/secret123
GH
chmod +x "$T/bin/gh"
export GH_LOG="$T/gh" PATH="$T/bin:$PATH"
pic() { "$PIC" --db "$T/j" --workspace "$T/ws" "$@"; }

# /settings: rows change settings in the user database.
drive settings <<'EOF'
expect pic fake/m
send /settings\r
expect Default thinking level
send \e[B
wait 100
send \r
expect high
send \e[B
wait 100
send \r
expect Pi session files           on
send \e[B
wait 100
send \r
expect Hide thinking              on
send \e
wait 400
send \x04
EOF
[ "$(pic settings defaultThinkingLevel)" = high ] || fail "settings: thinking level not saved"
[ "$(pic settings piSessionFiles)" = 1 ] || fail "settings: piSessionFiles not saved"
[ "$(pic settings hideThinking)" = true ] || fail "settings: hideThinking not saved"
pic settings --unset piSessionFiles
pic settings --unset hideThinking
printf 'PASS: /settings changes the default thinking level and toggles, stored in the user database\n'

# /thinking and Shift+Tab: the session's level, only the model's levels.
drive thinking --model fake/r <<'EOF'
expect pic fake/r
expect • high
send /thinking low\r
expect thinking level: low
expect • low
send \e[Z
expect thinking level: medium
send /thinking xhigh\r
expect does not take thinking level xhigh
send /thinking\r
expect Thinking level
send \e[B
wait 100
send \r
expect thinking level: high
send \e[Z
expect thinking level: off
send /thinking low\r
expect thinking level: low
send what\r
expect Hello, "world"
send \x04
EOF
grep -q '"budget_tokens":2048' "$(ls "$T/log"/* | sort -V | tail -1)" || fail "thinking: the request did not carry the low level"
drive thinking-none <<'EOF'
expect pic fake/m
send \e[Z
expect does not support thinking levels
send /thinking low\r
expect does not support thinking levels
send \x04
EOF
pic settings --unset defaultThinkingLevel
printf 'PASS: /thinking and Shift+Tab pick the session level among the model levels, and requests carry it\n'

# /scoped-models: the models Ctrl+P cycles.
drive scoped <<'EOF'
expect pic fake/m
send /scoped-models\r
expect Scoped models for Ctrl+P
send fake
expect m  [fake]
send \r
send \e[B
wait 100
send \r
send \x13
expect Ctrl+P cycles 2 models
send \x10
expect model fake/r
send \x10
expect model fake/m
send \x04
EOF
[ "$(pic scoped-models | sort | tr '\n' ' ')" = 'fake/m fake/r ' ] || fail "scoped: stored $(pic scoped-models | tr '\n' ' ')"
drive scoped-clear <<'EOF'
expect pic fake/m
send /scoped-models\r
expect Scoped models for Ctrl+P
send fake
expect m  [fake]
send \r
send \e[B
wait 100
send \r
send \x13
expect no scope
send \x04
EOF
[ -z "$(pic scoped-models)" ] || fail "scoped: not cleared by toggling off"
printf 'PASS: /scoped-models stores the chosen models and Ctrl+P cycles only them\n'

# /trust, /bug, /changelog, /login, /reload.
printf 'be kind\n' > "$T/ws/AGENTS.md"
drive misc <<'EOF'
expect pic fake/m
send /trust\r
expect Workspace trust
send \e[B
wait 100
send \r
expect saved trust decision: untrusted
send /trust yes\r
expect saved trust decision: trusted
send /bug the footer wraps\r
expect nothing was sent
send /bug\r
expect /bug needs a description
send /changelog\r
expect no changelog yet
send /login\r
expect /login is not available yet
send /logout\r
expect /logout is not available yet
send /reload\r
expect project instructions found
send \x04
EOF
rm -f "$T/ws/AGENTS.md"
[ "$(pic trust)" = trusted ] || fail "misc: trust not stored"
pic bug --list | grep -q 'the footer wraps' || fail "misc: bug not stored"
printf 'PASS: /trust, /bug, /changelog, /login, /logout and /reload\n'

# /import, /tree, /export and /share on an imported conversation.
cat > "$T/pi.jsonl" <<'JSONL'
{"type":"session","version":3,"id":"abc","timestamp":"2026-01-01T00:00:00.000Z","cwd":"/x"}
{"type":"message","id":"m1","parentId":null,"message":{"role":"user","content":"What is 2 + 2?"}}
{"type":"message","id":"m2","parentId":"m1","message":{"role":"assistant","content":[{"type":"text","text":"It is 4."}]}}
{"type":"message","id":"m3","parentId":"m2","message":{"role":"user","content":"And 3 + 3?"}}
{"type":"message","id":"m4","parentId":"m3","message":{"role":"assistant","content":[{"type":"text","text":"It is 6."}]}}
JSONL
before=$(pic sessions | wc -l | tr -d ' ')
drive import <<EOF
expect pic fake/m
send /import $T/pi.jsonl\r
expect imported
expect > What is 2 + 2?
expect It is 6.
send /export $T/out.html\r
expect written to
send /share\r
expect Share this conversation as a secret GitHub gist
wait 200
send n
expect not shared
send /share\r
expect Share this conversation
send \e
wait 400
send /share\r
expect Share this conversation
wait 200
send y
expect shared as a secret gist
expect https://gist.example/secret123
send /tree\r
expect Conversation tree
expect And 3 + 3?
send \e[A
wait 100
send \r
expect branched: now in
expect > What is 2 + 2?
send \x04
expect pic: conversation
EOF
[ "$(pic sessions | wc -l | tr -d ' ')" = $((before + 2)) ] || fail "import: expected the import and its fork"
grep -q 'It is 6.' "$T/out.html" || fail "export: page lacks the answer"
[ "$(wc -l < "$T/gh.args" | tr -d ' ')" = 1 ] || fail "share: gh ran $(wc -l < "$T/gh.args") times, want 1 (only after the yes)"
[ "$(cat "$T/gh.args")" = "gist create -" ] || fail "share: gh got $(cat "$T/gh.args")"
grep -q '<!doctype html>' "$T/gh.stdin" || fail "share: gh was not given the page"
printf 'PASS: /import, /export, /tree (branching) and /share (only after a yes) work\n'

# Prompt templates: the slash menu lists them with their hint and description,
# /name ARGS sends the expansion, a built-in command is never shadowed, and
# /reload picks up the project's templates once the workspace is trusted.
mkdir -p "$HOME/.pic/prompts" "$T/ws/.pic/prompts"
printf -- '---\ndescription: Say hello\nargument-hint: <who>\n---\nSay hello to $1.\n' > "$HOME/.pic/prompts/greet.md"
printf 'shadowed body\n' > "$HOME/.pic/prompts/bug.md"
printf 'Project late body\n' > "$T/ws/.pic/prompts/late.md"
pic trust no > /dev/null
reqs=$(ls "$T/log" | wc -l | tr -d ' ')
drive prompts <<'EOF2'
expect pic fake/m
send /gre
expect <who> — Say hello
send \r
send Ada\r
expect > /greet Ada
expect Hello, "world"
send /bug shadow check\r
expect nothing was sent
send /reload\r
expect 2 prompt templates
send /trust yes\r
expect saved trust decision: trusted
send /reload\r
expect 3 prompt templates
send /late\r
expect > /late
expect Hello, "world"
send /nosuch\r
expect unknown command /nosuch
send \x04
EOF2
[ "$(ls "$T/log" | wc -l | tr -d ' ')" = $((reqs + 2)) ] || fail "prompts: $(ls "$T/log" | wc -l) requests, want $((reqs + 2))"
grep -q '"content":"Say hello to Ada."' "$T/log/$((reqs + 1)).req" || fail "prompts: /greet Ada not expanded: $(grep -o '"messages".*' "$T/log/$((reqs + 1)).req" | cut -c1-200)"
grep -q '"content":"Project late body"' "$T/log/$((reqs + 2)).req" || fail "prompts: /late not expanded: $(grep -o '"messages".*' "$T/log/$((reqs + 2)).req" | cut -c1-200)"
pic bug --list | grep -q 'shadow check' || fail "prompts: /bug was shadowed by a template"
pic trust no > /dev/null
printf 'PASS: the slash menu lists prompt templates; /name ARGS sends the expansion; built-ins win; /reload re-reads them\n'

# Pi parity, round 1: tool blocks, thinking, history, footer, retries, model keys.
# A second fake server with its own script; the connection order is the test order.
V=$(cd "$(dirname "$0")/streams" && pwd)
mkdir "$T/log2" "$T/ws2" "$T/ws2/.git"
printf 'ref: refs/heads/feature\n' > "$T/ws2/.git/HEAD"
printf 'alpha\nbeta\ngamma\n' > "$T/ws2/e.txt"
"$SERVER" "$T/port2" "$T/log2" 240 "sse:$V/edit.sse" "sse:$V/done.sse" "sse:$V/bash.sse" "hang:$V/done.sse:200" \
    "sse:$V/thinking_text.sse" "sse:$V/thinking_text.sse" "sse:$V/length.sse" "cut:$V/done.sse:420" "hang:$V/done.sse:200" \
    "cut:$V/done.sse:420" "cut:$V/done.sse:420" "cut:$V/done.sse:420" "hang:$V/done.sse:200" "sse:$V/done.sse" &
spid2=$!
trap 'kill "$spid2" 2>/dev/null; cleanup' EXIT HUP INT TERM
i=0
while [ ! -s "$T/port2" ]; do i=$((i + 1)); [ "$i" -lt 100 ] || fail "server 2"; sleep 0.05; done
printf '{"providers":{"fake":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$ANTHROPIC_API_KEY","models":[{"id":"m","contextWindow":200000},{"id":"r","contextWindow":200000,"reasoning":true}]}}}' \
    "$(cat "$T/port2")" > "$T/models2.json"
export PIC_MODELS_FILE="$T/models2.json"
drive2() { # NAME [pic args]: the second journal and workspace
    name=$1
    shift
    "$DRIVE" "$T/$name.tx" 80 24 "$PIC" --db "$T/j2" --workspace "$T/ws2" --model fake/r "$@" ||
        fail "$name: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/$name.tx" | grep -v '^\s*$' | tail -8)"
}
pic2() { "$PIC" --db "$T/j2" --workspace "$T/ws2" "$@"; }

# An edit is a coloured diff; the block says it took time; the footer has the
# branch, the conversation name and the token counts.
drive2 edit <<'EOF2'
expect pic fake/r
send fix beta\r
expect ✓ edit e.txt
expect -beta
expect +BETA
expect +beta two
expect Took
expect All done.
expect ↑60 ↓40
size 200 24
wait 300
send /name tester\r
expect named tester
expect (feature) • tester
send \x04
EOF2
grep -q '^BETA$' "$T/ws2/e.txt" && grep -q '^beta two$' "$T/ws2/e.txt" || fail "edit: file not edited"
perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g' "$T/edit.tx" | grep -q 'ctrl+o to expand' && fail "edit: a short diff was collapsed"
grep -aq $'\e\[31m-beta' "$T/edit.tx" || fail "edit: removed line not red"
grep -aq $'\e\[32m+BETA' "$T/edit.tx" || fail "edit: added line not green"
printf 'PASS: an edit shows as a coloured diff with its duration; the footer shows branch, name and tokens\n'

# Long output is collapsed with a hint; Ctrl+O expands the block while it is
# still in the live area, and the block is committed in that state.
drive2 collapse <<'EOF2'
expect pic fake/r
send list\r
expect 7 earlier lines, ctrl+o to expand
expect Took
wait 700
send \x0f
expect 101
expect tools expanded
send \e
expect request cancelled
send \x04
EOF2
printf 'PASS: tool output is collapsed with a hint, and Ctrl+O expands blocks still live\n'

# Thinking stays in the transcript; Ctrl+T hides it behind a one-line marker
# and saves the setting; the setting is read when a reply is committed.
drive2 thinking <<'EOF2'
expect pic fake/r
send ponder\r
expect pondering the answer carefully
expect Done thinking.
expect ↑30 ↓20
wait 400
send \x14
expect thinking blocks: hidden
send again\r
expect Thinking...
expect ↑60 ↓40
wait 400
send \x14
expect thinking blocks: visible
send \x04
EOF2
[ -z "$(pic2 settings hideThinking)" ] || [ "$(pic2 settings hideThinking)" = false ] || fail "thinking: Ctrl+T did not save the setting"
grep -a 'pondering the answer carefully' "$T/thinking.tx" | grep -aq $'\e\[2;3m' || fail "thinking: not dim italic"
printf 'PASS: thinking is kept dim italic in the transcript; Ctrl+T hides it behind a marker and saves hideThinking\n'

# A reply cut at the length limit says so; a failed attempt shows the retry
# status; a request that fails on every attempt says how many were made.
drive2 stops <<'EOF2'
expect pic fake/r
send long\r
expect The long answer starts
expect Response was truncated before completion.
send retry\r
expect Retrying (2/3)
expect (esc to cancel)
wait 300
send \e
expect request cancelled
wait 1500
send fail\r
expect Retry failed after 3 attempts
send \x04
EOF2
printf 'PASS: a length stop is reported; retries show their status and the final failure counts attempts\n'

# Ctrl+L and Ctrl+P work while a reply runs and apply from the next request.
drive2 keys <<'EOF2'
expect pic fake/r
send hold\r
expect Working
wait 300
send \x10
expect model
send \x0c
expect Select a model
send fake/m
expect m [fake]
send \r
expect model fake/m
expect fake/m
send \e
expect request cancelled
send next\r
expect All done.
send \x04
EOF2
grep -q '"model":"m"' "$(ls "$T/log2"/*.req | sort -V | tail -1)" || fail "keys: the next request did not use fake/m"
printf 'PASS: Ctrl+L and Ctrl+P work while a reply runs and take effect from the next request\n'

# Up/Down recall the messages of the conversation being continued, oldest
# first, after a restart.
drive2 hist1 <<'EOF2'
expect pic fake/r
send first question\r
expect ↑30 ↓20
wait 300
send second question\r
expect ↑60 ↓40
wait 300
send \x04
EOF2
drive2 hist2 tui -c <<'EOF2'
expect continuing
expect > second question
wait 300
send \e[A\e[A
wait 200
send \r
expect All done.
send \x04
EOF2
[ "$(grep -o '"first question"' "$(ls "$T/log2"/*.req | sort -V | tail -1)" | wc -l | tr -d ' ')" = 2 ] || fail "history: the recalled message was not sent again"
printf 'PASS: Up recalls the earlier messages of a continued conversation\n'