exec(open('ui_test.py').read().split("with sync_playwright() as pw:")[0])
with sync_playwright() as pw:
    b = pw.chromium.launch(); ctx = b.new_context(viewport={'width': 1527, 'height': 766}); p, errs = login(ctx)
    p.goto(BASE + '#/settings/sample'); p.wait_for_selector('#smpLoad')
    p.click('#smpLoad')
    if p.query_selector('text=Replace the sample agents?'): p.click('text=Replace sample data')
    p.wait_for_url('**#/team', timeout=20000); p.wait_for_timeout(1200)
    ok(sql("SELECT COUNT(*) FROM agents a JOIN workspaces w ON w.id=a.workspace_id WHERE w.slug='prophog' AND a.agent_code LIKE 'DEMO%'") == '20', 'PropHog workspace: Load adds 20 sample agents')
    ok(not p.query_selector('#toast.err.show'), 'no error message')
    p.goto(BASE + '#/settings/sample'); p.wait_for_selector('#smpClear')
    ok('20 sample agents loaded' in p.inner_text('.wl-sample').lower(), 'panel shows 20 loaded')
    # settings menu tooltip sits beside the menu
    p.hover('.set-nav a[href="#/settings/brand"]'); p.wait_for_timeout(300)
    t = p.eval_on_selector('#tip', "e => { const r = e.getBoundingClientRect(), n = document.querySelector('.set-nav').getBoundingClientRect(); return [e.className, Math.round(r.left), Math.round(n.right)] }")
    ok('beside' in t[0] and t[1] >= t[2], 'settings tooltip beside the menu, not over it', t)
    p.screenshot(path='/tmp/pp-tip.png', clip={'x': 0, 'y': 0, 'width': 900, 'height': 300})
    p.evaluate("PH.toast('Example error', true)"); p.wait_for_timeout(300)
    ok(p.eval_on_selector('#toast', 'e => getComputedStyle(e).backgroundColor') == 'rgb(180, 35, 24)', 'error message is red, not brown')
    p.click('#smpClear'); p.click('text=Remove sample data >> nth=-1'); p.wait_for_timeout(1500)
    ok(sql("SELECT COUNT(*) FROM agents WHERE agent_code LIKE 'DEMO%'") == '0', 'Remove clears them')
    ok(not errs, 'no script errors', errs[:2])
print(f"{sum(R)} passed, {len(R)-sum(R)} failed")
