Merge branch 'fix/tests-ci': serial culture tests, screenshot diff, playback, installer and PostgreSQL CI
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
39 files changed
+1825
-218
No files matched your search
@@ -1,3 +1,4 @@
|
||||
using Titano.App.DesignTime;
|
||||
using Titano.Core.Archiving;
|
||||
using Titano.Data.Archiving;
|
||||
using Titano.Data.Repositories;
|
||||
@@ -34,18 +35,18 @@ public static class SampleReports
|
||||
/// <summary>The trash dialog's rows: the first two sample sequences, deleted an hour and a day ago (nothing moves on disk).</summary>
|
||||
public static IReadOnlyList<TrashEntry> Trash(Guid archiveId, IReadOnlyList<SequenceLocation> sequences)
|
||||
{
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var now = SampleData.Now;
|
||||
return
|
||||
[
|
||||
.. sequences.Take(2).Select((s, i) => new TrashEntry(archiveId, s.Id.ToString("N"), s.Id, s.Title ?? s.Folder, s.Folder, 1_200 * (i + 1), 1_206 * (i + 1),
|
||||
31_457_280L * 1_200 * (i + 1), now.AddHours(-1 - (23 * i)), Environment.UserName)),
|
||||
31_457_280L * 1_200 * (i + 1), now.AddHours(-1 - (23 * i)), "alberto")),
|
||||
];
|
||||
}
|
||||
|
||||
/// <summary>What the rebuild dialog counts: a sidecar in every sequence folder but the last two, read by metadata instead.</summary>
|
||||
public static IReadOnlyList<SequenceFolder> Folders(IReadOnlyList<SequenceLocation> sequences)
|
||||
{
|
||||
var stamp = new FileStamp(4_096, DateTime.UtcNow);
|
||||
var stamp = new FileStamp(4_096, SampleData.Now.UtcDateTime);
|
||||
return [.. sequences.Select((s, i) => new SequenceFolder(s.Folder, i < sequences.Count - 2 ? stamp : null, stamp))];
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,13 @@ public sealed class SampleData
|
||||
{
|
||||
public const string ServerFingerprint = "9A:3C:71:E0:5B:12:C4:88:0F:6D:2A:91:E7:44:B3:5C:19:D2:6F:80:A1:3E:57:C9:0B:64:F8:2D:93:A6:71:CE";
|
||||
|
||||
/// <summary>
|
||||
/// The sample's "now": every time it stores (jobs, transfers, renders, cards, the trash) is set from it, never from
|
||||
/// the computer's clock, and <c>--capture</c> freezes <see cref="AppClock"/> at it, so the screenshots are the same
|
||||
/// on any day (the running render's log reads 10:14 to 10:15).
|
||||
/// </summary>
|
||||
public static readonly DateTimeOffset Now = new(2026, 10, 2, 10, 18, 0, TimeSpan.Zero);
|
||||
|
||||
private const string ServerAddress = "https://nas.local:8443";
|
||||
|
||||
private readonly AppServices _services;
|
||||
@@ -85,7 +92,7 @@ public sealed class SampleData
|
||||
var settings = _services.Settings;
|
||||
var root = Path.Combine(_services.Environment.DataDirectory, "archive");
|
||||
Directory.CreateDirectory(root);
|
||||
var archive = new Archive { Id = Guid.CreateVersion7(), Name = "TitanoArchive", RootPath = root, Created = DateTimeOffset.UtcNow.AddYears(-2) };
|
||||
var archive = new Archive { Id = Guid.CreateVersion7(), Name = "TitanoArchive", RootPath = root, Created = Now.AddYears(-2) };
|
||||
await db.Archives.UpsertAsync(archive);
|
||||
var paths = new ArchivePaths(root);
|
||||
Directory.CreateDirectory(paths.MetaDirectory);
|
||||
@@ -132,7 +139,7 @@ public sealed class SampleData
|
||||
{
|
||||
var db = _db ?? throw new InvalidOperationException("FillAsync first.");
|
||||
async Task<Sequence> LocalAsync(string text) => (await db.Sequences.ListAsync(new Data.Repositories.SequenceQuery { Text = text, Limit = 1 })).Items[0];
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var now = Now;
|
||||
Job Transfer(JobKind kind, Guid target, string label, string parameters, JobState state, long done, long total) => new()
|
||||
{
|
||||
Id = Guid.CreateVersion7(),
|
||||
@@ -169,8 +176,16 @@ public sealed class SampleData
|
||||
{
|
||||
var db = _db ?? throw new InvalidOperationException("FillAsync first.");
|
||||
var service = _services.Sync.Service ?? throw new InvalidOperationException("The index is not open.");
|
||||
// The history is kept whatever the date: the sample's finished jobs are dated from Now, not from today.
|
||||
await using var engine = new JobEngine(db, service.CreateJobHandlers(_services.Sync),
|
||||
new JobEngineOptions { Workers = 1, WorkerId = "sample-proxies", Kinds = [JobKind.Download], PollInterval = TimeSpan.FromMilliseconds(50) });
|
||||
new JobEngineOptions
|
||||
{
|
||||
Workers = 1,
|
||||
WorkerId = "sample-proxies",
|
||||
Kinds = [JobKind.Download],
|
||||
PollInterval = TimeSpan.FromMilliseconds(50),
|
||||
HistoryRetention = TimeSpan.FromDays(36_500),
|
||||
});
|
||||
await engine.StartAsync();
|
||||
var job = await service.EnqueueDownloadAsync(engine, ServerAddress, _sunrise, "Sunrise over Superga", SyncDownloadMode.Proxies, null);
|
||||
for (var i = 0; i < 200 && await db.Jobs.GetAsync(job.Id) is { State: JobState.Queued or JobState.Running }; i++) await Task.Delay(50);
|
||||
@@ -248,14 +263,14 @@ public sealed class SampleData
|
||||
Folder = "2026/2026-07-02 Sony ILCE-7M4",
|
||||
CameraMake = "SONY",
|
||||
CameraModel = "ILCE-7M4",
|
||||
FirstCapture = DateTime.Today.AddDays(-96).AddHours(5.2),
|
||||
LastCapture = DateTime.Today.AddDays(-96).AddHours(6.8),
|
||||
FirstCapture = new DateTime(2026, 7, 2).AddHours(5.2),
|
||||
LastCapture = new DateTime(2026, 7, 2).AddHours(6.8),
|
||||
MedianInterval = 2,
|
||||
FrameCount = 2_880,
|
||||
TotalBytes = 2_880 * 21_000_000L,
|
||||
Status = SequenceStatus.ProxiesBuilt | SequenceStatus.ThumbsBuilt,
|
||||
RenderedCount = 1,
|
||||
UpdatedAt = DateTimeOffset.UtcNow,
|
||||
UpdatedAt = Now,
|
||||
}), null, 3);
|
||||
_lakeComo = _remote.Add(SampleSyncRemote.Dto(new Sequence
|
||||
{
|
||||
@@ -265,13 +280,13 @@ public sealed class SampleData
|
||||
Folder = "2026/2026-08-21 Canon EOS R5",
|
||||
CameraMake = "Canon",
|
||||
CameraModel = "Canon EOS R5",
|
||||
FirstCapture = DateTime.Today.AddDays(-46).AddHours(16.5),
|
||||
LastCapture = DateTime.Today.AddDays(-46).AddHours(17.3),
|
||||
FirstCapture = new DateTime(2026, 8, 21).AddHours(16.5),
|
||||
LastCapture = new DateTime(2026, 8, 21).AddHours(17.3),
|
||||
MedianInterval = 2,
|
||||
FrameCount = 1_450,
|
||||
TotalBytes = 1_450 * 45_000_000L,
|
||||
Status = SequenceStatus.ThumbsBuilt,
|
||||
UpdatedAt = DateTimeOffset.UtcNow,
|
||||
UpdatedAt = Now,
|
||||
}), null, 3);
|
||||
|
||||
static string DeflickerWindow(int frames) =>
|
||||
@@ -334,7 +349,7 @@ public sealed class SampleData
|
||||
Fps = 24,
|
||||
DurationSeconds = 5,
|
||||
Bytes = bytes,
|
||||
Created = DateTimeOffset.UtcNow.AddDays(-daysAgo),
|
||||
Created = Now.AddDays(-daysAgo),
|
||||
ProducedBy = "studio-pc",
|
||||
});
|
||||
}
|
||||
@@ -345,7 +360,7 @@ public sealed class SampleData
|
||||
public async Task MarkAnalysingAsync(Guid sequenceId)
|
||||
{
|
||||
var db = _db ?? throw new InvalidOperationException("OpenEmptyAsync first.");
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var now = Now;
|
||||
await db.Jobs.EnqueueAsync(new Job
|
||||
{
|
||||
Id = Guid.CreateVersion7(),
|
||||
@@ -357,7 +372,7 @@ public sealed class SampleData
|
||||
ProgressDone = 40,
|
||||
ProgressTotal = 96,
|
||||
ItemsPerSecond = 12,
|
||||
NodeId = Environment.MachineName,
|
||||
NodeId = _services.WorkerId,
|
||||
Attempts = 1,
|
||||
Created = now.AddSeconds(-5),
|
||||
Started = now.AddSeconds(-4),
|
||||
@@ -478,7 +493,7 @@ public sealed class SampleData
|
||||
WarningCount = warnings,
|
||||
Origin = origin,
|
||||
SyncState = sync,
|
||||
UpdatedAt = DateTimeOffset.UtcNow,
|
||||
UpdatedAt = Now,
|
||||
}, colours, raw);
|
||||
}
|
||||
|
||||
@@ -599,7 +614,7 @@ public sealed class SampleData
|
||||
|
||||
private IEnumerable<Job> Jobs(IReadOnlyList<Sequence> s, Guid archiveId)
|
||||
{
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var now = Now;
|
||||
var here = _services.WorkerId;
|
||||
Job Make(JobKind kind, Sequence? target, string label, JobState state, long done, long total, double? rate = null, TimeSpan? eta = null,
|
||||
TimeSpan? startedAgo = null, TimeSpan? runFor = null, string? node = null, string? error = null, string? log = null,
|
||||
@@ -653,7 +668,7 @@ public sealed class SampleData
|
||||
private sealed class SampleServerApi(SampleData sample, Uri baseAddress, IReadOnlyCollection<string> trusted) : IServerApi
|
||||
{
|
||||
private UserDto User => new(Guid.Empty, "alberto", "Alberto", "editor", (long)sample._permissions, 5_000_000_000_000, 2_100_000_000_000, false, false,
|
||||
new DateTimeOffset(2025, 1, 1, 0, 0, 0, TimeSpan.Zero), DateTimeOffset.UtcNow);
|
||||
new DateTimeOffset(2025, 1, 1, 0, 0, 0, TimeSpan.Zero), Now);
|
||||
|
||||
public Uri BaseAddress { get; } = baseAddress;
|
||||
|
||||
@@ -669,7 +684,7 @@ public sealed class SampleData
|
||||
public Task<LoginResponse> LoginAsync(string username, string password, CancellationToken ct)
|
||||
{
|
||||
Token = "sample-token";
|
||||
return Task.FromResult(new LoginResponse(Token, User, false, DateTimeOffset.UtcNow.AddDays(30)));
|
||||
return Task.FromResult(new LoginResponse(Token, User, false, Now.AddDays(30)));
|
||||
}
|
||||
|
||||
public Task<UserDto> MeAsync(CancellationToken ct) =>
|
||||
@@ -745,7 +760,7 @@ public sealed class SampleData
|
||||
lock (_sequences)
|
||||
{
|
||||
var (dto, settings, frames) = _sequences[id];
|
||||
var updated = dto with { SettingsVersion = dto.SettingsVersion + 1, UpdatedAt = DateTimeOffset.UtcNow };
|
||||
var updated = dto with { SettingsVersion = dto.SettingsVersion + 1, UpdatedAt = Now };
|
||||
_sequences[id] = (updated, request.SettingsJson ?? settings, frames);
|
||||
return Task.FromResult(updated);
|
||||
}
|
||||
@@ -780,7 +795,7 @@ public sealed class SampleData
|
||||
public Task<JobDto> CreateJobAsync(CreateJobRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var job = new JobDto(Guid.CreateVersion7(), request.Kind, request.Target, null, "queued", "user", 0, 0, null, null, null, null, 0, null,
|
||||
DateTimeOffset.UtcNow, null, null);
|
||||
Now, null, null);
|
||||
lock (_jobs) _jobs[job.Id] = job;
|
||||
return Task.FromResult(job);
|
||||
}
|
||||
@@ -788,7 +803,7 @@ public sealed class SampleData
|
||||
/// <summary>A job of <paramref name="kind"/> running for <paramref name="sequenceId"/>, <paramref name="done"/> of <paramref name="total"/> frames.</summary>
|
||||
public void Run(Guid sequenceId, string kind, long done, long total)
|
||||
{
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var now = Now;
|
||||
var job = new JobDto(Guid.CreateVersion7(), kind, [sequenceId], null, "running", "user", done, total, 11.3, (total - done) / 11.3, null, "studio-pc", 1,
|
||||
null, now.AddMinutes(-2), now.AddMinutes(-2), null);
|
||||
lock (_jobs) _jobs[job.Id] = job;
|
||||
@@ -810,7 +825,7 @@ public sealed class SampleData
|
||||
}
|
||||
|
||||
public Task<JobDto> CancelJobAsync(Guid id, CancellationToken cancellationToken) =>
|
||||
Task.FromResult(Update(id, job => job with { State = "cancelled", Finished = DateTimeOffset.UtcNow }));
|
||||
Task.FromResult(Update(id, job => job with { State = "cancelled", Finished = Now }));
|
||||
|
||||
public Task<JobDto> RetryJobAsync(Guid id, CancellationToken cancellationToken) =>
|
||||
Task.FromResult(Update(id, job => job with { State = "queued", Error = null, Finished = null }));
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.IO;
|
||||
using Titano.App.DesignTime;
|
||||
using Titano.Core.Archiving;
|
||||
using Titano.Core.Codecs.Jpeg;
|
||||
using Titano.Core.Imaging;
|
||||
@@ -61,7 +62,7 @@ public static class SampleCards
|
||||
CameraModel = "Canon EOS R5",
|
||||
CameraSerial = "0431",
|
||||
FirstSeen = LastImport.AddMonths(-7),
|
||||
LastSeen = DateTimeOffset.UtcNow,
|
||||
LastSeen = SampleData.Now,
|
||||
};
|
||||
await db.MediaCards.UpsertAsync(known);
|
||||
await db.MediaCards.RecordImportsAsync([new ImportRecord(known.Id, new ImportKey("DCIM/100CANON/IMG_9001.CR2", 24_000_000, LastImport), null, null, null,
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace Titano.App.Infrastructure;
|
||||
|
||||
/// <summary>
|
||||
/// Today's date for screens that lay themselves out around it (the Library timeline's years, the months of the
|
||||
/// Archive heatmap). <c>--capture</c> freezes it at the sample's moment, so the screenshots draw the same pixels on
|
||||
/// any day and the pixel diff against their baselines stays meaningful.
|
||||
/// </summary>
|
||||
public static class AppClock
|
||||
{
|
||||
private static DateTimeOffset? _frozen;
|
||||
|
||||
/// <summary>The local date: the frozen moment's while frozen, the computer's otherwise.</summary>
|
||||
public static DateTime Today => (_frozen?.ToLocalTime().DateTime ?? DateTime.Now).Date;
|
||||
|
||||
/// <summary>Stops the clock at <paramref name="moment"/> for the rest of the process (screenshots only).</summary>
|
||||
public static void Freeze(DateTimeOffset moment) => _frozen = moment;
|
||||
}
|
||||
@@ -469,7 +469,7 @@ public sealed class ArchiveViewModel : SectionViewModel
|
||||
{
|
||||
_withoutProxies = withoutProxies;
|
||||
Tiles = ArchiveFigures.Tiles(stats);
|
||||
MonthRows = [.. ArchiveFigures.PhotosPerMonth(stats, DateTime.Today).Select(r => new ChartRow(r.Label, r.Values, r.ToolTips))];
|
||||
MonthRows = [.. ArchiveFigures.PhotosPerMonth(stats, AppClock.Today).Select(r => new ChartRow(r.Label, r.Values, r.ToolTips))];
|
||||
StorageBars = Bars(ArchiveFigures.StorageByYear(stats));
|
||||
CameraBars = Bars(ArchiveFigures.ByCamera(stats));
|
||||
FormatBars = Bars(ArchiveFigures.ByFormat(stats));
|
||||
|
||||
@@ -729,7 +729,7 @@ public sealed class LibraryViewModel : SectionViewModel
|
||||
try
|
||||
{
|
||||
var stats = LibraryFacets.Of(local, _remote, _origin.Value);
|
||||
_timeline = LibraryTimeline.From(stats, DateTime.Today);
|
||||
_timeline = LibraryTimeline.From(stats, AppClock.Today);
|
||||
var year = _year.Value;
|
||||
var camera = _camera.Value;
|
||||
Years = WithChosen([AnyYear(), .. stats.ByYear.OrderByDescending(y => y.Year).Select(y => new FilterOption<int?>(y.Year, YearText(y.Year), YearText(y.Year)))],
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
@@ -12,6 +13,7 @@ using Titano.App.Library;
|
||||
using Titano.App.Server;
|
||||
using Titano.App.Settings;
|
||||
using Titano.Core.Diagnostics;
|
||||
using Titano.Ui.Controls;
|
||||
using Titano.Ui.Theming;
|
||||
|
||||
namespace Titano.App.Shell;
|
||||
@@ -21,8 +23,10 @@ namespace Titano.App.Shell;
|
||||
/// <c><screen>-<theme>.png</c> from <see cref="SampleData"/> (first run on an empty index, then a populated
|
||||
/// one, then the sync table with transfers, a conflict and its confirmation, then the sequence editor on an archive
|
||||
/// sequence and on a server sequence with proxies, its server job and a read-only account), plus the Library, the
|
||||
/// editor's Exposure tab and the import plan in Arabic (<c>*-rtl-dark.png</c>, right to left), for visual review and
|
||||
/// phase reports (BRIEF §20.2), then exits.
|
||||
/// editor's Exposure tab and the import plan in Arabic (<c>*-rtl-dark.png</c>, right to left), for visual review,
|
||||
/// the pixel diff against <c>tests/visual/baselines</c> and phase reports (BRIEF §20.2), then exits. The pictures are
|
||||
/// the same on any day and machine: the clock stands at <see cref="SampleData.Now"/>, formats are English and nothing
|
||||
/// animates.
|
||||
/// </summary>
|
||||
public static class ScreenCapture
|
||||
{
|
||||
@@ -34,6 +38,9 @@ public static class ScreenCapture
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(directory);
|
||||
AppClock.Freeze(SampleData.Now);
|
||||
CultureInfo.CurrentCulture = CultureInfo.DefaultThreadCurrentCulture = CultureInfo.GetCultureInfo("en");
|
||||
Motion.SetIsEnabled(window, false);
|
||||
window.WindowStartupLocation = WindowStartupLocation.Manual;
|
||||
window.Left = -20000;
|
||||
window.Top = -20000;
|
||||
@@ -196,6 +203,7 @@ public static class ScreenCapture
|
||||
dialog.Top = -20000;
|
||||
dialog.ShowActivated = false;
|
||||
dialog.ShowInTaskbar = false;
|
||||
Motion.SetIsEnabled(dialog, false);
|
||||
dialog.Show();
|
||||
try
|
||||
{
|
||||
|
||||
@@ -0,0 +1,310 @@
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using Titano.Core.Codecs.Png;
|
||||
|
||||
namespace Titano.Tools.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// Visual regression for the <c>--capture</c> screenshots (BRIEF §20.2). <c>screenshots diff <dir></c> compares every
|
||||
/// PNG with the baseline of the same name (<c>tests/visual/baselines</c> unless <c>--baseline</c>): a pixel differs when
|
||||
/// one of its channels moves by more than <c>--tolerance</c> (of 255), and a screen fails when more than <c>--max-diff</c>
|
||||
/// of its pixels differ (a fraction), when its size changed, or when a baseline has no screenshot any more. A screen
|
||||
/// without a baseline fails once the baseline folder holds any PNG, so a screen added later cannot stay ungated; before
|
||||
/// the first baselines are committed nothing is compared, and the run says so in a warning and in its summary instead
|
||||
/// of passing for a clean diff. Every screen with differing pixels gets <c><name>-diff.png</c> in <c>--out</c> (the
|
||||
/// baseline faded to grey, the differing pixels magenta) and the run a <c>summary.md</c> there, copied to the job summary
|
||||
/// in GitHub Actions. <c>screenshots accept <dir></c> copies the screenshots over the baselines.
|
||||
/// </summary>
|
||||
internal sealed class ScreenshotsCommand : ICommand
|
||||
{
|
||||
/// <summary>Absorbs the last-bit noise of anti-aliased edges, not a changed colour.</summary>
|
||||
public const int DefaultTolerance = 8;
|
||||
|
||||
/// <summary>
|
||||
/// 0.05 %: about 650 pixels of a 1440×900 screen. A few digits of a measured value (latency, the last comparison's
|
||||
/// time) stay below it; a moved or resized control, a new colour or a missing picture do not.
|
||||
/// </summary>
|
||||
public const double DefaultMaxDiff = 0.0005;
|
||||
|
||||
public string Name => "screenshots";
|
||||
public string Summary => "Pixel diff of --capture screenshots against tests/visual/baselines (diff <dir> [--baseline, --out, --tolerance, --max-diff]; accept <dir>)";
|
||||
|
||||
public Task<int> RunAsync(string[] args) => Task.FromResult(Run(args, Environment.GetEnvironmentVariable, Console.Out));
|
||||
|
||||
/// <summary>The command with its environment passed in: tests run it without GitHub annotations or job summary.</summary>
|
||||
internal static int Run(string[] args, Func<string, string?> environment, TextWriter output)
|
||||
{
|
||||
var a = new Args(args);
|
||||
if (a.Positional is not [var verb, var directory]) throw new ToolException("Usage: screenshots diff <dir> [--baseline <dir>] [--out <dir>] [--tolerance 0-255] [--max-diff fraction] | screenshots accept <dir> [--baseline <dir>]");
|
||||
if (!Directory.Exists(directory)) throw new ToolException($"Screenshot folder not found: {directory}");
|
||||
var baseline = a.Get("baseline") ?? Path.Combine(Args.RepoRoot(), "tests", "visual", "baselines");
|
||||
switch (verb)
|
||||
{
|
||||
case "diff":
|
||||
var options = new DiffOptions(
|
||||
directory,
|
||||
baseline,
|
||||
a.Get("out") ?? Path.TrimEndingDirectorySeparator(Path.GetFullPath(directory)) + "-diff",
|
||||
ParseTolerance(a.Get("tolerance")),
|
||||
ParseMaxDiff(a.Get("max-diff")));
|
||||
var actions = environment("GITHUB_ACTIONS") == "true";
|
||||
var summary = actions ? environment("GITHUB_STEP_SUMMARY") : null;
|
||||
return Diff(options, output, annotate: actions, stepSummary: summary).Fails ? 1 : 0;
|
||||
case "accept":
|
||||
Directory.CreateDirectory(baseline);
|
||||
var accepted = 0;
|
||||
foreach (var file in Directory.EnumerateFiles(directory, "*.png").Order(StringComparer.Ordinal))
|
||||
{
|
||||
File.Copy(file, Path.Combine(baseline, Path.GetFileName(file)), overwrite: true);
|
||||
accepted++;
|
||||
}
|
||||
output.WriteLine($"{accepted} screenshot(s) copied to {baseline}");
|
||||
return 0;
|
||||
default:
|
||||
throw new ToolException($"Unknown screenshots verb '{verb}' (use diff or accept).");
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed record DiffOptions(string Directory, string Baseline, string Output, int Tolerance, double MaxDiff);
|
||||
|
||||
internal enum Verdict
|
||||
{
|
||||
/// <summary>No pixel beyond the tolerance.</summary>
|
||||
Same,
|
||||
|
||||
/// <summary>Some pixels beyond the tolerance, no more than the allowed fraction.</summary>
|
||||
Within,
|
||||
|
||||
Changed,
|
||||
Resized,
|
||||
Unreadable,
|
||||
|
||||
/// <summary>A screenshot without a baseline while the baseline folder holds none at all: warns only.</summary>
|
||||
New,
|
||||
|
||||
/// <summary>A screenshot without a baseline while other screens have one.</summary>
|
||||
Unbased,
|
||||
|
||||
/// <summary>A baseline whose screen was not captured.</summary>
|
||||
Missing,
|
||||
}
|
||||
|
||||
internal sealed record ScreenResult(string Name, Verdict Verdict, long Differing, long Pixels, string? Detail = null)
|
||||
{
|
||||
public double Fraction => Pixels == 0 ? 0 : (double)Differing / Pixels;
|
||||
public bool Fails => Verdict is Verdict.Changed or Verdict.Resized or Verdict.Unreadable or Verdict.Unbased or Verdict.Missing;
|
||||
}
|
||||
|
||||
/// <param name="Armed">The baseline folder holds at least one PNG: from then on every screen needs its own.</param>
|
||||
internal sealed record DiffReport(IReadOnlyList<ScreenResult> Screens, bool Armed)
|
||||
{
|
||||
public bool Fails => Screens.Any(s => s.Fails);
|
||||
}
|
||||
|
||||
/// <summary>RGBA, 8 bits per channel, top-down.</summary>
|
||||
internal sealed record Picture(int Width, int Height, byte[] Pixels);
|
||||
|
||||
/// <summary>Compares the folders, writes the diff images and <c>summary.md</c>, and reports every screen.</summary>
|
||||
internal static DiffReport Diff(DiffOptions options, TextWriter output, bool annotate, string? stepSummary)
|
||||
{
|
||||
var captured = PngNames(options.Directory);
|
||||
var baselines = Directory.Exists(options.Baseline) ? PngNames(options.Baseline) : new SortedSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
var armed = baselines.Count > 0;
|
||||
// Diffs of an earlier run must not pass for this run's.
|
||||
if (Directory.Exists(options.Output))
|
||||
{
|
||||
foreach (var old in Directory.EnumerateFiles(options.Output, "*-diff.png")) File.Delete(old);
|
||||
}
|
||||
Directory.CreateDirectory(options.Output);
|
||||
|
||||
var results = new List<ScreenResult>();
|
||||
foreach (var name in captured)
|
||||
{
|
||||
if (!baselines.Contains(name))
|
||||
{
|
||||
results.Add(new ScreenResult(name, armed ? Verdict.Unbased : Verdict.New, 0, 0));
|
||||
continue;
|
||||
}
|
||||
results.Add(CompareFiles(name, Path.Combine(options.Baseline, name), Path.Combine(options.Directory, name), options));
|
||||
}
|
||||
results.AddRange(baselines.Where(b => !captured.Contains(b)).Select(b => new ScreenResult(b, Verdict.Missing, 0, 0)));
|
||||
results.Sort((x, y) => StringComparer.OrdinalIgnoreCase.Compare(x.Name, y.Name));
|
||||
|
||||
foreach (var r in results) output.WriteLine($"{Label(r.Verdict),-11} {r.Name}{Describe(r, options)}");
|
||||
var report = new DiffReport(results, armed);
|
||||
var markdown = Markdown(report, options);
|
||||
File.WriteAllText(Path.Combine(options.Output, "summary.md"), markdown);
|
||||
if (stepSummary is not null) File.AppendAllText(stepSummary, markdown);
|
||||
output.WriteLine(Totals(report));
|
||||
if (annotate)
|
||||
{
|
||||
foreach (var r in results.Where(r => r.Fails))
|
||||
{
|
||||
var title = r.Verdict == Verdict.Unbased ? "Screenshot without baseline" : "Screenshot " + Label(r.Verdict);
|
||||
output.WriteLine($"::error title={Escape(title, property: true)}::{Escape(r.Name + Describe(r, options), property: false)}");
|
||||
}
|
||||
// One warning for them all (GitHub shows only ten): before the first baselines every screen is new.
|
||||
var unbased = results.Where(r => r.Verdict == Verdict.New).Select(r => r.Name).ToList();
|
||||
if (unbased.Count > 0)
|
||||
{
|
||||
var names = string.Join(", ", unbased.Take(5)) + (unbased.Count > 5 ? ", …" : string.Empty);
|
||||
output.WriteLine($"::warning title={Escape("No screenshot baselines", property: true)}::" +
|
||||
Escape($"{NotArmed} {unbased.Count} screenshot(s) without a baseline ({names}); tests/visual/baselines/README.md says how to add them.", property: false));
|
||||
}
|
||||
}
|
||||
return report;
|
||||
}
|
||||
|
||||
private static ScreenResult CompareFiles(string name, string baselinePath, string capturedPath, DiffOptions options)
|
||||
{
|
||||
Picture expected, actual;
|
||||
try
|
||||
{
|
||||
expected = Read(baselinePath);
|
||||
actual = Read(capturedPath);
|
||||
}
|
||||
catch (InvalidDataException ex)
|
||||
{
|
||||
return new ScreenResult(name, Verdict.Unreadable, 0, 0, ex.Message);
|
||||
}
|
||||
if (expected.Width != actual.Width || expected.Height != actual.Height)
|
||||
return new ScreenResult(name, Verdict.Resized, 0, 0, $"{actual.Width}×{actual.Height}, baseline {expected.Width}×{expected.Height}");
|
||||
|
||||
var (differing, diff) = Compare(expected, actual, options.Tolerance);
|
||||
var pixels = (long)actual.Width * actual.Height;
|
||||
if (differing == 0) return new ScreenResult(name, Verdict.Same, 0, pixels);
|
||||
File.WriteAllBytes(Path.Combine(options.Output, Path.GetFileNameWithoutExtension(name) + "-diff.png"),
|
||||
PngEncoder.Encode(diff, actual.Width, actual.Height, PngColorType.Rgb, level: System.IO.Compression.CompressionLevel.Fastest));
|
||||
var verdict = (double)differing / pixels > options.MaxDiff ? Verdict.Changed : Verdict.Within;
|
||||
return new ScreenResult(name, verdict, differing, pixels);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Counts the pixels where a channel differs by more than <paramref name="tolerance"/> and draws the diff image: the
|
||||
/// baseline as faded grey, differing pixels magenta.
|
||||
/// </summary>
|
||||
internal static (long Differing, byte[] Diff) Compare(Picture expected, Picture actual, int tolerance)
|
||||
{
|
||||
if (expected.Width != actual.Width || expected.Height != actual.Height) throw new ArgumentException("The pictures differ in size.");
|
||||
var count = expected.Width * expected.Height;
|
||||
var diff = new byte[count * 3];
|
||||
long differing = 0;
|
||||
var e = expected.Pixels;
|
||||
var a = actual.Pixels;
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var p = i * 4;
|
||||
var delta = Math.Max(Math.Max(Math.Abs(e[p] - a[p]), Math.Abs(e[p + 1] - a[p + 1])), Math.Max(Math.Abs(e[p + 2] - a[p + 2]), Math.Abs(e[p + 3] - a[p + 3])));
|
||||
var q = i * 3;
|
||||
if (delta > tolerance)
|
||||
{
|
||||
differing++;
|
||||
diff[q] = 255;
|
||||
diff[q + 1] = 0;
|
||||
diff[q + 2] = 255;
|
||||
continue;
|
||||
}
|
||||
// Rec.709 luma of the baseline, over its alpha, squeezed into 112..207 so magenta stands out in light and dark themes.
|
||||
var luma = (e[p] * 54 + e[p + 1] * 183 + e[p + 2] * 19) >> 8;
|
||||
var grey = (byte)(112 + luma * e[p + 3] / 255 * 3 / 8);
|
||||
diff[q] = diff[q + 1] = diff[q + 2] = grey;
|
||||
}
|
||||
return (differing, diff);
|
||||
}
|
||||
|
||||
/// <summary>Decodes a PNG of any colour type and depth to <see cref="Picture"/> (16-bit samples keep their high byte).</summary>
|
||||
internal static Picture Read(string path)
|
||||
{
|
||||
var png = PngDecoder.Decode(File.ReadAllBytes(path));
|
||||
var count = png.Width * png.Height;
|
||||
var rgba = new byte[count * 4];
|
||||
var channels = png.Channels;
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
byte Sample(int c) => png.Pixels8 is { } p8 ? p8[i * channels + c] : (byte)(png.Pixels16![i * channels + c] >> 8);
|
||||
var (r, g, b, alpha) = channels switch
|
||||
{
|
||||
1 => (Sample(0), Sample(0), Sample(0), (byte)255),
|
||||
2 => (Sample(0), Sample(0), Sample(0), Sample(1)),
|
||||
3 => (Sample(0), Sample(1), Sample(2), (byte)255),
|
||||
_ => (Sample(0), Sample(1), Sample(2), Sample(3)),
|
||||
};
|
||||
rgba[i * 4] = r;
|
||||
rgba[i * 4 + 1] = g;
|
||||
rgba[i * 4 + 2] = b;
|
||||
rgba[i * 4 + 3] = alpha;
|
||||
}
|
||||
return new Picture(png.Width, png.Height, rgba);
|
||||
}
|
||||
|
||||
private static SortedSet<string> PngNames(string directory) =>
|
||||
new(Directory.EnumerateFiles(directory, "*.png").Select(f => Path.GetFileName(f)), StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
private static string Label(Verdict verdict) => verdict switch
|
||||
{
|
||||
Verdict.Same => "same",
|
||||
Verdict.Within => "tolerated",
|
||||
Verdict.Changed => "changed",
|
||||
Verdict.Resized => "resized",
|
||||
Verdict.Unreadable => "unreadable",
|
||||
Verdict.New => "new",
|
||||
Verdict.Unbased => "no baseline",
|
||||
_ => "missing",
|
||||
};
|
||||
|
||||
private static string Describe(ScreenResult r, DiffOptions options) => r.Verdict switch
|
||||
{
|
||||
Verdict.Same => string.Empty,
|
||||
Verdict.Within or Verdict.Changed => $": {Percent(r.Fraction)} of the pixels differ ({r.Differing.ToString("N0", CultureInfo.InvariantCulture)}; limit {Percent(options.MaxDiff)})",
|
||||
Verdict.Resized or Verdict.Unreadable => $": {r.Detail}",
|
||||
Verdict.New => ": no baseline",
|
||||
Verdict.Unbased => ": no baseline, while other screens have one (screenshots accept adds it)",
|
||||
_ => ": a baseline without a screenshot",
|
||||
};
|
||||
|
||||
/// <summary>What a run before the first baselines must not hide: it compared nothing, so it could not have failed.</summary>
|
||||
private const string NotArmed = "The baseline folder holds no PNG: nothing was compared, and no visual change can fail this step until baselines are committed.";
|
||||
|
||||
private static string Percent(double fraction) => (fraction * 100).ToString("0.###", CultureInfo.InvariantCulture) + " %";
|
||||
|
||||
private static string Totals(DiffReport report)
|
||||
{
|
||||
int Count(params Verdict[] verdicts) => report.Screens.Count(s => verdicts.Contains(s.Verdict));
|
||||
return $"{Count(Verdict.Same, Verdict.Within, Verdict.Changed, Verdict.Resized, Verdict.Unreadable)} compared: {Count(Verdict.Same, Verdict.Within)} within tolerance, " +
|
||||
$"{Count(Verdict.Changed, Verdict.Resized, Verdict.Unreadable)} changed; {Count(Verdict.New, Verdict.Unbased)} without a baseline; {Count(Verdict.Missing)} missing.";
|
||||
}
|
||||
|
||||
private static string Markdown(DiffReport report, DiffOptions options)
|
||||
{
|
||||
var md = new StringBuilder();
|
||||
md.AppendLine("### Screenshot diff").AppendLine();
|
||||
md.AppendLine(CultureInfo.InvariantCulture, $"Tolerance {options.Tolerance} of 255 per channel, at most {Percent(options.MaxDiff)} of the pixels.").AppendLine();
|
||||
if (!report.Armed) md.AppendLine(CultureInfo.InvariantCulture, $"**{NotArmed}**").AppendLine();
|
||||
var listed = report.Screens.Where(s => s.Verdict != Verdict.Same).ToList();
|
||||
if (listed.Count > 0)
|
||||
{
|
||||
md.AppendLine("| Screen | Result | Detail |").AppendLine("|---|---|---|");
|
||||
foreach (var r in listed) md.AppendLine(CultureInfo.InvariantCulture, $"| {r.Name} | {Label(r.Verdict)} | {Describe(r, options).TrimStart(':', ' ')} |");
|
||||
md.AppendLine();
|
||||
}
|
||||
md.AppendLine(Totals(report)).AppendLine();
|
||||
return md.ToString();
|
||||
}
|
||||
|
||||
private static int ParseTolerance(string? value) =>
|
||||
value is null ? DefaultTolerance
|
||||
: int.TryParse(value, NumberStyles.None, CultureInfo.InvariantCulture, out var t) && t is >= 0 and <= 255 ? t
|
||||
: throw new ToolException($"--tolerance takes 0 to 255, not '{value}'.");
|
||||
|
||||
private static double ParseMaxDiff(string? value) =>
|
||||
value is null ? DefaultMaxDiff
|
||||
: double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out var f) && f is >= 0 and <= 1 ? f
|
||||
: throw new ToolException($"--max-diff takes a fraction from 0 to 1, not '{value}'.");
|
||||
|
||||
/// <summary>Workflow-command escaping (properties also escape ':' and ',').</summary>
|
||||
private static string Escape(string value, bool property)
|
||||
{
|
||||
var escaped = value.Replace("%", "%25", StringComparison.Ordinal).Replace("\r", "%0D", StringComparison.Ordinal).Replace("\n", "%0A", StringComparison.Ordinal);
|
||||
return property ? escaped.Replace(":", "%3A", StringComparison.Ordinal).Replace(",", "%2C", StringComparison.Ordinal) : escaped;
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ var commands = new ICommand[]
|
||||
new SelfTestCommand(),
|
||||
new DiagnoseCommand(),
|
||||
new FixturesCommand(),
|
||||
new ScreenshotsCommand(),
|
||||
};
|
||||
|
||||
if (args.Length == 0 || args[0] is "-h" or "--help" or "help")
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace Titano.Ui.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// Whether controls animate, inherited down the visual tree from where it is set. The screenshots (<c>--capture</c>)
|
||||
/// turn it off on their windows: an indeterminate bar then stands still, so a screen draws the same pixels every run
|
||||
/// and the pixel diff against its baseline compares like with like.
|
||||
/// </summary>
|
||||
public static class Motion
|
||||
{
|
||||
public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached(
|
||||
"IsEnabled", typeof(bool), typeof(Motion), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.Inherits));
|
||||
|
||||
public static bool GetIsEnabled(DependencyObject d) => (bool)d.GetValue(IsEnabledProperty);
|
||||
|
||||
public static void SetIsEnabled(DependencyObject d, bool value) => d.SetValue(IsEnabledProperty, value);
|
||||
}
|
||||
@@ -611,7 +611,8 @@
|
||||
<Rectangle.Fill>
|
||||
<LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
|
||||
<LinearGradientBrush.RelativeTransform>
|
||||
<TranslateTransform x:Name="SweepShift" X="-1" />
|
||||
<!-- In the middle while it stands still (Motion off); the sweep animation drives it otherwise. -->
|
||||
<TranslateTransform x:Name="SweepShift" X="0" />
|
||||
</LinearGradientBrush.RelativeTransform>
|
||||
<GradientStop Color="Transparent" Offset="0.3" />
|
||||
<GradientStop Color="{DynamicResource Color.Accent}" Offset="0.5" />
|
||||
@@ -625,17 +626,23 @@
|
||||
<Trigger Property="IsIndeterminate" Value="True">
|
||||
<Setter TargetName="PART_Indicator" Property="Visibility" Value="Collapsed" />
|
||||
<Setter TargetName="Sweep" Property="Visibility" Value="Visible" />
|
||||
<Trigger.EnterActions>
|
||||
</Trigger>
|
||||
<MultiTrigger>
|
||||
<MultiTrigger.Conditions>
|
||||
<Condition Property="IsIndeterminate" Value="True" />
|
||||
<Condition Property="ui:Motion.IsEnabled" Value="True" />
|
||||
</MultiTrigger.Conditions>
|
||||
<MultiTrigger.EnterActions>
|
||||
<BeginStoryboard x:Name="SweepAnimation">
|
||||
<Storyboard RepeatBehavior="Forever">
|
||||
<DoubleAnimation Storyboard.TargetName="SweepShift" Storyboard.TargetProperty="X" From="-0.7" To="0.7" Duration="0:0:1.6" />
|
||||
</Storyboard>
|
||||
</BeginStoryboard>
|
||||
</Trigger.EnterActions>
|
||||
<Trigger.ExitActions>
|
||||
</MultiTrigger.EnterActions>
|
||||
<MultiTrigger.ExitActions>
|
||||
<StopStoryboard BeginStoryboardName="SweepAnimation" />
|
||||
</Trigger.ExitActions>
|
||||
</Trigger>
|
||||
</MultiTrigger.ExitActions>
|
||||
</MultiTrigger>
|
||||
</ControlTemplate.Triggers>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
|
||||
@@ -20,6 +20,9 @@ public sealed class SampleData
|
||||
private const int FileCount = 4_812;
|
||||
private const long FileSize = 20_000_000;
|
||||
|
||||
/// <summary>The sample's "now" (when the failed file failed): fixed, so the screenshots are the same on any day.</summary>
|
||||
private static readonly DateTime Now = new(2026, 9, 28, 19, 2, 0, DateTimeKind.Utc);
|
||||
|
||||
private readonly SampleSession _session = new();
|
||||
private readonly UploadQueue _queue = new();
|
||||
private MainViewModel? _vm;
|
||||
@@ -88,7 +91,7 @@ public sealed class SampleData
|
||||
_queue.Items[2_201].State = UploadItemState.Uploading;
|
||||
_queue.Items[2_201].BytesSent = FileSize * 46 / 100;
|
||||
_queue.Items[2_202].State = UploadItemState.Hashing;
|
||||
_queue.Fail(_queue.Items[2_203], UploadFailure.FileUnreadable, null, DateTime.UtcNow);
|
||||
_queue.Fail(_queue.Items[2_203], UploadFailure.FileUnreadable, null, Now);
|
||||
}
|
||||
|
||||
private sealed class SampleSession : IServerSession
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Threading;
|
||||
using Titano.Core.Diagnostics;
|
||||
using Titano.Ui.Controls;
|
||||
using Titano.Ui.Theming;
|
||||
using Titano.Uploader.DesignTime;
|
||||
|
||||
@@ -11,8 +13,9 @@ namespace Titano.Uploader.Shell;
|
||||
|
||||
/// <summary>
|
||||
/// <c>Titano.Uploader.exe --capture <dir></c>: renders the window at its default 760×700 — first run (connect),
|
||||
/// queue running, settings panel — from <see cref="SampleData"/> in the dark and light themes, then once more in
|
||||
/// Arabic (right to left, dark), and exits.
|
||||
/// queue running, settings panel — from <see cref="SampleData"/> in the dark and light themes, in English whatever the
|
||||
/// computer's language, then once more in Arabic (right to left, dark), and exits. Nothing animates, so the pictures
|
||||
/// compare with their baselines pixel for pixel.
|
||||
/// </summary>
|
||||
public static class ScreenCapture
|
||||
{
|
||||
@@ -20,7 +23,7 @@ public static class ScreenCapture
|
||||
private const double Height = 700;
|
||||
|
||||
/// <param name="directory">Where the PNG files go.</param>
|
||||
/// <param name="applyLanguage">Switches the UI language (a culture name; null = the OS language).</param>
|
||||
/// <param name="applyLanguage">Switches the UI language (a culture name).</param>
|
||||
public static void Run(string directory, Action<string?> applyLanguage)
|
||||
{
|
||||
Directory.CreateDirectory(directory);
|
||||
@@ -28,7 +31,8 @@ public static class ScreenCapture
|
||||
{
|
||||
try
|
||||
{
|
||||
applyLanguage(null);
|
||||
CultureInfo.CurrentCulture = CultureInfo.DefaultThreadCurrentCulture = CultureInfo.GetCultureInfo("en");
|
||||
applyLanguage("en");
|
||||
CaptureScreens(directory, string.Empty, [AppTheme.Dark, AppTheme.Light]);
|
||||
// Mirrored layout with paths, addresses and file names kept left to right (BRIEF §16).
|
||||
applyLanguage("ar");
|
||||
@@ -62,6 +66,7 @@ public static class ScreenCapture
|
||||
ShowActivated = false,
|
||||
ShowInTaskbar = false,
|
||||
};
|
||||
Motion.SetIsEnabled(window, false);
|
||||
window.Show();
|
||||
try
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user