import sys,os,json,threading,functools,http.server,socketserver
sys.path.insert(0,'/workspace/signup-v12-20nov/tools')
import harness
from playwright.sync_api import sync_playwright
OUT='/workspace/kit-wt/auth-kit/versions'
def serve(root):
    class H(http.server.SimpleHTTPRequestHandler):
        def log_message(self,*a): pass
    s=socketserver.ThreadingTCPServer(('127.0.0.1',0),functools.partial(H,directory=root)); s.daemon_threads=True
    threading.Thread(target=s.serve_forever,daemon=True).start()
    return f'http://127.0.0.1:{s.server_address[1]}'
D=[('01-v3-12nov','01','/components/auth-signin-signup/code/mock.html?s=signin-a&v=a&t=pearl&capture=1'),
 ('02-auth-touch-19nov','02','/components/pilot-ato-wired-19nov/auth/index.html?t=pearl&capture=1'),
 ('03-v10-rebuild-19nov','03','/components/pilot-ato-wired-19nov/auth-v10/index.html?t=pearl&capture=1'),
 ('04-combined-a-20nov','04','/components/pilot-ato-wired-19nov/auth-combined/index.html?t=pearl&capture=1'),
 ('05-combined-b-20nov','05','/components/pilot-ato-wired-19nov/auth-combined/index.html?t=pearl&capture=1'),
 ('06-topbar-unify-19nov','06','/components/pilot-ato-wired-19nov/auth/index.html?t=pearl&capture=1')]
Ll=[('07-launch-ours-ministeps-v10-19nov','07','/login/?theme=pearl'),('08-launch-v11-before-19nov','08','/login/?theme=pearl'),('09-launch-v11-designer-touch-20nov','09','/login/?theme=pearl'),('10-launch-v12-mt-edits-20nov','10','/login/?theme=pearl'),('11-launch-v12-designer-vision-20nov','11','/login/?theme=pearl')]
with sync_playwright() as p:
    b=p.chromium.launch(executable_path='/usr/bin/google-chrome',args=['--no-sandbox'])
    for vid,d,path in D:
        base=serve('/tmp/shot/'+d)
        ctx=b.new_context(viewport={'width':390,'height':844},device_scale_factor=2,is_mobile=True,has_touch=True)
        pg=ctx.new_page(); errs=[]; pg.on('pageerror',lambda e:errs.append(str(e)))
        pg.goto(base+path); pg.wait_for_timeout(2200)
        pg.screenshot(path=f'{OUT}/{vid}/screenshot.png'); print(vid,'errs',errs[:2]); ctx.close()
    for vid,d,path in Ll:
        base=harness.serve('/tmp/shot/'+d+'/site')
        ctx,pg=harness.page(b,base,390)
        pg.goto(base+path); pg.wait_for_timeout(2500)
        pg.screenshot(path=f'{OUT}/{vid}/screenshot.png'); print(vid,'errs',pg.errs[:2]); ctx.close()
    b.close()
