# -*- coding: utf-8 -*-
from pathlib import Path
from playwright.sync_api import sync_playwright

html_path = Path('/Users/bot1/Volumes/root_for_ai/AI工作区/文博商业化_行业看板_夏季清爽改版_20260607_0933/deliverables/index.html')
url = html_path.as_uri()
chrome = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'

with sync_playwright() as p:
    browser = p.chromium.launch(headless=True, executable_path=chrome, args=['--no-sandbox'])
    page = browser.new_page(viewport={'width': 1280, 'height': 900})
    errors = []
    page.on('console', lambda msg: errors.append(msg.text) if msg.type == 'error' else None)
    page.goto(url, wait_until='load')
    h1 = page.locator('h1').inner_text()
    tabs = ['product', 'aicomics', 'events', 'ip']
    states = []
    for tab in tabs:
        page.locator(f'button[data-tab="{tab}"]').click()
        active_page = page.locator('.page.active').get_attribute('id')
        active_tab = page.locator('.tab.active').get_attribute('data-tab')
        states.append((tab, active_page, active_tab))
    width = page.evaluate('document.documentElement.scrollWidth')
    client = page.evaluate('document.documentElement.clientWidth')
    browser.close()

with sync_playwright() as p:
    browser = p.chromium.launch(headless=True, executable_path=chrome, args=['--no-sandbox'])
    mobile = browser.new_page(viewport={'width': 390, 'height': 844}, is_mobile=True)
    mobile.goto(url, wait_until='load')
    mobile.locator('button[data-tab="events"]').click()
    mobile_active = mobile.locator('.page.active').get_attribute('id')
    m_width = mobile.evaluate('document.documentElement.scrollWidth')
    m_client = mobile.evaluate('document.documentElement.clientWidth')
    browser.close()

print('h1=', h1)
print('tab_states=', states)
print('desktop_overflow=', width, client, width > client + 1)
print('mobile_active=', mobile_active)
print('mobile_overflow=', m_width, m_client, m_width > m_client + 1)
print('console_errors=', errors)
if h1 != '内部综合看板':
    raise SystemExit('bad h1')
if any(a != t or b != t for t,a,b in states):
    raise SystemExit('tab switch failed')
if mobile_active != 'events':
    raise SystemExit('mobile tab switch failed')
if errors:
    raise SystemExit('console errors')
if width > client + 1 or m_width > m_client + 1:
    raise SystemExit('overflow detected')
