|
| 1 | +#!/usr/bin/env bash |
| 2 | +# On-device acceptance smoke for an installed opencode2. |
| 3 | +# RUNS ON: Termux on Android (NOT in CI, NOT on Linux) — the maintainer runs |
| 4 | +# this on a real device before publishing a release and pastes the report |
| 5 | +# block into the release notes. |
| 6 | +# |
| 7 | +# No provider auth is needed: every probe stays below the model-call line |
| 8 | +# (server lifecycle, PTY spawn, session CRUD, TUI boot, watcher subscribe). |
| 9 | +# |
| 10 | +# Usage: bash tests/smoke-device.sh [--cmd NAME] [--keep] |
| 11 | +# --cmd command to probe (default: opencode2) |
| 12 | +# --keep leave the scratch project + raw captures for inspection |
| 13 | +# Env: SMOKE_TUI_SECS (TUI probe window, default 25) |
| 14 | +# |
| 15 | +# Probes: |
| 16 | +# 1. version ... wrapper resolves .bin, binary boots |
| 17 | +# 2. service ... stop/start/status lifecycle of the background daemon |
| 18 | +# 3. api ..... server answers server.info |
| 19 | +# 4. pty ..... pty.create runs `echo` (daemon spawn on bionic), exits 0, |
| 20 | +# appears in pty.list, disappears after pty.remove |
| 21 | +# 5. session . session.create/delete in the scratch project (no auth) |
| 22 | +# 6. tui ..... renders frames under script(1) (embedded bionic renderer) |
| 23 | +# 7. watcher . server log shows subscribe+started for the scratch project |
| 24 | +# (CLI path) and a live type=directory backend=inotify record |
| 25 | +# (patched parcel android->inotify path) |
| 26 | +# Every captured output is also scanned for native-load failure signatures |
| 27 | +# (__errno_location, libc.so.6, dlopen errors): the bug class this repo's |
| 28 | +# patches exist to prevent must fail loudly here, not as a blank screen. |
| 29 | +set -uo pipefail |
| 30 | + |
| 31 | +CMD=opencode2 |
| 32 | +KEEP=0 |
| 33 | +while [ $# -gt 0 ]; do |
| 34 | + case "$1" in |
| 35 | + --cmd) [ $# -ge 2 ] || { echo "usage: $0 [--cmd NAME] [--keep]" >&2; exit 2; }; CMD="$2"; shift 2 ;; |
| 36 | + --cmd=*) CMD="${1#--cmd=}"; shift ;; |
| 37 | + --keep) KEEP=1; shift ;; |
| 38 | + -h|--help) echo "usage: $0 [--cmd NAME] [--keep]"; exit 0 ;; |
| 39 | + *) echo "unknown option: $1" >&2; echo "usage: $0 [--cmd NAME] [--keep]" >&2; exit 2 ;; |
| 40 | + esac |
| 41 | +done |
| 42 | + |
| 43 | +command -v "$CMD" >/dev/null 2>&1 || { echo "FAILED: command not found: $CMD" >&2; exit 1; } |
| 44 | +command -v script >/dev/null 2>&1 || { echo "FAILED: 'script' missing (pkg install util-linux)" >&2; exit 1; } |
| 45 | +command -v timeout >/dev/null 2>&1 || { echo "FAILED: 'timeout' missing (pkg install coreutils)" >&2; exit 1; } |
| 46 | + |
| 47 | +SCRATCH="$(mktemp -d "${TMPDIR:-/tmp}/opencode-smoke.XXXXXX")" |
| 48 | +PROJ="$SCRATCH/proj" |
| 49 | +CAP="$SCRATCH/captures" |
| 50 | +mkdir -p "$PROJ" "$CAP" |
| 51 | + |
| 52 | +cleanup() { |
| 53 | + # Best effort: remove test sessions/ptys even on failure, drop scratch. |
| 54 | + if [ -n "${PTY_ID:-}" ]; then |
| 55 | + "$CMD" api DELETE "/api/pty/$PTY_ID" >/dev/null 2>&1 || true |
| 56 | + fi |
| 57 | + if [ -n "${SES_ID:-}" ]; then |
| 58 | + "$CMD" session delete "$SES_ID" >/dev/null 2>&1 || true |
| 59 | + fi |
| 60 | + # The TUI probe may auto-create sessions in the scratch project; sweep |
| 61 | + # anything left behind (scoped to $PROJ only). |
| 62 | + for sid in $("$CMD" api GET "/api/session?directory=$PROJ" 2>/dev/null \ |
| 63 | + | grep -o '"id":"ses_[^"]*"' | cut -d'"' -f4); do |
| 64 | + "$CMD" session delete "$sid" >/dev/null 2>&1 || true |
| 65 | + done |
| 66 | + if [ "${WAS_RUNNING:-0}" = 1 ]; then |
| 67 | + "$CMD" service start >/dev/null 2>&1 || true |
| 68 | + fi |
| 69 | + if [ "$KEEP" = 0 ]; then |
| 70 | + rm -rf "$SCRATCH" |
| 71 | + else |
| 72 | + echo "kept: $SCRATCH" |
| 73 | + fi |
| 74 | +} |
| 75 | +trap cleanup EXIT |
| 76 | + |
| 77 | +PASS=0 |
| 78 | +FAIL=0 |
| 79 | +REPORT_LINES="" |
| 80 | + |
| 81 | +# Native-load failure signatures: any match fails the probe being scanned. |
| 82 | +NATIVE_SIG='__errno_location|libc\.so\.6|libm\.so\.6|dlopen failed|cannot load|Failed to load|MODULE_NOT_FOUND|ERR_DLOPEN|cannot open shared' |
| 83 | + |
| 84 | +pass() { |
| 85 | + PASS=$((PASS + 1)) |
| 86 | + REPORT_LINES="${REPORT_LINES} |
| 87 | +- $1: PASS$2" |
| 88 | + echo "PASS: $1$2" |
| 89 | +} |
| 90 | + |
| 91 | +fail() { |
| 92 | + FAIL=$((FAIL + 1)) |
| 93 | + REPORT_LINES="${REPORT_LINES} |
| 94 | +- $1: FAIL ($2)" |
| 95 | + echo "FAIL: $1 ($2)" |
| 96 | +} |
| 97 | + |
| 98 | +# scan_native <probe> <file>: fail the probe if native-load signatures appear. |
| 99 | +scan_native() { |
| 100 | + if grep -aEq "$NATIVE_SIG" "$2"; then |
| 101 | + fail "$1" "native-load signature in output: $(grep -aoE "$NATIVE_SIG" "$2" | head -1)" |
| 102 | + return 1 |
| 103 | + fi |
| 104 | + return 0 |
| 105 | +} |
| 106 | + |
| 107 | +echo "== smoke-device: probing '$CMD' (scratch: $SCRATCH)" |
| 108 | + |
| 109 | +# ── 1. version ───────────────────────────────────────────────────────── |
| 110 | +if "$CMD" --version >"$CAP/version.out" 2>&1 \ |
| 111 | + && grep -q "opencode v" "$CAP/version.out"; then |
| 112 | + if scan_native "version" "$CAP/version.out"; then |
| 113 | + pass "version" " ($(cat "$CAP/version.out"))" |
| 114 | + fi |
| 115 | +else |
| 116 | + fail "version" "unexpected output: $(head -c 120 "$CAP/version.out" 2>/dev/null)" |
| 117 | +fi |
| 118 | + |
| 119 | +# ── server log window: only lines written from here on count ────────── |
| 120 | +LOG_FILE="" |
| 121 | +if LOG_CANDIDATE="$("$CMD" debug paths 2>/dev/null | awk '$1 == "log" { $1 = ""; sub(/^ +/, ""); print; exit }')"; then |
| 122 | + if [ -n "$LOG_CANDIDATE" ] && [ -f "$LOG_CANDIDATE/opencode.log" ]; then |
| 123 | + LOG_FILE="$LOG_CANDIDATE/opencode.log" |
| 124 | + fi |
| 125 | +fi |
| 126 | +if [ -z "$LOG_FILE" ] && [ -f "$HOME/.local/share/opencode/log/opencode.log" ]; then |
| 127 | + LOG_FILE="$HOME/.local/share/opencode/log/opencode.log" |
| 128 | +fi |
| 129 | +if [ -n "$LOG_FILE" ]; then |
| 130 | + LOG_MARK="$(wc -c <"$LOG_FILE")" |
| 131 | +else |
| 132 | + echo "NOTE: server log not found; watcher probe will SKIP" |
| 133 | + LOG_MARK=0 |
| 134 | +fi |
| 135 | +log_window() { |
| 136 | + if [ -n "$LOG_FILE" ]; then |
| 137 | + tail -c +"$((LOG_MARK + 1))" "$LOG_FILE" 2>/dev/null || true |
| 138 | + fi |
| 139 | +} |
| 140 | + |
| 141 | +# ── 2. service lifecycle (save + restore maintainer state) ──────────── |
| 142 | +if "$CMD" service status >"$CAP/status.before" 2>&1 && grep -q "http" "$CAP/status.before"; then |
| 143 | + WAS_RUNNING=1 |
| 144 | +else |
| 145 | + WAS_RUNNING=0 |
| 146 | +fi |
| 147 | +"$CMD" service stop >"$CAP/service.stop" 2>&1 || true |
| 148 | +sleep 2 |
| 149 | +STOP_OK=0 |
| 150 | +"$CMD" service status >"$CAP/status.stopped" 2>&1 || true |
| 151 | +if ! grep -q "http" "$CAP/status.stopped" 2>/dev/null; then |
| 152 | + STOP_OK=1 |
| 153 | +fi |
| 154 | +"$CMD" service start >"$CAP/service.start" 2>&1 |
| 155 | +sleep 3 |
| 156 | +if "$CMD" service status >"$CAP/status.started" 2>&1 && grep -q "http" "$CAP/status.started"; then |
| 157 | + START_OK=1 |
| 158 | +else |
| 159 | + START_OK=0 |
| 160 | +fi |
| 161 | +if [ "$STOP_OK" = 1 ] && [ "$START_OK" = 1 ] \ |
| 162 | + && scan_native "service" "$CAP/service.start"; then |
| 163 | + pass "service" " (stop/start/status round-trip; was running: $WAS_RUNNING)" |
| 164 | +else |
| 165 | + fail "service" "stop_ok=$STOP_OK start_ok=$START_OK (was running: $WAS_RUNNING)" |
| 166 | +fi |
| 167 | + |
| 168 | +# ── 3. server.info ──────────────────────────────────────────────────── |
| 169 | +if "$CMD" api GET /api/info >"$CAP/info.out" 2>&1 \ |
| 170 | + && grep -q '"version"' "$CAP/info.out" \ |
| 171 | + && scan_native "api/info" "$CAP/info.out"; then |
| 172 | + pass "api/info" " ($(grep -o '"version":"[^"]*"' "$CAP/info.out" | head -1))" |
| 173 | +else |
| 174 | + fail "api/info" "server.info did not answer" |
| 175 | +fi |
| 176 | + |
| 177 | +# ── 4. pty lifecycle (daemon spawn on bionic) ───────────────────────── |
| 178 | +PTY_ID="" |
| 179 | +if "$CMD" api POST /api/pty \ |
| 180 | + -d "{\"command\":\"echo\",\"args\":[\"smoke-pty-ok\"],\"cwd\":\"$PROJ\"}" \ |
| 181 | + >"$CAP/pty.create" 2>&1; then |
| 182 | + PTY_ID="$(grep -o '"id":"pty_[^"]*"' "$CAP/pty.create" | head -1 | cut -d'"' -f4)" |
| 183 | +fi |
| 184 | +PTY_OK=0 |
| 185 | +if [ -n "$PTY_ID" ] && scan_native "pty" "$CAP/pty.create"; then |
| 186 | + for _ in $(seq 1 15); do |
| 187 | + if "$CMD" api GET "/api/pty/$PTY_ID" >"$CAP/pty.get" 2>/dev/null \ |
| 188 | + && grep -q '"status":"exited"' "$CAP/pty.get"; then |
| 189 | + if grep -q '"exitCode":0' "$CAP/pty.get"; then PTY_OK=1; fi |
| 190 | + break |
| 191 | + fi |
| 192 | + sleep 2 |
| 193 | + done |
| 194 | + if [ "$PTY_OK" = 1 ] && "$CMD" api GET /api/pty >"$CAP/pty.list" 2>/dev/null \ |
| 195 | + && grep -q "$PTY_ID" "$CAP/pty.list" \ |
| 196 | + && "$CMD" api DELETE "/api/pty/$PTY_ID" >/dev/null 2>&1 \ |
| 197 | + && "$CMD" api GET /api/pty >"$CAP/pty.list2" 2>/dev/null \ |
| 198 | + && ! grep -q "$PTY_ID" "$CAP/pty.list2"; then |
| 199 | + pass "pty" " (create/run/exit-0/list/remove $PTY_ID)" |
| 200 | + PTY_ID="" |
| 201 | + else |
| 202 | + fail "pty" "lifecycle incomplete for ${PTY_ID:-<none>}" |
| 203 | + fi |
| 204 | +else |
| 205 | + fail "pty" "pty.create failed" |
| 206 | +fi |
| 207 | + |
| 208 | +# ── 5. session create/delete (no auth) ─────────────────────────────── |
| 209 | +SES_ID="" |
| 210 | +if "$CMD" api POST /api/session \ |
| 211 | + -d "{\"title\":\"smoke-device\",\"location\":{\"directory\":\"$PROJ\"}}" \ |
| 212 | + >"$CAP/session.create" 2>&1; then |
| 213 | + SES_ID="$(grep -o '"id":"ses_[^"]*"' "$CAP/session.create" | head -1 | cut -d'"' -f4)" |
| 214 | +fi |
| 215 | +if [ -n "$SES_ID" ] && scan_native "session" "$CAP/session.create" \ |
| 216 | + && "$CMD" session delete "$SES_ID" >"$CAP/session.delete" 2>&1; then |
| 217 | + pass "session" " (create/delete $SES_ID)" |
| 218 | + SES_ID="" |
| 219 | +else |
| 220 | + fail "session" "session.create/delete failed" |
| 221 | +fi |
| 222 | + |
| 223 | +# ── 6. TUI renders frames under a pty ──────────────────────────────── |
| 224 | +# Window tunable for slow/flaky environments; the default suits releases. |
| 225 | +TUI_SECS="${SMOKE_TUI_SECS:-25}" |
| 226 | +if printf 'q' | TERM=xterm-256color timeout "$TUI_SECS" script -qec "$CMD $PROJ --print-logs" /dev/null \ |
| 227 | + >"$CAP/tui.out" 2>&1; then |
| 228 | + : # 'q' quit cleanly before the timeout |
| 229 | +fi |
| 230 | +TUI_SIZE="$(wc -c <"$CAP/tui.out" | tr -d ' ')" |
| 231 | +if [ "$TUI_SIZE" -ge 4096 ] \ |
| 232 | + && grep -qaF "$(printf '\033[?1049h')" "$CAP/tui.out" \ |
| 233 | + && scan_native "tui" "$CAP/tui.out"; then |
| 234 | + pass "tui" " (${TUI_SIZE} bytes of frames, alt-screen init seen)" |
| 235 | +else |
| 236 | + fail "tui" "no TUI frames (captured ${TUI_SIZE} bytes)" |
| 237 | +fi |
| 238 | +sleep 2 # let the server flush watch lines to its log |
| 239 | + |
| 240 | +# ── 7. watcher evidence in the server log ──────────────────────────── |
| 241 | +if [ -z "$LOG_FILE" ]; then |
| 242 | + REPORT_LINES="${REPORT_LINES} |
| 243 | +- watcher: SKIP (server log not found)" |
| 244 | + echo "SKIP: watcher (server log not found)" |
| 245 | +else |
| 246 | + WIN="$CAP/log.window" |
| 247 | + log_window >"$WIN" |
| 248 | + if grep -q "watcher subscribe.*$PROJ" "$WIN" \ |
| 249 | + && grep -q "watcher started.*$PROJ.*backend=" "$WIN"; then |
| 250 | + pass "watcher" " (subscribe+started for scratch project)" |
| 251 | + else |
| 252 | + fail "watcher" "no subscribe/started lines for scratch project" |
| 253 | + fi |
| 254 | + if grep -q "type=directory backend=inotify" "$WIN"; then |
| 255 | + pass "watcher/inotify" " (live directory/inotify record)" |
| 256 | + else |
| 257 | + fail "watcher/inotify" "no type=directory backend=inotify record" |
| 258 | + fi |
| 259 | +fi |
| 260 | + |
| 261 | +# ── report block for the release notes ─────────────────────────────── |
| 262 | +VERSION="$(cat "$CAP/version.out" 2>/dev/null | head -1)" |
| 263 | +ANDROID="$(getprop ro.build.version.release 2>/dev/null || echo unknown)" |
| 264 | +echo |
| 265 | +echo "SMOKE REPORT (paste into release notes):" |
| 266 | +echo '```' |
| 267 | +echo "Device smoke — $VERSION ($(date -u +%Y-%m-%d))" |
| 268 | +echo "Device: $(uname -m), Android $ANDROID, Termux" |
| 269 | +echo "$REPORT_LINES" | sed '/^$/d' |
| 270 | +echo '```' |
| 271 | +echo |
| 272 | +echo "smoke-device: $PASS passed, $FAIL failed" |
| 273 | +[ "$FAIL" -eq 0 ] |
0 commit comments