147 lines
6.5 KiB
Python
147 lines
6.5 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""反向验证:证明三道新闸门「坏了会响、好了不响」(2026-11 第七轮起常驻)。
|
||
|
||
`gridcheck` / `rhythmcheck` / `tagcheck` 都是对着渲染结果量像素的探针,
|
||
它们自己也需要被体检一次:豁免写宽一格,闸门就会永远安静地返回 0。
|
||
这里不碰源码、不触发 HMR——直接在页面上种两块 fixture:
|
||
|
||
· `pb-*`(bad) :故意造出「栅格该用却自然排列 / 同一容器两档纵向间距 /
|
||
收缩错位 / 标签铺语义色块」四种缺陷,三个闸门必须**点名**到它;
|
||
· `pg-*`(good):故意造出四种**合法**形态(卡底 mt-auto 钉底、整块居中的排版、
|
||
正文 max-w-[72ch] 栏宽、.nex-chipbar 胶囊流),闸门必须放行。
|
||
|
||
任何一条对不上就退出 1——说明闸门瞎了或者过敏,先修闸门再谈页面。
|
||
用法:
|
||
.venv/bin/python scripts/ui/gateprobe.py
|
||
"""
|
||
import importlib.util
|
||
import os
|
||
import pathlib
|
||
import sys
|
||
|
||
from playwright.sync_api import sync_playwright
|
||
|
||
HERE = pathlib.Path(__file__).resolve().parent
|
||
BASE = os.environ.get("NEX_BASE", "http://127.0.0.1:5173")
|
||
USER = os.environ.get("NEX_USER", "admin")
|
||
PWD = os.environ.get("NEX_PASS", "admin1234")
|
||
|
||
|
||
def load(mod_name: str):
|
||
"""按路径加载闸门模块,直接复用它们的探针 JS,避免同一份判据写两遍。"""
|
||
spec = importlib.util.spec_from_file_location(mod_name, HERE / f"{mod_name}.py")
|
||
mod = importlib.util.module_from_spec(spec)
|
||
spec.loader.exec_module(mod)
|
||
return mod
|
||
|
||
|
||
tagcheck = load("tagcheck")
|
||
rhythmcheck = load("rhythmcheck")
|
||
gridcheck = load("gridcheck")
|
||
|
||
FIXTURE = r"""() => {
|
||
const old = document.getElementById('nex-gateprobe'); if (old) old.remove();
|
||
const host = document.createElement('div');
|
||
host.id = 'nex-gateprobe';
|
||
host.style.cssText = 'padding:24px;display:flex;flex-direction:column;gap:0';
|
||
host.innerHTML = `
|
||
<div class="pb-gap" style="display:flex;flex-direction:column;width:400px">
|
||
<div style="height:30px"></div>
|
||
<div style="height:30px;margin-top:8px"></div>
|
||
<div style="height:30px;margin-top:20px"></div>
|
||
</div>
|
||
<span class="ant-tag pb-tag" style="display:inline-block;width:120px;height:24px;
|
||
background:#f6ffed;color:#237804;border:1px solid #b7eb8f">反向验证标签</span>
|
||
<div class="pb-rag" style="display:flex;flex-direction:column;align-items:flex-start;width:400px">
|
||
<div style="width:400px;height:30px"></div>
|
||
<div style="width:150px;height:30px"></div>
|
||
<div style="width:250px;height:30px"></div>
|
||
</div>
|
||
<div class="pb-grid" style="display:flex;flex-direction:row;flex-wrap:wrap;gap:8px;width:900px">
|
||
<input class="ant-input" style="width:120px" value="甲" />
|
||
<input class="ant-input" style="width:300px" value="乙" />
|
||
<input class="ant-input" style="width:160px" value="丙" />
|
||
</div>
|
||
<div class="pg-pin" style="display:flex;flex-direction:column;height:200px;width:400px">
|
||
<div style="height:40px"></div>
|
||
<div style="height:40px"></div>
|
||
<div class="mt-auto" style="height:40px"></div>
|
||
</div>
|
||
<div class="pg-center" style="display:flex;flex-direction:column;align-items:center;width:400px">
|
||
<div style="width:400px;height:30px;align-self:center"></div>
|
||
<div style="width:150px;height:30px;align-self:center"></div>
|
||
<div style="width:200px;height:30px;align-self:center"></div>
|
||
</div>
|
||
<div class="pg-measure" style="display:flex;flex-direction:column;width:400px">
|
||
<div style="height:30px"></div>
|
||
<div style="height:30px;max-width:200px"></div>
|
||
<div style="height:30px;max-width:240px"></div>
|
||
</div>
|
||
<div class="nex-chipbar pg-chipbar" style="display:flex;flex-direction:row;flex-wrap:wrap;gap:8px;width:900px">
|
||
<span class="nex-chip ant-tag ant-tag-checkable" style="display:inline-block;width:80px;height:24px">甲</span>
|
||
<span class="nex-chip ant-tag ant-tag-checkable" style="display:inline-block;width:210px;height:24px">乙乙乙</span>
|
||
<span class="nex-chip ant-tag ant-tag-checkable" style="display:inline-block;width:120px;height:24px">丙丙</span>
|
||
</div>`;
|
||
(document.querySelector('main') || document.body).appendChild(host);
|
||
return [...host.querySelectorAll('[class]')].length;
|
||
}"""
|
||
|
||
|
||
def names(rows, key):
|
||
"""把检出结果摊平成「元素标记串」,再用 fixture 的类名点名。"""
|
||
return " | ".join(str(r.get(key, "")) for r in rows)
|
||
|
||
|
||
def main() -> int:
|
||
fails = []
|
||
with sync_playwright() as pw:
|
||
b = pw.chromium.launch()
|
||
pg = b.new_context(viewport={"width": 1440, "height": 1600}).new_page()
|
||
pg.set_default_timeout(20000)
|
||
pg.goto(BASE + "/login")
|
||
pg.wait_for_selector("#login-user")
|
||
pg.fill("#login-user", USER)
|
||
pg.fill("#login-pass", PWD)
|
||
pg.click("button[type=submit]")
|
||
pg.wait_for_url(lambda u: "/login" not in u, timeout=20000)
|
||
pg.goto(BASE + "/assets")
|
||
pg.wait_for_timeout(1400)
|
||
n = pg.evaluate(FIXTURE)
|
||
pg.wait_for_timeout(300)
|
||
print("fixture nodes:", n)
|
||
|
||
checks = [
|
||
("tagcheck", tagcheck.JS, "el", [("pb-tag", True), ("ant-input", False)]),
|
||
("rhythmcheck.GAP", rhythmcheck.GAP, "el",
|
||
[("pb-gap", True), ("pg-pin", False)]),
|
||
("rhythmcheck.RAG", rhythmcheck.RAG, "el",
|
||
[("pb-rag", True), ("pg-center", False), ("pg-measure", False)]),
|
||
("gridcheck", gridcheck.JS, "el",
|
||
[("pb-grid", True), ("pg-chipbar", False)]),
|
||
]
|
||
for label, js, key, expectations in checks:
|
||
rows = pg.evaluate(js)
|
||
hay = names(rows, key)
|
||
for marker, should_fire in expectations:
|
||
hit = marker in hay
|
||
ok = hit == should_fire
|
||
print(f"{'OK ' if ok else 'FAIL'} {label:<18} {marker:<12} "
|
||
f"{'必须点名' if should_fire else '必须放行'} "
|
||
f"→ {'点名了' if hit else '没点名'}")
|
||
if not ok:
|
||
fails.append(f"{label}/{marker}")
|
||
print(" 实际:", hay[:400])
|
||
b.close()
|
||
|
||
if fails:
|
||
print("GATEPROBE_FAIL", " ".join(fails))
|
||
print("→ 闸门本身出问题:不是漏检(豁免写宽了)就是误报(豁免没生效),先修闸门")
|
||
return 1
|
||
print("GATEPROBE_OK 4/4 闸门坏了会响、好了不响")
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main())
|