feat(desktop): show a server nest's MaterialLedger holds and closeout note

Server nests in Database mode get a read-only status bar label comparing their
open MaterialLedger holds with the sheets they cut, e.g. "Ledger: 2 of 4 held,
2 short". Nothing writes to the ledger and nothing waits on it.

- MaterialLedgerClient.GetHoldsAsync reads GET /api/stock/holds?openNestNestId=
  with the closed LedgerHoldsResult set; unknown status strings are kept. The
  request plumbing is shared with GetStockAsync.
- LedgerHoldSummary (OpenNest.Data) counts Proposed/Approved/Released/
  AwaitingCloseout holds against the plate quantities, skipping the trailing
  empty plate as /sheets does.
- NestHoldStatus refreshes on server open, after save, after storage or
  Options changes and on label click; it never polls. The tooltip lists holds
  in nest units. File-mode, unsaved and ledger-less nests show nothing.
- Setting Has Been Cut in Nest Info with holds still open shows a non-modal
  note linking to {ledgerUrl}/Nests/{id}. Posting is not gated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-10-25 17:40:45 -04:00
1 parent 23f093fcc1
commit f847b216f2
17 files changed
+1020 -24

No files matched your search

@@ -0,0 +1,113 @@
using System.Globalization;
using OpenNest.CNC;
using OpenNest.Data.MaterialLedger;
using OpenNest.Geometry;
namespace OpenNest.Tests.Data;
public class LedgerHoldSummaryTests
{
[Fact]
public void SheetCountSumsPlateQuantitiesAndSkipsTheTrailingEmptyPlate()
{
var nest = new Nest("N");
nest.Plates.Add(WithPart(quantity: 3));
nest.Plates.Add(new Plate { Quantity = 2 }); // Interior: /sheets still lists it.
nest.Plates.Add(WithPart(quantity: 1));
nest.Plates.Add(new Plate { Quantity = 5 }); // The editor's new-plate slot.
Assert.Equal(6, LedgerHoldSummary.PhysicalSheetCount(nest));
Assert.Equal(0, LedgerHoldSummary.PhysicalSheetCount(new Nest("Empty")));
}
[Fact]
public void AllSheetsHeld()
{
var summary = Summarize(4, Hold("Approved"), Hold("Released"), Hold("Proposed"), Hold("AwaitingCloseout"));
Assert.Equal(LedgerHoldState.Held, summary.State);
Assert.Equal("Ledger: 4 of 4 sheets held", summary.Text);
Assert.Equal(0, summary.ShortSheets);
}
[Fact]
public void ClosedAndUnknownHoldsDoNotCountAsHeld()
{
var summary = Summarize(4, Hold("Approved"), Hold("Released"), Hold("ClosedConsumed"),
Hold("Cancelled"), Hold("Exception"), Hold("OnTruck"));
Assert.Equal(LedgerHoldState.Short, summary.State);
Assert.Equal((2, 4, 2), (summary.OpenHolds, summary.ClosedHolds, summary.ShortSheets));
Assert.Equal("Ledger: 2 of 4 held, 2 short", summary.Text);
}
[Fact]
public void NoHoldsAndNoOpenHolds()
{
var none = Summarize(4);
Assert.Equal(LedgerHoldState.None, none.State);
Assert.Equal("Ledger: no holds", none.Text);
var closed = Summarize(2, Hold("ClosedConsumed"), Hold("ClosedReturned"));
Assert.Equal(LedgerHoldState.None, closed.State);
Assert.Equal("Ledger: no open holds (2 closed)", closed.Text);
}
[Fact]
public void MoreHoldsThanSheetsIsHeldWithExtras()
{
var summary = Summarize(1, Hold("Approved"), Hold("Approved"));
Assert.Equal(LedgerHoldState.Held, summary.State);
Assert.Equal("Ledger: 1 of 1 sheets held, 1 extra", summary.Text);
}
[Fact]
public void FailuresAreUnavailableAndNotConfiguredShowsNothing()
{
Assert.Null(LedgerHoldSummary.From(new LedgerHoldsResult.NotConfigured(), 4));
var down = LedgerHoldSummary.From(new LedgerHoldsResult.Unreachable("timed out"), 4)!;
Assert.Equal((LedgerHoldState.Unavailable, "Ledger: unavailable", "timed out"), (down.State, down.Text, down.Reason));
var rejected = LedgerHoldSummary.From(new LedgerHoldsResult.Unauthorized(), 4)!;
Assert.Equal(LedgerHoldState.Unavailable, rejected.State);
Assert.Contains("API key", rejected.Reason);
}
[Fact]
public void ToolTipListsHoldsInNestUnits()
{
var previous = CultureInfo.CurrentCulture;
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
try
{
var summary = Summarize(2, new LedgerHold(91, "DROP-1182", 30, 52.5m, "AwaitingCloseout", "B3", default),
new LedgerHold(92, "FS-1", 48, 96, "Approved", null, default));
Assert.Equal("Ledger: 2 of 2 sheets held" + Environment.NewLine
+ "DROP-1182 30 x 52.5 in B3 Awaiting Closeout" + Environment.NewLine
+ "FS-1 48 x 96 in Approved", summary.ToolTip(Units.Inches));
Assert.Equal("DROP-1182 762 x 1333.5 mm B3 Awaiting Closeout",
LedgerHoldSummary.FormatHold(summary.Holds[0], Units.Millimeters));
}
finally
{
CultureInfo.CurrentCulture = previous;
}
}
private static LedgerHoldSummary Summarize(int sheets, params LedgerHold[] holds) =>
LedgerHoldSummary.From(new LedgerHoldsResult.Ok(holds), sheets)!;
private static LedgerHold Hold(string status) => new(1, "X", 48, 96, status, "A1", default);
private static Plate WithPart(int quantity)
{
var program = new Program();
program.Codes.Add(new RapidMove(new Vector(0, 0)));
program.Codes.Add(new LinearMove(new Vector(10, 0)));
program.Codes.Add(new LinearMove(new Vector(10, 10)));
program.Codes.Add(new LinearMove(new Vector(0, 0)));
var plate = new Plate { Quantity = quantity };
plate.Parts.Add(new Part(new Drawing("test", program)));
return plate;
}
}
@@ -139,6 +139,92 @@ public class MaterialLedgerClientTests
Assert.Equal("Circular", item.Shape);
}
private static readonly Guid NestId = Guid.Parse("4f7c2a1e-0b9d-4e55-8a6f-1c2d3e4f5a6b");
[Fact]
public async Task HoldsAreListedWithTheKeyAndNestId()
{
HttpRequestMessage? seen = null;
var client = Client(request =>
{
seen = request;
return Json("""
{
"openNestNestId": "4f7c2a1e-0b9d-4e55-8a6f-1c2d3e4f5a6b",
"holds": [
{ "materialReservationId": 91, "itemCode": "DROP-1182", "widthIn": 30, "lengthIn": 52.5,
"status": "Approved", "locationCode": "B3", "createdAtUtc": "2026-10-20T14:00:00Z" },
{ "materialReservationId": 92, "itemCode": "FS-4896", "widthIn": 48, "lengthIn": 96,
"status": "ClosedConsumed", "locationCode": null, "createdAtUtc": "2026-10-20T14:00:00Z" }
]
}
""");
});
var ok = Assert.IsType<LedgerHoldsResult.Ok>(await client.GetHoldsAsync(NestId, default));
Assert.Equal("/api/stock/holds?openNestNestId=4f7c2a1e-0b9d-4e55-8a6f-1c2d3e4f5a6b", seen!.RequestUri!.PathAndQuery);
Assert.Equal("secret-key", Assert.Single(seen.Headers.GetValues(MaterialLedgerClient.ApiKeyHeader)));
Assert.Equal(2, ok.Holds.Count);
var first = ok.Holds[0];
Assert.Equal((91L, "DROP-1182", 30m, 52.5m, LedgerHoldStatuses.Approved, "B3"),
(first.MaterialReservationId, first.ItemCode, first.WidthIn, first.LengthIn, first.Status, first.LocationCode));
Assert.Null(ok.Holds[1].LocationCode);
}
[Fact]
public async Task NoHoldsIsAnEmptyList()
{
var client = Client(_ => Json("""{ "openNestNestId": "4f7c2a1e-0b9d-4e55-8a6f-1c2d3e4f5a6b", "holds": [] }"""));
Assert.Empty(Assert.IsType<LedgerHoldsResult.Ok>(await client.GetHoldsAsync(NestId, default)).Holds);
var missing = Client(_ => Json("""{ "openNestNestId": "4f7c2a1e-0b9d-4e55-8a6f-1c2d3e4f5a6b" }"""));
Assert.Empty(Assert.IsType<LedgerHoldsResult.Ok>(await missing.GetHoldsAsync(NestId, default)).Holds);
}
[Fact]
public async Task UnknownHoldStatusIsKept()
{
var client = Client(_ => Json("""
{
"openNestNestId": "4f7c2a1e-0b9d-4e55-8a6f-1c2d3e4f5a6b",
"holds": [{ "materialReservationId": 5, "itemCode": "D-1", "widthIn": 10, "lengthIn": 20,
"status": "OnTruck", "locationCode": "Y", "createdAtUtc": "2026-10-20T14:00:00Z", "someNewField": 1 }]
}
"""));
var hold = Assert.Single(Assert.IsType<LedgerHoldsResult.Ok>(await client.GetHoldsAsync(NestId, default)).Holds);
Assert.Equal("OnTruck", hold.Status);
Assert.False(LedgerHoldStatuses.IsOpen(hold.Status));
}
[Fact]
public async Task HoldFailuresAreResults()
{
var sent = 0;
var notConfigured = Client(_ => { sent++; return Json("{}"); }, new MaterialLedgerSettings());
Assert.IsType<LedgerHoldsResult.NotConfigured>(await notConfigured.GetHoldsAsync(NestId, default));
Assert.Equal(0, sent);
var down = Client(_ => throw new HttpRequestException("No such host is known."));
Assert.Contains("No such host", Assert.IsType<LedgerHoldsResult.Unreachable>(
await down.GetHoldsAsync(NestId, default)).Reason);
var oldLedger = Client(_ => new HttpResponseMessage(HttpStatusCode.NotFound));
Assert.Contains("no OpenNest holds route", Assert.IsType<LedgerHoldsResult.Unreachable>(
await oldLedger.GetHoldsAsync(NestId, default)).Reason);
var rejected = Client(_ => new HttpResponseMessage(HttpStatusCode.Unauthorized));
Assert.IsType<LedgerHoldsResult.Unauthorized>(await rejected.GetHoldsAsync(NestId, default));
var garbage = Client(_ => Json("<html>login</html>"));
Assert.IsType<LedgerHoldsResult.Unreachable>(await garbage.GetHoldsAsync(NestId, default));
using var http = new HttpClient(new HangingHandler());
var slow = new MaterialLedgerClient(http, Configured, TimeSpan.FromMilliseconds(50));
Assert.Contains("No answer", Assert.IsType<LedgerHoldsResult.Unreachable>(
await slow.GetHoldsAsync(NestId, default)).Reason);
}
private static MaterialLedgerClient Client(Func<HttpRequestMessage, HttpResponseMessage> reply,
MaterialLedgerSettings? settings = null) =>
new(new HttpClient(new Handler(reply)), settings ?? Configured);