"""Verify three hardware renames and reverse-order undo in one live PLCSharp editor.""" import json import shutil import sys from pathlib import Path from mcp_smoke import Client DEVICES = (("Cylinders", "夹具", "定位夹具"), ("Robots", "R1", "搬运机器人"), ("Connects", "PLC", "主站连接")) def prepare(sample, root): shutil.copytree(sample, root) caller, = (p for p in root.rglob("*.cs") if "Controller.DI.FirstOrDefault" in p.read_text(encoding="utf-8")) source = caller.read_text(encoding="utf-8") lines = [ 'model.CylindersModel.Cylinders.FirstOrDefault(c => c.Name == "夹具")', 'model.CylindersModel.Cylinders.FirstOrDefault(c => c.Name == flow.Step.ToString())', 'model.GetRobot("R1")', 'model.GetRobot(flow.Step.ToString())', 'model.GetConnect("PLC")', 'model.GetConnect(flow.Step.ToString())', ] pos = source.rindex(" }") caller.write_bytes((source[:pos] + "".join(f" _ = {line};\n" for line in lines) + source[pos:]).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() hw = Path("configuration/hardware.json") client = Client(exe, root, "--allow-write") try: assert client.call("get_project_summary")["source"] == "editor" history, manual = [], [] for kind, old, new in DEVICES: item, = (r for r in client.call("list_objects", kind=kind)["items"] if r["name"] == old) before = snapshot() draft = client.call("preview_device_rename", kind=kind, id=item["id"], newName=new) plan = draft["Rename"] edit, = plan["Scripts"] hit, = plan["ManualReferences"] manual.append((kind, hit["Name"], hit["line"], hit["expression"])) assert plan["Compiled"] and hit["confidence"] == "dynamic" and before == snapshot() assert edit["After"] == edit["Before"].replace(f'"{old}"', f'"{new}"') print("PREVIEW", draft["DraftId"], manual[-1], flush=True) assert client.call("apply_changes", draftId=draft["DraftId"])["Status"] == "saved" assert client.call("get_object", kind=kind, id=item["id"])["Name"] == new assert client.call("get_object", kind="Workflows", id=edit["Id"])["Code"] == edit["After"] after = snapshot() assert set(after) == set(before) assert {p for p in before if after[p] != before[p]} == {hw, Path(edit["Path"])} expected = json.loads(before[hw]) next(r for r in expected[kind] if r["ID"].lower() == item["id"].lower())["Name"] = new assert json.loads(after[hw]) == expected history.append((draft["DraftId"], before, kind, item["id"], old)) for draft_id, before, kind, object_id, old in reversed(history): undo = client.call("preview_undo", draftId=draft_id) assert undo["Rename"]["Compiled"] and undo["Rename"]["UndoOf"] == draft_id assert client.call("apply_changes", draftId=undo["DraftId"])["Status"] == "saved" assert client.call("get_change_status", draftId=draft_id)["Status"] == "undone" assert client.call("get_object", kind=kind, id=object_id)["Name"] == old assert before == snapshot() assert original == snapshot() print("PASS", len(original), "files;", sum(map(len, original.values())), "bytes;", manual) finally: client.close() if __name__ == "__main__": {"verify": verify, "prepare": prepare}[sys.argv[1]](*map(Path, sys.argv[2:]))