using PLCSharp.Models; using System.IO; using System.Text.Json.Nodes; namespace PLCSharp.Tests; public class ObjectEditTests { [Fact] public void ReviewingDeletionDoesNotTouchFilesAndCancellationPreservesReferences() { var dir = Directory.CreateTempSubdirectory(); try { using var p = ProjectDirectory.New(); var channel = new PLCSharp.VVMs.MotionController.DI { Name = "Clamp", Number = 5 }; var cylinder = new PLCSharp.VVMs.Cylinders.Cylinder { Name = "Cylinder", InA = "Clamp" }; p.Data.AddRange(channel, cylinder); var script = new PLCSharp.VVMs.Workflows.Workflow { Name = "Cycle", RecipeID = p.DefaultRecipeId }; script.Code = $$""" using PLCSharp.Models; using PLCSharp.Core.Common; using System.Linq; public class Extension_{{script.ID:N}} { public void Run(GlobalModel model, FlowModel flow) { _ = model.ControllersModel.Controller.DI.FirstOrDefault(c => c.Name == "Clamp"); _ = model.ControllersModel.Controller.DI.FirstOrDefault(c => c.Name == flow.Step.ToString()); } } """; p.Flows(p.DefaultRecipeId).Add(script); p.Save(dir.FullName); var files = Directory.GetFiles(dir.FullName, "*", SearchOption.AllDirectories).ToDictionary(path => path, File.ReadAllBytes); var changes = new ProjectChanges(); var request = new JsonObject { ["kind"] = "DI", ["action"] = "delete", ["id"] = channel.ID.ToString() }; var draft = changes.Execute(p, "previewObjectEdit", request, false); var review = draft["Edit"]!; Assert.True((bool)review["Compiled"]!); Assert.Single(review["WorkflowReferences"]!.AsArray()); Assert.Equal("InA", (string?)Assert.Single(review["ConfigReferences"]!.AsArray())!["field"]); Assert.Single(review["ManualReferences"]!.AsArray()); var args = new JsonObject { ["draftId"] = (string?)draft["DraftId"] }; Assert.Throws(() => changes.Execute(p, "apply", args, false, _ => true)); Assert.Equal("rejected", (string?)changes.Execute(p, "apply", args, true, _ => false)["Status"]); foreach (var (path, bytes) in files) Assert.Equal(bytes, File.ReadAllBytes(path)); var stale = p.PlanObjectEdit("delete", "DI", channel.ID, null); p.ApplyObjectEdit(p.PlanObjectEdit("update", "Cylinders", cylinder.ID, new() { ["Comment"] = "reviewed" })); Assert.ThrowsAny(() => p.ApplyObjectEdit(stale)); Assert.NotNull(p.Data.DI.Find(channel.ID)); Assert.Throws(() => p.PlanObjectEdit("create", "DI", null, new() { ["Name"] = "Other", ["Number"] = 5 })); Assert.Throws(() => p.PlanObjectEdit("update", "Cylinders", cylinder.ID, new() { ["InB"] = "Unknown" })); Assert.Throws(() => p.PlanObjectEdit("create", "Variables", null, new() { ["Name"] = "Counter", ["RecipeID"] = p.DefaultRecipeId.ToString(), ["DefaultValueStr"] = "invalid" })); } finally { dir.Delete(true); } } [Fact] public void EverySupportedTableCreatesUpdatesDeletesAndUndoesEachSnapshot() { var dir = Directory.CreateTempSubdirectory(); try { using var p = ProjectDirectory.New(); p.Save(dir.FullName); var robot = p.PlanObjectEdit("create", "Robots", null, new() { ["Name"] = "Parent" }); p.ApplyObjectEdit(robot); foreach (var kind in new[] { "Variables", "Controllers", "Axes", "DI", "DQ", "Cylinders", "Robots", "Connects", "Cameras", "AxisPoints", "RobotPoints" }) { JsonObject fields = new(); if (kind != "Controllers") fields["Name"] = "QA"; if (kind is "Variables" or "AxisPoints" or "RobotPoints") fields["RecipeID"] = p.DefaultRecipeId.ToString(); if (kind == "RobotPoints") fields["RobotID"] = robot.Id.ToString(); string path = Path.Combine(dir.FullName, "configuration", ProjectDirectory.ObjectGroup(kind) + ".json"); var original = File.ReadAllBytes(path); var add = p.PlanObjectEdit("create", kind, null, fields); Assert.Equal(original, File.ReadAllBytes(path)); p.ApplyObjectEdit(add); var added = File.ReadAllBytes(path); JsonObject patch = kind switch { "DI" or "DQ" => new() { ["LineNumber"] = "Q9" }, "AxisPoints" or "RobotPoints" => new() { ["X"] = 3.5 }, _ => new() { ["Comment"] = "inspection" } }; var update = p.PlanObjectEdit("update", kind, add.Id, patch); p.ApplyObjectEdit(update); var changed = File.ReadAllBytes(path); var remove = p.PlanObjectEdit("delete", kind, add.Id, null); p.ApplyObjectEdit(remove); p.ApplyObjectEdit(p.UndoObject(Guid.NewGuid(), remove)); Assert.Equal(changed, File.ReadAllBytes(path)); p.ApplyObjectEdit(p.UndoObject(Guid.NewGuid(), update)); Assert.Equal(added, File.ReadAllBytes(path)); p.ApplyObjectEdit(p.UndoObject(Guid.NewGuid(), add)); Assert.Equal(original, File.ReadAllBytes(path)); } } finally { dir.Delete(true); } } }