using Titano.Core.Analysis.Deep; using Titano.Core.Archiving; using Titano.Core.Diagnostics.Synthetic; using Titano.Core.Localization; using Titano.Core.Model; using Titano.Core.Project; using Titano.Data.Processing; using Titano.Data.Repositories; using Titano.Testing; namespace Titano.Data.Tests.Processing; [Category("processing")] public sealed class AnalysisJobTests(TestContext context) { private static SyntheticSequence Flickering(int frames = 40) => SyntheticSequence.Create(new SyntheticSequenceOptions { FrameCount = frames, FlickerEv = 0.1, StartEv = 0.5, EndEv = -1.5, Latitude = 45.4, Longitude = 12.3, }); [Test] public async Task LightAnalysisFillsAutoValuesAndKeepsManualOnes() { await using var fx = await ProcessingFixture.CreateAsync(context); var sequence = await fx.AddAsync(Flickering()); var manual = new ProjectSettings(); manual = manual with { Timing = manual.Timing with { FrameRate = manual.Timing.FrameRate.WithManual("25") } }; await fx.Db.Sequences.EditAsync(sequence.Id, new SequenceEdit { SettingsJson = manual.ToJson() }, null, fx.Ct); await fx.StartEngineAsync(); var job = await fx.EnqueueAsync(ProcessingJobs.LightAnalysis([sequence.Id], sequence.DisplayTitle)); var done = await fx.WaitForAsync(job.Id, JobState.Done); var folder = fx.Folder(sequence); var project = ProjectSettings.FromJson(await File.ReadAllTextAsync(folder.ProjectPath, fx.Ct)); Assert.Equal((false, "25"), (project.Timing.FrameRate.IsAuto, project.Timing.FrameRate.Resolve("30"))); Assert.True(project.Timing.Mode.HasAdvice, "timing mode advised"); Assert.True(project.Timing.FrameRate.HasAdvice, "the advice is kept next to the manual frame rate"); var row = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!; Assert.Equal(project.ToJson(), ProjectSettings.FromJson(row.SettingsJson!).ToJson()); Assert.True(row.Status.HasFlag(SequenceStatus.AnalyzedLight), "index status"); Assert.False(row.Status.HasFlag(SequenceStatus.AnalyzedDeep), "no deep status without a report"); Assert.True(ManifestStore.ReadSequence(folder.ManifestPath).Status.HasFlag(SequenceStatus.AnalyzedLight), "sidecar status"); Assert.Contains(Loc.L("Advisor.Log.AutoUpdated", sequence.DisplayTitle), done.LogTail); // Idempotent: a second pass changes nothing. var version = row.SettingsVersion; var again = await fx.EnqueueAsync(ProcessingJobs.LightAnalysis([sequence.Id])); await fx.WaitForAsync(again.Id, JobState.Done); Assert.Equal(version, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.SettingsVersion); } [Test] public async Task DeepAnalysisResumesAfterCancellationWithTheSameReport() { await using var reference = await ProcessingFixture.CreateAsync(context); var expectedSequence = await reference.AddAsync(Flickering()); await reference.StartEngineAsync(); var full = await reference.EnqueueAsync(ProcessingJobs.DeepAnalysis([expectedSequence.Id])); await reference.WaitForAsync(full.Id, JobState.Done); var expected = await File.ReadAllTextAsync(reference.Folder(expectedSequence).AnalysisPath, reference.Ct); await using var fx = await ProcessingFixture.CreateAsync(context); var sequence = await fx.AddAsync(Flickering()); var folder = fx.Folder(sequence); var partial = Path.Combine(folder.MetaDirectory, DeepAnalysisCheckpointFile.FileName); // Frame 25 is not in the region sample, so the hold stops the pass inside the batch of frames 24–25. fx.Decoder.HoldFrame(25); await fx.StartEngineAsync(); var job = await fx.EnqueueAsync(ProcessingJobs.DeepAnalysis([sequence.Id])); await fx.Decoder.Holding.Task.WaitAsync(TimeSpan.FromSeconds(60), fx.Ct); await fx.Engine.CancelAsync(job.Id, fx.Ct); await fx.WaitForAsync(job.Id, JobState.Cancelled); var saved = File.ReadAllLines(partial).Length - 1; Assert.Equal(24, saved); Assert.FileMissing(folder.AnalysisPath); fx.Decoder.Hold = null; while (fx.Decoder.Requests.TryDequeue(out _)) { } await fx.Engine.ResumeAsync(job.Id, fx.Ct); var done = await fx.WaitForAsync(job.Id, JobState.Done); Assert.Equal(expected, await File.ReadAllTextAsync(folder.AnalysisPath, fx.Ct)); Assert.FileMissing(partial); // After the region sample, only the last checkpointed frame is decoded again (to restore the comparison state). var analysed = fx.Decoder.Requests.Skip(ProcessingFixture.DeepOptions.RegionSampleFrames) .Select(r => int.Parse(Path.GetFileNameWithoutExtension(r.Path)[4..]) - 1).ToList(); Assert.Equal(saved - 1, analysed.Min()); Assert.Equal(sequence.FrameCount, (int)done.ProgressDone); var row = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!; Assert.True(row.Status.HasFlag(SequenceStatus.AnalyzedDeep | SequenceStatus.AnalyzedLight), "index status"); var summary = AnalysisSummary.FromJson(row.AnalysisJson!); Assert.Equal(sequence.FrameCount, summary.FrameCount); Assert.NotNull((await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct))[5].MeanLogLuminance); var project = ProjectSettings.FromJson(await File.ReadAllTextAsync(folder.ProjectPath, fx.Ct)); Assert.True(project.Exposure.Deflicker.HasAdvice, "deflicker advised from the pixels"); Assert.Contains(Loc.L("Advisor.Log.Presets", string.Empty), done.LogTail); } [Test] public async Task LightAnalysisAfterNewFramesDropsTheStaleDeepStatus() { await using var fx = await ProcessingFixture.CreateAsync(context); var sequence = await fx.AddAsync(Flickering(12)); await fx.StartEngineAsync(); var deep = await fx.EnqueueAsync(ProcessingJobs.DeepAnalysis([sequence.Id])); await fx.WaitForAsync(deep.Id, JobState.Done); Assert.True((await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.Status.HasFlag(SequenceStatus.AnalyzedDeep), "deep status"); // A thirteenth frame arrives: the report describes 12 frames, so it no longer counts. await fx.AddAsync(Flickering(13), first: 12); var light = await fx.EnqueueAsync(ProcessingJobs.LightAnalysis([sequence.Id])); await fx.WaitForAsync(light.Id, JobState.Done); var row = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!; Assert.Equal(13, row.FrameCount); Assert.Equal((true, false), (row.Status.HasFlag(SequenceStatus.AnalyzedLight), row.Status.HasFlag(SequenceStatus.AnalyzedDeep))); } }