Files
xieyi-weixin-/tools/test_desktop_console.py
2026-11-02 14:05:03 +08:00

84 lines
4.8 KiB
Python

"""Real Chromium regression for incremental dashboard updates; fake bridge only."""
import json
import os
from pathlib import Path
import sys
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
os.environ.setdefault("QTWEBENGINE_CHROMIUM_FLAGS", "--disable-gpu")
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
from tools.test_proactive_console import LocalOnly, Page
from PySide6.QtCore import QEventLoop, QTimer, QUrl
from PySide6.QtTest import QTest
from PySide6.QtWidgets import QApplication
from PySide6.QtWebEngineCore import QWebEngineProfile
from PySide6.QtWebEngineWidgets import QWebEngineView
def main():
app = QApplication.instance() or QApplication([])
view = QWebEngineView()
profile = QWebEngineProfile(view)
interceptor = LocalOnly(profile)
profile.setUrlRequestInterceptor(interceptor)
page = Page(profile, view)
view.setPage(page)
view.resize(1440, 1000)
view.show()
loop = QEventLoop()
loaded = []
page.loadFinished.connect(lambda ok: (loaded.append(ok), loop.quit()))
page.load(QUrl.fromLocalFile(str(ROOT / "assets/ui/zhenyang-ai-console.html")))
QTimer.singleShot(15000, loop.quit)
loop.exec()
assert loaded == [True]
def js(script):
results = []
wait = QEventLoop()
page.runJavaScript("(()=>{try{" + script + "}catch(e){return {error:String(e)}}})()",
lambda value: (results.append(value), wait.quit()))
QTimer.singleShot(10000, wait.quit)
wait.exec()
assert results and not (isinstance(results[0], dict) and results[0].get("error")), results
return results[0]
js('window.commands=[];window.qtBridge={selectTask:k=>commands.push(["select",k]),'
'approveTask:k=>commands.push(["approve",k]),setQueueFilter:k=>commands.push(["filter",k])};return true;')
people = [dict(key=f"M:{n}", name=f"模拟客户{n}", status="等待中", avatar="客", bubbles=1, wait="1分") for n in range(30)]
task = dict(key="M:0", name="模拟客户0", answer="旧草稿", messages=[dict(time="12:00", text="原始消息")],
stepIndex=3, steps=[], schedule={}, awaitingReview=True, canApprove=True)
for name in ("auto", "queue"):
js('window.applyState(' + json.dumps(dict(view=name, demo=False, people=people, selectedKey="M:0", task=task)) + ');return true;')
js('window.savedQueue=document.querySelector(".queue-list");window.savedRow=savedQueue.firstElementChild;'
'window.savedScreen=document.querySelector("#main>.screen");return true;')
js('window.applyState({task:{...STATE.task,answer:"更新的草稿",messages:[{time:"12:01",text:"新来的消息"}]}});return true;')
assert js('return savedScreen===document.querySelector("#main>.screen") && savedQueue===document.querySelector(".queue-list") && savedRow===savedQueue.firstElementChild;')
assert js('return document.querySelector(".answer-block").textContent;') == "更新的草稿"
assert js('return document.querySelector(".copy-block").textContent.includes("新来的消息");')
js('document.querySelector("[data-action=approve-task]").click();savedRow.click();return true;')
commands = json.loads(js('return JSON.stringify(commands.slice(-2));'))
assert commands == [["approve", "M:0"], ["select", "M:0"]], (name, commands)
js('window.applyState({task:{...STATE.task,deliveryUncertain:true,canApprove:false,needsHuman:true}});return true;')
assert js('return !document.querySelector("[data-action=approve-task]") && !document.querySelector("[data-action=retry-task]") && !document.querySelector("[data-action=cancel-task]");')
js('window.applyState({people:[...STATE.people,{key:"M:new",name:"新客户",status:"等待中",bubbles:1}]});return true;')
assert js('return document.querySelectorAll("[data-action=select-task]").length;') == 31
js('window.applyState({people:[],task:null});return true;')
assert js('return document.querySelectorAll("[data-action=select-task]").length;') == 0
assert js('return !document.querySelector(".answer-block");')
if name == "queue":
js('document.querySelector("[data-action=queue-filter][data-key=待审核]").click();return true;')
assert json.loads(js('return JSON.stringify(commands.at(-1));')) == ["filter", "待审核"]
for name in ("archive", "ai", "automation", "logs", "wechatdb", "blacklist", "proactive", "system", "auto"):
js('window.applyState({view:' + json.dumps(name) + '});return true;')
assert js('return !!document.querySelector("#main .screen") && STATE.view;') == name
assert not page.errors, page.errors
print("Desktop console passed: partial updates, new messages, task removal, review/uncertain controls, all 10 views.")
view.close()
view.deleteLater()
QTest.qWait(30)
if __name__ == "__main__":
main()