Files
OpenNest/OpenNest.Desktop.Tests/Forms/NestCommandsTests.cs
T
ajandClaude Opus 5.5 6f1d21173d feat(desktop): share nest info, Clear Nest and the nest defaults choices
NestInfoValues is what Nest > Edit edits (name, dates, thickness,
material and grade, customer, status, author, units, notes and the plate
defaults): read from a nest, when Done is enabled, how it writes the
nest, which Made By to remember, the material and grade lists and the
fields undo puts back. NestCommands holds its commit as one "Nest Info"
step (a changed material or thickness takes the machine's spacing and an
untouched trailing sheet follows), the defaults "Use these defaults for
new nests" saves, and Clear Nest as one "Clear Nest" step with its
question and busy message. NestDefaultsChoices holds the Tools > Nest
Defaults dialog's machine order, common-line list and box ranges.

The WinForms Edit Nest dialog, nest window and Nest Defaults dialog
delegate to them with unchanged behavior.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 18:42:20 -05:00

117 lines
3.9 KiB
C#

using OpenNest.CommonLines;
using OpenNest.Data;
using OpenNest.Geometry;
using OpenNest.Undo;
namespace OpenNest.Desktop.Tests.Forms;
public class NestCommandsTests
{
/// <summary>A nest window's state: one plate with a part, the trailing empty plate and a history.</summary>
private sealed class Env
{
public Env()
{
var nest = TestNests.OneSquare();
nest.PlateDefaults.Size = new Size(48, 96);
nest.UpdateDrawingQuantities();
Document = new Document { Nest = nest };
Plates = new PlateManager(nest);
Plates.EnsureSentinel();
Undo = new NestUndoRecorder(Document);
Undo.Reset();
}
public Document Document { get; }
public Nest Nest => Document.Nest;
public PlateManager Plates { get; }
public NestUndoRecorder Undo { get; }
public ChangeScope BeginChange(string label) => Undo.BeginChange(label);
public int Steps => Undo.History.UndoCount;
}
[Fact]
public void EditNestInfo_IsOneNestInfoStep_TheTrailingPlateFollows_AndUndoes()
{
var env = new Env();
var values = NestInfoValues.FromNest(env.Nest) with { Customer = "Acme", SizeText = "60 x 120" };
var applied = false;
var changed = NestCommands.EditNestInfo(env.Nest, env.Plates, values, null, env.BeginChange,
() => applied = true);
Assert.True(changed);
Assert.True(applied);
Assert.Equal("Acme", env.Nest.Customer);
Assert.Equal(new Size(60, 120), env.Nest.PlateDefaults.Size);
Assert.Equal(new Size(60, 120), env.Nest.Plates[^1].Size);
Assert.Equal(new Size(48, 96), env.Nest.Plates[0].Size);
Assert.Equal("Undo Nest Info", env.Undo.UndoText);
env.Undo.Undo();
Assert.Equal(new Size(48, 96), env.Nest.PlateDefaults.Size);
Assert.NotEqual("Acme", env.Nest.Customer);
}
[Fact]
public void EditNestInfo_OfOnlyTheIdentity_RecordsNothing_ButKeepsIt()
{
var env = new Env();
var values = NestInfoValues.FromNest(env.Nest) with { Name = "renamed", Status = NestStatus.HasBeenCut };
var changed = NestCommands.EditNestInfo(env.Nest, env.Plates, values, null, env.BeginChange);
Assert.False(changed);
Assert.Equal(0, env.Steps);
Assert.Equal("renamed", env.Nest.Name);
Assert.Equal(NestStatus.HasBeenCut, env.Nest.Status);
}
[Fact]
public void NewNestDefaults_TakeTheNestsMachine_OrTheSavedOne()
{
var env = new Env();
env.Nest.CommonLineUsage = CommonLineUsage.Whenever;
var saved = new NestDefaults { MachineId = Guid.NewGuid() };
var withoutMachine = NestCommands.NewNestDefaults(env.Nest, saved);
Assert.Equal(saved.MachineId, withoutMachine.MachineId);
Assert.Equal(CommonLineUsage.Whenever, withoutMachine.CommonLineUsage);
Assert.Equal(new Size(48, 96), withoutMachine.Size);
var machine = Guid.NewGuid();
env.Nest.Machine = new NestMachine(machine, "Laser");
Assert.Equal(machine, NestCommands.NewNestDefaults(env.Nest, saved).MachineId);
}
[Fact]
public void ClearNest_IsOneClearNestStep_LeavingOneEmptyPlate_AndUndoes()
{
var env = new Env();
var before = env.Nest.Plates.Count;
NestCommands.ClearNest(env.Plates, env.BeginChange);
Assert.Single(env.Nest.Plates);
Assert.Empty(env.Nest.Plates[0].Parts);
Assert.Equal("Undo Clear Nest", env.Undo.UndoText);
env.Undo.Undo();
Assert.Equal(before, env.Nest.Plates.Count);
Assert.Single(env.Nest.Plates[0].Parts);
}
[Fact]
public void ClearNest_OfAnEmptyNest_RecordsNothing()
{
var env = new Env();
NestCommands.ClearNest(env.Plates, env.BeginChange);
var steps = env.Steps;
NestCommands.ClearNest(env.Plates, env.BeginChange);
Assert.Equal(steps, env.Steps);
}
}