import json, os from pathlib import Path os.environ['QT_QPA_PLATFORM'] = 'offscreen' os.environ['QTWEBENGINE_CHROMIUM_FLAGS'] = '--disable-gpu --disable-gpu-compositing' os.environ['QT_OPENGL'] = 'software' from PySide6.QtCore import QTimer, QUrl, Qt from PySide6.QtWidgets import QApplication QApplication.setAttribute(Qt.ApplicationAttribute.AA_ShareOpenGLContexts) from PySide6.QtWebEngineWidgets import QWebEngineView from PySide6.QtWebEngineCore import QWebEnginePage class DebugPage(QWebEnginePage): def javaScriptConsoleMessage(self, level, message, line, source): print(f'JS: {message} at {line}', flush=True) app = QApplication([]) view = QWebEngineView() view.setPage(DebugPage(view)) view.resize(1440, 1000) view.show() cases = [ {"loading": False, "accounts": [], "error": "", "manualSetupRequired": False}, {"loading": True, "accounts": [], "error": "", "manualSetupRequired": True}, {"loading": False, "accounts": [{"id": "test"}], "error": "", "manualSetupRequired": False}, {"loading": False, "accounts": [], "error": "自动解密未完成,请登录企业微信", "manualSetupRequired": True}, ] index = 0 def inspect(result): global index result = json.loads(result) if isinstance(result, str) and result else {} expected = cases[index] manual = bool(expected['manualSetupRequired'] and not expected['loading']) valid = result.get('manualCount') == (3 if manual else 0) and result.get('refreshDisabled') == expected['loading'] if expected['error']: valid = valid and result.get('unready') and not result.get('ready') print(f"Database UI case {index + 1}: {'PASS' if valid else 'FAIL'} {result}", flush=True) if not valid: app.exit(2) return index += 1 if index == len(cases): app.exit(0) else: render() def render(): payload = {'ready': True, 'demo': False, 'view': 'wechatdb', 'wechatDb': cases[index]} view.page().runJavaScript('window.applyState(' + json.dumps(payload) + '); window.showConsoleView("wechatdb");') probe = """JSON.stringify({ manualCount: document.querySelectorAll('[data-action="wechat-db-directory"], [data-action="wechat-db-acquire"], [data-action="wechat-db-import"]').length, refreshDisabled: document.querySelector('[data-action="wechat-db-refresh"]').disabled, unready: document.body.innerText.includes('未就绪'), ready: document.body.innerText.includes('已读取') })""" QTimer.singleShot(300, lambda: view.page().runJavaScript(probe, inspect)) def loaded(ok): if not ok: app.exit(3) return render() view.loadFinished.connect(loaded) view.load(QUrl.fromLocalFile(str(Path.cwd() / 'assets/ui/zhenyang-ai-console.html'))) QTimer.singleShot(25000, lambda: app.exit(4)) raise SystemExit(app.exec())