import subprocess,sys,time,pathlib
from PIL import Image,ImageChops
side,out,session=sys.argv[1:]
out=pathlib.Path(out);out.mkdir(parents=True,exist_ok=True)
def device(*args):
 result=subprocess.run(['agent-device',*args,'--session',session],check=True,text=True,capture_output=True)
 print(result.stdout,flush=True)
def still(label):
 a=out/(side+'-'+label+'-check.png');b=out/(side+'-'+label+'.png')
 for _ in range(5):
  device('screenshot',str(a));time.sleep(.5);device('screenshot',str(b))
  if ImageChops.difference(Image.open(a).convert("RGB"),Image.open(b).convert("RGB")).getbbox() is None:return
 raise RuntimeError('Screen did not stabilize')
device('record','start',str(out/(side+'-toggles.mp4')),'--hide-touches')
still('collapsed')
device('press','label="2 settled threads"','--settle')
still('settled-expanded')
device('press','label="1 snoozed thread"','--settle')
still('both-expanded')
# Header accessibility labels retain their counts in either state.
device('press','label="2 settled threads"','--settle')
device('press','label="1 snoozed thread"','--settle')
still('collapsed-again')
device('record','stop')
