refactor(engine): extract AccumulatingProgress from StripNestEngine

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-16 22:26:53 -04:00
parent ba7ded14b5
commit 31dbbbeedc
2 changed files with 86 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
namespace OpenNest
{
/// <summary>
/// Wraps an IProgress to prepend previously placed parts to each report,
/// so the UI shows the full picture during incremental fills.
/// </summary>
internal class AccumulatingProgress : IProgress<NestProgress>
{
private readonly IProgress<NestProgress> inner;
private readonly List<Part> previousParts;
public AccumulatingProgress(IProgress<NestProgress> inner, List<Part> previousParts)
{
this.inner = inner;
this.previousParts = previousParts;
}
public void Report(NestProgress value)
{
if (value.BestParts != null && previousParts.Count > 0)
{
var combined = new List<Part>(previousParts.Count + value.BestParts.Count);
combined.AddRange(previousParts);
combined.AddRange(value.BestParts);
value.BestParts = combined;
value.BestPartCount = combined.Count;
}
inner.Report(value);
}
}
}

View File

@@ -0,0 +1,51 @@
namespace OpenNest.Tests;
public class AccumulatingProgressTests
{
private class CapturingProgress : IProgress<NestProgress>
{
public NestProgress Last { get; private set; }
public void Report(NestProgress value) => Last = value;
}
[Fact]
public void Report_PrependsPreviousParts()
{
var inner = new CapturingProgress();
var previous = new List<Part> { TestHelpers.MakePartAt(0, 0, 10) };
var accumulating = new AccumulatingProgress(inner, previous);
var newParts = new List<Part> { TestHelpers.MakePartAt(20, 0, 10) };
accumulating.Report(new NestProgress { BestParts = newParts, BestPartCount = 1 });
Assert.NotNull(inner.Last);
Assert.Equal(2, inner.Last.BestParts.Count);
Assert.Equal(2, inner.Last.BestPartCount);
}
[Fact]
public void Report_NoPreviousParts_PassesThrough()
{
var inner = new CapturingProgress();
var accumulating = new AccumulatingProgress(inner, new List<Part>());
var newParts = new List<Part> { TestHelpers.MakePartAt(0, 0, 10) };
accumulating.Report(new NestProgress { BestParts = newParts, BestPartCount = 1 });
Assert.NotNull(inner.Last);
Assert.Single(inner.Last.BestParts);
}
[Fact]
public void Report_NullBestParts_PassesThrough()
{
var inner = new CapturingProgress();
var previous = new List<Part> { TestHelpers.MakePartAt(0, 0, 10) };
var accumulating = new AccumulatingProgress(inner, previous);
accumulating.Report(new NestProgress { BestParts = null });
Assert.NotNull(inner.Last);
Assert.Null(inner.Last.BestParts);
}
}