85 lines
2.8 KiB
Python
85 lines
2.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""on-screen 布局验证:正文 .md-segment 在真实窗口中的 offsetHeight。
|
|
offscreen 没有布局(一切 h=0),必须在真实窗口验证。
|
|
PASS 条件:流式中 + finish 后 正文段 h > 0。
|
|
"""
|
|
import os, sys
|
|
os.environ["QTWEBENGINE_CHROMIUM_FLAGS"] = "--disable-gpu"
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from PyQt6.QtWidgets import QApplication
|
|
import PyQt6.QtWebEngineWidgets # noqa
|
|
from PyQt6.QtCore import QTimer
|
|
from ui.views.main_window import MainWindow
|
|
|
|
app = QApplication(sys.argv)
|
|
w = MainWindow()
|
|
w.resize(1000, 700)
|
|
w.show()
|
|
|
|
AUDIT = {}
|
|
|
|
def js(expr, cb=None):
|
|
if cb:
|
|
w.browser.page().runJavaScript(expr, cb)
|
|
else:
|
|
w.browser.page().runJavaScript(expr)
|
|
|
|
def on_ready(_res):
|
|
js(f"createMessage('vtest', 'assistant');")
|
|
QTimer.singleShot(300, phase_stream)
|
|
|
|
def phase_stream():
|
|
js("appendReasoning('vtest', '这是思考第一段内容。');")
|
|
body = "测试!测试!测试!\n\n这是第二段正文,用于验证布局高度。"
|
|
import json as _json
|
|
for ch in body:
|
|
js("appendToken('vtest', " + _json.dumps(ch) + ");")
|
|
QTimer.singleShot(1500, phase_mid_check)
|
|
|
|
def phase_mid_check(_r=None):
|
|
js("""(function(){
|
|
var seg = document.querySelector('#vtest .md-segment');
|
|
var tc = document.querySelector('#vtest .think-content');
|
|
return JSON.stringify({
|
|
mid: true,
|
|
segH: seg ? seg.offsetHeight : -1,
|
|
segConnected: seg ? seg.isConnected : null,
|
|
segRect: seg ? Math.round(seg.getBoundingClientRect().height) : -1,
|
|
tcH: tc ? tc.offsetHeight : -1
|
|
});
|
|
})()""", on_mid)
|
|
|
|
def on_mid(res):
|
|
AUDIT["mid"] = res
|
|
js("finishMessage('vtest');")
|
|
QTimer.singleShot(1200, phase_finish_check)
|
|
|
|
def phase_finish_check(_r=None):
|
|
js("""(function(){
|
|
var seg = document.querySelector('#vtest .md-segment');
|
|
return JSON.stringify({
|
|
fin: true,
|
|
segH: seg ? seg.offsetHeight : -1,
|
|
segText: seg ? seg.textContent.length : -1,
|
|
segDisplay: seg ? getComputedStyle(seg).display : null
|
|
});
|
|
})()""", on_finish)
|
|
|
|
def on_finish(res):
|
|
AUDIT["fin"] = res
|
|
print("MID =", AUDIT.get("mid"))
|
|
print("FIN =", AUDIT.get("fin"))
|
|
try:
|
|
import json
|
|
m, f = json.loads(AUDIT["mid"]), json.loads(AUDIT["fin"])
|
|
ok = m["segH"] > 0 and m["tcH"] > 0 and f["segH"] > 0 and f["segText"] > 20
|
|
print("===== " + ("PASS: 正文段真实布局高度正常" if ok else "FAIL: 正文段高度异常") + f" mid.segH={m['segH']} fin.segH={f['segH']} =====")
|
|
except Exception as e:
|
|
print("===== FAIL: 解析异常", e, "=====")
|
|
app.quit()
|
|
|
|
QTimer.singleShot(2500, lambda: js("document.readyState", on_ready))
|
|
QTimer.singleShot(30000, app.quit)
|
|
app.exec()
|