Exercise live workflow rename, confirmation and exact undo on a sample caller
This commit is contained in:
parent
ea414f488e
commit
c81cd7851e
@ -51,7 +51,9 @@
|
||||
|
||||
调用 `preview_variable_rename(id, newName)` 可重命名一个配方用户变量。预览返回受影响工作流的代码差异、编译诊断和 `ManualReferences` 核对清单,包含脚本路径、行列和原表达式。自动替换覆盖同一配方中能够绑定的 `GetVariable` 常量参数,以及直接查询变量集合时的名称相等比较。动态名称和不确定的匹配保留原文。
|
||||
|
||||
核对后将草案 ID 交给 `apply_changes`,在 WPF 弹窗确认后一起保存变量与脚本。要撤销,把**已保存的重命名草案 ID** 传给 `preview_undo`,再对返回的**撤销草案 ID** 调用 `apply_changes` 并确认。变量 ID 始终保留。撤销记录保存在处理本次请求的编辑器或离线 MCP 服务会话中,沿用最近 256 个草案的缓存;关闭会话后记录失效。变量文件或该配方的脚本发生冲突时会停止写入。
|
||||
调用 `preview_workflow_rename(id, newName)` 可重命名配方中的工作流,并更新同一配方脚本中能确定的 `GetTask` 常量参数和直接查询 `WorkflowsModel.Workflows` 时的名称相等比较。请逐项检查 `ManualReferences` 中的运行时表达式。工作流 ID 和脚本文件路径保持稳定。
|
||||
|
||||
核对后将草案 ID 交给 `apply_changes`,在 WPF 弹窗确认后一起保存名称和受影响的脚本。要撤销,把**已保存的重命名草案 ID** 传给 `preview_undo`,再对返回的**撤销草案 ID** 调用 `apply_changes` 并确认。变量 ID 始终保留。撤销记录保存在处理本次请求的编辑器或离线 MCP 服务会话中,沿用最近 256 个草案的缓存;关闭会话后记录失效。名称所在的 JSON 文件或该配方的脚本发生冲突时会停止写入。
|
||||
|
||||
## 界面展示
|
||||
|
||||
|
||||
@ -3,6 +3,7 @@ import argparse
|
||||
import json
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import uuid
|
||||
|
||||
from mcp_smoke import Client
|
||||
|
||||
@ -21,34 +22,58 @@ def prepare(sample, directory):
|
||||
script.write_bytes(source.encode("utf-8"))
|
||||
|
||||
|
||||
def verify(exe, directory):
|
||||
def prepare_workflow(sample, directory):
|
||||
shutil.copytree(sample, directory)
|
||||
target_script, = directory.rglob("*.cs")
|
||||
manifest = target_script.parent.parent / "recipe.json"
|
||||
recipe = json.loads(manifest.read_bytes())
|
||||
target, = recipe["workflows"]
|
||||
caller = dict(target, id=str(uuid.uuid4()), name="调度", automaticExecution=False)
|
||||
recipe["workflows"].append(caller)
|
||||
manifest.write_text(json.dumps(recipe, ensure_ascii=False, indent=2), encoding="utf-8")
|
||||
source = (
|
||||
"using PLCSharp.Models;\nusing PLCSharp.Core.Common;\n"
|
||||
f"public class Extension_{caller['id'].replace('-', '')} {{\n"
|
||||
" public void Run(GlobalModel model, FlowModel flow) {\n"
|
||||
f' _ = model.GetTask("{target["name"]}");\n'
|
||||
' _ = model.GetTask(flow.Step.ToString());\n'
|
||||
" }\n}\n"
|
||||
)
|
||||
target_script.with_name(caller["id"] + ".cs").write_bytes(source.encode("utf-8"))
|
||||
|
||||
|
||||
def verify(exe, directory, workflow=False):
|
||||
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="完成计数")
|
||||
kind, old, new, tool, lookup = (
|
||||
("Workflows", "搬运", "工件搬运", "preview_workflow_rename", "GetTask") if workflow else
|
||||
("Variables", "计数", "完成计数", "preview_variable_rename", "GetVariable"))
|
||||
items = client.call("list_objects", kind=kind, recipeId=recipe)["items"]
|
||||
target, = (item for item in items if item["name"] == old)
|
||||
draft = client.call(tool, id=target["id"], newName=new)
|
||||
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"]
|
||||
assert f'{lookup}("{new}")' 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=kind, id=target["id"])["Name"] == new
|
||||
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"]}
|
||||
directory / (f"recipes/{recipe}/recipe.json" if workflow else "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=kind, id=target["id"])["Name"] == old
|
||||
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.")
|
||||
@ -58,9 +83,12 @@ def verify(exe, directory):
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("mode", choices=["prepare", "verify"])
|
||||
parser.add_argument("mode", choices=["prepare", "verify", "prepare-workflow", "verify-workflow"])
|
||||
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)
|
||||
if args.mode.startswith("prepare"):
|
||||
(prepare_workflow if args.mode == "prepare-workflow" else prepare)(args.source, args.project)
|
||||
else:
|
||||
verify(args.source, args.project, workflow=args.mode == "verify-workflow")
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user