Files
Chronos/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs
T
Claude d729d2d09c test(core): project settings keep defaults for members missing from partial JSON
The source-generated deserialiser sets every init-only member it does not
find to null/false/0, so a partial project (API edit, older file) lost its
sections and defaults. FromJson lays the JSON over the default project.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 16:45:37 +00:00

108 lines
4.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Titano.Core.Export;
using Titano.Core.Project;
using Titano.Core.Video;
using Titano.Testing;
namespace Titano.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.CurrentVersion, 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 ProjectMustBeAnObject() =>
Assert.Throws<System.Text.Json.JsonException>(() => ProjectSettings.FromJson("[1, 2]"));
[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);
}
}