using PLCSharp.Models; using System.IO; using System.Text.Json.Nodes; namespace PLCSharp.Tests; public class ObjectEditTests { [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); } } }