using OpenNest.Mcp; using OpenNest.Mcp.Tools; namespace OpenNest.FrontEnd.Tests; /// /// The MCP tools that take work out of the session (DXF/SVG export and posted CNC output) and /// make a plate an irregular sheet from a file. /// public sealed class McpOutputToolsTests : IDisposable { private readonly string directory = Path.Combine(Path.GetTempPath(), "opennest-mcp-output-" + Guid.NewGuid()); public McpOutputToolsTests() => Directory.CreateDirectory(directory); public void Dispose() { try { Directory.Delete(directory, true); } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) { } } private static (NestSession Session, OutputTools Output) FilledSession() { var session = new NestSession(); new InputTools(session).CreateDrawing("square", "rectangle", 10, 10); new SetupTools(session).CreatePlate(48, 48, partSpacing: 1); new NestingTools(session).FillPlate(0, "square", quantity: 4); Assert.Equal(4, session.GetPlate(0)!.Parts.Count); return (session, new OutputTools(session)); } [Fact] public void ExportPlateWritesDxfAndSvgAndRefusesToOverwrite() { var (_, output) = FilledSession(); var dxf = Path.Combine(directory, "plate.dxf"); var svg = Path.Combine(directory, "out", "plate.svg"); Assert.StartsWith("Exported plate 0 (4 parts)", output.ExportPlate(0, dxf)); Assert.StartsWith("Exported plate 0", output.ExportPlate(0, svg)); Assert.Contains(" 0); var before = File.ReadAllBytes(dxf); Assert.Contains("already exists", output.ExportPlate(0, dxf)); Assert.Equal(before, File.ReadAllBytes(dxf)); Assert.StartsWith("Exported", output.ExportPlate(0, dxf, overwrite: true)); Assert.StartsWith("Error", output.ExportPlate(0, Path.Combine(directory, "plate.png"))); Assert.StartsWith("Error", output.ExportPlate(0, Path.Combine(directory, "plate.dxf2"))); Assert.StartsWith("Error", output.ExportPlate(0, Path.Combine(directory, "laser.dxf"), forLaserSoftware: true)); Assert.StartsWith("Error: plate 3 not found", output.ExportPlate(3, Path.Combine(directory, "none.dxf"))); Assert.StartsWith("Error", output.ExportPlate(0, "relative.dxf")); } [Fact] public void OutlineFromExportedDrawingMakesAnIrregularSheetAndCanBeRemoved() { var session = new NestSession(); var input = new InputTools(session); input.CreateDrawing("remnant", "l_shape", 60, 40); var output = new OutputTools(session); var file = Path.Combine(directory, "remnant.dxf"); Assert.StartsWith("Exported drawing 'remnant'", output.ExportDrawing("remnant", file)); new SetupTools(session).CreatePlate(10, 10); var result = output.SetPlateOutline(0, file); var plate = session.GetPlate(0)!; Assert.True(plate.Outline != null, result); Assert.Equal(60, System.Math.Max(plate.Size.Width, plate.Size.Length), 3); Assert.Equal(40, System.Math.Min(plate.Size.Width, plate.Size.Length), 3); Assert.True(plate.Outline!.Area < 60 * 40); Assert.Contains("engine Irregular", result); Assert.StartsWith("Error: Fill strategies need a rectangular sheet", new NestingTools(session).FillPlate(0, "remnant")); // The Irregular engine nests on the outline: only the L's material holds these squares. input.CreateDrawing("small", "rectangle", 5, 5); var nested = new NestingTools(session).AutoNestPlate(0, "small", "3", "Irregular"); Assert.True(plate.Parts.Count == 3, nested); plate.Parts.Clear(); Assert.StartsWith("Removed the outline of plate 0", output.SetPlateOutline(0)); Assert.Null(plate.Outline); Assert.Equal(60, System.Math.Max(plate.Size.Width, plate.Size.Length), 3); } [Fact] public void OutlineRefusesAnOccupiedPlateAndAFileWithoutAClosedContour() { var (session, output) = FilledSession(); var file = Path.Combine(directory, "square.dxf"); output.ExportDrawing("square", file); Assert.Contains("has parts", output.SetPlateOutline(0, file)); Assert.Null(session.GetPlate(0)!.Outline); new SetupTools(session).CreatePlate(20, 20); var empty = Path.Combine(directory, "empty.dxf"); File.WriteAllText(empty, "not a drawing"); Assert.StartsWith("Error", output.SetPlateOutline(1, empty)); Assert.Null(session.GetPlate(1)!.Outline); Assert.Equal(20, session.GetPlate(1)!.Size.Width); } [Fact] public void PostIsBlockedByVerificationWarningsUntilThisCallAcknowledgesThem() { var (_, output) = FilledSession(); var posts = Path.Combine(directory, "Posts"); Directory.CreateDirectory(posts); File.Copy(typeof(FakePost).Assembly.Location, Path.Combine(posts, "Fake.Posts.dll")); var cnc = Path.Combine(directory, "job.cnc"); Assert.Contains(FakePost.PostName, output.ListPosts(posts)); Assert.Contains("not found", output.PostNest("No Such Post", cnc, postsDir: posts)); Assert.StartsWith("Error: outputPath", output.PostNest(FakePost.PostName, "job.cnc", postsDir: posts)); // The fake post does not declare it keeps the placed order, so verification always warns. var blocked = output.PostNest(FakePost.PostName, cnc, postsDir: posts); Assert.Contains("Posting blocked", blocked); Assert.False(File.Exists(cnc)); var posted = output.PostNest(FakePost.PostName, cnc, acknowledgeRisks: true, postsDir: posts); Assert.Contains($"Post: {FakePost.PostName} -> {cnc}", posted); Assert.Equal("plates=1 parts=4", File.ReadAllText(cnc)); // Consent is per call: the next attempt is checked again, and the file is not replaced unasked. Assert.Contains("Posting blocked", output.PostNest(FakePost.PostName, cnc, postsDir: posts)); Assert.Contains("already exists", output.PostNest(FakePost.PostName, cnc, acknowledgeRisks: true, postsDir: posts)); } [Fact] public void PostRefusesASessionWithNoParts() { var session = new NestSession(); new SetupTools(session).CreatePlate(48, 48); var posts = Path.Combine(directory, "Posts"); Directory.CreateDirectory(posts); File.Copy(typeof(FakePost).Assembly.Location, Path.Combine(posts, "Fake.Posts.dll")); var result = new OutputTools(session).PostNest(FakePost.PostName, Path.Combine(directory, "x.cnc"), true, postsDir: posts); Assert.Equal("Error: no plate has parts; nothing to post", result); } [Fact] public void SaveNestKeepsTheLoadedNestsMachineSettings() { var session = new NestSession { Nest = new Nest("job") { CommonLineUsage = CommonLines.CommonLineUsage.Whenever, MinimumRemnantDimension = 7, }, }; session.Nest.Drawings.Add(new Drawing("square", new CNC.Program())); var snapshot = session.Snapshot(null); Assert.Equal("job", snapshot.Name); Assert.Equal(CommonLines.CommonLineUsage.Whenever, snapshot.CommonLineUsage); Assert.Equal(7, snapshot.MinimumRemnantDimension); } internal sealed class FakePost : IPostProcessor { public const string PostName = "MCP Test Post"; public string Name => PostName; public string Author => "Tests"; public string Description => "Writes plate and part counts"; public void Post(Nest nest, Stream outputStream) { using var writer = new StreamWriter(outputStream); writer.Write($"plates={nest.Plates.Count} parts={nest.Plates.Sum(p => p.Parts.Count)}"); } public void Post(Nest nest, string outputFile) { using var stream = File.Create(outputFile); Post(nest, stream); } } }