#!/usr/bin/env bash
# Run from the checkout being tested. Prepare/install the dev client and start
# Metro first, then pair and order the routes through the UI as in driver-notes.md.
set -euo pipefail
: "${EVIDENCE_DIR:?}" "${SESSION:?}" "${BACKEND_PORT:?}"
mkdir -p "$EVIDENCE_DIR"
node -e 'require("fs").writeFileSync(process.env.EVIDENCE_DIR+"/mode.json",JSON.stringify({delay:600,silent:false,off:false}))'
node route-proxy.mjs "$BACKEND_PORT" > "$EVIDENCE_DIR/route-console.log" 2>&1 &
route_pid=$!
trap 'kill "$route_pid"' EXIT
# UI: pair :47124, Add route :47123, Edit and drag :47124 above :47123, Done.
# Record the backend restart and inspect the active route.
agent-device record start "$EVIDENCE_DIR/reconnect.mp4" --scope device --hide-touches --session "$SESSION"
# Stop/restart only the backend process owned by this run using its terminal.
# After Connected, inspect Settings > Environments > host > Routes.
# Then make the preferred route silent and disconnect existing route sockets:
node -e 'require("fs").writeFileSync(process.env.EVIDENCE_DIR+"/mode.json",JSON.stringify({delay:0,silent:true,off:false}))'
kill -USR1 "$route_pid"
# Wait for fallback and a logged scheduled preferred descriptor check (silent).
# Restore preferred route, briefly background the app, and return:
node -e 'require("fs").writeFileSync(process.env.EVIDENCE_DIR+"/mode.json",JSON.stringify({delay:0,silent:false,off:false}))'
agent-device home --session "$SESSION"
agent-device open com.t3tools.t3code.dev --session "$SESSION"
agent-device snapshot -i --session "$SESSION"
agent-device record stop --session "$SESSION"
