using System.Reflection; using Chronos.App.Editor; using Chronos.Core.Advice; using Chronos.Core.Analysis; using Chronos.Core.Analysis.Deep; using Chronos.Core.Localization; using Chronos.Core.Model; using Chronos.Core.Project; using Chronos.Testing; namespace Chronos.App.Tests.Editor; /// The Edit path (owner review 1, R7): where each step stands, the step of each finding, the opening step and the render comparison. public sealed class EditPathTests { private static readonly string[] Steps = [EditPath.Analysis, EditPath.Frames, EditPath.Exposure, EditPath.Motion, EditPath.Camera, EditPath.Time, EditPath.Stack, EditPath.Export]; [Test] public void TheAnalysisStepIsToDoRunningDoneOrStale() { var bare = EditorFixture.Content(EditorFixture.Sunset, deep: false); Assert.Equal(StepStatus.Todo, State(EditPath.Analysis, bare).Status); Assert.Equal(Loc.L("Editor.Chart.NotAnalysed"), State(EditPath.Analysis, bare).Text); var running = State(EditPath.Analysis, bare, analysis: Job(JobKind.DeepAnalyse, JobState.Running)); Assert.Equal(StepStatus.Running, running.Status); Assert.Equal("Deep analysis · running", running.Text); var stale = bare with { Sequence = bare.Sequence with { Status = SequenceStatus.AnalyzedDeep }, DeepOfOtherFrames = true }; Assert.Equal(StepStatus.Attention, State(EditPath.Analysis, stale).Status, "the saved analysis is of other frames"); Assert.Equal(Loc.L("Editor.Step.FramesChanged"), State(EditPath.Analysis, stale).Text); // Analysed, but the saved analysis cannot be read now (archive offline, file moved): not "frames changed". var unreadable = stale with { DeepOfOtherFrames = false }; Assert.Equal(StepStatus.Attention, State(EditPath.Analysis, unreadable).Status); Assert.Equal(Loc.L("Editor.Step.AnalysisUnavailable"), State(EditPath.Analysis, unreadable).Text); Assert.Equal(Loc.L("Tooltips.Editor.Step.AnalysisUnavailable"), State(EditPath.Analysis, unreadable).Detail); var analysed = EditorFixture.Content(EditorFixture.Sunset); var done = State(EditPath.Analysis, analysed); Assert.Equal(StepStatus.Done, done.Status); var warnings = Advice(analysed).Findings.Count(f => f.Severity >= FindingSeverity.Warning); Assert.Equal(warnings == 0 ? "Analysed" : "Analysed · " + Loc.Plural("Editor.Summary.Warnings", warnings), done.Text); } [Test] public void FramesWaitForTheAnalysisAndAskUntilTheFlaggedFramesAreExcluded() { Assert.Equal(StepStatus.Waiting, State(EditPath.Frames, EditorFixture.Content(EditorFixture.Sunset, deep: false)).Status); var analysed = EditorFixture.Content(EditorFixture.Sunset); var flagged = analysed with { Deep = analysed.Deep! with { Issues = new FrameIssues([], [5], [9], [], [3]) } }; var attention = State(EditPath.Frames, flagged); Assert.Equal(StepStatus.Attention, attention.Status); Assert.Equal("2 flagged frames", attention.Text, "a scene change is not a frame to exclude"); var one = State(EditPath.Frames, flagged, project: new ProjectSettings { ExcludedFrames = [5] }); Assert.Equal("1 flagged frame", one.Text); var done = State(EditPath.Frames, flagged, project: new ProjectSettings { ExcludedFrames = [5, 9, 20] }); Assert.Equal(StepStatus.Done, done.Status); Assert.Equal("Excluded: 3", done.Text); // A frame the render skips anyway (unreadable in the archive) needs nothing from the user. var frames = flagged.Frames.ToList(); frames[9] = frames[9] with { Flags = FrameFlags.Undecodable }; var skipped = flagged with { Frames = frames }; Assert.Equal("1 flagged frame", State(EditPath.Frames, skipped).Text); } [Test] public void SettingsStepsWaitForTheDeepAnalysisOnlyWhileAnAutoSettingNeedsIt() { var bare = EditorFixture.Content(EditorFixture.Sunset, deep: false); Assert.Equal(StepStatus.Waiting, State(EditPath.Exposure, bare).Status); Assert.Equal(Loc.L("Editor.Step.Waiting"), State(EditPath.Exposure, bare).Text); Assert.Equal(StepStatus.Waiting, State(EditPath.Motion, bare).Status); // Every setting of the section taken over by hand: nothing waits for the analysis any more. var manual = Advice(bare).In(AdvisorSection.Exposure).Aggregate(new ProjectSettings(), (p, r) => EditorSettings.All.FirstOrDefault(s => s.Id == r.Parameter) is { } setting ? Manual(setting, p) : p); var edited = State(EditPath.Exposure, bare, project: manual); Assert.Equal(StepStatus.Done, edited.Status); Assert.Equal(Loc.L("Editor.Step.Edited"), edited.Text); var analysed = EditorFixture.Content(EditorFixture.Sunset); var auto = State(EditPath.Exposure, analysed); Assert.Equal(StepStatus.Done, auto.Status); Assert.Equal(Loc.L("Common.Auto"), auto.Text); } [Test] public void MissingTimestampsNeedAttentionUntilTheTimingIsSetByHand() { var analysed = EditorFixture.Content(EditorFixture.Sunset); var frames = analysed.Frames.Select(f => f with { CapturedAt = null }).ToList(); var untimed = analysed with { Frames = frames, Light = LightAnalyzer.Analyze(frames) }; var time = State(EditPath.Time, untimed); Assert.Equal(StepStatus.Attention, time.Status); Assert.Equal(Loc.L("Editor.Step.MissingData"), time.Text); var reason = Advice(untimed).Find(AdvisorParameters.TimingMode)!; Assert.Equal(Advisor.Explain(reason), time.Detail, "the tooltip says what is missing"); var manual = Manual(EditorSettings.TimingMode, new ProjectSettings()); Assert.Equal(StepStatus.Done, State(EditPath.Time, untimed, project: manual).Status); } [Test] public void StackingIsOptional() { var content = EditorFixture.Content(EditorFixture.Sunset); var none = State(EditPath.Stack, content); Assert.Equal(StepStatus.Todo, none.Status); Assert.Equal(Loc.L("Editor.Step.Optional"), none.Text); var trails = State(EditPath.Stack, content, project: new ProjectSettings { Stack = new StackSettings { Mode = StackSetting.StarTrails } }); Assert.Equal(StepStatus.Done, trails.Status); Assert.Equal(Loc.L("Editor.Stack.StarTrails"), trails.Text); } [Test] public void ExportFollowsTheRenderItsBlockerAndWhetherTheVideoIsCurrent() { var content = EditorFixture.Content(EditorFixture.Sunset); Assert.Equal(StepStatus.Todo, State(EditPath.Export, content).Status); Assert.Equal(Loc.L("Editor.Step.NotRendered"), State(EditPath.Export, content).Text); var blocked = State(EditPath.Export, content, renderBlocked: "No encoder"); Assert.Equal(StepStatus.Attention, blocked.Status); Assert.Equal("No encoder", blocked.Detail); Assert.Equal(StepStatus.Running, State(EditPath.Export, content, render: Job(JobKind.Render, JobState.Queued)).Status); var rendered = content with { Renders = [Render(new ProjectSettings().ToJson())] }; Assert.Equal(StepStatus.Done, State(EditPath.Export, rendered, renderMatches: true).Status); var outdated = State(EditPath.Export, rendered, renderMatches: false); Assert.Equal(StepStatus.Attention, outdated.Status); Assert.Equal(Loc.L("Editor.Step.Outdated"), outdated.Text); } [Test] public void TheSignatureIgnoresWhatDoesNotChangeTheVideo() { var project = new ProjectSettings(); var same = project with { HeroFrame = 12, PendingFrames = [new PendingFrame { Sha256 = "ab", Size = 10, Excluded = true }], Export = project.Export with { Batch = ["4k-hevc"], Folder = @"D:\Videos", NameTemplate = "{title}", SyncToServer = true }, }; Assert.Equal(EditPath.Signature(project), EditPath.Signature(same)); Assert.NotEqual(EditPath.Signature(project), EditPath.Signature(project with { ExcludedFrames = [3] })); Assert.NotEqual(EditPath.Signature(project), EditPath.Signature(EditorSettings.Deflicker.WithManual(project, true))); } [Test] public void ARenderCountsAsCurrentWhenAnyRenderHasTheSettingsOrTheNewestKeepsNone() { var a = new ProjectSettings(); var b = EditorSettings.Deflicker.WithManual(a, true); Assert.False(new RenderedSettings([]).Matches(a), "no render"); var rendered = new RenderedSettings([Render(b.ToJson()), Render(a.ToJson())]); Assert.True(rendered.Matches(a), "an older render has these settings"); Assert.True(rendered.Matches(b)); Assert.False(rendered.Matches(a with { ExcludedFrames = [1] })); Assert.True(new RenderedSettings([Render(null), Render(a.ToJson())]).Matches(b), "the newest (a server's) says nothing about its settings"); Assert.True(new RenderedSettings([Render("{ not json")]).Matches(b), "unreadable settings say nothing either"); } [Test] public void TheEditorOpensOnTheRememberedStepElseAnalysisUntilTheSequenceIsAnalysed() { var bare = EditorFixture.Content(EditorFixture.Sunset, deep: false); var analysed = EditorFixture.Content(EditorFixture.Sunset); Assert.Equal(EditPath.Analysis, EditPath.Start(null, bare)); Assert.Equal(EditPath.Frames, EditPath.Start(null, analysed)); Assert.Equal(EditPath.Export, EditPath.Start(EditPath.Export, bare)); // A deep analysis that waits, runs or is paused shows first, also for an analysed sequence; the memory still wins. Assert.Equal(EditPath.Analysis, EditPath.Start(null, analysed, Job(JobKind.DeepAnalyse, JobState.Queued))); Assert.Equal(EditPath.Camera, EditPath.Start(EditPath.Camera, analysed, Job(JobKind.DeepAnalyse, JobState.Running))); } [Test] public void FramesOfAnotherSizeOrOrientationAreForTheFramesStep() { Assert.Equal(EditPath.Frames, EditPath.StepOf(FindingKeys.MixedResolution), "Frames excludes them; no Camera setting acts on single frames"); Assert.Equal(EditPath.Frames, EditPath.StepOf(FindingKeys.MixedOrientation)); } [Test] public void ValuesSetByHandReadEditedEvenWhenEveryAutoSettingIsAuto() { var content = EditorFixture.Content(EditorFixture.Sunset); Assert.Equal(Loc.L("Common.Auto"), State(EditPath.Camera, content).Text); Assert.Equal(Loc.L("Common.Auto"), State(EditPath.Time, content).Text); Assert.Equal(Loc.L("Common.Auto"), State(EditPath.Exposure, content).Text); var project = new ProjectSettings(); Assert.Equal(Loc.L("Editor.Step.Edited"), State(EditPath.Camera, content, project: project with { Camera = new CameraSettings { Crop = [0, 0.1, 1, 0.9] } }).Text, "a crop"); Assert.Equal(Loc.L("Editor.Step.Edited"), State(EditPath.Camera, content, project: project with { Camera = new CameraSettings { Keyframes = [new CameraKey(10, 0.5, 0.5, 1, 0)] } }).Text, "a camera move"); Assert.Equal(Loc.L("Editor.Step.Edited"), State(EditPath.Camera, content, project: project with { Camera = new CameraSettings { OffsetX = 0.05 } }).Text, "an offset"); Assert.Equal(Loc.L("Editor.Step.Edited"), State(EditPath.Time, content, project: project with { Timing = project.Timing with { FitToSeconds = 30 } }).Text, "fit to 30 s"); Assert.Equal(Loc.L("Editor.Step.Edited"), State(EditPath.Exposure, content, project: project with { Exposure = project.Exposure with { ExposureOffsetEv = 0.5 } }).Text, "an exposure offset"); Assert.Equal(Loc.L("Common.Auto"), State(EditPath.Camera, content, project: project with { Camera = new CameraSettings { Crop = [0, 0, 1, 1] } }).Text, "a crop of the whole frame is no crop"); } [Test] public void TheOrientationAndTheLookReadEdited() { var content = EditorFixture.Content(EditorFixture.Sunset); var project = new ProjectSettings(); foreach (var (camera, what) in new[] { (new CameraSettings { Rotate = 90 }, "a quarter turn"), (new CameraSettings { Mirror = true }, "a mirror"), (new CameraSettings { Flip = true }, "a flip"), (new CameraSettings { Straighten = -1.2 }, "a straighten"), }) Assert.Equal(Loc.L("Editor.Step.Edited"), State(EditPath.Camera, content, project: project with { Camera = camera }).Text, what); Assert.Equal(Loc.L("Editor.Step.Edited"), State(EditPath.Exposure, content, project: project with { Look = new LookSettings { Filter = "sepia" } }).Text, "a look"); Assert.Equal(Loc.L("Common.Auto"), State(EditPath.Exposure, content, project: project with { Look = null }).Text, "no look"); } [Test] public void AMissingValueOfASettingThatDoesNotApplyAsksNothing() { // No aperture in the metadata (a manual lens): holy grail and its transition length cannot be decided. var analysed = EditorFixture.Content(EditorFixture.Sunset); var frames = analysed.Frames.Select(f => f with { FNumber = null }).ToList(); var blind = analysed with { Frames = frames, Light = LightAnalyzer.Analyze(frames) }; Assert.True(Advice(blind).Find(AdvisorParameters.TransitionLength) is { IsMissingData: true }, "the transition length lacks data"); Assert.Equal(StepStatus.Attention, State(EditPath.Exposure, blind).Status, "holy grail is to be decided"); // Holy grail switched off by hand: the transition length shows n/a and cannot be set, so it is not asked for. var off = EditorSettings.HolyGrail.WithManual(new ProjectSettings(), false); var decided = State(EditPath.Exposure, blind, project: off); Assert.Equal(StepStatus.Done, decided.Status); Assert.Equal(Loc.L("Editor.Step.Edited"), decided.Text); Assert.False(EditorSettings.Applies(EditorSettings.TransitionLength, off, blind.Deep)); Assert.True(EditorSettings.Applies(EditorSettings.TransitionLength, EditorSettings.HolyGrail.WithManual(off, true), blind.Deep)); } [Test] public void AnAnalysisThatFoundNoRegionsDoesNotKeepExposureWaiting() { var analysed = EditorFixture.Content(EditorFixture.Sunset); var noRegions = analysed with { Deep = analysed.Deep! with { Regions = null } }; Assert.True(Advice(noRegions).Find(AdvisorParameters.Regions) is { IsMissingData: true }, "the advisor has no region value"); Assert.NotEqual(StepStatus.Waiting, State(EditPath.Exposure, noRegions).Status, "the analysis has run: regions stay at the engine's fallback"); } [Test] public void TheAnalysisStampTellsOneAnalysisFromAnother() { var bare = EditorFixture.Content(EditorFixture.Sunset, deep: false); var analysed = EditorFixture.Content(EditorFixture.Sunset); Assert.Null(EditPath.AnalysisStamp(bare)); Assert.NotNull(EditPath.AnalysisStamp(analysed)); Assert.Equal(EditPath.AnalysisStamp(analysed), EditPath.AnalysisStamp(EditorFixture.Content(EditorFixture.Sunset)), "the same analysis read again"); var again = analysed with { Deep = analysed.Deep! with { Shake = analysed.Deep.Shake with { RmsPx = analysed.Deep.Shake.RmsPx + 1 } } }; Assert.NotEqual(EditPath.AnalysisStamp(analysed), EditPath.AnalysisStamp(again)); } [Test] public void EveryFindingHasTheStepThatDealsWithIt() { var keys = typeof(FindingKeys).GetFields(BindingFlags.Public | BindingFlags.Static) .Concat(typeof(DeepFindingKeys).GetFields(BindingFlags.Public | BindingFlags.Static)) .Where(f => f.IsLiteral && f.FieldType == typeof(string)) .Select(f => (string)f.GetRawConstantValue()!) .ToList(); Assert.Count(29, keys); foreach (var key in keys) { var step = EditPath.StepOf(key); Assert.NotNull(step, key); Assert.Contains(step!, Steps, key); Assert.NotEqual(EditPath.Analysis, step, $"{key}: the Analysis step lists every finding, it deals with none"); } } private static StepState State(string step, SequenceContent content, ProjectSettings? project = null, Job? analysis = null, Job? render = null, string? renderBlocked = null, bool renderMatches = false) => EditPath.Of(step, new PathFacts(content, Advice(content), project ?? content.Project, analysis, render, renderBlocked, renderMatches)); private static AdvisorResult Advice(SequenceContent content) => Advisor.Recommend(content.Light, content.Deep, SequenceContext.FromFrames(content.Frames, [])); /// taken over by hand at its current value. private static ProjectSettings Manual(IAutoSetting setting, ProjectSettings project) => setting switch { AutoSetting b => b.WithManual(project, b.Effective(project, null)), AutoSetting i => i.WithManual(project, i.Effective(project, null)), AutoSetting d => d.WithManual(project, d.Effective(project, null)), AutoSetting s => s.WithManual(project, s.Effective(project, null)), AutoSetting t => t.WithManual(project, t.Effective(project, null)), AutoSetting r => r.WithManual(project, r.Effective(project, null)), AutoSetting f => f.WithManual(project, f.Effective(project, null)), _ => throw new InvalidOperationException(setting.Id), }; private static Job Job(JobKind kind, JobState state) => new() { Id = Guid.CreateVersion7(), Kind = kind, State = state, Created = DateTimeOffset.UtcNow, }; private static Render Render(string? settings) => new() { Id = Guid.CreateVersion7(), SequenceId = Guid.CreateVersion7(), PresetName = "4K", OutputPath = "exports/a.mp4", SettingsJson = settings, Created = DateTimeOffset.UtcNow, }; }