mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-03 11:11:36 -05:00
Share the nest server client setup and page validation, entity endpoints, post marker checks and the repeated desktop list, cut-time and busy-warning code. Persistence tests use NestArchiveJson. No behavior change intended. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
69 lines
2.2 KiB
C#
69 lines
2.2 KiB
C#
using System.Text.Json.Nodes;
|
|
using OpenNest.Geometry;
|
|
using static OpenNest.IO.Tests.NestArchiveJson;
|
|
|
|
namespace OpenNest.IO.Tests;
|
|
|
|
/// <summary>
|
|
/// The nest's assigned machine persists as top-level <c>machine</c> (<c>id</c> and <c>name</c>);
|
|
/// a nest without one writes no key, and a malformed one loads as none.
|
|
/// </summary>
|
|
public class NestMachinePersistenceTests
|
|
{
|
|
private static readonly NestMachine Machine = new(Guid.Parse("6f1a3c2e-5b7d-4e8f-9a0b-1c2d3e4f5a6b"), "Fiber 1");
|
|
|
|
private static Nest NestWith(NestMachine machine)
|
|
{
|
|
var nest = new Nest("machine") { Machine = machine };
|
|
nest.PlateDefaults.Size = new Size(48, 96);
|
|
nest.CreatePlate();
|
|
return nest;
|
|
}
|
|
|
|
[Fact]
|
|
public void MachineRoundTrips()
|
|
{
|
|
var reopened = new NestReader(Write(NestWith(Machine))).Read();
|
|
|
|
Assert.Equal(Machine.Id, reopened.Machine!.Id);
|
|
Assert.Equal("Fiber 1", reopened.Machine.Name);
|
|
}
|
|
|
|
[Fact]
|
|
public void NestWithoutMachine_WritesNoKeyAndLoadsWithoutOne()
|
|
{
|
|
var zip = Write(NestWith(null));
|
|
|
|
Assert.False(NestJson(zip).AsObject().ContainsKey("machine"));
|
|
Assert.Null(new NestReader(zip).Read().Machine);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("\"Fiber 1\"")]
|
|
[InlineData("{\"id\":42,\"name\":\"Fiber 1\"}")]
|
|
[InlineData("{\"id\":\"not-a-guid\",\"name\":\"Fiber 1\"}")]
|
|
[InlineData("{\"id\":\"00000000-0000-0000-0000-000000000000\"}")]
|
|
[InlineData("{\"name\":\"Fiber 1\"}")]
|
|
public void MalformedMachine_LoadsAsNone(string machine)
|
|
{
|
|
var zip = Rewrite(Write(NestWith(Machine)), json => json["machine"] = JsonNode.Parse(machine));
|
|
|
|
var reopened = new NestReader(zip).Read();
|
|
|
|
Assert.Null(reopened.Machine);
|
|
Assert.Equal("machine", reopened.Name);
|
|
}
|
|
|
|
[Fact]
|
|
public void MachineWithoutName_LoadsWithEmptyName()
|
|
{
|
|
var zip = Rewrite(Write(NestWith(Machine)),
|
|
json => json["machine"] = JsonNode.Parse($"{{\"id\":\"{Machine.Id}\",\"name\":7}}"));
|
|
|
|
var reopened = new NestReader(zip).Read();
|
|
|
|
Assert.Equal(Machine.Id, reopened.Machine!.Id);
|
|
Assert.Equal("", reopened.Machine.Name);
|
|
}
|
|
}
|