Files
OrcaSlicer/tools/mcp/test_set_view_preview.py
T
SoftFever d64e10f4dd Regroup the slicer's MCP commands and let each run from its arguments
Outside the Design tab an agent had 175 commands, 50 of them in the tool
list. There are now 118, named by what they act on (`object_`, `plate_`,
`preset_`, `filament_`, `plugin_` and so on), and 115 are listed. Each takes
its target and values in the call, acts on the selection when it names no
target and says what it acted on; an old name is refused. Printers and
system filaments install and uninstall, preferences read and write, plugins
enable, reload, uninstall and refresh, and objects reorder, with no dialog
to drive.

The Speed Dial keeps its 129 tiles under the same ids. Eight defects in the
rows these commands share with the menus are fixed. For a person: an object
row that cannot run says why instead of "Select an object first.", rows
that failed no longer claim success, Duplicate Plate can be undone, and
Refresh Plugins picked while a refresh runs now runs after it instead of
beside it.
2026-11-18 16:50:32 +08:00

148 lines
8.0 KiB
Python
Executable File

#!/usr/bin/env python3
"""Checks what view_set does to the Preview of a sliced cube where a replay cannot look.
tools/mcp/orca_mcp.py launch
tools/mcp/test_set_view_preview.py
Picking a view type is the legend combo's pick: a feature type hidden by a click on its legend row
shows again when Line Type is picked, and the pick is written to the app config for the "Last used"
preview default. A canvas screenshot taken straight after a layer change shows the new layers, not
the previous ones under the new slider position, and one after a view type change shows the legend
laid out for it. A range given backwards shows its first layer alone, and in the slider's one-layer
mode the reply still echoes the range asked for while the one layer shown is its first. The refusals:
legend rows before a slice, a height above the top layer, layer with
layer_z, an unknown legend row (named with the rows the print has), a value that is not true or
false, and a silent-mode estimate the print does not have; a refused call changes nothing. Exits 0
when every check passes.
"""
import json
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()
conf = os.path.join(session["datadir"], "OrcaSlicer.conf")
checks = []
def check(name, ok, detail=""):
checks.append((name, bool(ok), detail))
def refusal(**args):
try:
client.call("view_set", **args)
return ""
except orca_mcp.McpError as e:
return str(e)
def legend():
return client.call("plate_slice_stats", include=["view"])["view"]["legend"]
def last_view_type(expected):
# The app writes its config at an idle moment, so wait for it.
value = None
for _ in range(50):
try:
with open(conf, encoding="utf-8") as f:
text = f.read()
# The JSON is followed by a checksum line on Windows.
value = json.loads(text[:text.rfind("}") + 1])["app"].get("preview_last_view_type")
except (OSError, ValueError, KeyError):
pass
if value == expected:
break
time.sleep(0.1)
return value
client.call("dialog_auto_answer", rules=[{"match": "Uniform scale", "button": "ok"}])
client.call("project_new", discard_changes=True)
check("legend rows need a slice", "slice first" in refusal(legend={"travel": False}))
client.call("action_run", id="orca_command:add_primitive_cube:orca")
client.call("app_wait", **{"for": "idle"})
sliced = client.await_job(client.call("plate_slice"))["plates"][0]
client.call("view_set", view_type="feature_type", layer_range=[1, 128])
# A click on the legend row and plate_slice_stats's legend are one state. The snapshot draws the legend
# first, since a window the desktop hides may not have drawn it since the slice.
client.call("widget_list", query="Sparse infill")
client.call("widget_click", target={"imgui_name": "Sparse infill"})
check("a legend row's click hides its feature type", legend()["features"]["Sparse infill"] is False)
client.call("view_set", view_type="speed")
check("the pick is remembered", last_view_type("speed") == "speed", last_view_type("speed"))
check("another view type keeps it hidden", legend()["features"]["Sparse infill"] is False)
client.call("view_set", view_type="feature_type")
check("picking Line Type shows it again", legend()["features"]["Sparse infill"] is True)
check("Line Type is remembered", last_view_type("feature_type") == "feature_type", last_view_type("feature_type"))
# Each change is captured twice. One replay runs the calls back to back, which seldom leaves the app
# an idle frame between a change and its first capture to draw the change before the capture does.
# The last view type is the one picked above.
out_dir = os.path.join(session["datadir"], "set_view_captures")
changes = [{"layer_range": [1, 40]}, {"view_type": "speed"}, {"layer_range": [64, 64]}, {"view_type": "height"},
{"layer_range": [20, 100]}, {"view_type": "fan_speed"}, {"layer_range": [1, 128]}, {"view_type": "feature_type"}]
steps = []
for i, change in enumerate(changes):
steps.append({"tool": "view_set", "args": change})
steps += [{"tool": "window_screenshot", "args": {"target": "canvas", "save_path": f"${{out_dir}}/{i}{shot}.png",
"overwrite": True, "include_image": False}}
for shot in "ab"]
ran = client.await_job(client.call("replay_run", replay={"orca_replay_version": "0.2.0", "name": "captures after view changes", "steps": steps},
out_dir=out_dir))
check("the capture replay runs", ran.get("passed") is True, ran)
if ran.get("passed") is True:
def png(name):
with open(os.path.join(out_dir, name), "rb") as f:
return f.read()
a = [png(f"{i}a.png") for i in range(len(changes))]
b = [png(f"{i}b.png") for i in range(len(changes))]
stale = [c for c, first, second in zip(changes, a, b) if first != second]
unchanged = [c for c, first, before in zip(changes[1:], a[1:], b) if first == before]
check("a capture right after a layer or view type change shows it settled", not stale, f"stale after {stale}")
check("every change shows in the capture", not unchanged, f"unchanged after {unchanged}")
reply = client.call("view_set", legend={"Sparse infill": False, "travel": True}, layer_z=1.05)
check("layer_z between two tops shows up to the upper one", reply.get("layers") == [1, 6, 128] and reply.get("z") == [0.2, 1.2], reply)
check("the legend reply gives each row's state", reply.get("legend") == {"Sparse infill": False, "travel": True}, reply)
reply = client.call("view_set", layer_range=[60, 20])
check("a range given backwards shows its first layer alone", reply.get("layers") == [60, 60, 128] and reply.get("z") == [12.0, 12.0],
reply)
# L switches the layer slider's one-layer mode.
client.call("keyboard_press", keys="L")
reply = client.call("view_set", layer_range=[2, 5])
shown = client.call("app_state")["preview"]
check("in one-layer mode the reply echoes the range and the slider shows its first layer",
reply.get("layers") == [2, 5, 128] and reply.get("z") == [0.4, 0.4] and shown.get("layers") == [2, 2], (reply, shown))
client.call("keyboard_press", keys="L")
shown = client.call("app_state")["preview"]
check("leaving one-layer mode shows every layer again", shown.get("layers") == [1, 128], shown)
message = refusal(layer_z=80)
check("a height above the top layer is refused", "above the top layer (25.6 mm)" in message, message)
check("layer with layer_z is refused", "not both" in refusal(layer=3, layer_z=1.0))
message = refusal(view_type="speed", legend={"Infill": False})
check("an unknown row is refused with the print's rows", "Sparse infill" in message and "travel" in message, message)
check("a refused call leaves the view type", client.call("app_state")["preview"]["view_type"] == "feature_type")
check("a value that is not true or false is refused", "true (show) or false (hide)" in refusal(legend={"travel": "yes"}))
if "silent_time_s" not in sliced:
message = refusal(time_mode="silent")
check("silent mode without its own estimate is refused", "no separate silent-mode estimate" in message, message)
check("normal mode is always offered", client.call("view_set", time_mode="normal").get("time_mode") == "normal")
client.call("dialog_auto_answer", rules=[])
client.call("project_new", discard_changes=True)
failures = 0
for name, ok, detail in checks:
print(("ok " if ok else "FAIL ") + name + ("" if ok or detail == "" else f": {detail}"))
failures += not ok
return 1 if failures else 0
if __name__ == "__main__":
sys.exit(main())