plcsharp/Tests/mcp_batch_smoke.py

72 lines
3.7 KiB
Python

"""Verify Agent approval and rollback against a live editor or an offline sample."""
from pathlib import Path
import sys
from mcp_smoke import Client
def verify(exe, root, offline=False):
def snapshot():
return {p.relative_to(root): p.read_bytes() for p in root.rglob("*") if p.is_file()}
initial = snapshot()
client = Client(exe, root, "--allow-write")
try:
summary = client.call("get_project_summary")
if offline:
assert summary.get("source") != "editor"
recipe = summary["DefaultRecipeId"]
else:
assert summary["source"] == "editor"
recipe = summary["currentRecipeId"]
def named(kind, name):
return next(x["id"] for x in client.call("list_objects", kind=kind, recipeId=recipe)["items"] if x["name"] == name)
vision = named("VisionFunctions", "检查")
workflow = named("Workflows", "调度")
control = named("CustomControls", "显示")
refs = client.call("find_references", kind="VisionFunctions", id=vision)
assert refs["hits"]
script = client.call("get_object", kind="Workflows", id=workflow)["Code"]
marker = "批次验收"
operations = [
{"action": "update", "kind": "VisionFunctions", "id": vision, "properties": {"Comment": marker}},
{"action": "update", "kind": "CustomControls", "id": control, "properties": {"Comment": marker}},
{"action": "update", "kind": "Workflows", "id": workflow, "properties": {"Code": script + "\n// " + marker + "\n"}},
]
denied = client.request("tools/call", {"name": "preview_object_rename", "arguments":
{"kind": "Recipes", "id": recipe, "newName": "受保护"}})
assert denied["isError"] and snapshot() == initial
rejected = client.call("preview_batch", operations=operations)
assert client.call("apply_changes", draftId=rejected["DraftId"], confirmed=False)["Status"] == "rejected"
draft = client.call("preview_batch", operations=operations)
missing = client.request("tools/call", {"name": "apply_changes", "arguments": {"draftId": draft["DraftId"]}})
assert missing.get("isError") and snapshot() == initial
batch = draft["Batch"]
assert draft["Status"] == "pending" and batch["Compiled"] and batch["Workflows"]
assert "需要人工核对" in batch["Summary"] and "调度(通过)" in batch["Summary"]
assert len(batch["Steps"]) == 3 and any(s["Edit"]["ManualReferences"] for s in batch["Steps"])
assert initial == snapshot()
print("ANALYZED", len(refs["hits"]), "references;", len(batch["Workflows"]), "compiled workflows", flush=True)
assert client.call("apply_changes", confirmed=True, draftId=draft["DraftId"])["Status"] == "saved"
saved = snapshot()
assert {p for p in initial if initial[p] != saved[p]} == set(map(Path, batch["ChangedFiles"]))
for op in operations:
row = client.call("get_object", kind=op["kind"], id=op["id"])
assert all(row[k] == v for k, v in op["properties"].items())
undo = client.call("preview_undo", draftId=draft["DraftId"])
assert undo["Batch"]["UndoOf"] == draft["DraftId"] and saved == snapshot()
print("SAVED", batch["ChangedFiles"], flush=True)
assert client.call("apply_changes", confirmed=True, draftId=undo["DraftId"])["Status"] == "saved"
assert client.call("get_change_status", draftId=draft["DraftId"])["Status"] == "undone"
assert snapshot() == initial
print("PASS", len(initial), "files;", sum(map(len, initial.values())), "bytes")
finally:
client.close()
if __name__ == "__main__":
verify(Path(sys.argv[1]), Path(sys.argv[2]), offline="--offline" in sys.argv[3:])