refactor(engine): make jobs plate nesters filler-backed
This commit is contained in:
@@ -5,125 +5,13 @@ using OpenNest.Engine.Jobs.Adapters;
|
||||
|
||||
namespace OpenNest.Engine.Tests.Jobs;
|
||||
|
||||
/// <summary>
|
||||
/// Domain-boundary adapters: geometry snapshots, mapper round-trips, and materialization. The
|
||||
/// former adapter-vs-runner contract tests moved to <see cref="NesterContractTests"/> when the
|
||||
/// legacy plate-nester adapter was deleted in the jobs-only placement migration.
|
||||
/// </summary>
|
||||
public class JobAdapterTests
|
||||
{
|
||||
[Fact]
|
||||
public void LegacyMutationsCannotDoubleSubtractOrReachCallerObjects()
|
||||
{
|
||||
var drawing = new Drawing("same name", TestDrawingFactory.Rectangle());
|
||||
drawing.Quantity.Required = 9;
|
||||
var item = new NestItem
|
||||
{
|
||||
Drawing = drawing,
|
||||
Quantity = 3,
|
||||
Priority = 7,
|
||||
StepAngle = 0,
|
||||
};
|
||||
var sourcePlate = new Plate(100, 200) { Quantity = 3, PartSpacing = 2 };
|
||||
var job = new NestJob(
|
||||
new[] { DrawingJobMapper.FromItem("requirement", item) },
|
||||
new[] { DrawingJobMapper.FromPlate("stock", sourcePlate, 3) }
|
||||
);
|
||||
var quantities = new List<int>();
|
||||
var adapter = new LegacyPlateNesterAdapter(p => new MutatingEngine(
|
||||
p,
|
||||
items =>
|
||||
{
|
||||
var privateItem = Assert.Single(items);
|
||||
quantities.Add(privateItem.Quantity);
|
||||
Assert.NotSame(drawing, privateItem.Drawing);
|
||||
Assert.Equal(0, privateItem.StepAngle);
|
||||
Assert.Equal(7, privateItem.Priority);
|
||||
var part = new Part(privateItem.Drawing);
|
||||
privateItem.Quantity = 0;
|
||||
privateItem.Drawing.Quantity.Required = 0;
|
||||
return new List<Part> { part };
|
||||
}
|
||||
));
|
||||
var result = new NestJobRunner(_ => adapter).Solve(job);
|
||||
var materialized = NestResultMaterializer.Materialize(job, result);
|
||||
Assert.Equal(new[] { 3, 2, 1 }, quantities);
|
||||
Assert.Equal(NestJobStatus.Complete, result.Status);
|
||||
Assert.Equal(3, materialized.Nest.Plates.Count);
|
||||
Assert.All(
|
||||
materialized.Nest.Plates,
|
||||
p =>
|
||||
{
|
||||
Assert.Equal(1, p.Quantity);
|
||||
Assert.Single(p.Parts);
|
||||
}
|
||||
);
|
||||
var outputDrawing = materialized.DrawingsByPartId["requirement"];
|
||||
Assert.Equal(3, outputDrawing.Quantity.Required);
|
||||
Assert.Equal(3, outputDrawing.Quantity.Nested);
|
||||
Assert.All(
|
||||
materialized.Nest.Plates,
|
||||
p => Assert.Same(outputDrawing, p.Parts[0].BaseDrawing)
|
||||
);
|
||||
Assert.NotSame(drawing, outputDrawing);
|
||||
Assert.Equal(9, drawing.Quantity.Required);
|
||||
Assert.Equal(0, drawing.Quantity.Nested);
|
||||
Assert.Equal(3, item.Quantity);
|
||||
Assert.Equal(3, sourcePlate.Quantity);
|
||||
Assert.Empty(sourcePlate.Parts);
|
||||
Assert.Equal(2, sourcePlate.PartSpacing);
|
||||
Assert.Equal(
|
||||
PartGeometrySnapshot.FromProgram(TestDrawingFactory.Rectangle()).Motions,
|
||||
PartGeometrySnapshot.FromProgram(drawing.Program).Motions
|
||||
);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReferenceIdentityNotNamesControlsLegacyPlacements()
|
||||
{
|
||||
var drawing = new Drawing("duplicate", TestDrawingFactory.Rectangle());
|
||||
var job = new NestJob(
|
||||
new[]
|
||||
{
|
||||
DrawingJobMapper.FromDrawing("a", drawing, 1),
|
||||
DrawingJobMapper.FromDrawing("b", drawing, 1),
|
||||
},
|
||||
FiniteStockJobTests.Job(1).Plates
|
||||
);
|
||||
var adapter = new LegacyPlateNesterAdapter(p => new MutatingEngine(
|
||||
p,
|
||||
items =>
|
||||
{
|
||||
Assert.NotSame(items[0].Drawing, items[1].Drawing);
|
||||
foreach (var item in items)
|
||||
item.Drawing.Name = "identical";
|
||||
return new List<Part>
|
||||
{
|
||||
new Part(items[0].Drawing, new Vector(0, 0)),
|
||||
new Part(items[1].Drawing, new Vector(10, 0)),
|
||||
};
|
||||
}
|
||||
));
|
||||
var result = new NestJobRunner(_ => adapter).Solve(job);
|
||||
Assert.Equal(new[] { "a", "b" }, result.Plates[0].Placements.Select(p => p.PartId));
|
||||
var output = NestResultMaterializer.Materialize(job, result);
|
||||
Assert.NotSame(output.DrawingsByPartId["a"], output.DrawingsByPartId["b"]);
|
||||
Assert.All(output.DrawingsByPartId.Values, d => Assert.Equal(1, d.Quantity.Nested));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UnknownPrivateDrawingIsRejectedEvenWithMatchingName()
|
||||
{
|
||||
var adapter = new LegacyPlateNesterAdapter(p => new MutatingEngine(
|
||||
p,
|
||||
items => new List<Part>
|
||||
{
|
||||
new(new Drawing(items[0].Drawing.Name, TestDrawingFactory.Rectangle())),
|
||||
}
|
||||
));
|
||||
Assert.Throws<InvalidOperationException>(() =>
|
||||
new NestJobRunner(_ => adapter).Solve(FiniteStockJobTests.Job())
|
||||
);
|
||||
Assert.Throws<NotSupportedException>(() =>
|
||||
LegacyPlateNesterAdapter.Create("not registered")
|
||||
);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExactGeometryRoundTripsIncludingOriginArcHoleAndMode()
|
||||
{
|
||||
@@ -167,7 +55,7 @@ public class JobAdapterTests
|
||||
new[] { DrawingJobMapper.FromDrawing("rectangle", drawing, 1) },
|
||||
new[] { new NestPlateStock("sheet", new Size(40, 60), 1, 1, new Spacing(2, 2, 2, 2)) }
|
||||
);
|
||||
var result = new NestJobRunner(LegacyPlateNesterAdapter.Create).Solve(job);
|
||||
var result = new NestJobRunner(PlateNesterFactory.Create).Solve(job);
|
||||
Assert.Equal(NestJobStatus.Complete, result.Status);
|
||||
Assert.Equal(new StockUsage("sheet", 1, 0), Assert.Single(result.StockUsage));
|
||||
var pose = Assert.Single(Assert.Single(result.Plates).Placements);
|
||||
@@ -207,17 +95,4 @@ public class JobAdapterTests
|
||||
Assert.Equal(expected.Y, ((Motion)part.Program.Codes[0]).EndPoint.Y, 10);
|
||||
Assert.Equal(new Vector(23, 31), part.Location);
|
||||
}
|
||||
|
||||
private sealed class MutatingEngine(Plate plate, Func<List<NestItem>, List<Part>> nest)
|
||||
: NestEngineBase(plate)
|
||||
{
|
||||
public override string Name => "test";
|
||||
public override string Description => "mutates private demand";
|
||||
|
||||
public override List<Part> Nest(
|
||||
List<NestItem> items,
|
||||
IProgress<NestProgress> progress,
|
||||
CancellationToken token
|
||||
) => nest(items);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user