import subprocess,sys,pathlib,time,hashlib,json
out=pathlib.Path(sys.argv[1]); side=sys.argv[2]; out.mkdir(parents=True,exist_ok=True)
session='issue35402-proof'
def run(*args):
 p=subprocess.run(['agent-device',*args,'--session',session],text=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
 with (out/'actions.txt').open('a') as f:f.write(' '.join(args)+'\n'+p.stdout+'\n')
 if p.returncode:raise RuntimeError(p.stdout)
 return p.stdout
def press(label):return run('press','label='+json.dumps(label),'--settle')
def shot(label):
 for n in range(8):
  a=out/(side+'-'+label+'-a.png'); b=out/(side+'-'+label+'.png')
  run('screenshot',str(a));time.sleep(.5);run('screenshot',str(b))
  if a.read_bytes()==b.read_bytes():return
 raise RuntimeError('screenshots did not stabilize: '+label)
run('record','start',str(out/(side+'-flow.mp4')),'--hide-touches')
for path,label in [('notes/clip.wav','plain-audio'),('notes/clip#1.wav','hash-audio'),('notes/clip?1.wav','question-audio'),('notes#old/clip.wav','parent-hash-audio'),('notes?old/clip.wav','parent-question-audio'),('docs/draft.md','plain-markdown'),('docs/draft#1.md','hash-markdown'),('docs/draft?1.md','question-markdown')]:
 press('Open files')
 tree=run('snapshot','-i')
 if '"'+path+'"' not in tree:press(path.split('/')[0])
 press(path)
 if path.endswith('.md'):press('File actions')
 run('snapshot','-i');shot(label)
 if path.endswith('.md'):run('back','--settle')
 press('Navigate up')
run('record','stop')
