"""Prepare or verify both IO renames in one portable-project editor session.""" import json import shutil import sys from pathlib import Path from mcp_smoke import Client def prepare(sample, root): shutil.copytree(sample, root) hardware = root / "configuration/hardware.json" config = json.loads(hardware.read_bytes()) for kind in ("DI", "DQ"): config[kind][0]["Name"] = "夹具信号" config["Cylinders"][0].update(InA="夹具信号", OutB="夹具信号") hardware.write_text(json.dumps(config, ensure_ascii=False, indent=2), encoding="utf-8") caller, = (p for p in root.rglob("*.cs") if "ControllersModel.Axes" in p.read_text(encoding="utf-8")) source = caller.read_text(encoding="utf-8") lookups = "\n".join( f' _ = model.ControllersModel.Controller.{kind}.FirstOrDefault(p => p.Name == {name});' for kind in ("DI", "DQ") for name in ('"夹具信号"', 'flow.Step.ToString()')) caller.write_bytes(source[:source.index(" _ =")].encode() + (lookups + "\n }\n}\n").encode()) def verify(exe, root, expected_source="editor"): def snapshot(): return {p.relative_to(root): p.read_bytes() for p in root.rglob("*") if p.is_file()} original = snapshot() client = Client(exe, root, "--allow-write") try: assert client.call("get_project_summary")["source"] == expected_source saved = [] for kind, field, name in (("DI", "InA", "夹具到位"), ("DQ", "OutB", "夹具松开")): channel, = client.call("list_objects", kind=kind)["items"] before = snapshot() draft = client.call("preview_io_rename", kind=kind, id=channel["id"], newName=name) plan = draft["Rename"] script, = plan["Scripts"] link, = plan["CylinderReferences"] manual, = plan["ManualReferences"] assert link["Field"] == field and plan["Compiled"] and manual["confidence"] == "dynamic" assert before == snapshot() print(kind, name, manual["expression"], manual["line"], flush=True) assert client.call("apply_changes", draftId=draft["DraftId"])["Status"] == "saved" assert client.call("get_object", kind=kind, id=channel["id"])["Name"] == name cylinder = client.call("get_object", kind="Cylinders", id=link["Id"]) assert cylinder[field] == name assert f'{kind}.FirstOrDefault(p => p.Name == "{name}")' in script["After"] assert client.call("get_object", kind="Workflows", id=script["Id"])["Code"] == script["After"] assert {p for p,v in snapshot().items() if v != before[p]} == {Path(draft["Path"]), Path(script["Path"])} saved.append((draft["DraftId"], before)) for draft_id, before in reversed(saved): undo = client.call("preview_undo", draftId=draft_id) assert undo["Rename"]["Compiled"] assert client.call("apply_changes", draftId=undo["DraftId"])["Status"] == "saved" assert client.call("get_change_status", draftId=draft_id)["Status"] == "undone" assert snapshot() == before assert snapshot() == original print(f"PASS: DI/DQ batch, exact restore of {len(original)} files.") finally: client.close() if __name__ == "__main__": mode, first, second = sys.argv[1:] if mode == "prepare": prepare(Path(first), Path(second)) else: verify(Path(first), Path(second), {"verify": "editor", "verify-directory": "directory"}[mode])