using PLCSharp.Models; using PLCSharp.VVMs.GlobalVariables; using System.IO; using System.Text.Json.Nodes; namespace PLCSharp.Tests; public class ProjectMcpTests { [Fact] public void EditorPipeUsesLiveObjectsAndOnlyPersistsTheApprovedField() { var folder = Directory.CreateTempSubdirectory(); try { using var project = ProjectDirectory.New(); var recipe = project.Recipes.Single(); var variable = new Variable { RecipeID = recipe.ID, Name = "Counter", Comment = "old" }; project.Data.Add(variable); project.Save(folder.FullName); recipe.Name = "unsaved"; var changes = new ProjectChanges(); bool approve = false; using var pipe = new ProjectPipe(folder.FullName, request => { var op = (string)request["operation"]!; var args = request["arguments"]!.AsObject(); return op == "get" ? ProjectInspection.Query(project, op, args) : changes.Execute(project, op, args, true, _ => approve); }); JsonNode Call(string operation, JsonObject args) => ProjectPipe.Call(folder.FullName, new() { ["operation"] = operation, ["arguments"] = args }, () => throw new Exception("Unexpected offline access")); var live = Call("get", new() { ["kind"] = "Recipes", ["id"] = recipe.ID.ToString() }); Assert.Equal("unsaved", (string?)live["Name"]); JsonNode Preview() => Call("preview", new() { ["kind"] = "Variables", ["id"] = variable.ID.ToString(), ["comment"] = "new" }); JsonNode Apply(JsonNode draft) => Call("apply", new() { ["draftId"] = (string?)draft["DraftId"] }); Assert.Equal("rejected", (string?)Apply(Preview())["Status"]); Assert.Equal("old", variable.Comment); approve = true; Assert.Equal("saved", (string?)Apply(Preview())["Status"]); Assert.Equal("new", variable.Comment); Assert.Equal("unsaved", recipe.Name); using var disk = ProjectDirectory.Open(folder.FullName); Assert.Equal("First", disk.Recipes.Single().Name); Assert.Equal("new", disk.Data.Variables.Single().Comment); } finally { folder.Delete(true); } } }