Files
Chronos/tests/Chronos.Core.Tests/Analysis/Deep/DeepAnalyzerTests.cs
T
Alby96andClaude Opus 5.5 5f27a86406
CI / Build and test (pull_request) Failing after 2m3s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / Docker image (pull_request) Skipped
CI / Windows tests and installer (pull_request) Skipped
CI / YAML check (pull_request) Successful in 4s
refactor!: rename Titano to Chronos and move CI and releases to Gitea
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. 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) and
  pe-check.

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>
2026-10-23 11:34:48 +02:00

345 lines
16 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Chronos.Core.Analysis;
using Chronos.Core.Analysis.Deep;
using Chronos.Core.Diagnostics.Synthetic;
using Chronos.Core.Imaging;
using Chronos.Testing;
namespace Chronos.Core.Tests.Analysis.Deep;
public sealed class DeepAnalyzerTests(TestContext context)
{
[Test]
public async Task NightSkyShowsStarsDarknessFlickerAndNoShake()
{
var report = await DeepReports.Night;
context.Log($"{report.Sky} {report.Flicker} {report.Shake}");
Assert.True(report.Sky.StarField);
Assert.Equal(DayNight.Night, report.Sky.Light);
Assert.Equal(report.FrameCount, report.Sky.StarFrames);
// Uniform ±0.12 EV flicker has an RMS of 0.069 EV.
Assert.Approximately(0.069, report.Flicker.AmplitudeEv, 0.01);
Assert.Equal(15, report.Flicker.RecommendedWindow);
Assert.Less(report.Shake.RmsPx, 0.05);
Assert.Equal(1.0, report.Shake.MeasuredFraction);
Assert.Empty(report.Issues.SceneChanges);
Assert.Equal(0.0, report.Motion.P95Px!.Value);
Assert.Equal(RegionMethod.Horizon, report.Regions!.Method);
Assert.Contains(report.Findings, f => f.Key == DeepFindingKeys.StarField);
Assert.Contains(report.Findings, f => f.Key == DeepFindingKeys.Flicker);
Assert.True(report.Frames.FlagsOf(0).HasFlag(DeepFrameFlags.StarField | DeepFrameFlags.Dark));
}
[Test]
public async Task InjectedCameraJitterIsMeasuredAsShake()
{
var report = await DeepReports.NightJitter;
context.Log($"{report.Shake} {report.Motion}");
// Integer offsets uniform in −2..2 per axis: 2 px RMS in 2D.
Assert.InRange(report.Shake.RmsPx, 1.6, 2.4);
Assert.InRange(report.Shake.CropCost, 0.02, 0.2);
Assert.False(report.Shake.PanDetected);
Assert.Greater(report.Motion.P95Px!.Value, 1);
Assert.Contains(report.Findings, f => f.Key == DeepFindingKeys.Shake);
}
[Test]
public async Task HolyGrailStepsAreReceivedAndSpacedByHalfTheirDistance()
{
var report = await DeepReports.HolyGrail;
Assert.Equal(5, report.Exposure.MetadataSteps);
Assert.Equal(5, report.Exposure.ReceivedSteps);
Assert.Equal(40.0, report.Exposure.TypicalSpacing);
Assert.Equal(20, report.Exposure.TransitionLength);
Assert.True(report.Exposure.Steps.All(s => s.MeasuredEv is > 0.9 and < 1.1));
Assert.InRange(report.Exposure.SceneRangeEv, 5, 6);
Assert.Equal(DayNight.DayToNight, report.Sky.Light);
// Brightness ramps and steps are levelled before the flow, so the static scene shows no motion.
Assert.Equal(0.0, report.Motion.P95Px!.Value);
Assert.Empty(report.Issues.SceneChanges);
}
[Theory]
[Case(8.0, 33)]
[Case(12.5, 51)]
[Case(40.0, 151)]
public async Task PeriodicFlickerGivesAWindowOfFourPeriods(double period, int window)
{
var sequence = SyntheticSequence.Create(new SyntheticSequenceOptions
{
Width = 96,
Height = 64,
FrameCount = 240,
FlickerEv = 0.15,
FlickerPeriodFrames = period,
});
var report = await DeepAnalyzer.AnalyzeAsync(new SyntheticFrameSource(sequence), DeepScenes.Options);
Assert.Approximately(period, report.Flicker.PeriodFrames!.Value, 0.05 * period);
Assert.Equal(window, report.Flicker.RecommendedWindow);
Assert.Equal(1, window % 2);
}
[Test]
public async Task BlackWhiteDuplicateAndUnreadableFramesAreFlagged()
{
var sequence = SyntheticSequence.Create(new SyntheticSequenceOptions
{
Width = 96,
Height = 64,
FrameCount = 60,
FlickerEv = 0.02,
BlackFrames = [10],
WhiteFrames = [20],
DuplicateFrames = [30],
});
var report = await DeepAnalyzer.AnalyzeAsync(new SyntheticFrameSource(sequence) { Undecodable = [40] }, DeepScenes.Options);
Assert.SequenceEqual([10], report.Issues.Black);
Assert.SequenceEqual([20], report.Issues.White);
Assert.SequenceEqual([30], report.Issues.Duplicate);
Assert.SequenceEqual([40], report.Issues.Undecodable);
Assert.SequenceEqual([10, 20, 30, 40], report.Issues.Excludable());
Assert.Empty(report.Issues.SceneChanges);
Assert.Null(report.Frames.LogLuminance[40]);
Assert.True(report.Frames.FlagsOf(40).HasFlag(DeepFrameFlags.Undecodable));
Assert.False(report.Issues.Excludable().Contains(report.HeroIndex!.Value));
foreach (var key in new[] { DeepFindingKeys.BlackFrames, DeepFindingKeys.WhiteFrames, DeepFindingKeys.DuplicateFrames, DeepFindingKeys.UnreadableFrames })
Assert.Equal(FindingSeverity.Warning, report.Findings.Single(f => f.Key == key).Severity);
Assert.Equal(4, AnalysisSummary.From(report).WarningCount);
}
[Test]
public async Task ACutToAnotherViewIsASceneChange()
{
var first = new SyntheticFrameSource(SyntheticSequence.Create(new SyntheticSequenceOptions { Width = 96, Height = 64, FrameCount = 50, FlickerEv = 0.05 }));
var second = new SyntheticFrameSource(SyntheticSequence.Create(new SyntheticSequenceOptions
{
Width = 96,
Height = 64,
FrameCount = 50,
FlickerEv = 0.05,
Seed = 5,
HorizonFraction = 0.75,
SkyLevel = 0.6,
LandLevel = 0.01,
}));
var report = await DeepAnalyzer.AnalyzeAsync(new SplicedSource(first, second, 30), DeepScenes.Options);
Assert.SequenceEqual([30], report.Issues.SceneChanges);
Assert.True(report.Frames.FlagsOf(30).HasFlag(DeepFrameFlags.SceneChange));
}
[Test]
public async Task ResumingFromACheckpointGivesTheSameReport()
{
var expected = (await DeepReports.HolyGrail).ToJson();
var checkpoint = new MemoryCheckpoint();
var crashing = new SyntheticFrameSource(DeepScenes.HolyGrail())
{
OnLoad = i =>
{
if (i == 101) throw new IOException("simulated crash");
},
};
await Assert.ThrowsAsync<IOException>(() => DeepAnalyzer.AnalyzeAsync(crashing, DeepScenes.Options, checkpoint));
var persisted = checkpoint.Frames.Count;
Assert.InRange(persisted, 90, 101);
var resumed = new SyntheticFrameSource(DeepScenes.HolyGrail());
var report = await DeepAnalyzer.AnalyzeAsync(resumed, DeepScenes.Options, checkpoint);
Assert.Equal(expected, report.ToJson());
// Region sample + one priming frame + the frames not yet persisted.
Assert.Equal(DeepScenes.Options.RegionSampleFrames + 1 + (240 - persisted), resumed.Loads);
Assert.Equal(240, checkpoint.Frames.Count);
}
[Test]
public async Task FileCheckpointDropsATornLineAndResumes()
{
var path = Path.Combine(context.CreateTempDirectory(), ".chronos", DeepAnalysisCheckpointFile.FileName);
var sequence = SyntheticSequence.Create(new SyntheticSequenceOptions { Width = 96, Height = 64, FrameCount = 40, FlickerEv = 0.05, DuplicateFrames = [7] });
var expected = (await DeepAnalyzer.AnalyzeAsync(new SyntheticFrameSource(sequence), DeepScenes.Options)).ToJson();
var file = new DeepAnalysisCheckpointFile(path);
var crashing = new SyntheticFrameSource(sequence) { OnLoad = i => { if (i == 21) throw new IOException("crash"); } };
await Assert.ThrowsAsync<IOException>(() => DeepAnalyzer.AnalyzeAsync(crashing, DeepScenes.Options, file));
await File.AppendAllTextAsync(path, "{\"index\":20,\"flags\":\"No", context.Cancellation);
var report = await DeepAnalyzer.AnalyzeAsync(new SyntheticFrameSource(sequence), DeepScenes.Options, file);
Assert.Equal(expected, report.ToJson());
Assert.Equal(41, File.ReadAllLines(path).Length);
// Other options: the stale checkpoint is discarded instead of mixed in.
var other = DeepScenes.Options with { FlowEvery = 3 };
var fresh = await new DeepAnalysisCheckpointFile(path).LoadAsync(DeepAnalyzer.Fingerprint(40, other), context.Cancellation);
Assert.Empty(fresh);
file.Delete();
Assert.FileMissing(path);
}
[Test]
public async Task ReportRoundTripsThroughJsonWithinTheSizeBudget()
{
var report = await DeepReports.NightJitter;
var json = report.ToJson();
var copy = DeepAnalysisReport.FromJson(json);
Assert.Equal(json, copy.ToJson());
var perFrame = (double)System.Text.Encoding.UTF8.GetByteCount(json) / report.FrameCount;
context.Log($"{perFrame:0.0} bytes per frame");
Assert.LessOrEqual(perFrame, 120);
var path = Path.Combine(context.CreateTempDirectory(), DeepAnalysisReport.FileName);
report.Save(path);
Assert.Equal(json, DeepAnalysisReport.Load(path).ToJson());
var summary = AnalysisSummary.From(report);
Assert.Equal(summary, AnalysisSummary.FromJson(summary.ToJson()));
Assert.True(summary.StarField);
Assert.Equal(DayNight.Night, summary.Light);
Assert.Equal(report.HeroIndex, summary.HeroIndex);
Assert.Less(summary.ToJson().Length, 400);
Assert.Throws<InvalidDataException>(() => DeepAnalysisReport.FromJson(json.Replace("\"version\":1", "\"version\":99", StringComparison.Ordinal)));
}
[Test]
public async Task ResultsDoNotDependOnParallelism()
{
var sequence = DeepScenes.NightSky(frames: 40, jitter: 2);
var serial = await DeepAnalyzer.AnalyzeAsync(new SyntheticFrameSource(sequence, 2), DeepScenes.Options with { MaxDegreeOfParallelism = 1 });
var parallel = await DeepAnalyzer.AnalyzeAsync(new SyntheticFrameSource(sequence, 2), DeepScenes.Options with { MaxDegreeOfParallelism = 3 });
Assert.Equal(serial.ToJson(), parallel.ToJson());
}
[Test]
public async Task CancellationStopsWithinOneBatch()
{
using var cancellation = new CancellationTokenSource();
var checkpoint = new MemoryCheckpoint();
var source = new SyntheticFrameSource(DeepScenes.HolyGrail())
{
OnLoad = i =>
{
if (i == 50) cancellation.Cancel();
},
};
await Assert.ThrowsAsync<OperationCanceledException>(() => DeepAnalyzer.AnalyzeAsync(source, DeepScenes.Options, checkpoint, null, cancellation.Token));
Assert.LessOrEqual(source.Loads, DeepScenes.Options.RegionSampleFrames + 52);
Assert.Equal(50, checkpoint.Frames.Count);
}
[Test]
public async Task ProgressCountsEveryFrameUpToTheTotal()
{
var sequence = SyntheticSequence.Create(new SyntheticSequenceOptions { Width = 64, Height = 48, FrameCount = 21 });
var reports = new List<AnalysisProgress>();
await DeepAnalyzer.AnalyzeAsync(new SyntheticFrameSource(sequence), DeepScenes.Options, progress: new SyncProgress(reports.Add));
Assert.Equal(11, reports.Count);
Assert.Monotonic(reports.Select(r => (double)r.Done), strict: true);
Assert.Equal(21, reports[^1].Done);
Assert.True(reports.All(r => r.Total == 21 && r.FramesPerSecond > 0));
Assert.Equal(TimeSpan.Zero, reports[^1].Eta);
}
[Test]
public async Task EmptyAndUnreadableSequencesGiveEmptyReports()
{
var empty = await DeepAnalyzer.AnalyzeAsync(new SplicedSource(new SyntheticFrameSource(DeepScenes.HolyGrail()), null!, 0) { Count = 0 }, DeepScenes.Options);
Assert.Equal(0, empty.FrameCount);
Assert.Equal(DayNight.Unknown, empty.Sky.Light);
Assert.Empty(empty.Findings);
var unreadable = await DeepAnalyzer.AnalyzeAsync(new SyntheticFrameSource(SyntheticSequence.Create(new SyntheticSequenceOptions { FrameCount = 3 })) { Undecodable = [0, 1, 2] }, DeepScenes.Options);
Assert.Equal(DayNight.Unknown, unreadable.Sky.Light);
Assert.Null(unreadable.HeroIndex);
Assert.Null(unreadable.Regions);
Assert.SequenceEqual([0, 1, 2], unreadable.Issues.Undecodable);
Assert.Equal(unreadable.ToJson(), DeepAnalysisReport.FromJson(unreadable.ToJson()).ToJson());
}
[Test]
public void StarDetectorCountsPointsButNotBlobs()
{
using var image = new ImageBuffer(128, 96, 3);
image.Data.Fill(1f / 2048);
var random = new Random(3);
var stars = 0;
for (var y = 6; y < 90; y += 12)
{
for (var x = 6; x < 122; x += 12)
{
var v = (float)(0.2 + 0.8 * random.NextDouble());
for (var c = 0; c < 3; c++) image[c, x, y] = v;
stars++;
}
}
// A lit window: 4×4 bright block (extended, not a star).
for (var y = 40; y < 44; y++)
{
for (var x = 60; x < 64; x++)
{
for (var c = 0; c < 3; c++) image[c, x, y] = 0.8f;
}
}
var density = StarFieldDetector.Density(image, noiseSigma: 0);
Assert.Approximately(stars * 1e6 / (128 * 96), density, 1e-6);
}
[Test]
public void CloudCoverageCountsTexturedSkyBlocks()
{
using var image = new ImageBuffer(128, 64, 3);
var sky = new float[image.PlaneLength];
var random = new Random(9);
for (var y = 0; y < 64; y++)
{
for (var x = 0; x < 128; x++)
{
var i = y * 128 + x;
sky[i] = y < 32 ? 1 : 0;
// Smooth gradient everywhere; cloud texture on the left half of the sky.
var v = 0.2 + 0.002 * y + (y < 32 && x < 64 ? 0.15 * random.NextDouble() : 0);
for (var c = 0; c < 3; c++) image[c, x, y] = (float)v;
}
}
Assert.Approximately(0.5, CloudCoverage.Estimate(image, sky), 1e-9);
Assert.True(double.IsNaN(CloudCoverage.Estimate(image, new float[image.PlaneLength])));
}
[Test]
public void HistogramDistanceIgnoresExposureButNotContent()
{
using var scene = SyntheticSequence.Create(new SyntheticSequenceOptions { Width = 96, Height = 64 }).Render(0);
using var brighter = scene.Clone();
foreach (ref var v in brighter.Data) v *= 1.7f;
using var other = SyntheticSequence.Create(new SyntheticSequenceOptions { Width = 96, Height = 64, HorizonFraction = 0.8, SkyLevel = 0.6 }).Render(0);
var a = FrameStatistics.Measure(scene);
Assert.Less(HistogramDistance.ExposureAligned(a, FrameStatistics.Measure(brighter)), 0.02);
Assert.Greater(HistogramDistance.ExposureAligned(a, FrameStatistics.Measure(other)), 0.3);
}
/// <summary>Frames of <paramref name="first"/> before <paramref name="cut"/>, then frames of <paramref name="second"/>.</summary>
private sealed class SplicedSource(IFrameSource first, IFrameSource second, int cut) : IFrameSource
{
public int Count { get; init; } = first.Count;
public LightFrame Metadata(int index) => first.Metadata(index);
public ValueTask<ImageBuffer?> LoadAsync(int index, int maxLongEdge, CancellationToken cancellationToken) =>
(index < cut ? first : second).LoadAsync(index, maxLongEdge, cancellationToken);
}
/// <summary>Reports synchronously (Progress&lt;T&gt; posts to the thread pool and may reorder).</summary>
private sealed class SyncProgress(Action<AnalysisProgress> report) : IProgress<AnalysisProgress>
{
public void Report(AnalysisProgress value) => report(value);
}
}
/// <summary>Deep reports of the shared scenes, computed once per test run.</summary>
internal static class DeepReports
{
private static readonly Lazy<Task<DeepAnalysisReport>> NightReport = new(() => DeepAnalyzer.AnalyzeAsync(new SyntheticFrameSource(DeepScenes.NightSky()), DeepScenes.Options));
private static readonly Lazy<Task<DeepAnalysisReport>> NightJitterReport = new(() => DeepAnalyzer.AnalyzeAsync(new SyntheticFrameSource(DeepScenes.NightSky(jitter: 2), 2), DeepScenes.Options));
private static readonly Lazy<Task<DeepAnalysisReport>> HolyGrailReport = new(() => DeepAnalyzer.AnalyzeAsync(new SyntheticFrameSource(DeepScenes.HolyGrail()), DeepScenes.Options));
public static Task<DeepAnalysisReport> Night => NightReport.Value;
public static Task<DeepAnalysisReport> NightJitter => NightJitterReport.Value;
public static Task<DeepAnalysisReport> HolyGrail => HolyGrailReport.Value;
}