mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-02 14:31:35 -05:00
Set OPENNEST_NEST_LOG to a directory to write one JSON Lines file of events and a text summary per Default solve: each candidate engine's result, time and passes, and why the winning layout was chosen. It only observes, so layouts are the same with it on or off. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
99 lines
3.6 KiB
C#
99 lines
3.6 KiB
C#
#nullable enable
|
|
using System.Text.Json;
|
|
using OpenNest.Engine.Jobs;
|
|
using OpenNest.Engine.NestingEngines;
|
|
using OpenNest.Engine.NestingEngines.Default;
|
|
using static OpenNest.Engine.Tests.NestingEngines.JobBuilder;
|
|
using static OpenNest.Engine.Tests.NestingEngines.Shapes;
|
|
|
|
namespace OpenNest.Engine.Tests.NestingEngines;
|
|
|
|
public sealed class NestTraceTests : IDisposable
|
|
{
|
|
private readonly string directory = Path.Combine(Path.GetTempPath(), "opennest-trace-" + Guid.NewGuid().ToString("N"));
|
|
|
|
public void Dispose()
|
|
{
|
|
if (Directory.Exists(directory))
|
|
Directory.Delete(directory, recursive: true);
|
|
}
|
|
|
|
// Two part types of very different size, three of each, on one of two stocks.
|
|
private static NestJob MixedJob() => Job(
|
|
[
|
|
Part("large", LShape(20, 20, 6), 3, RotationPolicy.Automatic),
|
|
Part("small", Triangle(5, 4), 6, RotationPolicy.Automatic),
|
|
],
|
|
[Stock("s48", 48, 48, spacing: 0.25), Stock("s60", 48, 60, spacing: 0.25)]);
|
|
|
|
private List<JsonElement> Events() =>
|
|
File.ReadAllLines(Assert.Single(Directory.GetFiles(directory, "*.jsonl")))
|
|
.Select(line => JsonDocument.Parse(line).RootElement)
|
|
.ToList();
|
|
|
|
private static string Name(JsonElement e) => e.GetProperty("event").GetString()!;
|
|
|
|
[Fact]
|
|
public void TracingLogsEveryCandidateAndNeverChangesTheResult()
|
|
{
|
|
var job = MixedJob();
|
|
var plain = new DefaultNestingEngine { TraceDirectory = null }.Solve(job);
|
|
|
|
var traced = new DefaultNestingEngine { TraceDirectory = directory }.Solve(job);
|
|
|
|
Assert.True(CandidateProgress.SameLayout(plain, traced));
|
|
var events = Events();
|
|
Assert.Equal("job", Name(events[0]));
|
|
Assert.Equal(4, events.Count(e => Name(e) == "candidate.finished"));
|
|
foreach (var name in new[]
|
|
{
|
|
"default.chosen", "irregular.start", "irregular.decode", "irregular.pass",
|
|
"rectangles.run", "raster.variant", "circles.noBid",
|
|
})
|
|
Assert.Contains(events, e => Name(e) == name);
|
|
Assert.DoesNotContain(events, e => Name(e) == "solve.aborted");
|
|
Assert.Contains("Chosen: ", File.ReadAllText(Assert.Single(Directory.GetFiles(directory, "*.summary.txt"))));
|
|
Assert.Null(NestTrace.Current);
|
|
}
|
|
|
|
[Fact]
|
|
public void ACanceledSolveLeavesItsLog()
|
|
{
|
|
var job = MixedJob();
|
|
using var canceled = new CancellationTokenSource();
|
|
var engine = new DefaultNestingEngine(new Func<INestingEngine>[] { () => new Canceling(canceled) })
|
|
{
|
|
TraceDirectory = directory,
|
|
};
|
|
|
|
Assert.Throws<OperationCanceledException>(() => engine.Solve(job, token: canceled.Token));
|
|
|
|
Assert.Equal("solve.aborted", Name(Events()[^1]));
|
|
Assert.Null(NestTrace.Current);
|
|
}
|
|
|
|
[Fact]
|
|
public void AnUnwritableDirectoryStillSolves()
|
|
{
|
|
Directory.CreateDirectory(directory);
|
|
var file = Path.Combine(directory, "taken");
|
|
File.WriteAllText(file, "");
|
|
var job = MixedJob();
|
|
|
|
var result = new DefaultNestingEngine { TraceDirectory = file }.Solve(job);
|
|
|
|
Assert.NotEmpty(result.Plates);
|
|
}
|
|
|
|
private sealed class Canceling(CancellationTokenSource source) : INestingEngine
|
|
{
|
|
public NestJobResult Solve(NestJob job, IProgress<NestJobProgress>? progress = null,
|
|
CancellationToken token = default)
|
|
{
|
|
source.Cancel();
|
|
token.ThrowIfCancellationRequested();
|
|
throw new InvalidOperationException("Not canceled.");
|
|
}
|
|
}
|
|
}
|