plcsharp/Tests/mcp_script_dialog_smoke.py

61 lines
3.2 KiB
Python

"""Interactive acceptance test. Click Cancel in the first WPF dialog and Confirm Save in the second."""
from pathlib import Path
import shutil
import sys
import tempfile
from mcp_smoke import Client
def verify(exe, sample):
with tempfile.TemporaryDirectory() as folder:
project = Path(folder) / "脚本验收副本"
shutil.copytree(sample, project)
client = Client(exe, project, "--allow-write")
try:
recipe = client.call("get_project_summary")["DefaultRecipeId"]
flow, = client.call("list_objects", kind="Workflows", recipeId=recipe)["items"]
flow_id = flow["id"]
script = next(project.rglob("*.cs"))
before = {p: p.read_bytes() for p in project.rglob("*") if p.is_file()}
source = (f'using PLCSharp.Models;\nusing PLCSharp.Core.Common;\n\n'
f'public class Extension_{flow_id.replace("-", "")}\n{{\n'
' public void Run(GlobalModel model, FlowModel flow)\n {\n'
' _ = model.GetVariable("计数");\n }\n}\n')
def preview(code):
return client.call("preview_workflow_script", id=flow_id, code=code)
invalid = preview(source.replace("GetVariable", "MissingLookup"))
assert invalid["Status"] == "invalid" and not invalid["Review"]["Compiled"]
assert any(d["Code"] == "CS1061" and d["Line"] == 8 for d in invalid["Review"]["Diagnostics"])
refused = client.request("tools/call", {"name": "apply_changes", "arguments": {"draftId": invalid["DraftId"]}})
assert refused["isError"]
for expected in ("rejected", "saved"):
draft = preview(source)
assert draft["Review"]["Compiled"] and draft["Before"] == "// portability fixture"
assert '- // portability fixture' in draft["Review"]["Diff"]
assert all(p.read_bytes() == old for p, old in before.items())
print(f'Awaiting WPF: {expected}', flush=True)
result = client.call("apply_changes", draftId=draft["DraftId"])
print("WPF result:", result["Status"], flush=True)
assert result["Status"] == expected, (expected, result["Status"])
assert client.call("get_change_status", draftId=draft["DraftId"])["Status"] == expected
assert [p for p, old in before.items() if p.read_bytes() != old] == [script]
assert script.read_bytes() == source.encode("utf-8")
finally:
client.close()
reopened = Client(exe, project)
try:
assert reopened.call("get_object", kind="Workflows", id=flow_id)["Code"] == source
variable, = reopened.call("list_objects", kind="Variables", recipeId=recipe)["items"]
hit, = reopened.call("find_references", kind="Variables", id=variable["id"])["hits"]
assert (hit["line"], hit["confidence"]) == (8, "confirmed")
finally:
reopened.close()
print("PASS: invalid code blocked, WPF rejected then saved, only .cs changed, reopened and resolved reference.")
if __name__ == "__main__":
verify(*sys.argv[1:])