"""Real Android UI driver. Start on Chats after pairing; use a fresh server/client per commit.
Requires agent-device session supplied as argv[1], output folder argv[2].
No database seed: Chats > New thread > server is driven through the UI.
"""
import subprocess, re, sys
from pathlib import Path
session, out = sys.argv[1:3]
out=Path(out); out.mkdir(parents=True,exist_ok=True)
def call(*args):
 return subprocess.check_output(['agent-device',*args,'--session',session],text=True)
def target(label):
 snapshot=call('snapshot','-i')
 for line in snapshot.splitlines():
  if label in line:
   return line.split()[0]
 raise RuntimeError('Control missing: '+label)
def press(label): return call('press',target(label))
# From Chats, create the default server project thread entirely through the UI.
press('[button] "New thread"')
press('[button] "server,')
press('[button] "Model and reasoning settings,')
# Find the Replay model in the real model picker.
call('fill',target('"Find a model"'),'Long stream')
press('[radiobutton] "Long stream"')
press('[button] "Save"')
call('fill',target('[text-field] "Ask anything"'),'Stream for a while')
press('[button] "Send"')
call('wait','label="Stop agent"')
call('record','start',str(out/'interaction.mp4'),'--hide-touches','--quality','medium')
for count,word in [(1,'one'),(2,'two')]:
 call('fill',target('[text-field] "Ask anything"'),'Follow-up '+word)
 call('long-press',target('[button] "Steer"'))
 press('[group] "Queue, Run after the current turn"')
 call('wait',f'text="{count} queued"')
 snapshot=call('snapshot','-i')
 # Keep the observed queue label, count, model and running control only; no machine data.
 lines=[line for line in snapshot.splitlines() if any(x in line for x in ['Open queue,',' queued"','Stop agent','Model and reasoning settings'])]
 (out/f'{word}-accessibility.txt').write_text('\n'.join(lines)+'\n')
 call('screenshot',str(out/f'{word}.png'))
call('record','stop')
print('Captured both queue counts in the real app.')
