#!/bin/sh
# Pi's themes and keybindings in the terminal interface, on a pseudo-terminal
# against a fake server: --theme and the theme setting change the colour codes
# the user message is drawn with (truecolor, and the 256-colour fallback),
# custom themes load from ~/.pic/themes and from a trusted workspace only, a
# keybindings.json rebinding takes effect, and /reload picks up changes to
# both. The terminal's colour codes are read from each run's transcript.
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)
FIX=$(cd "$(dirname "$0")/fixtures" && pwd)
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-themes.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
export T FIX # pty_drive scripts and the commands they run name files as $T/..., $FIX/...
export PIC_NO_NATIVE_CLIPBOARD=1
unset COLORFGBG COLORTERM
"$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" "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","contextWindow":200000}]}}}' \
"$(cat "$T/port")" > "$T/models.json"
drive() { # NAME COLORTERM: run pic (regular mode) with the script on stdin; the rest are pic's arguments
name=$1
ct=$2
shift 2
env "COLORTERM=$ct" "COLORFGBG=${FGBG-}" "$DRIVE" "$T/$name.tx" 80 24 "$PIC" --tui-mode regular --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)"
}
sgr() { printf '\033[%sm' "$1"; }
has() { grep -aqF "$(sgr "$2")" "$T/$1.tx"; } # TRANSCRIPT SGR-PARAMS
dark_bg='48;2;33;59;73'
light_bg='48;2;223;231;236'
say() { printf 'expect pic fake/m\nsend hello\\r\nexpect Hello, "world"\nsend \\x04\\x04\nexpect pic: conversation\n'; } # one message, then quit
say | drive dark truecolor
has dark "$dark_bg" || fail "dark: the user message is not on Pi's dark userMessageBg"
has dark '38;2;108;118;123' || fail "dark: the editor border is not Pi's thinkingOff colour"
has dark '38;2;222;224;225' || fail "dark: the user text is not Pi's userMessageText"
has dark "$light_bg" && fail "dark: the light background appeared"
printf 'PASS: the default theme is Pi dark\n'
say | drive light truecolor --theme light
has light "$light_bg" || fail "light: --theme light did not give Pi's light userMessageBg"
has light "$dark_bg" && fail "light: the dark background appeared"
has light '38;2;194;200;202' || fail "light: the editor border is not Pi's light thinkingOff colour"
printf 'PASS: --theme light changes the user message background\n'
FGBG='0;15'
say | drive colorfgbg2 truecolor
has colorfgbg2 "$light_bg" || fail "COLORFGBG=0;15 did not pick the light theme"
FGBG='15;0'
say | drive colorfgbg3 truecolor
has colorfgbg3 "$dark_bg" || fail "COLORFGBG=15;0 did not pick the dark theme"
FGBG=''
printf 'PASS: the system theme follows COLORFGBG as Pi decides\n'
say | drive light256 '' --theme light
has light256 '48;5;189' || fail "256 colours: Pi's rgbToAnsi256 for the light userMessageBg is 189"
has light256 '48;2;' && fail "256 colours: truecolor codes were used"
say | drive dark256 '' --theme dark
has dark256 '48;5;23' || fail "256 colours: Pi's rgbToAnsi256 for the dark userMessageBg is 23"
printf 'PASS: 256-colour fallback\n'
say | drive auto truecolor --theme light/dark
has auto "$dark_bg" || fail "--theme light/dark in a terminal without COLORFGBG: dark"
printf 'PASS: --theme light/dark picks by the terminal\n'
mkdir -p "$T/home/.pic/themes"
cp "$FIX/theme-mine.json" "$T/home/.pic/themes/mine.json"
say | drive custom truecolor --theme mine
has custom '48;2;18;52;86' || fail "custom: ~/.pic/themes/mine.json userMessageBg #123456 not used"
has custom '38;2;254;220;186' || fail "custom: userMessageText #fedcba not used"
printf 'PASS: a custom theme loads from ~/.pic/themes\n'
printf '{"name":"broken","colors":{}}' > "$T/home/.pic/themes/broken.json"
drive invalid truecolor --theme nosuch <<'EOF2'
expect pic fake/m
expect Invalid theme
expect Missing required color tokens
expect Theme "nosuch" not found; using the system theme
send \x04\x04
EOF2
printf 'PASS: an unknown theme and an invalid theme file are reported, and the system theme is used\n'
rm -f "$T/home/.pic/themes/broken.json"
# A workspace's own themes are read only once it is trusted; /reload picks up the trust.
mkdir -p "$T/ws/.pic/themes"
cp "$FIX/theme-proj.json" "$T/ws/.pic/themes/proj.json"
drive project truecolor --theme proj <<'EOF2'
expect Theme "proj" not found; using the system theme
send /trust yes\r
expect saved trust decision
send /reload\r
expect reloaded
send hello\r
expect Hello, "world"
send \x04\x04
expect pic: conversation
EOF2
has project '48;2;101;67;33' || fail "project: the trusted workspace's theme (#654321) was not used after /reload"
printf 'PASS: project themes need trust, and /reload picks them up\n'
# /settings: the Theme row opens a picker that previews as the selection moves, and enter saves the choice.
drive settings truecolor <<'EOF2'
expect pic fake/m
send /settings\r
expect type to filter
send theme
expect → Theme
send \r
expect Theme (up/down: preview
send \e[B\e[B\e[B
wait 200
send \r
expect theme: light
expect type to filter
send \e
wait 200
send hello\r
expect Hello, "world"
send \x04\x04
expect pic: conversation
EOF2
has settings "$light_bg" || fail "settings: choosing light in the theme picker did not switch to it"
say | drive saved truecolor
has saved "$light_bg" || fail "settings: the chosen theme was not saved as the theme setting"
printf 'PASS: /settings theme picker switches live and saves\n'
# keybindings.json: a rebinding takes effect, a default it replaces stops working, problems are reported.
cat > "$T/home/.pic/keybindings.json" <<'EOF2'
{"app.exit": "ctrl+q", "app.clear": "ctrl+x"}
EOF2
drive keys truecolor <<'EOF2'
expect pic fake/m
send \x04\x04
send /hotkeys\r
expect (app.exit)
send \x11\x11
EOF2
printf 'PASS: a keybindings.json rebinding takes effect (ctrl+q exits; ctrl+d no longer does)\n'
cat > "$T/home/.pic/keybindings.json" <<'EOF2'
{"app.exit": "ctrl+ctrl+q", "app.suspend": "ctrl+r", "app.thinking.cycle": "ctrl+r", "nonsense.action": "a"}
EOF2
drive keywarn truecolor <<'EOF2'
expect pic fake/m
expect "ctrl+ctrl+q" is not a valid key for app.exit
expect unknown action "nonsense.action" is ignored
expect ctrl+r is bound to more than one action
send \x04\x04
EOF2
printf 'PASS: invalid keys, unknown actions and conflicts are reported\n'
# /hotkeys shows the effective bindings; /reload picks up a change made while pic runs.
rm -f "$T/home/.pic/keybindings.json"
drive reload truecolor <<'EOF2'
expect pic fake/m
send \x11\x11
send /hotkeys\r
expect (app.exit)
send !!printf '{"app.exit":"ctrl+q"}' > "$HOME/.pic/keybindings.json"\r
wait 500
send /reload\r
expect reloaded
send /hotkeys\r
expect ctrl+q
send \x04\x04
send /hotkeys\r
expect (app.exit)
send \x11\x11
EOF2
printf 'PASS: /hotkeys lists the bindings and /reload applies a changed keybindings.json\n'
rm -f "$T/home/.pic/keybindings.json"
# a theme file added while pic runs is found by /reload, and the picker lists it
rm -rf "$T/home/.pic/themes"
drive reloadtheme truecolor --theme later <<'EOF2'
expect Theme "later" not found
send !!mkdir -p "$HOME/.pic/themes" && sed s/mine/later/ "$FIX/theme-mine.json" > "$HOME/.pic/themes/t" && mv "$HOME/.pic/themes/t" "$HOME/.pic/themes/later.json"\r
waitfile $HOME/.pic/themes/later.json
send /reload\r
expect reloaded
send hello\r
expect Hello, "world"
send \x04\x04
expect pic: conversation
EOF2
has reloadtheme '48;2;18;52;86' || fail "/reload did not pick up the theme file added while pic ran"
printf 'PASS: /reload picks up a new theme file\n'
# Claude Code's keys, which a stock Mac Terminal sends: a backslash then Enter is a newline, Ctrl+L redraws
# (it does not open the model picker), Esc Esc clears a draft, Ctrl+Q queues a follow-up (nothing to send here).
drive macdefaults truecolor <<'EOF2'
expect pic fake/m
send abc\\\r
send def
expect def
send \x0c
send \x11\x11
send \e
wait 100
send \e
wait 200
send /hotkeys\r
expect (app.exit)
expect (app.screen.redraw)
send \x04\x04
EOF2
printf 'PASS: backslash-Enter, Ctrl+L redraw, Esc Esc and Ctrl+Q behave as Claude Code'"'"'s defaults do\n'