112 lines
6.3 KiB
Python
112 lines
6.3 KiB
Python
"""Exercise the unified MCP tool on a disposable copy of the portable vision fixture."""
|
|
import json
|
|
from pathlib import Path
|
|
import shutil
|
|
import sys
|
|
import tempfile
|
|
|
|
from mcp_smoke import Client
|
|
|
|
|
|
def prepare(root):
|
|
def config(name, patch):
|
|
path = root / "configuration" / (name + ".json")
|
|
doc = json.loads(path.read_bytes())
|
|
patch(doc)
|
|
path.write_text(json.dumps(doc, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
|
|
def points(doc):
|
|
for kind, name in [("Matrices", "轴阵列"), ("RobotMatrices", "机器人阵列"), ("InterpolationGroups", "轨迹")]:
|
|
doc[kind][0]["Name"] = name
|
|
doc["InterpolationGroups"][0]["Interpolations"] = [
|
|
{"Name": "线段", "Type": 0, "SerializedInterpolations": json.dumps([{"Name": "终点", "X": 2, "Y": 3}]), "Params": {}}
|
|
]
|
|
|
|
def vision(doc):
|
|
f = doc["VisionFunctions"][0]
|
|
f["ControlName"] = "显示"
|
|
f["VisionParams"]["Variables"] = [{"Name": "读数", "VarType": "Double", "StrValue": "7"}]
|
|
for typ, args in [(18, {"TransformMat": "状态", "InputVar": "读数"}), (7, {"CustomControlName": "显示"})]:
|
|
f["VisionFlows"].append(dict(Type=typ, Comment="", StringParams=args, DoubleParams={}, LongParams={}, IntParams={}, BoolParams={}))
|
|
|
|
config("points", points)
|
|
config("vision", vision)
|
|
config("interface", lambda d: d["CustomControls"][0]["RangeValues"].append({"Name": "测量"}))
|
|
caller, = (p for p in root.rglob("*.cs") if "model.VisionsModel.Cameras" in p.read_text(encoding="utf-8"))
|
|
code = caller.read_text(encoding="utf-8")
|
|
pos = code.rindex(" }")
|
|
fixture = """
|
|
_ = model.CurrentRecipe.Name == "First";
|
|
_ = model.VariablesModel.SystemVariables.First(v => v.Name == "状态");
|
|
_ = model.ControllersModel.Matrices.First(v => v.Name == "轴阵列");
|
|
_ = model.GetRobot("R1").RobotMatrices.First(v => v.Name == "机器人阵列");
|
|
_ = model.GetCustomControl("显示");
|
|
_ = model.GetVisionFunction("检查").Params.Variables.First(v => v.Name == "读数");
|
|
model.GetCustomControl("显示").SetMeasurementValue("测量", 0);
|
|
model.GetCustomControl("显示").SetMeasurementValue(flow.Step.ToString(), 0);
|
|
_ = model.ControllersModel.InterpolationGroups.First(g => g.Name == "轨迹").Interpolations.First(s => s.Name == "线段").InterpolationPoints.First(p => p.Name == "终点");
|
|
"""
|
|
caller.write_bytes((code[:pos] + fixture + code[pos:]).encode())
|
|
|
|
|
|
def run(exe, source):
|
|
with tempfile.TemporaryDirectory(prefix="plcsharp-all-names-") as folder:
|
|
root = Path(folder) / "sample"
|
|
shutil.copytree(source, root)
|
|
prepare(root)
|
|
snapshot = lambda: {p.relative_to(root): p.read_bytes() for p in root.rglob("*") if p.is_file()}
|
|
baseline = snapshot()
|
|
client = Client(exe, root, "--allow-write")
|
|
try:
|
|
summary = client.call("get_project_summary")
|
|
ids = {}
|
|
protected = {"Recipes", "Axes", "AxisPoints", "RobotPoints", "Matrices", "RobotMatrices", "InterpolationGroups",
|
|
"InterpolationSegments", "InterpolationPoints", "DI", "DQ", "Connects", "Cylinders", "Robots", "Cameras"}
|
|
for kind in summary["kinds"]:
|
|
if kind in {"Controllers", "CanvasConfigs"}:
|
|
continue
|
|
row = client.call("list_objects", kind=kind)["items"][0]
|
|
ids[kind] = row["id"]
|
|
if kind in protected:
|
|
denied = client.request("tools/call", {"name": "preview_object_rename",
|
|
"arguments": {"kind": kind, "id": row["id"], "newName": "QA_" + kind}})
|
|
assert denied["isError"] and snapshot() == baseline
|
|
continue
|
|
draft = client.call("preview_object_rename", kind=kind, id=row["id"], newName="QA_" + kind)
|
|
assert draft["Rename"]["Compiled"], draft
|
|
assert len(ids) == 20 and snapshot() == baseline
|
|
order = [("Recipes", "Recipes"), ("Matrices", "Matrices"), ("RobotMatrices", "RobotMatrices"),
|
|
("SystemVariables", "SystemVariables"), ("CustomControls", "CustomControls"),
|
|
("InterpolationGroups", "InterpolationGroups"), ("VisionLocalVariables", "VisionFunctions"),
|
|
("ControlMeasurements", "CustomControls"), ("InterpolationSegments", "InterpolationGroups"),
|
|
("InterpolationPoints", "InterpolationGroups")]
|
|
members = dict(VisionLocalVariables="读数", ControlMeasurements="测量", InterpolationSegments="线段", InterpolationPoints="终点")
|
|
history = []
|
|
order = [(kind, parent) for kind, parent in order if kind not in protected]
|
|
for kind, parent in order:
|
|
options = {"memberName": members[kind]} if kind in members else {}
|
|
if kind == "InterpolationPoints":
|
|
options["segmentName"] = "QA_InterpolationSegments"
|
|
before = snapshot()
|
|
draft = client.call("preview_object_rename", kind=kind, id=ids[parent], newName="QA_" + kind, **options)
|
|
plan = draft["Rename"]
|
|
assert plan["Compiled"] and len(plan["Scripts"]) == 1 and snapshot() == before, (kind, plan)
|
|
print("SAVE", kind, plan["OldName"], "manual", [(h.get("line"), h["expression"]) for h in plan["ManualReferences"]], flush=True)
|
|
assert client.call("apply_changes", confirmed=True, draftId=draft["DraftId"])["Status"] == "saved"
|
|
assert snapshot() != before
|
|
history.append((kind, draft["DraftId"], before))
|
|
for kind, draft_id, before in reversed(history):
|
|
undo = client.call("preview_undo", draftId=draft_id)
|
|
assert undo["Rename"]["Compiled"]
|
|
print("UNDO", kind, flush=True)
|
|
assert client.call("apply_changes", confirmed=True, draftId=undo["DraftId"])["Status"] == "saved"
|
|
assert before == snapshot(), kind
|
|
assert snapshot() == baseline
|
|
print("PASS", len(ids), "object kinds;", len(order), "saves and undos;", len(baseline), "files;", sum(map(len, baseline.values())), "bytes")
|
|
finally:
|
|
client.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run(*map(Path, sys.argv[1:]))
|