Routing sizes render jobs from the resolved preset, and the node wait applies the same routing as the claim, so a re-routed job neither keeps a stale size nor waits for a node that will never take it. Settings an older server cannot read stay on this side for the remote editor, archive sync and workspace push, and the reason is shown where it applies. A turn on one side and a framing on the other merge as one conflict. Orientation and framing fixes: an out-of-range crop turns with the content it shows, the aspect text transposes exactly, the Rotation row counts turns as shown on screen, and Reset framing undoes the orientation press by press, so keyframes and aspect stay on their content. The advisor places the horizon in the view as shown. A look at zero strength is cleared and no longer reaches the motion vectors. Preview changes: the middle button pans, a press inside a box with no slack pans while zoomed, the selected box shows a ring, and screen readers hear when the crop value changes. Tooltips in en and it updated; stale translations of changed tooltips removed so they fall back to English. Test changes: the pinned hash is OS-independent, and the tautological encoder and warning assertions became real checks. Added a non-screen test of the Straighten slider binding. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
629 lines
34 KiB
C#
629 lines
34 KiB
C#
using Chronos.Core.Export;
|
||
using Chronos.Core.Project;
|
||
using Chronos.Core.Video;
|
||
using Chronos.Testing;
|
||
|
||
namespace Chronos.Core.Tests.Project;
|
||
|
||
public sealed class ProjectSettingsTests
|
||
{
|
||
[Test]
|
||
public void JsonRoundTripKeepsAutoAndManualValues()
|
||
{
|
||
var settings = new ProjectSettings
|
||
{
|
||
Exposure = new ExposureSettings
|
||
{
|
||
DeflickerWindow = new Auto<int>().WithAdvice(41, "Advisor.Reason.FlickerPeriod", "10.2"),
|
||
DeflickerStrength = new Auto<double>().WithManual(0.8),
|
||
},
|
||
Timing = new TimingSettings { FrameRate = new Auto<string>().WithAdvice("24", null) },
|
||
ExcludedFrames = [3, 7],
|
||
};
|
||
var back = ProjectSettings.FromJson(settings.ToJson());
|
||
Assert.Equal(41, back.Exposure.DeflickerWindow.Resolve(0));
|
||
Assert.True(back.Exposure.DeflickerWindow.IsAuto);
|
||
Assert.Equal("Advisor.Reason.FlickerPeriod", back.Exposure.DeflickerWindow.ReasonKey);
|
||
Assert.Equal(0.8, back.Exposure.DeflickerStrength.Resolve(1.0));
|
||
Assert.False(back.Exposure.DeflickerStrength.IsAuto);
|
||
Assert.SequenceEqual(new[] { 3, 7 }, back.ExcludedFrames);
|
||
Assert.Contains("\"deflickerWindow\"", settings.ToJson());
|
||
}
|
||
|
||
[Test]
|
||
public void ValueTypesFallBackOnlyWithoutAdvice()
|
||
{
|
||
Assert.Equal(21, new Auto<int>().Resolve(21));
|
||
Assert.Equal(FlowQualitySetting.Balanced, new Auto<FlowQualitySetting>().Resolve(FlowQualitySetting.Balanced));
|
||
Assert.Equal(0, new Auto<int>().WithAdvice(0, null).Resolve(21));
|
||
Assert.False(new Auto<bool>().WithAdvice(false, null).Resolve(true));
|
||
Assert.Equal(0.5, new Auto<double>().WithAdvice(0.8, null).WithManual(0.5).Resolve(1.0));
|
||
Assert.False(new Auto<bool>().WithManual(false).Resolve(true));
|
||
var back = ProjectSettings.FromJson(new ProjectSettings
|
||
{
|
||
Exposure = new ExposureSettings { Deflicker = new Auto<bool>().WithAdvice(false, null) },
|
||
}.ToJson());
|
||
Assert.False(back.Exposure.Deflicker.Resolve(true));
|
||
Assert.True(ProjectSettings.FromJson(new ProjectSettings().ToJson()).Exposure.Deflicker.Resolve(true));
|
||
}
|
||
|
||
[Test]
|
||
public void ResetReturnsToAdvice()
|
||
{
|
||
var a = new Auto<int>().WithAdvice(41, null).WithManual(15);
|
||
Assert.Equal(15, a.Resolve(0));
|
||
Assert.Equal(41, a.Reset().Resolve(0));
|
||
}
|
||
|
||
[Test]
|
||
public void NewerVersionIsRefused() =>
|
||
Assert.Throws<InvalidDataException>(() => ProjectSettings.FromJson("{\"version\": 99}"));
|
||
|
||
[Test]
|
||
public void MissingMembersKeepTheirDefaults()
|
||
{
|
||
var defaults = new ProjectSettings().ToJson();
|
||
Assert.Equal(defaults, ProjectSettings.FromJson("{}").ToJson());
|
||
Assert.Equal(defaults, ProjectSettings.FromJson("null").ToJson());
|
||
|
||
// An API client's partial edit: one manual value, a section given as null, keys in another casing.
|
||
var partial = ProjectSettings.FromJson(
|
||
"""{ "Exposure": { "deflicker": { "isAuto": false, "manual": false } }, "motion": null, "export": { "preset": { "isAuto": false, "manual": "4k-hevc" } } }""");
|
||
Assert.False(partial.Exposure.Deflicker.Resolve(true));
|
||
Assert.True(partial.Exposure.HolyGrail.IsAuto);
|
||
Assert.Equal(0.9, partial.Exposure.HighlightKnee);
|
||
Assert.True(partial.Motion.Stabilize.IsAuto);
|
||
Assert.Equal("4k-hevc", partial.Export.Preset.Resolve("1080p-h264"));
|
||
Assert.Equal("{date} {title} - {preset}", partial.Export.NameTemplate);
|
||
Assert.SequenceEqual(new[] { 0.0, 1.0 }, partial.Timing.RemapCurve.Select(p => p.X));
|
||
Assert.Equal(9, partial.Stack.Length);
|
||
Assert.Equal(ProjectSettings.BaseVersion, partial.Version);
|
||
|
||
// An explicit null clears a nullable value; arrays are replaced, not merged.
|
||
var cleared = ProjectSettings.FromJson("""{ "exposure": { "highlightKnee": null }, "timing": { "remapCurve": [ { "x": 0, "y": 0.5 } ] } }""");
|
||
Assert.Null(cleared.Exposure.HighlightKnee);
|
||
Assert.Equal(1, cleared.Timing.RemapCurve.Count);
|
||
}
|
||
|
||
[Test]
|
||
public void NullHighlightKneeSurvivesTheRoundTrip()
|
||
{
|
||
// "No knee, hard clip" must not come back as the default knee when the project is rewritten.
|
||
var off = new ProjectSettings { Exposure = new ExposureSettings { HighlightKnee = null } };
|
||
Assert.Contains("\"highlightKnee\": null", off.ToJson());
|
||
Assert.Null(ProjectSettings.FromJson(off.ToJson()).Exposure.HighlightKnee);
|
||
Assert.Equal(0.9, ProjectSettings.FromJson(new ProjectSettings().ToJson()).Exposure.HighlightKnee);
|
||
}
|
||
|
||
[Test]
|
||
public void NullListsAndTextsKeepTheirDefaults()
|
||
{
|
||
var project = ProjectSettings.FromJson(
|
||
"""{ "excludedFrames": null, "export": { "batch": null, "nameTemplate": null }, "timing": { "remapCurve": null }, "camera": { "keyframes": null } }""");
|
||
Assert.Count(0, project.ExcludedFrames);
|
||
Assert.Count(0, project.Export.Batch);
|
||
Assert.Equal("{date} {title} - {preset}", project.Export.NameTemplate);
|
||
Assert.Count(2, project.Timing.RemapCurve);
|
||
Assert.Count(0, project.Camera.Keyframes);
|
||
// Nullable members without a default still clear.
|
||
Assert.Null(ProjectSettings.FromJson("""{ "export": { "folder": null }, "heroFrame": null }""").HeroFrame);
|
||
}
|
||
|
||
[Test]
|
||
public void DuplicateKeysAreAJsonError()
|
||
{
|
||
Assert.Throws<System.Text.Json.JsonException>(() => ProjectSettings.FromJson("""{ "version": 1, "version": 1 }"""));
|
||
Assert.Throws<System.Text.Json.JsonException>(() => ProjectSettings.FromJson("""{ "Export": {}, "export": {} }"""));
|
||
Assert.Throws<System.Text.Json.JsonException>(() => ProjectSettings.FromJson("""{ "export": { "batch": [], "Batch": [] } }"""));
|
||
}
|
||
|
||
[Test]
|
||
public void ProjectMustBeAnObject() =>
|
||
Assert.Throws<System.Text.Json.JsonException>(() => ProjectSettings.FromJson("[1, 2]"));
|
||
|
||
[Test]
|
||
public void FramePositionsFollowInsertedFramesAndOtherMembersStay()
|
||
{
|
||
var project = new ProjectSettings
|
||
{
|
||
ExcludedFrames = [3, 7, 40],
|
||
HeroFrame = 2,
|
||
Camera = new CameraSettings { Keyframes = [new CameraKey(0, 0.5, 0.5, 1, 0), new CameraKey(7, 0.4, 0.5, 1.5, 0)] },
|
||
Exposure = new ExposureSettings { DeflickerWindow = new Auto<int>().WithManual(41) },
|
||
};
|
||
// Two frames inserted before position 3 and one before position 7; 40 is no frame of the old list and stays.
|
||
var moves = new Dictionary<int, int> { [0] = 0, [1] = 1, [2] = 4, [3] = 5, [4] = 6, [5] = 7, [6] = 8, [7] = 10 };
|
||
|
||
var moved = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.MoveFramePositions(project.ToJson(), moves)));
|
||
|
||
Assert.SequenceEqual([5, 10, 40], moved.ExcludedFrames);
|
||
Assert.Equal(4, moved.HeroFrame);
|
||
Assert.SequenceEqual([0, 10], moved.Camera.Keyframes.Select(k => k.Frame));
|
||
Assert.Equal((0.4, 1.5), (moved.Camera.Keyframes[1].CenterX, moved.Camera.Keyframes[1].Zoom));
|
||
Assert.Equal(41, moved.Exposure.DeflickerWindow.Manual);
|
||
}
|
||
|
||
[Test]
|
||
public void MovingPositionsEditsTheJsonInPlace()
|
||
{
|
||
const string project = """{ "ExcludedFrames": [1, 2], "heroFrame": null, "futureOption": { "keep": [1, 2] }, "camera": { "keyframes": [ { "frame": 1.5 } ] } }""";
|
||
|
||
var moved = Assert.NotNull(ProjectSettings.MoveFramePositions(project, new Dictionary<int, int> { [1] = 3 }));
|
||
|
||
var json = System.Text.Json.Nodes.JsonNode.Parse(moved)!;
|
||
Assert.SequenceEqual([3, 2], json["ExcludedFrames"]!.AsArray().Select(n => n!.GetValue<int>()));
|
||
Assert.Equal("""{"keep":[1,2]}""", json["futureOption"]!.ToJsonString());
|
||
Assert.Null(json["heroFrame"]);
|
||
Assert.Equal(1.5, json["camera"]!["keyframes"]![0]!["frame"]!.GetValue<double>());
|
||
}
|
||
|
||
[Test]
|
||
public void NothingToMoveGivesNoNewProject()
|
||
{
|
||
Assert.Null(ProjectSettings.MoveFramePositions(new ProjectSettings { ExcludedFrames = [1] }.ToJson(), new Dictionary<int, int>()));
|
||
Assert.Null(ProjectSettings.MoveFramePositions(new ProjectSettings { ExcludedFrames = [1] }.ToJson(), new Dictionary<int, int> { [2] = 3, [1] = 1 }));
|
||
Assert.Null(ProjectSettings.MoveFramePositions("not json", new Dictionary<int, int> { [1] = 2 }));
|
||
Assert.Null(ProjectSettings.MoveFramePositions("[1, 2]", new Dictionary<int, int> { [1] = 2 }));
|
||
Assert.Null(ProjectSettings.MoveFramePositions("""{ "heroFrame": 1, "HeroFrame": 1 }""", new Dictionary<int, int> { [1] = 2 }));
|
||
}
|
||
|
||
[Test]
|
||
public void AnEditorsPositionsFollowFramesInsertedUnderItAsTheIndexMovedTheStoredOnes()
|
||
{
|
||
// An editor read four frames; a download then inserted two earlier photos, one of which an exclusion waited for.
|
||
var photos = Enumerable.Range(0, 6).Select(Photo).ToList();
|
||
var ids = Enumerable.Range(0, 6).Select(_ => Guid.CreateVersion7()).ToList();
|
||
IReadOnlyList<Guid> before = ids[2..];
|
||
var after = ids.Select((id, i) => new Chronos.Core.Model.Frame
|
||
{
|
||
Id = id,
|
||
SequenceId = Guid.Empty,
|
||
Index = i,
|
||
RelativePath = $"s/IMG_{i}.JPG",
|
||
FileName = $"IMG_{i}.JPG",
|
||
Sha256 = photos[i].Sha256,
|
||
Size = photos[i].Size,
|
||
CapturedAt = photos[i].CapturedAt,
|
||
}).ToList();
|
||
var project = new ProjectSettings
|
||
{
|
||
ExcludedFrames = [1, 3],
|
||
HeroFrame = 0,
|
||
Camera = new CameraSettings { Keyframes = [new CameraKey(2, 0.5, 0.5, 1, 0)] },
|
||
Exposure = new ExposureSettings { DeflickerWindow = new Auto<int>().WithManual(41) },
|
||
PendingFrames = [new PendingFrame { Sha256 = photos[1].Sha256, Size = photos[1].Size, Excluded = true }],
|
||
};
|
||
|
||
var followed = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.FollowFrames(project.ToJson(), before, after)));
|
||
|
||
Assert.SequenceEqual([1, 3, 5], followed.ExcludedFrames);
|
||
Assert.Equal(2, followed.HeroFrame);
|
||
Assert.SequenceEqual([4], followed.Camera.Keyframes.Select(k => k.Frame));
|
||
Assert.Equal(41, followed.Exposure.DeflickerWindow.Manual);
|
||
Assert.Null(followed.PendingFrames);
|
||
|
||
// The same frames: nothing moves. A frame the editor read that is gone: positions stay, as the index leaves them.
|
||
Assert.Null(ProjectSettings.FollowFrames(new ProjectSettings { ExcludedFrames = [1] }.ToJson(), ids, after));
|
||
var removed = after.Where(f => f.Id != ids[3]).Select((f, i) => f with { Index = i }).ToList();
|
||
Assert.Null(ProjectSettings.FollowFrames(new ProjectSettings { ExcludedFrames = [1, 3] }.ToJson(), before, removed));
|
||
}
|
||
|
||
[Test]
|
||
public void PositionsCrossToAnotherCopyByPhotoAndWaitForPhotosItLacks()
|
||
{
|
||
// The server holds six photos; this copy only the last four, and one of its own the server lacks.
|
||
var photos = Enumerable.Range(0, 7).Select(Photo).ToList();
|
||
var server = Frames(photos[..6]);
|
||
var here = Frames([.. photos[2..6], photos[6]]);
|
||
var project = new ProjectSettings
|
||
{
|
||
ExcludedFrames = [0, 3],
|
||
HeroFrame = 1,
|
||
Camera = new CameraSettings { Keyframes = [new CameraKey(0, 0.5, 0.5, 1, 0), new CameraKey(4, 0.4, 0.5, 1.5, 0)] },
|
||
Exposure = new ExposureSettings { DeflickerWindow = new Auto<int>().WithManual(41) },
|
||
};
|
||
|
||
var taken = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(project.ToJson(), server, here)));
|
||
|
||
Assert.SequenceEqual([1], taken.ExcludedFrames);
|
||
Assert.Null(taken.HeroFrame);
|
||
Assert.SequenceEqual([2], taken.Camera.Keyframes.Select(k => k.Frame));
|
||
Assert.Equal(1.5, taken.Camera.Keyframes[0].Zoom);
|
||
Assert.Equal(41, taken.Exposure.DeflickerWindow.Manual);
|
||
var pending = Assert.NotNull(taken.PendingFrames);
|
||
Assert.Equal(2, pending.Count);
|
||
var first = Assert.Contains(pending, p => p.Sha256 == photos[0].Sha256);
|
||
Assert.True(first.Excluded && !first.Hero);
|
||
Assert.Count(1, first.Keyframes);
|
||
Assert.Equal(0.5, first.Keyframes[0].CenterX);
|
||
Assert.True(Assert.Contains(pending, p => p.Sha256 == photos[1].Sha256).Hero);
|
||
Assert.True(taken.HeroPending);
|
||
|
||
// The two photos arrive (inserted before the others): what waited takes its place, and the rest moved with its frames.
|
||
var arrived = Frames(photos);
|
||
var moved = ProjectSettings.MoveFramePositions(taken.ToJson(), new Dictionary<int, int> { [0] = 2, [1] = 3, [2] = 4, [3] = 5, [4] = 6 })!;
|
||
var placed = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(moved, null, arrived)));
|
||
Assert.SequenceEqual([0, 3], placed.ExcludedFrames);
|
||
Assert.Equal(1, placed.HeroFrame);
|
||
Assert.SequenceEqual([0, 4], placed.Camera.Keyframes.Select(k => k.Frame));
|
||
Assert.Null(placed.PendingFrames);
|
||
Assert.Null(ProjectSettings.TranslateFramePositions(placed.ToJson(), null, arrived));
|
||
}
|
||
|
||
[Test]
|
||
public void SettingsSentBackLandOnTheSamePhotos()
|
||
{
|
||
var photos = Enumerable.Range(0, 5).Select(Photo).ToList();
|
||
var server = Frames(photos[1..]);
|
||
var here = Frames(photos);
|
||
// Photo 0 is only here: its exclusion waits on the server, and comes back as a position.
|
||
var project = new ProjectSettings { ExcludedFrames = [0, 2], HeroFrame = 4 };
|
||
|
||
var sent = Assert.NotNull(ProjectSettings.TranslateFramePositions(project.ToJson(), here, server));
|
||
var back = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(sent, server, here)));
|
||
|
||
Assert.SequenceEqual([1], ProjectSettings.FromJson(sent).ExcludedFrames);
|
||
Assert.Equal(3, ProjectSettings.FromJson(sent).HeroFrame);
|
||
Assert.Equal(project.ToJson(), back.ToJson());
|
||
}
|
||
|
||
[Test]
|
||
public void AHeroOrKeyframeChosenSinceIsNewerThanTheOneThatWaited()
|
||
{
|
||
var photos = Enumerable.Range(0, 3).Select(Photo).ToList();
|
||
var waiting = new ProjectSettings
|
||
{
|
||
HeroFrame = 2,
|
||
Camera = new CameraSettings { Keyframes = [new CameraKey(0, 0.2, 0.2, 2, 0)] },
|
||
PendingFrames = [new PendingFrame { Sha256 = photos[0].Sha256, Size = photos[0].Size, Hero = true, Keyframes = [new CameraKey(9, 0.7, 0.7, 1, 0)] }],
|
||
};
|
||
|
||
var placed = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(waiting.ToJson(), null, Frames(photos))));
|
||
|
||
Assert.Equal(2, placed.HeroFrame);
|
||
Assert.Count(1, placed.Camera.Keyframes);
|
||
Assert.Equal(0.2, placed.Camera.Keyframes[0].CenterX);
|
||
Assert.Null(placed.PendingFrames);
|
||
}
|
||
|
||
[Test]
|
||
public void AHeroChosenSinceAnotherWaitedStaysTheHeroWhenItMustWaitToo()
|
||
{
|
||
// This PC chose photo 3 as the hero before the server had it, so it waits there; an editor on the server then
|
||
// chose photo 2, which this PC lacks.
|
||
var photos = Enumerable.Range(0, 4).Select(Photo).ToList();
|
||
var server = Frames(photos[..3]);
|
||
var here = Frames([photos[0], photos[1], photos[3]]);
|
||
var edited = new ProjectSettings
|
||
{
|
||
ExcludedFrames = [0],
|
||
HeroFrame = 2,
|
||
PendingFrames = [new PendingFrame { Sha256 = photos[3].Sha256, Size = photos[3].Size, CapturedAt = photos[3].CapturedAt, Hero = true }],
|
||
};
|
||
|
||
var taken = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(edited.ToJson(), server, here)));
|
||
|
||
Assert.Null(taken.HeroFrame);
|
||
Assert.SequenceEqual([0], taken.ExcludedFrames);
|
||
var waiting = Assert.NotNull(taken.PendingFrames);
|
||
Assert.Count(1, waiting);
|
||
Assert.True(waiting[0].Hero && waiting[0].Sha256 == photos[2].Sha256, "the newer hero waits for its photo");
|
||
Assert.True(taken.HeroPending);
|
||
|
||
// Its photo arrives: it is the hero again.
|
||
var arrived = Frames([photos[0], photos[1], photos[2], photos[3]]);
|
||
var moved = ProjectSettings.MoveFramePositions(taken.ToJson(), new Dictionary<int, int> { [2] = 3 }) ?? taken.ToJson();
|
||
var placed = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(moved, null, arrived)));
|
||
Assert.Equal(2, placed.HeroFrame);
|
||
Assert.Null(placed.PendingFrames);
|
||
|
||
// A hero chosen here while one waits clears the waiting one, even before its photo arrives.
|
||
var chosen = (taken with { HeroFrame = 1 }).ToJson();
|
||
var cleared = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(chosen, null, here)));
|
||
Assert.Equal(1, cleared.HeroFrame);
|
||
Assert.Null(cleared.PendingFrames);
|
||
}
|
||
|
||
[Test]
|
||
public void APhotoHeldTwiceKeepsEachCopysPositions()
|
||
{
|
||
// The server adopted a folder holding photo 1 twice ('IMG_0001.JPG', 'IMG_0001 (1).JPG'); this PC keeps it once.
|
||
var photos = Enumerable.Range(0, 3).Select(Photo).ToList();
|
||
var server = Frames([photos[0], photos[1], photos[1], photos[2]]);
|
||
var here = Frames(photos);
|
||
var project = new ProjectSettings
|
||
{
|
||
ExcludedFrames = [2],
|
||
HeroFrame = 2,
|
||
Camera = new CameraSettings { Keyframes = [new CameraKey(1, 0.1, 0.5, 1, 0), new CameraKey(2, 0.2, 0.5, 1, 0)] },
|
||
};
|
||
|
||
// The second copy, which the deep analysis flags as a duplicate, is excluded there: this PC still renders its only copy.
|
||
var taken = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(project.ToJson(), server, here)));
|
||
Assert.Empty(taken.ExcludedFrames);
|
||
Assert.Null(taken.HeroFrame);
|
||
Assert.SequenceEqual([1], taken.Camera.Keyframes.Select(k => k.Frame));
|
||
Assert.Equal(0.1, taken.Camera.Keyframes[0].CenterX);
|
||
var waiting = Assert.NotNull(taken.PendingFrames);
|
||
Assert.Count(1, waiting);
|
||
Assert.True(waiting[0] is { Excluded: true, Hero: true, Occurrence: 1 } && waiting[0].Sha256 == photos[1].Sha256);
|
||
Assert.Count(1, waiting[0].Keyframes);
|
||
Assert.Equal(0.2, waiting[0].Keyframes[0].CenterX);
|
||
Assert.Contains("\"occurrence\": 1", taken.ToJson());
|
||
|
||
// Sent back after an edit of something else, every position lands on the copy it named.
|
||
var edited = taken with { Exposure = new ExposureSettings { ExposureOffsetEv = 0.3 } };
|
||
var back = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(edited.ToJson(), here, server)));
|
||
Assert.SequenceEqual([2], back.ExcludedFrames);
|
||
Assert.Equal(2, back.HeroFrame);
|
||
Assert.SequenceEqual([1, 2], back.Camera.Keyframes.Select(k => k.Frame));
|
||
Assert.SequenceEqual([0.1, 0.2], back.Camera.Keyframes.Select(k => k.CenterX));
|
||
Assert.Null(back.PendingFrames);
|
||
|
||
// Both copies excluded there: this PC excludes its copy, and the server gets both back.
|
||
var both = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(new ProjectSettings { ExcludedFrames = [1, 2] }.ToJson(), server, here)));
|
||
Assert.SequenceEqual([1], both.ExcludedFrames);
|
||
var excluded = Assert.NotNull(both.PendingFrames);
|
||
Assert.Count(1, excluded);
|
||
Assert.True(excluded[0] is { Excluded: true, Hero: false, Occurrence: 1 });
|
||
var returned = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(both.ToJson(), here, server)));
|
||
Assert.SequenceEqual([1, 2], returned.ExcludedFrames);
|
||
Assert.Null(returned.PendingFrames);
|
||
}
|
||
|
||
[Test]
|
||
public void TheCopiesOfAPhotoAreToldApartByTheirOrder()
|
||
{
|
||
var time = new DateTime(2026, 9, 28, 18, 0, 0);
|
||
var hashed = new FramePhoto(new string('a', 64), 100, time);
|
||
var other = new FramePhoto(new string('b', 64), 200, time);
|
||
var unhashed = new FramePhoto(null, 100, time);
|
||
// The frame at 3 has not been hashed yet: it may be a copy of the photo, or another photo of the same size taken in
|
||
// the same second. The frames with the photo's hash are its copies; size and capture time match the unhashed one.
|
||
var frames = Frames([other, hashed, other, unhashed, hashed]);
|
||
|
||
Assert.SequenceEqual([1, 4], new[] { 0, 1, 2 }.Select(n => frames.IndexOf(hashed, n)).OfType<int>());
|
||
Assert.Null(frames.IndexOf(hashed, 2));
|
||
Assert.SequenceEqual([1, 3, 4], new[] { 0, 1, 2 }.Select(n => frames.IndexOf(unhashed, n)).OfType<int>());
|
||
Assert.SequenceEqual([0, 0, 1, 1, 1], Enumerable.Range(0, 5).Select(frames.Occurrence));
|
||
Assert.Equal(0, frames.Occurrence(9));
|
||
Assert.Equal(2, frames.IndexOf(new FramePhoto(new string('B', 64), 0, null), 1));
|
||
// Without a frame holding its hash, a hashed photo is the frames not hashed yet with its size and capture time.
|
||
Assert.Equal(3, frames.IndexOf(new FramePhoto(new string('c', 64), 100, time)));
|
||
}
|
||
|
||
[Test]
|
||
public void APhotoNotHashedYetDoesNotTakeThePositionsOfAnotherWithItsSizeAndTime()
|
||
{
|
||
// Two photos of a burst share size and capture time (a RAW of constant size, no sub-second times). The server has
|
||
// not hashed the first yet; this PC has hashed both and does not have the third photo yet.
|
||
var time = new DateTime(2026, 9, 28, 18, 0, 0);
|
||
var (first, second) = (new FramePhoto(new string('a', 64), 100, time), new FramePhoto(new string('b', 64), 100, time));
|
||
var server = Frames([first with { Sha256 = null }, second, Photo(3)]);
|
||
var here = Frames([first, second]);
|
||
|
||
// The second photo excluded on the server is the second here, and back; the first stays the hero on both.
|
||
var project = new ProjectSettings { ExcludedFrames = [1, 2], HeroFrame = 0 };
|
||
var taken = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(project.ToJson(), server, here)));
|
||
Assert.SequenceEqual([1], taken.ExcludedFrames);
|
||
Assert.Equal(0, taken.HeroFrame);
|
||
var waiting = Assert.NotNull(taken.PendingFrames);
|
||
Assert.True(waiting is [{ Excluded: true, Occurrence: 0 }] && waiting[0].Sha256 == Photo(3).Sha256, "only the third photo waits");
|
||
var back = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(taken.ToJson(), here, server)));
|
||
Assert.SequenceEqual([1, 2], back.ExcludedFrames);
|
||
Assert.Equal(0, back.HeroFrame);
|
||
Assert.Null(back.PendingFrames);
|
||
|
||
// A copy that has not hashed its first frame still finds the second photo by its hash, not on that frame.
|
||
Assert.Equal(1, server.IndexOf(second));
|
||
Assert.Equal(0, server.Occurrence(1));
|
||
Assert.Equal(0, server.IndexOf(first));
|
||
}
|
||
|
||
[Test]
|
||
public void AProjectNamingNoFrameCrossesAsItIs()
|
||
{
|
||
var json = new ProjectSettings { Exposure = new ExposureSettings { DeflickerWindow = new Auto<int>().WithManual(41) } }.ToJson();
|
||
|
||
Assert.False(ProjectSettings.NamesFrames(json));
|
||
Assert.False(json.Contains("pendingFrames", StringComparison.OrdinalIgnoreCase), "an old project reads and writes as it did");
|
||
Assert.False(ProjectSettings.NamesFrames("not json"));
|
||
Assert.True(ProjectSettings.NamesFrames("""{ "HeroFrame": 0 }"""));
|
||
Assert.Null(ProjectSettings.TranslateFramePositions(json, Frames([Photo(1)]), Frames([Photo(2)])));
|
||
Assert.Null(ProjectSettings.TranslateFramePositions("[1]", null, Frames([])));
|
||
}
|
||
|
||
[Test]
|
||
public void PhotosMatchByHashElseBySizeAndCaptureTime()
|
||
{
|
||
var time = new DateTime(2026, 9, 28, 18, 0, 0, 123);
|
||
var hashed = new FramePhoto(new string('a', 64), 100, time);
|
||
|
||
Assert.True(hashed.Matches(new FramePhoto(new string('A', 64), 1, null)));
|
||
Assert.False(hashed.Matches(new FramePhoto(new string('b', 64), 100, time)));
|
||
Assert.True(hashed.Matches(new FramePhoto("", 100, time.AddTicks(9_999))));
|
||
Assert.False(hashed.Matches(new FramePhoto(null, 100, time.AddMilliseconds(1))));
|
||
Assert.False(new FramePhoto(null, 100, null).Matches(new FramePhoto(null, 100, null)));
|
||
var frames = Frames([new FramePhoto(null, 100, time), hashed]);
|
||
Assert.Equal(1, frames.IndexOf(new FramePhoto(new string('a', 64), 100, null)));
|
||
Assert.Equal(0, frames.IndexOf(new FramePhoto(null, 100, time)));
|
||
Assert.Null(frames.IndexOf(new FramePhoto(null, 101, time)));
|
||
}
|
||
|
||
private static FramePhoto Photo(int seed) =>
|
||
new(Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData([(byte)seed])), 1000 + seed, new DateTime(2026, 9, 28, 18, 0, 0).AddSeconds(10 * seed));
|
||
|
||
private static FramePhotos Frames(IEnumerable<FramePhoto> photos) => new(photos.Select((p, i) => (i, p)));
|
||
|
||
[Test]
|
||
public void BuiltInPresetIdsAreUnique() =>
|
||
Assert.Equal(ExportPresets.BuiltIn.Count, ExportPresets.BuiltIn.Select(p => p.Id).Distinct().Count());
|
||
|
||
[Test]
|
||
public void BitRateFollowsBitsPerPixel()
|
||
{
|
||
var p = Assert.NotNull(ExportPresets.Find("4k-hevc10-archive"));
|
||
// 0.30 × 3840 × 2160 × 24 ≈ 59.7 Mb/s (archive budget); share presets use the codec default.
|
||
Assert.Equal(59_719_680L, p.EffectiveBitRate(3840, 2160, new Rational(24, 1)));
|
||
Assert.Equal(19_906_560L, ExportPresets.Find("4k-h264")!.EffectiveBitRate(3840, 2160, new Rational(24, 1)));
|
||
var s = p.ToEncodeSettings("out.mp4", 3840, 2160);
|
||
Assert.Equal(24, s.GopLength);
|
||
Assert.Equal(10, s.BitDepth);
|
||
}
|
||
|
||
/// <summary>
|
||
/// SHA-256 of the default project's canonical text, as format-1 builds write it: none of phase 4's members, version 1.
|
||
/// The text is indented with the OS's line ends (CRLF on Windows, LF on Linux), so the pin is of the text with CRLF.
|
||
/// </summary>
|
||
private const string DefaultProjectHash = "df0b0f50b113475ece3753a80df0be05cc281cdaa1fa08a73a438ea9161358e0";
|
||
|
||
[Test]
|
||
public void ProjectsWithoutTheNewMembersKeepTheirTextAndHash()
|
||
{
|
||
var defaults = new ProjectSettings().ToJson();
|
||
Assert.Equal(DefaultProjectHash, ProjectContent.Hash(ProjectContent.Canonical(defaults).ReplaceLineEndings("\r\n")));
|
||
Assert.Equal(OldBuild.Write(new OldProject()), defaults);
|
||
Assert.Contains("\"version\": 1", defaults);
|
||
foreach (var member in new[] { "rotate", "mirror", "flip", "straighten", "\"look\"" })
|
||
Assert.False(defaults.Contains(member, StringComparison.Ordinal), member);
|
||
|
||
// A project written by a format-1 build reads and writes back the same, byte for byte.
|
||
var older = new ProjectSettings
|
||
{
|
||
Camera = new CameraSettings { Crop = [0.1, 0.2, 0.5, 0.6], Aspect = new Auto<string>().WithManual("9:16"), Keyframes = [new CameraKey(3, 0.4, 0.5, 1.5, 2)] },
|
||
Exposure = new ExposureSettings { ExposureOffsetEv = 0.3 },
|
||
}.ToJson();
|
||
Assert.Equal(older, ProjectContent.Canonical(older));
|
||
Assert.Equal(older, OldBuild.Write(OldBuild.Read(older)));
|
||
Assert.Equal(ProjectSettings.BaseVersion, ProjectContent.FormatOf(older));
|
||
}
|
||
|
||
[Test]
|
||
public void TheNewMembersRoundTripAndRaiseTheFormatOnlyWhileUsed()
|
||
{
|
||
var plain = new ProjectSettings { Exposure = new ExposureSettings { ExposureOffsetEv = 0.5 } };
|
||
Assert.Equal(ProjectSettings.BaseVersion, plain.RequiredFormat);
|
||
var turned = plain with { Camera = plain.Camera with { Rotate = 90, Mirror = true, Flip = true, Straighten = -3.5 } };
|
||
var looked = plain with { Look = new LookSettings { Filter = "sepia", Strength = 0.6 } };
|
||
foreach (var settings in new[] { turned, looked, plain with { Camera = new CameraSettings { Straighten = 1 } }, plain with { Camera = new CameraSettings { Flip = true } } })
|
||
{
|
||
Assert.Equal(2, settings.RequiredFormat);
|
||
var json = settings.ToJson();
|
||
Assert.Contains("\"version\": 2", json);
|
||
var back = ProjectSettings.FromJson(json);
|
||
Assert.Equal(2, back.Version);
|
||
Assert.False(back.IsNewerFormat);
|
||
Assert.Equal(json, back.ToJson());
|
||
}
|
||
var read = ProjectSettings.FromJson(turned.ToJson());
|
||
Assert.Equal((90, true, true, -3.5), (read.Camera.Rotate, read.Camera.Mirror, read.Camera.Flip, read.Camera.Straighten));
|
||
Assert.Equal((1, true), (read.Camera.Turns, read.Camera.IsOriented));
|
||
var look = Assert.NotNull(ProjectSettings.FromJson(looked.ToJson()).Look);
|
||
Assert.Equal(("sepia", (double?)0.6), (look.Filter, look.Strength));
|
||
|
||
// Taken away again, the project is format 1 with the text it had before.
|
||
var undone = ProjectSettings.FromJson(turned.ToJson()) with { Camera = plain.Camera };
|
||
Assert.Equal(ProjectSettings.BaseVersion, undone.RequiredFormat);
|
||
Assert.Equal(plain.ToJson(), undone.ToJson());
|
||
Assert.Equal(plain.ToJson(), (ProjectSettings.FromJson(looked.ToJson()) with { Look = null }).ToJson());
|
||
|
||
// An identity look and a whole turn need nothing newer; an unknown look does (it is a newer Chronos's).
|
||
Assert.Equal(1, (plain with { Look = new LookSettings { Filter = "none" } }).RequiredFormat);
|
||
Assert.Equal(1, (plain with { Look = new LookSettings { Filter = "mono", Strength = 0 } }).RequiredFormat);
|
||
Assert.Equal(1, (plain with { Camera = new CameraSettings { Rotate = 360 } }).RequiredFormat);
|
||
Assert.Equal(2, (plain with { Look = new LookSettings { Filter = "film" } }).RequiredFormat);
|
||
Assert.Equal(3, new CameraSettings { Rotate = -90 }.Turns);
|
||
}
|
||
|
||
[Test]
|
||
public void TheNewMembersComeAfterTheOldOnesSoAnOlderBuildWritesThemBackInPlace()
|
||
{
|
||
var settings = new ProjectSettings
|
||
{
|
||
Camera = new CameraSettings { Crop = [0.2, 0, 0.6, 1], Keyframes = [new CameraKey(1, 0.5, 0.5, 2, 0)], Rotate = 270, Straighten = 2.25 },
|
||
HeroFrame = 4,
|
||
Look = new LookSettings { Filter = "warm" },
|
||
};
|
||
var canonical = ProjectContent.Canonical(settings.ToJson());
|
||
|
||
// A format-1 build keeps them as members it does not know, and writes them where they were.
|
||
var old = OldBuild.Read(canonical);
|
||
Assert.True(old.Camera.Extra!.ContainsKey("rotate") && old.Extra!.ContainsKey("look"));
|
||
Assert.Equal(canonical, OldBuild.Write(old));
|
||
Assert.Equal(ProjectContent.Hash(canonical), ProjectContent.Hash(OldBuild.Write(old)));
|
||
var camera = System.Text.Json.Nodes.JsonNode.Parse(canonical)!["camera"]!.AsObject().Select(p => p.Key).ToList();
|
||
Assert.SequenceEqual(["crop", "aspect", "offsetX", "offsetY", "keyframes", "rotate", "straighten"], camera);
|
||
}
|
||
|
||
[Test]
|
||
public void ANewerFormatStaysReadOnlyAndKeepsItsNumber()
|
||
{
|
||
const string newer = """{ "version": 3, "camera": { "rotate": 90 }, "look": { "filter": "film", "grain": 0.2 } }""";
|
||
Assert.Throws<InvalidDataException>(() => ProjectSettings.FromJson(newer));
|
||
var read = ProjectSettings.FromJson(newer, allowNewer: true);
|
||
Assert.True(read.IsNewerFormat);
|
||
Assert.Equal(3, read.WrittenFormat);
|
||
var canonical = ProjectContent.Canonical(newer);
|
||
Assert.Equal(3, ProjectContent.FormatOf(canonical));
|
||
Assert.Contains("\"grain\": 0.2", canonical);
|
||
Assert.Equal(canonical, ProjectContent.Canonical(canonical));
|
||
|
||
// A format-2 project is this Chronos's own (written as the format it needs); text without a version is format 1.
|
||
Assert.False(ProjectSettings.FromJson("""{ "version": 2 }""").IsNewerFormat);
|
||
Assert.Equal(1, ProjectSettings.FromJson("""{ "version": 2 }""").WrittenFormat);
|
||
Assert.Equal(1, ProjectContent.FormatOf("""{ "camera": {} }"""));
|
||
Assert.Equal(1, ProjectContent.FormatOf(null));
|
||
}
|
||
|
||
/// <summary>
|
||
/// The settings as a format-1 build reads and writes them: its camera without phase 4's members and its project without
|
||
/// the look, with the same serialiser options. What it does not know lands in <c>Extra</c>.
|
||
/// </summary>
|
||
private static class OldBuild
|
||
{
|
||
public static OldProject Read(string json) => System.Text.Json.JsonSerializer.Deserialize(json, OldJson.Default.OldProject)!;
|
||
|
||
public static string Write(OldProject project) => System.Text.Json.JsonSerializer.Serialize(project, OldJson.Default.OldProject);
|
||
}
|
||
}
|
||
|
||
/// <summary>A format-1 build's camera (before owner review 1, phase 4).</summary>
|
||
internal sealed record OldCamera
|
||
{
|
||
public double[]? Crop { get; init; }
|
||
public Auto<string> Aspect { get; init; } = new();
|
||
public double OffsetX { get; init; }
|
||
public double OffsetY { get; init; }
|
||
public IReadOnlyList<CameraKey> Keyframes { get; init; } = [];
|
||
|
||
[System.Text.Json.Serialization.JsonExtensionData]
|
||
public Dictionary<string, System.Text.Json.JsonElement>? Extra { get; set; }
|
||
}
|
||
|
||
/// <summary>A format-1 build's project (before owner review 1, phase 4).</summary>
|
||
internal sealed record OldProject
|
||
{
|
||
public int Version { get; init; } = 1;
|
||
public TimingSettings Timing { get; init; } = new();
|
||
public ExposureSettings Exposure { get; init; } = new();
|
||
public MotionSettings Motion { get; init; } = new();
|
||
public OldCamera Camera { get; init; } = new();
|
||
public StackSettings Stack { get; init; } = new();
|
||
public ExportSettings Export { get; init; } = new();
|
||
public IReadOnlyList<int> ExcludedFrames { get; init; } = [];
|
||
public int? HeroFrame { get; init; }
|
||
public IReadOnlyList<PendingFrame>? PendingFrames { get; init; }
|
||
|
||
[System.Text.Json.Serialization.JsonExtensionData]
|
||
public Dictionary<string, System.Text.Json.JsonElement>? Extra { get; set; }
|
||
}
|
||
|
||
[System.Text.Json.Serialization.JsonSourceGenerationOptions(
|
||
WriteIndented = true,
|
||
PropertyNamingPolicy = System.Text.Json.Serialization.JsonKnownNamingPolicy.CamelCase,
|
||
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull,
|
||
UseStringEnumConverter = true,
|
||
PropertyNameCaseInsensitive = true)]
|
||
[System.Text.Json.Serialization.JsonSerializable(typeof(OldProject))]
|
||
internal sealed partial class OldJson : System.Text.Json.Serialization.JsonSerializerContext;
|