# -*- coding: utf-8 -*- """验证 probeStream 探针在真实 WebEngine 页返回正确数据""" import os, sys os.environ["QTWEBENGINE_CHROMIUM_FLAGS"] = "--disable-gpu" os.environ.setdefault("QT_QPA_PLATFORM", "offscreen") 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 PASS = 0 FAIL = 0 def check(name, cond, detail=""): global PASS, FAIL if cond: PASS += 1 print(f" PASS {name}") else: FAIL += 1 print(f" FAIL {name} {detail}") app = QApplication(sys.argv) window = MainWindow() window.resize(1280, 800) window.show() PROBE_JS = r""" (function() { var m = 'probe-test-' + Date.now(); createMessage(m, 'assistant', '', 'LG'); appendReasoning(m, '思考探针内容'); appendToken(m, '正文探针 '); appendToken(m, '第二段'); // 同步强制渲染 if (typeof forceRenderNow === 'function') forceRenderNow(m); var res = probeStream(m); finishMessage(m); return res; })() """ def step1(): window.browser.page().runJavaScript(PROBE_JS, on_probe) def on_probe(res): import json d = json.loads(str(res)) print(" probe:", str(d)[:300]) check("probe 返回缓冲段", len(d.get("segs", [])) >= 1, str(d)) if d.get("segs"): s0 = d["segs"][0] check("缓冲长度>0", s0["b"] > 0, str(s0)) check("DOM 已写入", s0["d"] > 0, str(s0)) check("思考段探针", len(d.get("thinks", [])) >= 1, str(d)) check("无 JS 异常", "err" not in d, str(d)) print(f"===== {'ALL PASS' if FAIL == 0 else 'HAS FAILURES'}: {PASS}/{PASS+FAIL} =====") app.quit() QTimer.singleShot(1500, step1) app.exec() sys.exit(1 if FAIL else 0)