diff --git a/README.md b/README.md index 3a4cbbd..91eafe2 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,8 @@ 调用 `preview_axis_rename(id, newName)` 可按硬件 ID 重命名工程运动轴。预览扫描所有配方的脚本,识别直接查询轴集合时的轴名相等比较,并将动态或不确定的匹配列入 `ManualReferences`。点位的 X、Y、Z、U 轴名及插补组的 X、Y 轴名列在 `PointReferences`。确认后一起保存硬件配置、点位配置和受影响的脚本。 +调用 `preview_device_rename(kind, id, newName)` 可按 ID 重命名气缸(`Cylinders`)、机器人(`Robots`)或通信连接(`Connects`)。工具检查所有配方脚本,识别设备集合上的名称相等比较、`GetRobot`、`GetConnect` 和通信数据写入方法中可确定的连接名参数。预览返回代码差异、编译结果和 `ManualReferences`。机器人点位和矩阵通过 `RobotID` 关联。连续保存多项修改时,可按保存的相反顺序逐项撤销。 + 核对后将草案 ID 交给 `apply_changes`,在 WPF 弹窗确认后一起保存名称和受影响的脚本。要撤销,把**已保存的重命名草案 ID** 传给 `preview_undo`,再对返回的**撤销草案 ID** 调用 `apply_changes` 并确认。变量 ID 始终保留。撤销记录保存在处理本次请求的编辑器或离线 MCP 服务会话中,沿用最近 256 个草案的缓存;关闭会话后记录失效。名称所在的 JSON 文件或该配方的脚本发生冲突时会停止写入。 ## 界面展示 diff --git a/Tests/mcp_device_batch_smoke.py b/Tests/mcp_device_batch_smoke.py new file mode 100644 index 0000000..376148f --- /dev/null +++ b/Tests/mcp_device_batch_smoke.py @@ -0,0 +1,73 @@ +"""Verify three hardware renames and reverse-order undo in one live PLCSharp editor.""" +import json +import shutil +import sys +from pathlib import Path + +from mcp_smoke import Client + + +DEVICES = (("Cylinders", "夹具", "定位夹具"), ("Robots", "R1", "搬运机器人"), + ("Connects", "PLC", "主站连接")) + + +def prepare(sample, root): + shutil.copytree(sample, root) + caller, = (p for p in root.rglob("*.cs") if "Controller.DI.FirstOrDefault" in p.read_text(encoding="utf-8")) + source = caller.read_text(encoding="utf-8") + lines = [ + 'model.CylindersModel.Cylinders.FirstOrDefault(c => c.Name == "夹具")', + 'model.CylindersModel.Cylinders.FirstOrDefault(c => c.Name == flow.Step.ToString())', + 'model.GetRobot("R1")', 'model.GetRobot(flow.Step.ToString())', + 'model.GetConnect("PLC")', 'model.GetConnect(flow.Step.ToString())', + ] + pos = source.rindex(" }") + caller.write_bytes((source[:pos] + "".join(f" _ = {line};\n" for line in lines) + source[pos:]).encode()) + + +def verify(exe, root): + def snapshot(): + return {p.relative_to(root): p.read_bytes() for p in root.rglob("*") if p.is_file()} + + original = snapshot() + hw = Path("configuration/hardware.json") + 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) + finally: + client.close() + + +if __name__ == "__main__": + {"verify": verify, "prepare": prepare}[sys.argv[1]](*map(Path, sys.argv[2:]))