#!/usr/bin/env python3 """The forms a call takes, as the test_call_forms entry of tools/mcp/test-entries/gui_tools.md designs them. tools/mcp/orca_mcp.py launch --cad tools/mcp/test_call_forms.py [--out DIR] It needs the Design tab (launch with --cad). The files it exports go to --out, else to a new temporary folder. Exits 0 when every check passes. """ import argparse import os import struct import sys import tempfile import threading import time sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import orca_mcp # noqa: E402 from checks import GENERIC_PLA, KLIPPER, KLIPPER_STD, Checks, plates, run, start, until, wait # noqa: E402 REPO = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) CUBE = os.path.join(REPO, "resources", "handy_models", "OrcaCube_v2.drc") NAME = "OrcaCube_v2.drc" PREFERENCES = "orca_command:open_preferences:orca" # What replay_export lists unfinished while the second client's app_wait runs. WAITING = [{"tool": "app_wait"}] def stl(path): """A binary STL's triangle count and its size in bytes.""" with open(path, "rb") as f: head = f.read(84) return struct.unpack("= 1000, reply) self.t.check(f"{s} 11: replay_export lists nothing unfinished", "left_out_running" not in self.c.call("replay_export"), self.c.call("replay_export").get("left_out_running")) self.t.equal(f"{s} 11: no dialog", self.dialogs(), []) def apart_on_plate_0(self, label): objects = self.objects() self.t.equal(f"{label}: both objects on plate 0", [o["plate"] for o in objects], [0, 0]) item = self.c.call("plate_list")["items"][0] (x, y), (w, d) = item["origin"], item["size"] for o in objects: box = o["bbox"] inside = box["min"][0] >= x and box["min"][1] >= y and box["max"][0] <= x + w and box["max"][1] <= y + d self.t.check(f"{label}: {o['name']} lies inside plate 0", inside, (box, (x, y, w, d))) a, b = (o["bbox"] for o in objects) apart = (a["max"][0] <= b["min"][0] or b["max"][0] <= a["min"][0] or a["max"][1] <= b["min"][1] or b["max"][1] <= a["min"][1]) self.t.check(f"{label}: the objects' footprints are apart", apart, (a, b)) def test_dialog(self): s = "a test's dialog, asked or silent" self.start(s) project = self.state()["project"] self.t.equal(f"{s}: the start: the project's name", project.get("name"), "Untitled") self.t.equal(f"{s}: the start: no preset modified", self.modified(), []) job = self.c.call("calibration_generate_test", test="temperature") self.t.equal(f"{s} 1: the test asks in its dialog", job.get("status"), "blocked_by_dialog") self.t.equal(f"{s} 1: the dialogs open", self.dialogs(), ["Temperature calibration"]) self.c.call("dialog_answer", dialog={"title": "Temperature calibration"}, button="cancel") self.job_ends(f"{s} 2: the step 1 call", job) wait(self.c, "idle") self.t.equal(f"{s} 2: the dialogs open", self.dialogs(), []) self.t.equal(f"{s} 2: Cancel adds no object", self.objects(), []) project = self.state()["project"] self.t.equal(f"{s} 2: the project is as it was", (project.get("name"), project.get("dirty")), ("Untitled", False)) self.t.equal(f"{s} 2: no preset modified", self.modified(), []) reply = self.run("calibration_generate_test", test="temperature", silent=True, discard_changes=True, preset_changes="discard") wait(self.c, "idle") self.t.equal(f"{s} 3: the reply", (reply.get("test"), reply.get("names")), ("temperature", ["temperature_tower.drc"])) self.t.equal(f"{s} 3: the names", self.names(), ["temperature_tower.drc"]) self.t.equal(f"{s} 3: the project's name", self.state()["project"].get("name"), "Nozzle temperature test") self.t.equal(f"{s} 3: silent asks nothing", self.dialogs(), []) def shown_view(self): s = "the shown view" self.start(s) self.run("project_import", paths=[CUBE]) self.run("object_transform", object=0, size=[20, 20, 20]) objects = self.objects() self.t.equal(f"{s}: the start: one object on plate 0", [o["plate"] for o in objects], [0]) self.t.near(f"{s}: the start: 20 mm", objects[0]["bbox"]["size"] if objects else None, [20, 20, 20], 0.01) self.run("view_set", tab="prepare", camera="front") self.t.equal(f"{s} 1: the tab", self.state()["tab"], "prepare") self.t.near(f"{s} 1: the Prepare view's elevation", self.camera()["elevation"], 0, 0.1) reply = self.run("view_set", tab="design", camera="front") self.t.near(f"{s} 2: the reply's elevation", (reply.get("camera_state") or {}).get("elevation"), 0, 0.1) self.t.equal(f"{s} 2: the tab", self.state()["tab"], "design") self.t.near(f"{s} 2: the Design tab's elevation", self.camera()["elevation"], 0, 0.1) reply = self.run("action_run", id="view_top") self.t.equal(f"{s} 3: the action run", reply.get("ran"), "orca_command:view_top:orca") self.t.near(f"{s} 3: the reply's elevation", (reply.get("camera_state") or {}).get("elevation"), 90, 0.1) self.t.equal(f"{s} 3: the tab", self.state()["tab"], "design") self.t.near(f"{s} 3: the Design tab's elevation", self.camera()["elevation"], 90, 0.1) self.run("view_set", tab="prepare") self.t.equal(f"{s} 4: the tab", self.state()["tab"], "prepare") self.t.near(f"{s} 4: View: Top left the Prepare view's camera", self.camera()["elevation"], 0, 0.1) self.run("object_select", objects=[0]) self.run("widget_click", target={"gizmo": "Support Painting"}) self.selected(f"{s} 5", 0) self.t.equal(f"{s} 5: the gizmo open", (self.state().get("gizmo") or {}).get("name"), "FdmSupports") reply = self.run("action_run", id="deselect_all") self.t.equal(f"{s} 6: the reply", (reply.get("ran"), reply.get("empty"), reply.get("canvas")), ("orca_command:deselect_all:orca", True, "3d")) self.selected(f"{s} 6") self.t.equal(f"{s} 6: Deselect All closes the open gizmo too", self.state().get("gizmo"), None) reply = self.run("plate_slice", preview=True) self.t.equal(f"{s} 7: the slice", (reply.get("status"), [p.get("outcome") for p in reply.get("plates", [])]), ("done", ["ok"])) self.t.equal(f"{s} 7: the tab", self.state()["tab"], "preview") self.t.equal(f"{s} 7: plate 0 is sliced", self.c.call("plate_list")["items"][0]["sliced"], True) self.run("action_run", id="go_to_layer", args={"percent": 50}) view = self.c.call("plate_slice_stats", include=["view"])["view"] self.t.equal(f"{s} 8: the layers", view.get("layer_count"), 100) self.t.equal(f"{s} 8: the range shown", view.get("layers"), [1, 51]) def run_all(self): self.t.equal("the start: the presets", start(self.c), {"printer": KLIPPER, "process": KLIPPER_STD, "filaments": [GENERIC_PLA]}) for name, scenario in [("names and values agents send", self.agent_names), ("a test's dialog, asked or silent", self.test_dialog), ("the shown view", self.shown_view)]: self.t.scenario(name, scenario) return self.t.exit_code() def main(): ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument("--out", help="folder for the exported files (default: a new temporary folder)") args = ap.parse_args() folder = args.out or tempfile.mkdtemp(prefix="orca-call-forms-") os.makedirs(folder, exist_ok=True) print(f"output folder: {folder}", flush=True) client, session = orca_mcp.session_client() return CallFormsTest(client, session, folder).run_all() if __name__ == "__main__": sys.exit(main())