"""Exercise four project object families in one WPF session and compare raw files after each undo.""" import json from pathlib import Path import shutil import sys from mcp_smoke import Client NAMES = {"Recipes": "验收配方", "Workflows": "验收流程", "VisionFunctions": "验收视觉", "CustomControls": "验收控件"} def prepare(source, root): shutil.copytree(source, root) 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(" }") lines = [] for expression, kind in (("model.GetTask", "Workflows"), ("model.GetVisionFunction", "VisionFunctions"), ("model.GetCustomControl", "CustomControls")): for arg in (json.dumps(NAMES[kind], ensure_ascii=False), "flow.Step.ToString()"): lines.append(f" _ = {expression}({arg});\n") for arg in ('"验收配方"', 'flow.Step.ToString()'): lines.append(f" _ = model.Recipes.FirstOrDefault(r => r.Name == {arg});\n") caller.write_bytes((code[:pos] + "".join(lines) + code[pos:]).encode()) path = root / "configuration" / "vision.json" doc = json.loads(path.read_bytes()) doc["VisionFunctions"][0]["ControlName"] = NAMES["CustomControls"] path.write_bytes(json.dumps(doc, indent=2, ensure_ascii=False).encode()) def verify(exe, root): def snapshot(): return {p.relative_to(root): p.read_bytes() for p in root.rglob("*") if p.is_file()} original = snapshot() history, ids = [], {} client = Client(exe, root, "--allow-write") try: summary = client.call("get_project_summary") assert summary["source"] == "editor" recipe = summary["currentRecipeId"] for action in ("create", "update", "delete"): for kind, name in NAMES.items(): before = snapshot() if action == "create": fields = {"Name": name} if kind != "Recipes": fields["RecipeID"] = recipe args = {"properties": fields} else: args = {"id": ids[kind]} if action == "update": args["properties"] = {"Comment": "批次验收"} draft = client.call("preview_object_edit", action=action, kind=kind, **args) edit = draft["Edit"] print(action, kind, draft["Status"], [(f["Name"], f["Compiled"]) for f in edit["Workflows"]], flush=True) assert draft["Status"] == "pending" and edit["Compiled"] and before == snapshot() if action == "delete": print("IMPACT", kind, json.dumps({k: edit[k] for k in ("WorkflowReferences", "ConfigReferences", "ManualReferences")}, ensure_ascii=False), flush=True) assert edit["WorkflowReferences"] and edit["ManualReferences"] if kind == "CustomControls": assert any(r["field"] == "ControlName" for r in edit["ConfigReferences"]) assert client.call("apply_changes", confirmed=True, draftId=draft["DraftId"])["Status"] == "saved" after = snapshot() changed = {p for p in before.keys() | after.keys() if before.get(p) != after.get(p)} assert changed == set(map(Path, edit["ChangedFiles"])) history.append((draft["DraftId"], before)) ids[kind] = edit["Id"] for draft_id, before in reversed(history): undo = client.call("preview_undo", draftId=draft_id) assert undo["Edit"]["Compiled"] and undo["Edit"]["UndoOf"] == draft_id print("UNDO", undo["Edit"]["Kind"], undo["Edit"]["Action"], flush=True) assert client.call("apply_changes", confirmed=True, draftId=undo["DraftId"])["Status"] == "saved" assert client.call("get_change_status", draftId=draft_id)["Status"] == "undone" assert snapshot() == before assert snapshot() == original print("PASS", len(history), "saves and undos;", len(original), "files;", sum(map(len, original.values())), "bytes") finally: client.close() if __name__ == "__main__": {"prepare": prepare, "verify": verify}[sys.argv[1]](*map(Path, sys.argv[2:]))