using System.IO; using System.Text.Json.Nodes; using PLCSharp.Models; using PLCSharp.VVMs.Robots; namespace PLCSharp.Tests; public class RemainingEditTests { [Fact] public void RemainingObjectTypesAndOwnedDeletesRestoreEverySnapshot() { var dir = Directory.CreateTempSubdirectory(); try { using var project = ProjectDirectory.New(); var robot = new Robot { Name = "QA" }; project.Data.Add(robot); project.Save(dir.FullName); Dictionary Snapshot() => Directory.GetFiles(dir.FullName, "*", SearchOption.AllDirectories).ToDictionary(p => p, File.ReadAllBytes); var history = new Stack<(ObjectEdit, Dictionary)>(); ObjectEdit Save(string op, string kind, Guid? id, JsonObject? props) { var before = Snapshot(); var draft = project.PlanObjectEdit(op, kind, id, props); Assert.True(draft.Compiled); project.ApplyObjectEdit(draft); history.Push((draft, before)); return draft; } var recipe = Save("create", "Recipes", null, new() { ["Name"] = "Product" }).Id; Save("update", "Recipes", recipe, new() { ["Comment"] = "reviewed" }); Guid function = default; foreach (var kind in new[] { "Workflows", "VisionFunctions", "LocalImageDatas", "ImageDatas", "SystemVariables", "CustomControls", "CanvasConfigs", "Matrices", "RobotMatrices", "InterpolationGroups" }) { var props = new JsonObject(); if (kind != "CanvasConfigs") props["Name"] = kind; if (kind == "LocalImageDatas") props["FuncID"] = function.ToString(); else props["RecipeID"] = recipe.ToString(); if (kind == "RobotMatrices") props["RobotID"] = robot.ID.ToString(); var add = Save("create", kind, null, props); if (kind == "VisionFunctions") function = add.Id; JsonObject patch = kind switch { "CanvasConfigs" => new() { ["Rows"] = 3 }, "InterpolationGroups" => new() { ["Params"] = new JsonObject { ["Ahead"] = 1 } }, "Matrices" or "RobotMatrices" => new() { ["XCount"] = 2 }, _ => new() { ["Comment"] = "updated" } }; Save("update", kind, add.Id, patch); } var drop = Save("delete", "Recipes", recipe, null); Assert.Equal(9, drop.OwnedRows.Count); using (var opened = ProjectDirectory.Open(dir.FullName)) Assert.Single(opened.Recipes); while (history.TryPop(out var item)) { project.ApplyObjectEdit(project.UndoObject(Guid.NewGuid(), item.Item1)); var current = Snapshot(); Assert.Equal(item.Item2.Keys.Order(), current.Keys.Order()); foreach (var (path, bytes) in current) Assert.Equal(item.Item2[path], bytes); } } finally { dir.Delete(true); } } }