67 lines
3.3 KiB
Python
67 lines
3.3 KiB
Python
"""Prepare a portable sample copy, open it in PLCSharp and enable AI access, then run verify."""
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
import shutil
|
|
|
|
from mcp_smoke import Client
|
|
|
|
|
|
def prepare(sample, directory):
|
|
shutil.copytree(sample, directory)
|
|
script, = directory.rglob("*.cs")
|
|
source = (
|
|
"using PLCSharp.Models;\nusing PLCSharp.Core.Common;\n"
|
|
f"public class Extension_{script.stem.replace('-', '')} {{\n"
|
|
" public void Run(GlobalModel model, FlowModel flow) {\n"
|
|
' _ = model.GetVariable("计数");\n'
|
|
' _ = model.GetVariable(flow.Step.ToString());\n'
|
|
" }\n}\n"
|
|
)
|
|
script.write_bytes(source.encode("utf-8"))
|
|
|
|
|
|
def verify(exe, directory):
|
|
before = {p: p.read_bytes() for p in directory.rglob("*") if p.is_file()}
|
|
client = Client(exe, directory, "--allow-write")
|
|
try:
|
|
summary = client.call("get_project_summary")
|
|
assert summary["source"] == "editor"
|
|
recipe = summary["DefaultRecipeId"]
|
|
var, = client.call("list_objects", kind="Variables", recipeId=recipe)["items"]
|
|
draft = client.call("preview_variable_rename", id=var["id"], newName="完成计数")
|
|
plan = draft["Rename"]
|
|
script, = plan["Scripts"]
|
|
manual, = plan["ManualReferences"]
|
|
assert draft["Status"] == "pending" and script["Review"]["Compiled"]
|
|
assert manual["confidence"] == "dynamic" and manual["line"] == 6
|
|
assert 'GetVariable("完成计数")' in script["After"] and manual["expression"] in script["After"]
|
|
print(json.dumps({"source": summary["source"], "dynamic": manual, "compiled": True}, ensure_ascii=False), flush=True)
|
|
assert all(p.read_bytes() == old for p, old in before.items())
|
|
saved = client.call("apply_changes", draftId=draft["DraftId"])
|
|
assert saved["Status"] == "saved"
|
|
assert client.call("get_object", kind="Variables", id=var["id"])["Name"] == "完成计数"
|
|
assert client.call("get_object", kind="Workflows", id=script["Id"])["Code"] == script["After"]
|
|
assert {p for p, old in before.items() if p.read_bytes() != old} == {
|
|
directory / "configuration/variables.json", directory / script["Path"]}
|
|
undo = client.call("preview_undo", draftId=draft["DraftId"])
|
|
assert undo["Rename"]["Compiled"] and undo["Rename"]["UndoOf"] == draft["DraftId"]
|
|
assert client.call("apply_changes", draftId=undo["DraftId"])["Status"] == "saved"
|
|
assert client.call("get_change_status", draftId=draft["DraftId"])["Status"] == "undone"
|
|
assert client.call("get_object", kind="Variables", id=var["id"])["Name"] == "计数"
|
|
assert client.call("get_object", kind="Workflows", id=script["Id"])["Code"] == script["Before"]
|
|
assert before == {p: p.read_bytes() for p in directory.rglob("*") if p.is_file()}
|
|
print("PASS: live editor rename, confirmation, undo and byte-for-byte restore.")
|
|
finally:
|
|
client.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("mode", choices=["prepare", "verify"])
|
|
parser.add_argument("source", type=Path, help="Sample directory for prepare, MCP executable for verify")
|
|
parser.add_argument("project", type=Path)
|
|
args = parser.parse_args()
|
|
(prepare if args.mode == "prepare" else verify)(args.source, args.project)
|
|
|