using System.Globalization;
using Titano.App.Editor;
using Titano.Core.Analysis;
using Titano.Core.Analysis.Deep;
using Titano.Core.Diagnostics.Synthetic;
using Titano.Core.Imaging;
using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Core.Project;
using Titano.Core.Render.Diagnostics;
using Titano.Data.Archiving.Jobs;
namespace Titano.App.Tests.Editor;
/// A synthetic sunset with flicker and two exposure steps, as the editor tests open it.
internal static class EditorFixture
{
private static readonly Lazy Shared = new(() => Sequence());
static EditorFixture() => Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
public static SyntheticSequence Sunset => Shared.Value;
public static SyntheticSequence Sequence(int frames = 72) => SyntheticSequence.Create(new SyntheticSequenceOptions
{
Width = 96,
Height = 64,
FrameCount = frames,
StartEv = 0,
EndEv = -2,
FlickerEv = 0.25,
ExposureSteps = [new SyntheticExposureStep(24, -1), new SyntheticExposureStep(48, -1)],
StartKelvin = 5200,
EndKelvin = 3400,
});
/// The sequence as returns it, with a deep analysis when asked.
public static SequenceContent Content(SyntheticSequence sequence, ProjectSettings? project = null, bool deep = true)
{
var id = Guid.CreateVersion7();
var frames = sequence.Frames.Select(f => f with { SequenceId = id }).ToList();
var row = new Sequence
{
Id = id,
ArchiveId = Guid.CreateVersion7(),
Title = "Harbour sunset",
Folder = "2026/2026-09-28 Synthetic",
CameraMake = sequence.Options.CameraMake,
CameraModel = sequence.Options.CameraModel,
FirstCapture = frames[0].CapturedAt,
LastCapture = frames[^1].CapturedAt,
FrameCount = frames.Count,
MedianInterval = sequence.Options.IntervalSeconds,
};
var report = deep ? DeepAnalyzer.AnalyzeAsync(new Frames(sequence)).GetAwaiter().GetResult() : null;
return new SequenceContent(row, frames, project ?? new ProjectSettings(), LightAnalyzer.Analyze(frames), report, sequence.Options.Width,
sequence.Options.Height, null, []);
}
/// Synthetic frames as the deep analysis reads them.
private sealed class Frames(SyntheticSequence sequence) : IFrameSource
{
public int Count => sequence.Count;
public LightFrame Metadata(int index) => LightFrame.From(sequence.Metadata[index], index);
public ValueTask LoadAsync(int index, int maxLongEdge, CancellationToken cancellationToken) =>
ValueTask.FromResult(sequence.Render(index));
}
}
///
/// An in-memory : synthetic pixels, a stored project with the same merge rule as the
/// archive, recorded jobs. simulates someone else changing the stored project before a save.
///
internal sealed class FakeSequenceSource(SyntheticSequence sequence, SequenceContent content) : ISequenceSource
{
private readonly SyntheticFrameSource _pixels = new(sequence);
public ProjectSettings Stored { get; set; } = content.Project;
public SequenceContent Content { get; set; } = content;
public List Submitted { get; } = [];
public int Saves { get; private set; }
public int Decodes { get; private set; }
/// Applied to the stored project before the next save reads it.
public Func? ExternalEdit { get; set; }
/// Thrown by the next save.
public Exception? FailNextSave { get; set; }
/// Awaited by every decode (tests of cancellation).
public Task DecodeGate { get; set; } = Task.CompletedTask;
public Task LoadAsync(CancellationToken cancellationToken) => Task.FromResult(Content with { Project = Stored });
public async ValueTask DecodeAsync(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken)
{
await DecodeGate.WaitAsync(cancellationToken);
Decodes++;
return await _pixels.GetFrameAsync(index, maxLongEdge, purpose, cancellationToken);
}
public Task SaveProjectAsync(ProjectSettings project, ProjectSettings basis, CancellationToken cancellationToken)
{
if (FailNextSave is { } failure)
{
FailNextSave = null;
return Task.FromException(failure);
}
if (ExternalEdit is { } edit)
{
Stored = edit(Stored);
ExternalEdit = null;
}
Stored = ProjectMerge.SameValues(Stored, basis) ? project : ProjectMerge.Merge(basis, project, Stored);
Saves++;
return Task.FromResult(Stored);
}
public Task SubmitAsync(ArchiveJobRequest request, CancellationToken cancellationToken)
{
Submitted.Add(request);
return Task.FromResult(Guid.CreateVersion7());
}
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
}