import subprocess,sys,pathlib,time,datetime
from PIL import Image,ImageChops
side=sys.argv[1]
out=pathlib.Path(__file__).parent/(side+'-'+datetime.datetime.now().strftime('%H%M%S'));out.mkdir()
def cmd(*args):
 r=subprocess.run(['agent-device',*args,'--session','dst43610'],text=True,capture_output=True);print(r.stdout,flush=True)
 if r.returncode: raise RuntimeError(r.stderr)
 return r.stdout
def shot(label):
 a=out/(label+'-check.png'); b=out/(label+'.png')
 cmd('screenshot',str(a));time.sleep(.5);cmd('screenshot',str(b))
 
 for f in [a,b]:
  im=Image.open(f); im.crop((0,70,im.width,im.height)).save(f)
 if ImageChops.difference(Image.open(a),Image.open(b)).getbbox(): raise RuntimeError('Screenshots not stable: '+label)
cmd('record','start',str(out/'interaction.mp4'))
shot('list')
cmd('press','label="Edit DST time check"','--settle')
cmd('scroll','down','700','--settle')
cmd('scroll','down','500','--settle')
print('OUTPUT',out,flush=True)
