#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""验收内部通知 HTML：渲染完整 + 关键区块 + 0 报错。"""
import sys
from playwright.sync_api import sync_playwright

F = '/Users/liuxinyuan/Desktop/Hermes输出-工作类/营销活动/2026-10实验加速季/活动内部通知_实验加速季.html'
ok = True
def chk(label, cond, detail=''):
    global ok
    print('  %s %-42s %s' % ('✅' if cond else '❌', label, detail))
    if not cond:
        ok = False

with sync_playwright() as p:
    br = p.chromium.launch(channel='chrome', headless=True)
    pg = br.new_page(viewport={'width': 1240, 'height': 1200})
    errs = []
    pg.on('pageerror', lambda e: errs.append(str(e)))
    pg.goto('file://' + F, wait_until='load')
    pg.wait_for_timeout(1200)
    print('① 结构')
    chk('品牌页眉', pg.evaluate("() => !!document.querySelector('.bior-header')"))
    chk('品牌页脚', pg.evaluate("() => !!document.querySelector('.bior-footer')"))
    chk('标题正确', '内部执行通知' in pg.title(), pg.title()[:46])
    n_sec = pg.evaluate("() => document.querySelectorAll('.sec').length")
    chk('章节数 ≥ 8', n_sec >= 8, str(n_sec))
    print('\n② 内容要素')
    txt = pg.evaluate("() => document.body.innerText.replace(/\\s+/g,' ')")
    for kw in ['BR1D303', 'BR1E102', 'BR1C103', '10.12', '10.19', '11 月 10 日',
               '逐条', '0 赞', '打码', '不得对外发送', '不与采购量', '口径确认记录',
               '含 8 或 8 的倍数', '28%']:
        chk('含「%s」' % kw, kw in txt)
    print('\n③ 中奖序号 chips')
    chips = pg.evaluate("() => [...document.querySelectorAll('.chip')].map(e=>e.innerText.trim())")
    chk('chip 数 40', len(chips) == 40, str(len(chips)))
    chk('前 6 个正确', chips[:6] == ['8', '16', '18', '24', '28', '32'], str(chips[:6]))
    print('\n④ 表格与步骤')
    chk('时间表 7 行', pg.evaluate("() => document.querySelectorAll('.sec table tbody tr').length") >= 12)
    chk('取证步骤 6 条', pg.evaluate("() => document.querySelectorAll('.steps li').length") == 6)
    chk('话术 5 条', pg.evaluate("() => document.querySelectorAll('.qa').length") == 5)
    chk('两个子活动卡', pg.evaluate("() => document.querySelectorAll('.act').length") == 2)
    print('\n⑤ 无 JS 错误')
    chk('0 JS 错误', len(errs) == 0, str(errs[:2]))
    pg.screenshot(path='/tmp/notice_full.png', full_page=True)
    # 打印时也看一版宽屏
    pg.screenshot(path='/tmp/notice_top.png')
    br.close()

print('\n%s' % ('✅ 全部通过' if ok else '❌ 有失败项'))
sys.exit(0 if ok else 1)
