@@ -0,0 +1,225 @@
1+#!/usr/bin/env bash
2+#
3+# qa.sh - drive the real TUI in a real kitty window through a timed script of
4+# keystrokes and prompts, screenshotting throughout. stress.sh types one prompt
5+# and watches; this fires submissions, cancellations and pauses at chosen times
6+# so turn control paths can be exercised on a repeatable run.
7+#
8+# tools/qa.sh -C <cwd> -o <outdir> -e <xdg config home> -s <spec> [-s <spec>...]
9+#
10+# A spec is a sequence of steps separated by ``;;``:
11+#
12+# wait=<seconds> pause before the next step
13+# text=<string> type the string into the editor (may contain commas)
14+# key=<name> press a key: enter, shift+enter, ctrl+c, esc, tab, ctrl+d
15+# shot=<label> write the pane text to <outdir>/mark-<n>-<label>.txt
16+#
17+# The first step runs once the banner is up. Every action is logged to
18+# <outdir>/steps.txt with its wall time. Frames land in <outdir>/frame-NNNNN.png
19+# and the final pane in final.txt / final-screen.txt.
20+
21+set -euo pipefail
22+
23+fps=15
24+seconds=180
25+name="qa-$$"
26+here=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
27+bin=$here/../zig-out/bin/mini
28+cwd=$PWD
29+out=
30+xdg=
31+specs=()
32+
33+usage() {
34+ cat <<'EOF'
35+usage: qa.sh [options]
36+ -d seconds how long to capture at most (default 180)
37+ -r fps capture rate (default 15)
38+ -o outdir where frames go (required)
39+ -C cwd working directory mini runs in (default $PWD)
40+ -e path XDG_CONFIG_HOME holding mini-coding-agent/config.json
41+ -b path mini binary (default ../zig-out/bin/mini)
42+ -n name kitty session name (default qa-$$)
43+ -s spec a step list (repeatable), see the header comment
44+EOF
45+}
46+
47+while getopts 'd:r:o:C:e:b:n:s:h' opt; do
48+ case $opt in
49+ d) seconds=$OPTARG ;;
50+ r) fps=$OPTARG ;;
51+ o) out=$OPTARG ;;
52+ C) cwd=$OPTARG ;;
53+ e) xdg=$OPTARG ;;
54+ b) bin=$OPTARG ;;
55+ n) name=$OPTARG ;;
56+ s) specs+=("$OPTARG") ;;
57+ h) usage; exit 0 ;;
58+ *) usage; exit 2 ;;
59+ esac
60+done
61+
62+if (( fps <= 0 || seconds <= 0 )); then
63+ echo "qa: fps and seconds must be positive" >&2
64+ exit 2
65+fi
66+if [[ -z $out || ${#specs[@]} -eq 0 ]]; then
67+ usage >&2
68+ exit 2
69+fi
70+if [[ ! -x $bin ]]; then
71+ echo "qa: no executable at $bin (run zig build first)" >&2
72+ exit 2
73+fi
74+bin=$(readlink -f -- "$bin")
75+cwd=$(cd -- "$cwd" && pwd)
76+mkdir -p -- "$out"
77+out=$(readlink -f -- "$out")
78+socket=/tmp/mini-$name.sock
79+
80+now_ms() {
81+ local t=${EPOCHREALTIME/,/.}
82+ echo $(( ${t%.*} * 1000 + 10#${t#*.} / 1000 ))
83+}
84+
85+kt() { kitty @ --to "unix:$socket" "$@"; }
86+pane() { kt get-text -m "id:$wid" --extent="$1" --ansi 2>/dev/null; }
87+
88+wid=
89+cleanup() {
90+ if [[ -n $wid ]]; then
91+ kt close-window -m "id:$wid" >/dev/null 2>&1 || true
92+ fi
93+ rm -f -- "$socket"
94+}
95+trap cleanup EXIT
96+trap 'exit 1' INT TERM
97+
98+printf -v launch 'cd %q; %q; printf "\nMINI-EXIT %%d\n" $?; exec sleep 86400' "$cwd" "$bin"
99+
100+rm -f -- "$socket"
101+env_args=()
102+if [[ -n $xdg ]]; then
103+ env_args=(env "XDG_CONFIG_HOME=$xdg")
104+fi
105+kitty --detach --listen-on "unix:$socket" \
106+ -o allow_remote_control=yes -o remember_window_size=no \
107+ -o initial_window_width=110c -o initial_window_height=34c \
108+ --title "mini-$name" \
109+ "${env_args[@]}" bash --norc --noprofile -c "$launch"
110+
111+for _ in $(seq 100); do
112+ wid=$(kt ls 2>/dev/null | grep -o '"id": [0-9]*' | tail -n1 | tr -dc '0-9' || true)
113+ [[ -n $wid ]] && break
114+ sleep 0.1
115+done
116+if [[ -z $wid ]]; then
117+ echo "qa: kitty never reported a window on $socket" >&2
118+ exit 1
119+fi
120+
121+for _ in $(seq 300); do
122+ pane all | grep -q 'mini ·' && break
123+ sleep 0.1
124+done
125+if ! pane all | grep -q 'mini ·'; then
126+ echo "qa: mini did not start; pane says:" >&2
127+ pane all >&2
128+ exit 1
129+fi
130+
131+log=$out/steps.txt
132+: > "$log"
133+echo "qa: $out"
134+shot=0
135+
136+send_text() {
137+ kt send-text -m "id:$wid" "$1"
138+}
139+
140+send_key() {
141+ case $1 in
142+ enter) kt send-key -m "id:$wid" enter ;;
143+ shift+enter) kt send-key -m "id:$wid" shift+enter ;;
144+ ctrl+c) kt send-key -m "id:$wid" ctrl+c ;;
145+ esc) kt send-key -m "id:$wid" escape ;;
146+ tab) kt send-key -m "id:$wid" tab ;;
147+ ctrl+d) kt send-key -m "id:$wid" ctrl+d ;;
148+ *) echo "qa: unknown key $1" >&2; exit 2 ;;
149+ esac
150+}
151+
152+# Expand every -s spec into a flat list of "at_ms<TAB>verb<TAB>arg" lines.
153+steps=()
154+t=0
155+for spec in "${specs[@]}"; do
156+ spec=${spec//;;/$'\n'}
157+ while IFS= read -r part; do
158+ part=${part#"${part%%[![:space:]]*}"}
159+ [[ -z $part ]] && continue
160+ verb=${part%%=*}
161+ arg=${part#*=}
162+ if [[ $verb == wait ]]; then
163+ t=$(awk "BEGIN { printf \"%d\", $t + $arg * 1000 }")
164+ continue
165+ fi
166+ steps+=("$t $verb $arg")
167+ done < <(printf '%s\n' "$spec")
168+done
169+
170+exec_steps() {
171+ local now=$1
172+ while (( ${#steps[@]} > 0 )); do
173+ local line=${steps[0]}
174+ local at=${line%% *}
175+ if (( at > now - start )); then break; fi
176+ steps=("${steps[@]:1}")
177+ local rest=${line#* }
178+ local verb=${rest%% *}
179+ local arg=${rest#* }
180+ case $verb in
181+ text) send_text "$arg"; printf '%s text %s\n' "$(( now - start ))" "$arg" >> "$log" ;;
182+ key) send_key "$arg"; printf '%s key %s\n' "$(( now - start ))" "$arg" >> "$log" ;;
183+ shot)
184+ shot=$(( shot + 1 ))
185+ pane all > "$out/mark-$(printf '%02d' "$shot")-$arg.txt" || true
186+ printf '%s shot %s\n' "$(( now - start ))" "$arg" >> "$log"
187+ ;;
188+ *) echo "qa: unknown step $verb" >&2; exit 2 ;;
189+ esac
190+ done
191+}
192+
193+end_reason=
194+start=$(now_ms)
195+next=$start
196+frames=0
197+while :; do
198+ now=$(now_ms)
199+ (( now - start >= seconds * 1000 )) && break
200+ exec_steps $now
201+
202+ if ! kt screenshot -m "id:$wid" "$out/frame-$(printf '%05d' "$frames").png" >/dev/null 2>&1; then
203+ end_reason="window vanished after $frames frames"
204+ break
205+ fi
206+ frames=$(( frames + 1 ))
207+
208+ if pane screen | grep -q 'MINI-EXIT'; then
209+ end_reason=$(pane screen | grep -o 'MINI-EXIT -\?[0-9]*' | tail -n1)
210+ break
211+ fi
212+
213+ next=$(( next + 1000 / fps ))
214+ sleep "$(awk "BEGIN { printf \"%.3f\", 1 / $fps }")"
215+done
216+
217+exec_steps 999999999
218+elapsed=$(( $(now_ms) - start ))
219+pane all > "$out/final.txt" || true
220+pane screen > "$out/final-screen.txt" || true
221+
222+echo "qa: $frames frames in $(( elapsed / 1000 ))s"
223+if [[ -n $end_reason ]]; then
224+ echo "qa: ended early: $end_reason"
225+fi