mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-04 22:51:38 -05:00
Priority worked like a rank (lower first) with a default of 0, and the Edit Drawing form stopped at 0, so moving one part ahead of the rest meant renumbering every other drawing. Higher-first lets a user raise a single part to 1 and leave everything else at the default. Every engine, filler and candidate comparer now serves the highest tier first. Stored values keep their order: - .nest files add `nestPriority` and keep writing the negated value under `priority`, so older readers nest in the same order; files without `nestPriority` are negated on read. Drawing packages strip both, so package hashes are unchanged. - Library settings send both keys the same way for older clients. - Server schema v18 negates stored drawing priorities (an older server refuses the upgraded database). - Job documents move to version 2; version 1 jobs are converted, and an older worker refuses version 2 instead of reversing the order. The Edit Drawing form now accepts negative priorities and says higher numbers nest first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
93 lines
2.9 KiB
C#
93 lines
2.9 KiB
C#
using System.IO.Compression;
|
|
using System.Text;
|
|
using System.Text.Json.Nodes;
|
|
|
|
namespace OpenNest.IO.Tests;
|
|
|
|
/// <summary>
|
|
/// Priority nests higher numbers first. Files keep writing the lower-first legacy value under
|
|
/// <c>priority</c> so older readers keep the same order, and files without <c>nestPriority</c>
|
|
/// are converted when read.
|
|
/// </summary>
|
|
public class NestPriorityTests
|
|
{
|
|
[Fact]
|
|
public void Write_KeepsTheLegacyValueBesideTheNewOne()
|
|
{
|
|
var file = Write(5);
|
|
|
|
var drawing = DrawingJson(file);
|
|
Assert.Equal(-5, (int)drawing["priority"]!);
|
|
Assert.Equal(5, (int)drawing["nestPriority"]!);
|
|
Assert.Equal(5, Read(file).Priority);
|
|
}
|
|
|
|
[Fact]
|
|
public void Write_OmitsAZeroPriority()
|
|
{
|
|
var file = Write(0);
|
|
|
|
Assert.False(DrawingJson(file).ContainsKey("nestPriority"));
|
|
Assert.Equal(0, Read(file).Priority);
|
|
}
|
|
|
|
[Fact]
|
|
public void Read_ConvertsAFileWithOnlyTheLegacyValue()
|
|
{
|
|
var file = Rewrite(Write(5), drawing =>
|
|
{
|
|
drawing.Remove("nestPriority");
|
|
drawing["priority"] = 3;
|
|
});
|
|
|
|
Assert.Equal(-3, Read(file).Priority);
|
|
}
|
|
|
|
[Fact]
|
|
public void Read_PrefersTheNewValue()
|
|
{
|
|
var file = Rewrite(Write(5), drawing => drawing["priority"] = 9);
|
|
|
|
Assert.Equal(5, Read(file).Priority);
|
|
}
|
|
|
|
private static byte[] Write(int priority)
|
|
{
|
|
var drawing = DrawingPackageFixture.Build();
|
|
drawing.Priority = priority;
|
|
var nest = new Nest(drawing.Name) { Units = Units.Millimeters };
|
|
nest.Drawings.Add(drawing);
|
|
using var stream = new MemoryStream();
|
|
Assert.True(new NestWriter(nest).Write(stream));
|
|
return stream.ToArray();
|
|
}
|
|
|
|
private static Drawing Read(byte[] file) =>
|
|
Assert.Single(new NestReader(new MemoryStream(file)).Read().Drawings);
|
|
|
|
private static JsonObject DrawingJson(byte[] file)
|
|
{
|
|
using var archive = new ZipArchive(new MemoryStream(file), ZipArchiveMode.Read);
|
|
using var reader = new StreamReader(archive.GetEntry("nest.json")!.Open(), Encoding.UTF8);
|
|
return JsonNode.Parse(reader.ReadToEnd())!["drawings"]![0]!.AsObject();
|
|
}
|
|
|
|
private static byte[] Rewrite(byte[] file, Action<JsonObject> change)
|
|
{
|
|
using var stream = new MemoryStream();
|
|
stream.Write(file);
|
|
using (var archive = new ZipArchive(stream, ZipArchiveMode.Update, leaveOpen: true))
|
|
{
|
|
var entry = archive.GetEntry("nest.json")!;
|
|
JsonNode root;
|
|
using (var reader = new StreamReader(entry.Open(), Encoding.UTF8))
|
|
root = JsonNode.Parse(reader.ReadToEnd())!;
|
|
change(root["drawings"]![0]!.AsObject());
|
|
entry.Delete();
|
|
using var writer = new StreamWriter(archive.CreateEntry("nest.json").Open(), new UTF8Encoding(false));
|
|
writer.Write(root.ToJsonString());
|
|
}
|
|
return stream.ToArray();
|
|
}
|
|
}
|