diff --git a/Tests/mcp_all_names_smoke.py b/Tests/mcp_all_names_smoke.py index 982e2de..1f39e62 100644 --- a/Tests/mcp_all_names_smoke.py +++ b/Tests/mcp_all_names_smoke.py @@ -92,14 +92,14 @@ def run(exe, source): plan = draft["Rename"] assert plan["Compiled"] and len(plan["Scripts"]) == 1 and snapshot() == before, (kind, plan) print("SAVE", kind, plan["OldName"], "manual", [(h.get("line"), h["expression"]) for h in plan["ManualReferences"]], flush=True) - assert client.call("apply_changes", draftId=draft["DraftId"])["Status"] == "saved" + assert client.call("apply_changes", confirmed=True, draftId=draft["DraftId"])["Status"] == "saved" assert snapshot() != before history.append((kind, draft["DraftId"], before)) for kind, draft_id, before in reversed(history): undo = client.call("preview_undo", draftId=draft_id) assert undo["Rename"]["Compiled"] print("UNDO", kind, flush=True) - assert client.call("apply_changes", draftId=undo["DraftId"])["Status"] == "saved" + assert client.call("apply_changes", confirmed=True, draftId=undo["DraftId"])["Status"] == "saved" assert before == snapshot(), kind assert snapshot() == baseline print("PASS", len(ids), "object kinds;", len(order), "saves and undos;", len(baseline), "files;", sum(map(len, baseline.values())), "bytes") diff --git a/Tests/mcp_batch_smoke.py b/Tests/mcp_batch_smoke.py index f0dc22f..7522291 100644 --- a/Tests/mcp_batch_smoke.py +++ b/Tests/mcp_batch_smoke.py @@ -1,4 +1,4 @@ -"""Review three changes in a single live editor dialog, then undo with one confirmation.""" +"""Verify Agent approval and rollback against a live editor or an offline sample.""" from pathlib import Path import sys @@ -35,13 +35,21 @@ def verify(exe, root, offline=False): {"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", draftId=draft["DraftId"])["Status"] == "saved" + 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: @@ -50,7 +58,7 @@ def verify(exe, root, offline=False): 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", draftId=undo["DraftId"])["Status"] == "saved" + 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") diff --git a/Tests/mcp_device_batch_smoke.py b/Tests/mcp_device_batch_smoke.py index 376148f..0d2a324 100644 --- a/Tests/mcp_device_batch_smoke.py +++ b/Tests/mcp_device_batch_smoke.py @@ -34,37 +34,12 @@ def verify(exe, root): 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) + result = client.request("tools/call", {"name": "preview_object_rename", "arguments": + {"kind": kind, "id": item["id"], "newName": new}}) + assert result["isError"] and snapshot() == original + print("PASS: device names are protected.") finally: client.close() diff --git a/Tests/mcp_io_batch_smoke.py b/Tests/mcp_io_batch_smoke.py index 5054697..bbebb5c 100644 --- a/Tests/mcp_io_batch_smoke.py +++ b/Tests/mcp_io_batch_smoke.py @@ -31,34 +31,12 @@ def verify(exe, root, expected_source="editor"): 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", "夹具松开")): + for kind in ("DI", "DQ"): 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.") + result = client.request("tools/call", {"name": "preview_object_rename", "arguments": + {"kind": kind, "id": channel["id"], "newName": "禁止改名"}}) + assert result["isError"] and snapshot() == original + print("PASS: input and output names are protected.") finally: client.close() diff --git a/Tests/mcp_object_edit_smoke.py b/Tests/mcp_object_edit_smoke.py index 9748e89..3b1f132 100644 --- a/Tests/mcp_object_edit_smoke.py +++ b/Tests/mcp_object_edit_smoke.py @@ -67,7 +67,7 @@ def verify(exe, root): if kind in {"DI", "AxisPoints"}: assert edit["ConfigReferences"] print(action, kind, name, flush=True) - assert client.call("apply_changes", draftId=draft["DraftId"])["Status"] == "saved" + assert client.call("apply_changes", confirmed=True, draftId=draft["DraftId"])["Status"] == "saved" created[kind] = edit["Id"] after = snapshot() assert after.keys() == before.keys() @@ -85,7 +85,7 @@ def verify(exe, root): undo = client.call("preview_undo", draftId=draft_id) assert undo["Edit"]["Compiled"] and undo["Edit"]["UndoOf"] == draft_id print("UNDO", undo["Edit"]["Kind"], undo["Edit"]["Action"], flush=True) - assert client.call("apply_changes", draftId=undo["DraftId"])["Status"] == "saved" + assert client.call("apply_changes", confirmed=True, draftId=undo["DraftId"])["Status"] == "saved" assert client.call("get_change_status", draftId=draft_id)["Status"] == "undone" assert before == snapshot() assert snapshot() == original diff --git a/Tests/mcp_remaining_object_edit_smoke.py b/Tests/mcp_remaining_object_edit_smoke.py index b56a5be..679c029 100644 --- a/Tests/mcp_remaining_object_edit_smoke.py +++ b/Tests/mcp_remaining_object_edit_smoke.py @@ -60,7 +60,7 @@ def verify(exe, root): assert edit["WorkflowReferences"] and edit["ManualReferences"] if kind == "CustomControls": assert any(r["field"] == "ControlName" for r in edit["ConfigReferences"]) - assert client.call("apply_changes", draftId=draft["DraftId"])["Status"] == "saved" + assert client.call("apply_changes", confirmed=True, draftId=draft["DraftId"])["Status"] == "saved" after = snapshot() changed = {p for p in before.keys() | after.keys() if before.get(p) != after.get(p)} assert changed == set(map(Path, edit["ChangedFiles"])) @@ -70,7 +70,7 @@ def verify(exe, root): undo = client.call("preview_undo", draftId=draft_id) assert undo["Edit"]["Compiled"] and undo["Edit"]["UndoOf"] == draft_id print("UNDO", undo["Edit"]["Kind"], undo["Edit"]["Action"], flush=True) - assert client.call("apply_changes", draftId=undo["DraftId"])["Status"] == "saved" + assert client.call("apply_changes", confirmed=True, draftId=undo["DraftId"])["Status"] == "saved" assert client.call("get_change_status", draftId=draft_id)["Status"] == "undone" assert snapshot() == before assert snapshot() == original diff --git a/Tests/mcp_rename_editor_smoke.py b/Tests/mcp_rename_editor_smoke.py index 982e7fd..2798280 100644 --- a/Tests/mcp_rename_editor_smoke.py +++ b/Tests/mcp_rename_editor_smoke.py @@ -93,6 +93,11 @@ def verify(exe, directory, workflow=False, point=False, vision=False, axis=False kind, old, new, tool = "Axes", "X", "横移轴", "preview_axis_rename" items = client.call("list_objects", kind=kind, **({} if axis else {"recipeId": recipe}))["items"] target, = (item for item in items if item["name"] == old) + if axis or point: + denied = client.request("tools/call", {"name": "preview_object_rename", "arguments": + {"kind": kind, "id": target["id"], "newName": new}}) + assert denied["isError"] and all(p.read_bytes() == data for p, data in before.items()) + return draft = client.call(tool, id=target["id"], newName=new, **({"kind": kind} if point else {})) plan = draft["Rename"] script, = plan["Scripts"] @@ -110,7 +115,7 @@ def verify(exe, directory, workflow=False, point=False, vision=False, axis=False assert replacement 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"]) + saved = client.call("apply_changes", confirmed=True, draftId=draft["DraftId"]) assert saved["Status"] == "saved" assert client.call("get_object", kind=kind, id=target["id"])["Name"] == new assert client.call("get_object", kind="Workflows", id=script["Id"])["Code"] == script["After"] @@ -125,7 +130,7 @@ def verify(exe, directory, workflow=False, point=False, vision=False, axis=False assert {p for p, old in before.items() if p.read_bytes() != old} == changed 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("apply_changes", confirmed=True, draftId=undo["DraftId"])["Status"] == "saved" assert client.call("get_change_status", draftId=draft["DraftId"])["Status"] == "undone" assert client.call("get_object", kind=kind, id=target["id"])["Name"] == old assert client.call("get_object", kind="Workflows", id=script["Id"])["Code"] == script["Before"] diff --git a/Tests/mcp_script_dialog_smoke.py b/Tests/mcp_script_dialog_smoke.py index bb9bce7..77e1209 100644 --- a/Tests/mcp_script_dialog_smoke.py +++ b/Tests/mcp_script_dialog_smoke.py @@ -1,4 +1,4 @@ -"""Interactive acceptance test. Click Cancel in the first WPF dialog and Confirm Save in the second.""" +"""Verify rejection and approval of a reviewed script from the Agent.""" from pathlib import Path import shutil import sys @@ -28,16 +28,15 @@ def verify(exe, sample): 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"]}}) + refused = client.request("tools/call", {"name": "apply_changes", "arguments": {"confirmed": True, "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) + result = client.call("apply_changes", confirmed=expected == "saved", draftId=draft["DraftId"]) + print("Agent decision:", 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] @@ -52,7 +51,7 @@ def verify(exe, sample): 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.") + print("PASS: invalid code blocked, Agent rejected then saved, only .cs changed, reopened and resolved reference.") if __name__ == "__main__": diff --git a/Tests/mcp_smoke.py b/Tests/mcp_smoke.py index c75265e..396849b 100644 --- a/Tests/mcp_smoke.py +++ b/Tests/mcp_smoke.py @@ -76,7 +76,7 @@ class Sample { script.write_text(source_object["Code"], encoding="utf-8") args = {"kind": "Variables", "id": variable["id"], "comment": "已核对计数变量"} draft = client.call("preview_changes", **args) - denied = client.request("tools/call", {"name": "apply_changes", "arguments": {"draftId": draft["DraftId"]}}) + denied = client.request("tools/call", {"name": "apply_changes", "arguments": {"confirmed": True, "draftId": draft["DraftId"]}}) assert denied["isError"] client.close() client = Client(exe, project, "--allow-write") @@ -85,8 +85,8 @@ class Sample { assert draft["Before"] == "A" and draft["After"] == args["comment"] assert all(p.read_bytes() == old for p, old in snapshot.items()) key = {"draftId": draft["DraftId"]} - assert client.call("apply_changes", **key)["Status"] == "saved" - assert client.call("apply_changes", **key)["Status"] == "saved" + assert client.call("apply_changes", confirmed=True, **key)["Status"] == "saved" + assert client.call("apply_changes", confirmed=True, **key)["Status"] == "saved" assert client.call("get_change_status", **key)["Status"] == "saved" changed = [p.relative_to(project).as_posix() for p, old in snapshot.items() if p.read_bytes() != old] assert changed == ["configuration/variables.json"], changed @@ -97,7 +97,7 @@ class Sample { file = project / changed[0] external = file.read_bytes() + b"\n" file.write_bytes(external) - conflict = client.request("tools/call", {"name": "apply_changes", "arguments": {"draftId": pending["DraftId"]}}) + conflict = client.request("tools/call", {"name": "apply_changes", "arguments": {"confirmed": True, "draftId": pending["DraftId"]}}) assert conflict["isError"] and file.read_bytes() == external assert client.call("get_change_status", draftId=pending["DraftId"])["Status"] == "conflict" print(f'{summary["Id"]}: {len(summary["recipes"])} recipes, {workflow["name"]}, {variable["name"]}; ' diff --git a/Tests/mcp_vision_asset_batch_smoke.py b/Tests/mcp_vision_asset_batch_smoke.py index 89be527..440d0cb 100644 --- a/Tests/mcp_vision_asset_batch_smoke.py +++ b/Tests/mcp_vision_asset_batch_smoke.py @@ -49,6 +49,11 @@ def verify(exe, root): for (kind, old, new), steps in zip(ASSETS, STEPS): item, = (r for r in client.call("list_objects", kind=kind)["items"] if r["name"] == old) before = snapshot() + if kind == "Cameras": + result = client.request("tools/call", {"name": "preview_vision_asset_rename", "arguments": + {"kind": kind, "id": item["id"], "newName": new}}) + assert result["isError"] and before == snapshot() + continue draft = client.call("preview_vision_asset_rename", kind=kind, id=item["id"], newName=new) plan = draft["Rename"] assert len(plan["VisionReferences"]) == len(steps), plan @@ -58,20 +63,13 @@ def verify(exe, root): 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("apply_changes", confirmed=True, 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) vision = Path("configuration/vision.json") paths = {vision, Path(edit["Path"])} - if kind == "Cameras": - paths.add(hw) - hardware = json.loads(before[hw]) - camera, = hardware[kind] - camera["Name"] = new - camera["Params"]["DiscoveryName"] = old - assert json.loads(after[hw]) == hardware assert {p for p in before if after[p] != before[p]} == paths expected = json.loads(before[vision]) if kind != "Cameras": @@ -85,7 +83,7 @@ def verify(exe, root): 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("apply_changes", confirmed=True, 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()