Files
Chronos/tests/Titano.Core.Tests/Export/ExportResolverTests.cs
T
Claude 262a0455db feat(export): preset resolution with the Custom rule, output naming and estimates
ExportResolver applies overrides and names the result after the preset
it equals (or custom); ExportNaming expands the name template with
collision suffixes; ExportEstimator predicts size and time from a
calibratable throughput model.

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

132 lines
5.6 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.Export;
public sealed class ExportResolverTests
{
private static ExportSettings Export(string preset, ExportOverrides? overrides = null) =>
new() { Preset = new Auto<string>().WithManual(preset), Overrides = overrides ?? new ExportOverrides() };
[Test]
public void EveryBuiltInPresetResolvesToItself()
{
foreach (var preset in ExportPresets.BuiltIn)
{
var resolved = ExportResolver.Resolve(Export(preset.Id));
Assert.Equal(preset.Id, resolved.PresetId);
Assert.False(resolved.IsCustom);
Assert.Equal(preset.NameKey, resolved.NameKey);
Assert.Equal(preset.Codec, resolved.Codec);
Assert.Empty(resolved.Warnings);
}
}
[Test]
public void EditingAValueMakesTheExportCustom()
{
var resolved = ExportResolver.Resolve(Export("4k-hevc", new ExportOverrides { BitRate = 50_000_000 }));
Assert.True(resolved.IsCustom);
Assert.Equal(ExportResolver.CustomId, resolved.PresetId);
Assert.Equal("4k-hevc", resolved.BasePresetId);
Assert.Null(resolved.NameKey);
Assert.Equal(50_000_000L, resolved.EffectiveBitRate(3840, 2160));
Assert.Equal(3840, resolved.Width);
}
[Test]
public void ValuesEqualToThePresetRestoreItsName()
{
var resolved = ExportResolver.Resolve(Export("1080p-h264", new ExportOverrides
{
Codec = "h.264",
Container = "mp4",
Width = 1920,
Height = 1080,
FrameRate = "30",
BitRate = 6_220_800, // the preset's 0.10 bpp at 1920×1080×30
GopFrames = 30,
Profile = "H264High",
RateControl = "vbr",
}));
Assert.Equal("1080p-h264", resolved.PresetId);
Assert.Empty(resolved.Warnings);
}
[Test]
public void ValuesEqualToAnotherPresetTakeItsName()
{
var resolved = ExportResolver.Resolve(Export("1080p-h264", new ExportOverrides { FrameRate = "60" }));
Assert.Equal("1080p60", resolved.PresetId);
Assert.Equal(60, resolved.GopFrames);
Assert.Equal("1080p-h264", resolved.BasePresetId);
}
[Test]
public void FractionalRatesAndGopFollowTheOverride()
{
var resolved = ExportResolver.Resolve(Export("4k-h264", new ExportOverrides { FrameRate = "24000/1001" }));
Assert.True(resolved.IsCustom);
Assert.Equal(Rational.Fps23976, resolved.FrameRate);
Assert.Equal(24, resolved.GopFrames);
var settings = resolved.ToEncodeSettings("x.mp4", 3840, 2160);
Assert.Equal(24, settings.GopLength);
Assert.Equal((long)Math.Round(0.10 * 3840 * 2160 * 24000 / 1001.0), settings.BitRate);
Assert.Equal(CodecProfile.H264High, settings.Profile);
}
[Test]
public void BadOverridesAreIgnoredWithWarnings()
{
var resolved = ExportResolver.Resolve(Export("1080p-h264", new ExportOverrides { Codec = "divx", FrameRate = "fast", Container = "avi", Profile = "ultra", RateControl = "magic" }));
Assert.Equal("1080p-h264", resolved.PresetId);
Assert.SequenceEqual(new[] { "Export.Warning.UnknownCodec", "Export.Warning.ContainerIgnored", "Export.Warning.UnknownProfile", "Export.Warning.UnknownFrameRate", "Export.Warning.UnknownRateControl" }, resolved.Warnings);
}
[Test]
public void UnknownOrCustomSelectionsStartFromTheDefaultPreset()
{
var unknown = ExportResolver.Resolve(Export("deleted-user-preset"));
Assert.Equal(ExportResolver.DefaultPresetId, unknown.PresetId);
Assert.Contains("Export.Warning.UnknownPreset", unknown.Warnings);
var custom = ExportResolver.Resolve(Export("custom", new ExportOverrides { Codec = "png", Width = 0, Height = 0, BitDepth = 16, FrameRate = "30", GopFrames = 30, BFrames = 0, Quality = 92 }));
Assert.Empty(custom.Warnings);
Assert.Equal(VideoCodec.PngSequence, custom.Codec);
Assert.True(custom.IsCustom);
var auto = ExportResolver.Resolve(new ExportSettings());
Assert.Equal(ExportResolver.DefaultPresetId, auto.PresetId);
}
[Test]
public void ChangingTheCodecResetsCodecSpecificValues()
{
var resolved = ExportResolver.Resolve(Export("1080p-h264", new ExportOverrides { Codec = "HEVC" }));
Assert.Equal(VideoCodec.Hevc, resolved.Codec);
Assert.Equal(CodecProfile.Auto, resolved.Profile);
Assert.Equal((long)Math.Round(0.07 * 1920 * 1080 * 30), resolved.EffectiveBitRate(1920, 1080));
Assert.True(resolved.IsCustom);
Assert.True(ExportResolver.TryParseCodec("TIFF-16", out var tiff) && tiff == VideoCodec.Tiff16Sequence);
Assert.False(ExportResolver.TryParseCodec("", out _));
}
[Test]
public void FrameRateTextRoundTrips()
{
Assert.True(FrameRateText.TryParse("30000/1001", out var ntsc));
Assert.Equal(Rational.Fps2997, ntsc);
Assert.True(FrameRateText.TryParse("29.97", out var decimalNtsc));
Assert.Equal(Rational.Fps2997, decimalNtsc);
Assert.True(FrameRateText.TryParse(" 25 ", out var pal));
Assert.Equal("25", FrameRateText.Format(pal));
Assert.Equal("24000/1001", FrameRateText.Format(Rational.Fps23976));
Assert.True(FrameRateText.AreEqual(new Rational(48000, 2002), Rational.Fps23976));
Assert.False(FrameRateText.TryParse("0", out _));
Assert.False(FrameRateText.TryParse("1/0", out _));
Assert.False(FrameRateText.TryParse(null, out _));
}
}