#!/usr/bin/env bash
set -euo pipefail
: "${SESSION:?Set the leased agent-device session}"
: "${EVIDENCE_DIR:?Set a scratch output directory}"
run_dir="$EVIDENCE_DIR/$(date -u +%Y%m%dT%H%M%SZ)"
mkdir -p "$run_dir"
ad() { agent-device "$@" --session "$SESSION"; }
eval_js() { agent-device --session "$SESSION" cdp runtime eval --expr "$1" --await; }
# The real app must already be on the synthetic thread, with TalkBack and Whisper enabled.
# Select its Metro CDP target before invoking this driver.
eval_js 'globalThis.RN=__r([...__r.getModules()].find(([id,m])=>/react-native\/index\.js$/.test(m.verboseName||""))[0]); RN.LogBox.ignoreAllLogs(); globalThis.findAX=(label)=>{let out=[];function walk(f){if(!f)return;if(f.memoizedProps?.accessibilityLabel===label && f.stateNode?.canonical?.publicInstance)out.push(f);walk(f.child);walk(f.sibling)};for(let id of __REACT_DEVTOOLS_GLOBAL_HOOK__.renderers.keys())for(let r of __REACT_DEVTOOLS_GLOBAL_HOOK__.getFiberRoots(id))walk(r.current);return out}; RN.AccessibilityInfo.sendAccessibilityEvent(findAX("Start dictation")[0].stateNode.canonical.publicInstance,"focus"); "focused Start dictation"'
ad record start "$run_dir/interaction-raw.mp4" --scope app --hide-touches
ad screenshot "$run_dir/start-raw.png"
ad click 'label="Start dictation"' --settle
ad screenshot "$run_dir/recording-raw.png"
# Match triage: establish focus on Finish, then activate the click delivered by double-tap.
eval_js 'RN.AccessibilityInfo.sendAccessibilityEvent(findAX("Finish dictation")[0].stateNode.canonical.publicInstance,"focus"); "focused Finish dictation"'
ad screenshot "$run_dir/finish-focused-raw.png"
ad click 'label="Finish dictation"' --settle
ad wait text 'No speech was detected.'
ad screenshot "$run_dir/finished-raw.png"
sleep 0.5
ad screenshot "$run_dir/finished-check-raw.png"
ad record stop
printf 'Captured %s\n' "$run_dir"
