exec(open('ui_test.py').read().split("with sync_playwright() as pw:")[0])
aid2 = sql("SELECT id FROM agents WHERE agent_code='DEMO2'"); nm = sql(f"SELECT full_name FROM agents WHERE id={aid2}").split(' ')[0]
with sync_playwright() as pw:
    b = pw.chromium.launch(); ctx = b.new_context(viewport={'width': 1440, 'height': 900}); p, errs = login(ctx)
    r = p.evaluate("id => PH.post('/auth/view-as', {agent_id: id}).then(() => 'ok').catch(e => e.message)", int(aid2)); print('view-as:', r)
    p.goto(BASE + '#/agents/' + aid2 + '/overview'); p.reload(); p.wait_for_selector('.hero2 h2'); p.wait_for_timeout(800)
    h = p.inner_text('.hero2 h2'); ok(h.startswith('Welcome back') and nm in h and not h.endswith('.'), 'viewing as the agent: "Welcome back, ' + nm + '" (no trailing period)', h)
    p.screenshot(path='/tmp/viewas.png')
print(f"{sum(R)} passed, {len(R)-sum(R)} failed")
