Files
OrcaSlicer/tools/mcp/test_webview_modal.py
T
SoftFever 6cd2ce1a2e Check every area of the MCP sweep with Python tests that read each step back
The sweep's objects, presets, filaments, settings, project files, slicing, gizmos and GUI tools are tested by Python
tests that compare the app's state after every step with the values their entries write out, in place of the
replays, and the existing tests' checks that could not fail now can. The calls these tests found wrong now behave
as described: moving one instance no longer sinks the others, a toolpath export writes the layers shown, an
auto-answered dialog no longer reports its call blocked, and a web dialog that has just opened takes requests.
2026-11-19 13:49:20 +08:00

128 lines
5.7 KiB
Python
Executable File

#!/usr/bin/env python3
"""Checks where webview_run_javascript runs while a modal dialog is open, where a script cannot look.
tools/mcp/orca_mcp.py launch
tools/mcp/test_webview_modal.py
Scripts run in the web view of the topmost modal dialog, whichever dialog it is (the preset bundle
dialog, by default and by locator, the export bundle dialog above it, the Flushing volumes dialog, each
known by its page's URL or title);
the Home tab's web view behind a modal is refused, so is the bundle dialog's own web view once the
export bundle dialog is above it. A page that closes its dialog replies closed at once, not after the
timeout. Without a modal, the Home tab's web view still takes scripts. Exits 0 when every check passes.
"""
import os
import sys
import time
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import orca_mcp # noqa: E402
def main():
client, _session = orca_mcp.session_client()
checks = []
def check(name, ok, detail=""):
checks.append((name, bool(ok), detail))
def refusal(**args):
try:
client.call("webview_run_javascript", **args)
return ""
except orca_mcp.McpError as e:
return str(e)
def dialogs():
return [d["title"] for d in client.call("app_state")["dialogs"]]
def wait_dialogs(count):
for _ in range(100):
if len(dialogs()) == count:
return True
time.sleep(0.1)
return False
def home_webview():
snap, texts = client.call_with_text("widget_list", target="main", query="webview")
for line in (texts[0] if texts else "").splitlines():
if "webview" in line and "[" in line:
return line.split("[", 1)[1].split("]", 1)[0]
return None
client.call("project_new", discard_changes=True)
# ---- The Home tab's web view, without and with a modal open ----
client.call("view_set", tab="home")
home = home_webview()
check("the Home tab shows a web view", home is not None)
try:
value = client.call("webview_run_javascript", code="1 + 1")["result"]
except orca_mcp.McpError as e:
value = str(e)
check("without a modal, the only web view takes a script", value == 2, value)
bundle = client.call("action_run", id="orca_command:preset_bundle:orca")
check("the bundle dialog opens", wait_dialogs(1), dialogs())
message = refusal(code="1", target=home)
check("a web view behind the modal is refused", "modal dialog is open" in message, message)
check("the bundle dialog's page takes a script", client.call("webview_run_javascript", code="document.title")["result"] == "Preset Bundle")
found = client.call("webview_run_javascript", code="!!document.getElementById('close_btn')",
target={"class": "webview", "within": "Preset Bundle"})["result"]
check("and takes one by locator", found is True, found)
# ---- A bundle dialog under the export bundle dialog ----
client.call("webview_run_javascript", code="SendWXMessage(JSON.stringify({command: 'export_page'}))")
check("the export bundle dialog opens above it", wait_dialogs(2), dialogs())
# Its web view shows about:blank until its first navigation starts.
for _ in range(50):
if "ExportPresetDialog" in client.call_with_text("widget_list", target="dialog", query="webview")[1][0]:
break
time.sleep(0.1)
message = refusal(code="1", target={"class": "webview", "within": "Preset Bundle"})
check("the dialog under the topmost is refused", "is under" in message, message)
url = client.call("webview_run_javascript", code="1")["url"]
check("without a target the topmost dialog's page is used", "ExportPresetDialog" in url, url)
client.call("dialog_answer", button="close")
check("the export dialog closes", wait_dialogs(1), dialogs())
# ---- A page that closes its dialog replies at once ----
start = time.time()
reply = client.call("webview_run_javascript", code="document.getElementById('close_btn').click()")
elapsed = time.time() - start
check("closing the dialog from its page replies closed", reply.get("closed") is True, reply)
check("and does so within a second", elapsed < 1.0, f"{elapsed:.2f} s")
client.await_job(bundle)
check("no dialog is left", wait_dialogs(0), dialogs())
# ---- The Flushing volumes dialog takes scripts as any topmost dialog does ----
client.call("view_set", tab="prepare")
client.call("widget_click", target={"label": "Add one filament"})
flush = client.call("widget_click", target={"label": "Flushing volumes"})
check("the Flushing volumes dialog opens",
wait_dialogs(1) and dialogs() == ["Flushing volumes for filament change"], dialogs())
# Its web view, too, shows about:blank until its first navigation starts.
for _ in range(50):
if "WipingDialog.html" in client.call_with_text("widget_list", target="dialog", query="webview")[1][0]:
break
time.sleep(0.1)
try:
reply = client.call("webview_run_javascript", code="typeof document.title")
except orca_mcp.McpError as e:
reply = {"error": str(e)}
check("its page takes a script",
reply.get("result") == "string" and "web/flush/WipingDialog.html" in reply.get("url", ""), reply)
client.call("dialog_answer", button="close")
client.await_job(flush)
check("no dialog is left at the end", wait_dialogs(0), dialogs())
failed = [c for c in checks if not c[1]]
for name, ok, detail in checks:
print(("PASS " if ok else "FAIL ") + name + ("" if ok or detail == "" else f": {detail}"))
print(f"{len(checks) - len(failed)}/{len(checks)} checks passed")
return 1 if failed else 0
if __name__ == "__main__":
sys.exit(main())