#!/usr/bin/env bash
# Run from each tested checkout, after vp i and T3CODE_DEV_SEED=0 vp run dev.
# Requires a real local Android SDK/AVD and Device hub 0.26.1.
# Place the accompanying proof-driver.ts in apps/server/ (scratch only).
# Arguments: server origin, isolated base-dir, thread id, device id.
set -eu
origin=$1
state_dir=$2
thread_id=$3
device_id=${4:-emulator-5554}
node apps/server/proof-driver.ts "$origin" "$state_dir" device.configure '{"enabled":true,"onboardingCompleted":true}'
node apps/server/proof-driver.ts "$origin" "$state_dir" device.open "$(jq -nc --arg t "$thread_id" --arg d "$device_id" '{threadId:$t,deviceId:$d,platform:"android"}')"
# In the real web client, open the thread and show the Device panel.
# Start browser recording before the external shutdown below.
adb -s "$device_id" emu kill
# Wait until the serial disappears from this output.
adb devices -l
# In the browser: More device actions -> Power off device.
# On base, retry once. On head, the panel closes after the first attempt.
# These RPC calls also expose the resulting server state.
node apps/server/proof-driver.ts "$origin" "$state_dir" device.list '{}'
node apps/server/proof-driver.ts "$origin" "$state_dir" device.close "$(jq -nc --arg t "$thread_id" --arg d "$device_id" '{threadId:$t,deviceId:$d,shutdown:true}')"
node apps/server/proof-driver.ts "$origin" "$state_dir" device.list '{}'
