#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""修测试脚本：人员一律从接口动态取，禁止硬编码（避免再次挑错部门归属）。"""
import io
import sys

F = '/Users/liuxinyuan/Desktop/Hermes输出-工作类/脚本/_verify_p2c_real.py'
s = io.open(F, encoding='utf-8').read()

P = []

P.append(("""    print('\\n④ 下钻：部门 → 销售')
    pg.evaluate("() => visitDrillTo('销售拓展部','')")
    pg.wait_for_timeout(1000)
    s = snap()
    chk('面包屑 全部部门 › 销售拓展部', '销售拓展部' in s['crumb'], s['crumb'][:70])
    chk('列出该部门销售（含王诗香）', '王诗香' in s['txt'], s['rows'][0][:70] if s['rows'] else '')
    pg.screenshot(path='/tmp/p3_3_sales.png')

    print('\\n⑤ 下钻：销售 → 客户')
    pg.evaluate("() => visitDrillTo('销售拓展部','王诗香')")
    pg.wait_for_timeout(1200)""",
"""    print('\\n④ 下钻：部门 → 销售')
    dep_top = pg.evaluate(\"\"\"() => { const d=(window.__vj.departments||[]).find(x=>x.dept==='销售拓展部');
        return d&&d.sales&&d.sales.length?{name:d.sales[0].name,total:d.sales[0].total,cust:d.sales[0].customer_count}:null; }\"\"\")
    assert dep_top, '接口没给出销售拓展部的销售'
    print('     接口给出的第一位销售：%s（活动 %d 条 / 客户 %d 家）' % (dep_top['name'], dep_top['total'], dep_top['cust']))
    pg.evaluate("() => visitDrillTo('销售拓展部','')")
    pg.wait_for_timeout(1000)
    s = snap()
    chk('面包屑 全部部门 › 销售拓展部', '销售拓展部' in s['crumb'], s['crumb'][:70])
    chk('列出该部门销售（动态取第一位）', dep_top['name'] in s['txt'], '找「%s」' % dep_top['name'])
    chk('销售行带数字（非空态）', len(s['rows']) >= 5, '%d 行' % len(s['rows']))
    pg.screenshot(path='/tmp/p3_3_sales.png')

    print('\\n⑤ 下钻：销售 → 客户')
    pg.evaluate("(n) => visitDrillTo('销售拓展部', n)", dep_top['name'])
    pg.wait_for_timeout(1200)"""))

P.append(("""    chk('面包屑到销售级', '王诗香' in s['crumb'], s['crumb'][:80])
    chk('列出该销售的客户', len(s['rows']) >= 1, '%d 行' % len(s['rows']))""",
"""    chk('面包屑到销售级', dep_top['name'] in s['crumb'], s['crumb'][:80])
    chk('列出该销售的客户', len(s['rows']) >= 1, '%d 行' % len(s['rows']))"""))

P.append(("""    n_acts = pg.evaluate("() => document.querySelectorAll('#visitBody tbody td div[id^=visitDetail]').length")
    chk('展开出活动明细', n_acts >= 1, '%d 条' % n_acts)
    s = snap()
    chk('明细带「飞书历史」标签', '飞书历史' in s['txt'])""",
"""    n_acts = pg.evaluate("() => document.querySelectorAll('#visitBody tbody td div[id^=visitDetail]').length")
    chk('展开出活动明细', n_acts >= 1, '%d 条' % n_acts)
    acts_txt = pg.evaluate("() => { const t=document.querySelector('#visitBody tbody td div[id^=visitActs], #visitBody tbody td[colspan]'); return t?t.innerText.replace(/\\\\s+/g,' ').trim():''; }")
    chk('明细带「飞书历史」标签', '飞书历史' in acts_txt, acts_txt[:60])"""))

P.append(("""    chk('切「我的」后客户清单出现', '家客户' in s['all'], [x for x in s['all'].split(' ') if '家客户' in x][:1])
    mine_n = pg.evaluate("() => (window.__vj.customers||[]).length")
    chk('「我的」客户数 < 全队', mine_n < 369, '我的 %d 家' % mine_n)""",
"""    mine_n = pg.evaluate("() => (window.__vj.customers||[]).length")
    chk('「我的」客户清单出现且少于全队', ('家客户' in s['all']) and 0 < mine_n < 369, '我的 %d 家' % mine_n)"""))

ok = True
for i, (a, b) in enumerate(P, 1):
    n = s.count(a)
    print('  %s 编辑%d 命中 %d' % ('✅' if n == 1 else '❌', i, n))
    if n != 1:
        ok = False
    else:
        s = s.replace(a, b, 1)
if ok:
    io.open(F, 'w', encoding='utf-8').write(s)
    print('\n已写回')
else:
    print('\n有未命中，未写回')
sys.exit(0 if ok else 1)
