Files
Chronos/tests/Chronos.App.Tests/Editor/PreviewRendererTests.cs
T
Alby96andClaude Opus 5.5 f2b58522b9
CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
refactor!: rename Titano to Chronos and move CI and releases to Gitea (#6)
The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout and the system SQLite. The release cross-compiles the Windows
  programs, checks their icon, version and DPI manifest, packs installers
  and zips, publishes the Gitea release and pushes the server image
  (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows
  runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow; the
  throw-away server runs without durability) and pe-check.
- Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR,
  3 on the shared runner; the weekly performance gates keep theirs).

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-23 12:45:18 +02:00

218 lines
11 KiB
C#

using Chronos.App.Editor;
using Chronos.Core.Imaging;
using Chronos.Core.Project;
using Chronos.Testing;
namespace Chronos.App.Tests.Editor;
/// <summary>The corrected-frame preview through the render pipeline at preview size.</summary>
public sealed class PreviewRendererTests
{
private static (PreviewRenderer Renderer, FakeSequenceSource Source) Create()
{
var content = EditorFixture.Content(EditorFixture.Sunset);
var source = new FakeSequenceSource(EditorFixture.Sunset, content);
return (new PreviewRenderer(content, source), source);
}
private static ProjectSettings Deflickered()
{
var p = new ProjectSettings();
return p with { Exposure = p.Exposure with { Deflicker = p.Exposure.Deflicker.WithManual(true), HighlightKnee = null } };
}
[Test]
public async Task DeflickerFlattensThePreviewedLuminance()
{
var (renderer, _) = Create();
var project = Deflickered();
var corrections = await renderer.PrepareAsync(project, CancellationToken.None);
Assert.Same(corrections, renderer.Cached(project));
Assert.Null(renderer.Cached(new ProjectSettings()), "other exposure settings need their own preparation");
var corrected = new List<double>();
var original = new List<double>();
for (var i = 4; i < 20; i++)
{
using var frame = await renderer.RenderAsync(new PreviewRequest(i, project, 64, PreviewFraming.Output), corrections, CancellationToken.None);
Assert.NotNull(frame);
corrected.Add(LogMean(frame!.Corrected));
original.Add(LogMean(frame.Original));
}
Assert.Less(Spread(corrected), Spread(original) * 0.6, "flicker removed between the steps");
}
[Test]
public async Task OutputFramingFollowsTheExportAspect()
{
var (renderer, _) = Create();
var project = new ProjectSettings();
project = project with { Export = project.Export with { Preset = project.Export.Preset.WithManual("square-1440") } };
using var output = await renderer.RenderAsync(new PreviewRequest(0, project, 80, PreviewFraming.Output), null, CancellationToken.None);
Assert.Equal(80, output!.Corrected.Width, "the square output at the preview's long edge");
Assert.Equal(80, output.Corrected.Height);
Assert.Equal(output.Corrected.Width, output.Original.Width);
using var source = await renderer.RenderAsync(new PreviewRequest(0, project, 80, PreviewFraming.Source), null, CancellationToken.None);
Assert.Equal(80, source!.Corrected.Width);
Assert.Approximately(64.0 / 96, source.Crop.Width, 0.02, "the square crop keeps two thirds of the 3:2 frame");
Assert.Approximately(1, source.Crop.Height, 0.02);
Assert.Approximately((1 - 64.0 / 96) / 2, source.Crop.X, 0.02, "centred");
}
[Test]
public async Task VectorsDescribeTheMotionToTheNextFrame()
{
var (renderer, _) = Create();
using var frame = await renderer.RenderAsync(new PreviewRequest(10, new ProjectSettings(), 96, PreviewFraming.Source, Vectors: true), null,
CancellationToken.None);
Assert.NotEmpty(frame!.Vectors);
Assert.True(frame.Vectors.All(v => v.X >= 0 && v.X < 96 && v.Y >= 0 && v.Y < 64));
using var last = await renderer.RenderAsync(new PreviewRequest(EditorFixture.Sunset.Count - 1, new ProjectSettings(), 96, PreviewFraming.Source, true),
null, CancellationToken.None);
Assert.Empty(last!.Vectors);
}
[Test]
public async Task ACancelledPreviewStopsWhileDecoding()
{
var (renderer, source) = Create();
source.DecodeGate = new TaskCompletionSource().Task;
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100));
await Assert.ThrowsAsync<OperationCanceledException>(() => renderer.RenderAsync(new PreviewRequest(0, new ProjectSettings(), 64, PreviewFraming.Output), null, cts.Token));
}
[Test]
public async Task AnUnknownFrameHasNoPreview()
{
var (renderer, _) = Create();
Assert.Null(await renderer.RenderAsync(new PreviewRequest(-1, new ProjectSettings(), 64, PreviewFraming.Output), null, CancellationToken.None));
Assert.Null(await renderer.RenderAsync(new PreviewRequest(10_000, new ProjectSettings(), 64, PreviewFraming.Output), null, CancellationToken.None));
}
[Test]
public async Task PreparedDecodesAreReused()
{
var (renderer, source) = Create();
var project = new ProjectSettings();
project = project with { Exposure = project.Exposure with { WhiteBalanceSmoothing = project.Exposure.WhiteBalanceSmoothing.WithManual(true) } };
await renderer.PrepareAsync(project, CancellationToken.None);
var decodes = source.Decodes;
Assert.GreaterOrEqual(decodes, EditorFixture.Sunset.Count, "white balance is measured on every frame");
await renderer.PrepareAsync(project with { Exposure = project.Exposure with { ExposureOffsetEv = 0.5 } }, CancellationToken.None);
Assert.Equal(decodes, source.Decodes);
}
[Test]
public async Task SolveSettingsReuseTheMeasurementsWithoutDecoding()
{
var (renderer, source) = Create();
var project = Sunset();
await renderer.PrepareAsync(project, CancellationToken.None);
var decodes = source.Decodes;
Assert.Greater(decodes, 0, "stabilisation and colour are measured once");
foreach (var strength in new[] { 0.3, 0.6, 0.9 })
{
var changed = project with { Exposure = project.Exposure with { DeflickerStrength = project.Exposure.DeflickerStrength.WithManual(strength), TransitionLength = project.Exposure.TransitionLength.WithManual(6) } };
await renderer.PrepareAsync(changed, CancellationToken.None);
}
Assert.Equal(decodes, source.Decodes, "a strength or transition change re-solves from the measured series");
}
[Test]
public async Task WithoutAnalysisNothingIsMeasuredUntilAModuleNeedsIt()
{
var content = EditorFixture.Content(EditorFixture.Sunset, deep: false);
var source = new FakeSequenceSource(EditorFixture.Sunset, content);
var renderer = new PreviewRenderer(content, source);
await renderer.PrepareAsync(new ProjectSettings(), CancellationToken.None);
Assert.Equal(0, source.Decodes, "no module on, nothing to measure");
var project = Deflickered();
await renderer.PrepareAsync(project, CancellationToken.None);
var decodes = source.Decodes;
Assert.GreaterOrEqual(decodes, EditorFixture.Sunset.Count, "deflicker measures the luminance once");
await renderer.PrepareAsync(project with { Exposure = project.Exposure with { DeflickerStrength = project.Exposure.DeflickerStrength.WithManual(0.4) } },
CancellationToken.None);
Assert.Equal(decodes, source.Decodes);
}
[Test]
public async Task AReSolveMatchesAFreshPreparation()
{
var (warm, _) = Create();
var project = Sunset();
await warm.PrepareAsync(project, CancellationToken.None);
var changed = project with { Exposure = project.Exposure with { DeflickerStrength = project.Exposure.DeflickerStrength.WithManual(0.5), ExposureOffsetEv = 0.3 } };
var resolved = await warm.PrepareAsync(changed, CancellationToken.None);
var (cold, _) = Create();
var fresh = await cold.PrepareAsync(changed, CancellationToken.None);
Assert.Equal(fresh.ExposureEv.Length, resolved.ExposureEv.Length);
for (var i = 0; i < fresh.ExposureEv.Length; i++) Assert.Approximately(fresh.ExposureEv[i], resolved.ExposureEv[i], 1e-9, $"frame {i}");
Assert.NotNull(resolved.WhiteBalance);
for (var i = 0; i < fresh.WhiteBalance!.Length; i++) Assert.Equal(fresh.WhiteBalance[i], resolved.WhiteBalance![i]);
Assert.Equal(fresh.StabilizationZoom, resolved.StabilizationZoom);
Assert.NotNull(resolved.Stabilization);
Assert.SequenceEqual(fresh.Stabilization!, resolved.Stabilization!);
}
[Test]
public async Task ActualSizeRendersTheSourcePixels()
{
var (renderer, _) = Create();
var edge = int.Max(EditorFixture.Sunset.Options.Width, EditorFixture.Sunset.Options.Height);
using var frame = await renderer.RenderAsync(new PreviewRequest(3, new ProjectSettings(), edge, PreviewFraming.Source, Vectors: true), null,
CancellationToken.None);
Assert.Equal(EditorFixture.Sunset.Options.Width, frame!.Corrected.Width, "one picture pixel per source pixel");
Assert.Equal(EditorFixture.Sunset.Options.Height, frame.Corrected.Height);
Assert.NotEmpty(frame.Vectors);
Assert.True(frame.Vectors.All(v => v.X < frame.Corrected.Width && v.Y < frame.Corrected.Height), "vectors in picture pixels");
}
[Test]
public async Task TheWaveformIsMeasuredOnRequest()
{
var (renderer, _) = Create();
using var plain = await renderer.RenderAsync(new PreviewRequest(3, new ProjectSettings(), 64, PreviewFraming.Output), null, CancellationToken.None);
Assert.Null(plain!.Waveform);
using var frame = await renderer.RenderAsync(new PreviewRequest(3, new ProjectSettings(), 64, PreviewFraming.Output, Waveform: true), null,
CancellationToken.None);
var waveform = Assert.NotNull(frame!.Waveform);
Assert.Equal(frame.Corrected.Width, waveform.Columns);
Assert.True(waveform.Density.Any(d => d > 0));
}
/// <summary>A sunset project as the advisor leaves it: deflicker, holy grail, white balance and stabilisation on.</summary>
private static ProjectSettings Sunset()
{
var p = new ProjectSettings();
return p with
{
Exposure = p.Exposure with
{
Deflicker = p.Exposure.Deflicker.WithManual(true),
HolyGrail = p.Exposure.HolyGrail.WithManual(true),
WhiteBalanceSmoothing = p.Exposure.WhiteBalanceSmoothing.WithManual(true),
},
Motion = p.Motion with { Stabilize = p.Motion.Stabilize.WithManual(true) },
};
}
private static double LogMean(ImageBuffer image)
{
double sum = 0;
var n = image.PlaneLength;
for (var i = 0; i < n; i++) sum += 0.2126 * image.Plane(0)[i] + 0.7152 * image.Plane(1)[i] + 0.0722 * image.Plane(2)[i];
return Math.Log2(sum / n);
}
/// <summary>Standard deviation of the frame-to-frame differences (flicker, not the slow ramp).</summary>
private static double Spread(List<double> values)
{
var diffs = values.Zip(values.Skip(1), (a, b) => b - a).ToList();
var mean = diffs.Average();
return Math.Sqrt(diffs.Sum(d => (d - mean) * (d - mean)) / diffs.Count);
}
}