using Titano.Core.Project;
using Titano.Testing;
namespace Titano.Core.Tests.Project;
/// The .titano project file (BRIEF ยง18): a sequence reference plus the settings to open it with.
public sealed class ProjectBundleTests(TestContext context)
{
private static readonly Guid ArchiveId = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01");
private static readonly Guid SequenceId = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a02");
[Test]
public void ARoundTripKeepsTheReferenceAndTheSettings()
{
var bundle = Sample();
var path = Path.Combine(context.CreateTempDirectory("bundle"), ProjectBundle.FileNameFor("Harbour sunset"));
bundle.Save(path);
var back = ProjectBundle.Load(path);
Assert.Equal(bundle.Archive, back.Archive);
Assert.Equal(bundle.Sequence, back.Sequence);
Assert.Equal(bundle.Saved, back.Saved);
Assert.Equal(bundle.Project.ToJson(), back.Project.ToJson());
Assert.Equal(0.8, back.Project.Exposure.DeflickerStrength.Resolve(1.0));
Assert.SequenceEqual([3, 7], back.Project.ExcludedFrames);
var json = File.ReadAllText(path);
Assert.Contains("\"format\": \"titano-project\"", json);
Assert.Contains("\"formatVersion\": 1", json);
}
[Test]
public void OtherFilesAreRefusedAsNotProjectFiles()
{
Assert.Throws(() => ProjectBundle.Parse("not json"));
Assert.Throws(() => ProjectBundle.Parse("[1, 2]"));
// The archive's own meta files are JSON too, but not project files.
Assert.Throws(() => ProjectBundle.Parse("""{ "schemaVersion": 1, "id": "0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01" }"""));
var json = Sample().ToJson();
Assert.Throws(() => ProjectBundle.Parse(json.Replace("\"formatVersion\": 1", "\"formatVersion\": 2", StringComparison.Ordinal)));
Assert.Throws(() => ProjectBundle.Parse(json.Replace(SequenceId.ToString(), "not-a-guid", StringComparison.Ordinal)));
Assert.Throws(() => ProjectBundle.Parse(json.Replace("\"version\": 1", "\"version\": 99", StringComparison.Ordinal)));
Assert.Throws(() => ProjectBundle.Parse(json.Replace("\"folder\"", "\"place\"", StringComparison.Ordinal)));
}
[Test]
public void AFileTooLargeToBeAProjectIsNotRead()
{
var path = Path.Combine(context.CreateTempDirectory("bundle"), "huge.titano");
using (var stream = File.Create(path)) stream.SetLength(ProjectBundle.MaxBytes + 1L);
Assert.Throws(() => ProjectBundle.Load(path));
}
[Test]
public void FramePositionsApplyOnlyToTheFramesTheyWereSavedFor()
{
var bundle = Sample();
var current = new ProjectSettings
{
ExcludedFrames = [1],
HeroFrame = 2,
Camera = new CameraSettings { Keyframes = [new CameraKey(0, 0.5, 0.5, 1, 0)] },
};
Assert.Same(bundle.Project, bundle.ProjectFor(current, frameCount: 3600));
var changed = bundle.ProjectFor(current, frameCount: 3610);
Assert.SequenceEqual([1], changed.ExcludedFrames);
Assert.Equal(2, changed.HeroFrame);
Assert.Same(current.Camera.Keyframes, changed.Camera.Keyframes);
// Everything else comes from the file.
Assert.Equal(0.8, changed.Exposure.DeflickerStrength.Resolve(1.0));
Assert.Equal(bundle.Project.Camera.Crop, changed.Camera.Crop);
}
[Theory]
[Case("Harbour sunset", "Harbour sunset.titano")]
[Case("Roof: 12/03 \"storm\"?", "Roof_ 12_03 _storm__.titano")]
[Case("CON", "_CON.titano")]
[Case(" ", "Titano.titano")]
public void TheFileNameComesFromTheTitle(string title, string expected) => Assert.Equal(expected, ProjectBundle.FileNameFor(title));
[Test]
public void SavingNeverReplacesAnEarlierProjectFile()
{
var folder = context.CreateTempDirectory("bundle");
Assert.Equal(Path.Combine(folder, "Harbour sunset.titano"), ProjectBundle.FreePathIn(folder, "Harbour sunset"));
File.WriteAllText(Path.Combine(folder, "Harbour sunset.titano"), "{}");
Directory.CreateDirectory(Path.Combine(folder, "Harbour sunset (2).titano"));
Assert.Equal(Path.Combine(folder, "Harbour sunset (3).titano"), ProjectBundle.FreePathIn(folder, "Harbour sunset"));
}
private static ProjectBundle Sample() => new(
new BundleArchive(ArchiveId, "Main archive", @"D:\Timelapse"),
new BundleSequence(SequenceId, "2026/2026-09-28 Harbour sunset", "Harbour sunset", 3600),
new ProjectSettings
{
Exposure = new ExposureSettings { DeflickerStrength = new Auto().WithManual(0.8) },
Camera = new CameraSettings { Crop = [0.1, 0.1, 0.9, 0.9], Keyframes = [new CameraKey(120, 0.4, 0.5, 1.2, 0)] },
ExcludedFrames = [3, 7],
HeroFrame = 1800,
},
new DateTimeOffset(2026, 10, 12, 9, 30, 0, TimeSpan.Zero));
}