Files
Chronos/tests/Titano.App.Tests/Archive/ArchiveLogicTests.cs
T
Claude 3241b4710d fix(rtl): the preview's frame data keeps its fields in order; charts keep European digits
The right-to-left separator broke the frame data over the preview, a left-to-right
island: after the localized time ("6:03:41 م") the shutter's number read as part
of the previous field and swapped places with the dot. Fields shown in such an
island now use Loc.IslandSeparator, with left-to-right marks in right-to-left
languages. Charts formatted text with the UI culture's digit substitution, so a
number after Arabic words came out in Arabic-Indic digits there only; they now
draw European digits like every text block. The direction marks in source are
written as escapes instead of invisible characters.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-12 00:16:56 +00:00

380 lines
20 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 System.Globalization;
using System.IO;
using Titano.App.Archiving;
using Titano.App.Infrastructure;
using Titano.App.Library;
using Titano.Core.Archiving;
using Titano.Core.Imaging;
using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Data;
using Titano.Data.Archiving;
using Titano.Data.Repositories;
using Titano.Testing;
using Titano.Ui.Controls;
namespace Titano.App.Tests.Archiving;
public sealed class ArchiveLogicTests
{
private static readonly Core.Model.Archive Main = new() { Id = Guid.CreateVersion7(), Name = "TitanoArchive", RootPath = "/archive" };
public ArchiveLogicTests() => Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
private static Sequence Seq(string title, DateTime? first, int frames = 100, long bytes = 1_000_000, params string[] tags) => new()
{
Id = Guid.CreateVersion7(),
ArchiveId = Main.Id,
Title = title,
Folder = $"{first?.Year.ToString(CultureInfo.InvariantCulture) ?? "misc"}/{title}",
FirstCapture = first,
FrameCount = frames,
TotalBytes = bytes,
Tags = tags,
};
[Test]
public void TheTreeGroupsByYearNewestFirstWithUndatedLast()
{
var a = Seq("Harbour", new DateTime(2026, 9, 28), 3_600, 72_000_000_000);
var b = Seq("Comet", new DateTime(2026, 8, 13));
var c = Seq("Stars", new DateTime(2025, 8, 2));
var d = Seq("Scans", null);
var tree = ArchiveTree.Build(Main, ArchiveAvailability.Online, [c, d, b, a]);
Assert.Equal("TitanoArchive", tree.Root.Title);
Assert.SequenceEqual(new[] { "2026", "2025", "Undated" }, tree.Root.Children.Select(n => n.Title));
Assert.SequenceEqual(new[] { "Harbour", "Comet" }, tree.Root.Children[0].Children.Select(n => n.Title));
Assert.Equal("3,600 photos · 72 GB", tree.Root.Children[0].Children[0].Detail);
Assert.Equal("2 sequences · 72 GB", tree.Root.Children[0].Detail);
// Collapsed years show only themselves; expanding one reveals its sequences.
Assert.Equal(4, tree.Visible().Count);
tree.Root.Children[1].IsExpanded = true;
Assert.SequenceEqual(new[] { "TitanoArchive", "2026", "2025", "Stars", "Undated" }, tree.Visible().Select(n => n.Title));
var expanded = tree.ExpandedKeys();
var rebuilt = ArchiveTree.Build(Main, ArchiveAvailability.ReadOnly, [a, b, c, d]);
rebuilt.Expand(expanded);
Assert.Equal(5, rebuilt.Visible().Count);
Assert.Equal(ArchiveAvailability.ReadOnly, rebuilt.Availability);
}
[Test]
public void TheMassOperationSetIsTheSelectionOrEverything()
{
var a = Seq("Harbour", new DateTime(2026, 9, 28));
var b = Seq("Comet", new DateTime(2026, 8, 13));
var c = Seq("Stars", new DateTime(2025, 8, 2));
var tree = ArchiveTree.Build(Main, ArchiveAvailability.Online, [a, b, c]);
Assert.Equal(3, ArchiveTree.SetOf(tree, []).Count);
var year = tree.Root.Children[0];
Assert.SequenceEqual(new[] { a.Id, b.Id }, ArchiveTree.SetOf(tree, [year]).Select(s => s.Id));
// A year and one of its sequences count that sequence once.
Assert.Equal(2, ArchiveTree.SetOf(tree, [year, year.Children[1]]).Count);
Assert.Equal(3, ArchiveTree.SetOf(tree, [tree.Root]).Count);
}
private static ArchiveStats Stats() => new(
Sequences: 12,
Photos: 10_000,
Bytes: 9_400_000_000_000,
ByYear: [new YearBucket(2026, 8, 6_000, 2_100_000_000_000), new YearBucket(2024, 4, 4_000, 1_400_000_000_000)],
ByMonth: [new MonthBucket(2024, 3, 2, 1_000), new MonthBucket(2026, 7, 5, 5_000), new MonthBucket(2026, 9, 1, 2_000)],
ByCamera:
[
new CameraBucket("Canon", "Canon EOS R5", 4, 4_100, 0),
new CameraBucket("SONY", "ILCE-7M4", 2, 2_200, 0),
new CameraBucket("NIKON CORPORATION", "NIKON Z 8", 2, 1_400, 0),
new CameraBucket("GoPro", "HERO12 Black", 1, 900, 0),
new CameraBucket("Apple", "iPhone 16 Pro", 1, 600, 0),
new CameraBucket("DJI", "Osmo Action 5 Pro", 1, 500, 0),
new CameraBucket(null, null, 1, 300, 0),
],
ByFormat:
[
new FormatBucket(ImageFileFormat.Cr2, 3_800, 0),
new FormatBucket(ImageFileFormat.Arw, 2_100, 0),
new FormatBucket(ImageFileFormat.Jpeg, 2_000, 0),
new FormatBucket(ImageFileFormat.Nef, 1_400, 0),
new FormatBucket(ImageFileFormat.Dng, 400, 0),
new FormatBucket(ImageFileFormat.Heif, 200, 0),
new FormatBucket(ImageFileFormat.ThreeFr, 60, 0),
new FormatBucket(ImageFileFormat.Unknown, 40, 0),
],
Health: new ArchiveHealth(Unverified: 3, WithWarnings: 1, WithErrors: 0, NotAnalyzed: 2, MissingFrames: 0, CorruptFrames: 0, DuplicateFrames: 5));
[Test]
public void TheSetFilterNarrowsByCameraStatusAndTag()
{
var canon = Seq("Harbour", new DateTime(2026, 9, 28), tags: "sunset") with
{
CameraMake = "Canon",
CameraModel = "Canon EOS R5",
Status = SequenceStatus.AnalyzedLight | SequenceStatus.ProxiesBuilt,
};
var sony = Seq("Comet", new DateTime(2026, 8, 13), tags: "night") with { CameraMake = "SONY", CameraModel = "ILCE-7M4" };
var unknown = Seq("Old", null);
Assert.True(new ArchiveSetFilter().IsEmpty);
Assert.True(new ArchiveSetFilter().Matches(unknown), "no filter keeps everything");
var byCamera = new ArchiveSetFilter(Camera: ArchiveSetFilter.CameraOf(canon));
Assert.True(byCamera.Matches(canon));
Assert.False(byCamera.Matches(sony));
Assert.Equal(Loc.L("Archive.Chart.UnknownCamera"), ArchiveSetFilter.CameraOf(unknown));
Assert.True(new ArchiveSetFilter(Status: StatusFilter.NotAnalysed).Matches(sony));
Assert.False(new ArchiveSetFilter(Status: StatusFilter.NotAnalysed).Matches(canon));
Assert.True(new ArchiveSetFilter(Status: StatusFilter.Proxies).Matches(canon));
Assert.True(new ArchiveSetFilter(Tag: "SUNSET").Matches(canon), "tags compare without case");
Assert.False(new ArchiveSetFilter(Tag: "sunset").Matches(sony));
Assert.Equal(ArchiveSetFilter.CameraOf(canon) + " · Not analysed · sunset",
new ArchiveSetFilter(ArchiveSetFilter.CameraOf(canon), StatusFilter.NotAnalysed, "sunset").Describe());
Assert.Equal(string.Empty, new ArchiveSetFilter().Describe());
}
[Test]
public void ChartsAggregateYearsCamerasAndFormats()
{
var stats = Stats();
var years = ArchiveFigures.StorageByYear(stats);
Assert.SequenceEqual(new[] { "2024", "2026" }, years.Select(b => b.Label));
Assert.Equal("2.1 TB", years[1].ValueText);
var cameras = ArchiveFigures.ByCamera(stats);
Assert.Equal(ArchiveFigures.TopCameras + 1, cameras.Count);
Assert.Equal("Canon EOS R5", cameras[0].Label);
Assert.Equal("41 %", cameras[0].ValueText);
Assert.Equal("SONY ILCE-7M4", cameras[1].Label);
Assert.Equal("Nikon Z 8", cameras[2].Label);
Assert.Equal("2 others", cameras[^1].Label);
Assert.Equal(800, cameras[^1].Value);
Assert.Equal(stats.Photos, (long)cameras.Sum(c => c.Value));
var formats = ArchiveFigures.ByFormat(stats);
Assert.SequenceEqual(new[] { "CR2", "ARW", "JPEG", "NEF", "DNG", "HEIF", "Other" }, formats.Select(f => f.Label));
Assert.Equal(100, formats[^1].Value);
Assert.Equal("3FR", ArchiveFigures.FormatName(ImageFileFormat.ThreeFr));
}
[Test]
public void ThePhotosPerMonthHeatmapHasOneRowPerYearAndNoFuture()
{
var rows = ArchiveFigures.PhotosPerMonth(Stats(), new DateTime(2026, 10, 2));
Assert.SequenceEqual(new[] { "2024", "2025", "2026" }, rows.Select(r => r.Label));
Assert.Equal(1_000, rows[0].Values[2]);
Assert.Equal(0, rows[1].Values[5]);
Assert.Equal(5_000, rows[2].Values[6]);
Assert.Equal(0, rows[2].Values[9]);
Assert.True(double.IsNaN(rows[2].Values[10]));
Assert.Null(rows[2].ToolTips[11]);
Assert.Equal("July 2026 · 5,000 photos · 5 sequences", rows[2].ToolTips[6]);
Assert.Empty(ArchiveFigures.PhotosPerMonth(Stats() with { ByMonth = [] }, DateTime.Today));
}
[Test]
public void MonthColumnsAreInitialsOnlyWhereTheyAreLetters()
{
var numbers = Enumerable.Range(1, 12).Select(m => m.ToString(CultureInfo.InvariantCulture)).ToArray();
try
{
Assert.Equal("J F M A M J J A S O N D", string.Join(' ', ArchiveFigures.MonthLabels()));
Loc.SetCulture(CultureInfo.GetCultureInfo("it"));
Assert.Equal("G F M A M G L A S O N D", string.Join(' ', ArchiveFigures.MonthLabels()));
// Numbered abbreviations ("10月", "Tháng 10") would read "1" for October to December, or "T" for every month.
foreach (var culture in new[] { "ja", "ko", "zh-Hans", "zh-Hant", "vi", "th", "ar", "he" })
{
Loc.SetCulture(CultureInfo.GetCultureInfo(culture));
Assert.SequenceEqual(numbers, ArchiveFigures.MonthLabels(), culture);
}
}
finally
{
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
}
}
[Test]
public void TilesAndHealthSummariseTheIndex()
{
var tiles = ArchiveFigures.Tiles(Stats());
Assert.Equal(5, tiles.Count);
Assert.Equal("10,000", tiles[0].Value);
Assert.Equal("9.4 TB", tiles[2].Value);
Assert.Equal("6", tiles[3].Value);
Assert.Equal("cameras · 8 formats", tiles[3].Caption);
Assert.Equal(Formatting.LeftToRight("2024 – 2026"), tiles[4].Value, "a year range keeps its order in right-to-left text");
var health = ArchiveFigures.Health(Stats(), withoutProxies: 0);
Assert.Equal(HealthAction.Verify, health[0].Action);
Assert.False(health[0].IsOk);
Assert.Equal("3 sequences unverified", health[0].Text);
Assert.True(health[1].IsOk);
Assert.True(health[2].IsOk);
Assert.Equal(HealthAction.Analyse, health[3].Action);
Assert.Equal(HealthAction.Duplicates, health[4].Action);
Assert.Equal("5 duplicate frames", health[4].Text);
}
[Test]
public void HealthDuplicatesFollowTheLastDuplicatesSearch()
{
var unflagged = Stats() with { Health = Stats().Health with { DuplicateFrames = 0 } };
var before = ArchiveFigures.Health(unflagged, withoutProxies: 0)[4];
Assert.True(before.IsOk);
Assert.Equal("No copies flagged", before.Text, "before a search the line says what it measures");
var found = ArchiveFigures.Health(unflagged, withoutProxies: 0, duplicateGroups: 1)[4];
Assert.False(found.IsOk, "never a tick while the Duplicates card lists groups");
Assert.Equal("1 duplicate group", found.Text);
Assert.Equal(HealthAction.MarkDuplicates, found.Action);
var none = ArchiveFigures.Health(Stats(), withoutProxies: 0, duplicateGroups: 0)[4];
Assert.True(none.IsOk);
Assert.Equal(HealthAction.None, none.Action);
}
[Test]
public void FailuresAreOneLocalizedSentenceByCause()
{
Assert.Equal("The archive " + Formatting.LeftToRight("/archive") + " is offline.",
ArchiveFailures.Cause(new ArchiveOfflineException("/archive", ArchiveAvailability.Offline)));
Assert.Equal("The archive " + Formatting.LeftToRight("/archive") + " is read-only.",
ArchiveFailures.Cause(new ArchiveOfflineException("/archive", ArchiveAvailability.ReadOnly)));
Assert.Equal("Titano was denied access to the archive.", ArchiveFailures.Cause(new UnauthorizedAccessException("Access to '/x' is denied.")));
Assert.Equal("The index could not be read or written.", ArchiveFailures.Cause(new DatabaseConnectionException("socket closed")));
Assert.Equal("The sequence is no longer in the index.", ArchiveFailures.Cause(new ArgumentException("Unknown sequence 42.")));
Assert.Equal("A file of the archive could not be read or written.", ArchiveFailures.Cause(new IOException("The device is not ready.")));
Assert.Equal(Loc.L("Archive.Error.StalePlan"), ArchiveFailures.Cause(new StaleRenamePlanException(Guid.NewGuid())));
}
[Test]
public void TreeRowsShowTheirWholeTitleAndTheKeysOfTheReadingDirection()
{
var harbour = Seq("Construction cam · weekly", new DateTime(2026, 9, 28), 3_600, 72_000_000_000);
var tree = ArchiveTree.Build(Main, ArchiveAvailability.Online, [harbour]);
var year = new ArchiveTreeRowViewModel(tree.Root.Children[0], ArchiveAvailability.Online, _ => { });
var sequence = new ArchiveTreeRowViewModel(tree.Root.Children[0].Children[0], ArchiveAvailability.Online, _ => { });
Assert.Equal("Construction cam · weekly · 3,600 photos · 72 GB", sequence.RowToolTip);
Assert.Equal("2026 · 1 sequence · 72 GB · Right arrow opens the year, Left arrow closes it", year.RowToolTip);
try
{
Loc.SetCulture(CultureInfo.GetCultureInfo("he"));
Assert.Equal(Loc.L("Tooltips.Archive.YearRowRtl", "2026" + Loc.Separator + year.Detail), year.RowToolTip, "the arrow keys of a mirrored tree");
Assert.Contains("\u200F", year.RowToolTip, "right-to-left marks keep the year and its figures apart");
}
finally
{
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
}
}
[Test]
public void RenameTemplatesAreCheckedAndThePlanListedByMode()
{
Assert.Null(MassRename.Validate(RenameMode.Titles, "{title} · {camera}"));
Assert.NotNull(MassRename.Validate(RenameMode.Titles, "{title"));
Assert.NotNull(MassRename.Validate(RenameMode.Titles, " "));
Assert.Null(MassRename.Validate(RenameMode.Folders, "{year}/{date} {title}"));
Assert.NotNull(MassRename.Validate(RenameMode.Folders, "{nope}"));
var a = Seq("Harbour", new DateTime(2026, 9, 28));
var b = Seq("Comet", new DateTime(2026, 8, 13));
var subjects = new[] { a, b }.Select(s => new RenameSubject(s.Id, s.Folder, s.Title, s.FirstCapture, "Canon", "Canon EOS R5", null)).ToList();
var titles = RenamePlanner.Plan(Main.Id, subjects, null, "{title} · {camera}", subjects.Select(s => s.Folder), _ => false);
var rows = MassRename.Rows(titles, RenameMode.Titles);
Assert.Equal("Harbour", rows[0].From);
Assert.Equal("Harbour · Canon EOS R5", rows[0].To);
Assert.Equal("2 of 5 change", MassRename.Summary(titles, 5));
Assert.Empty(RenamePlanner.Plan(Main.Id, subjects, "{year}/{title}", null, subjects.Select(s => s.Folder), _ => false).Steps);
var folders = RenamePlanner.Plan(Main.Id, subjects, "{year}/{date} {title}", null, subjects.Select(s => s.Folder), _ => false);
var moves = MassRename.Rows(folders, RenameMode.Folders);
Assert.SequenceEqual(new[] { "2026/Harbour", "2026/Comet" }, moves.Select(r => r.From));
Assert.SequenceEqual(new[] { "2026/2026-09-28 Harbour", "2026/2026-08-13 Comet" }, moves.Select(r => r.To));
}
[Test]
public void RetagAddsAndRemovesWithoutDuplicates()
{
Assert.SequenceEqual(new[] { "sunset", "harbour", "Holy grail" }, Retag.Parse(" sunset, harbour; Holy grail ,SUNSET"));
Assert.Empty(Retag.Parse(" "));
Assert.Null(Retag.Validate("sunset, dusk"));
Assert.NotNull(Retag.Validate(new string('x', 65)));
Assert.Empty(Retag.Parse(new string('x', 65)));
Assert.SequenceEqual(new[] { "harbour", "dusk" }, Retag.Apply(["Sunset", "harbour"], ["dusk", "HARBOUR"], ["sunset"]));
var a = Seq("A", DateTime.Today, tags: ["sunset"]);
var b = Seq("B", DateTime.Today, tags: ["dusk"]);
var changes = Retag.Changes([a, b], ["dusk"], []);
Assert.Equal(a.Id, changes.Single().Sequence.Id);
Assert.SequenceEqual(new[] { "sunset", "dusk" }, changes.Single().Tags);
}
[Test]
public void ReportsListDuplicatesOrphansAndRecentMaintenance()
{
var a = Seq("Harbour", new DateTime(2026, 9, 28));
var b = Seq("Comet", new DateTime(2026, 8, 13));
var locations = new[] { new SequenceLocation(a.Id, a.Folder, a.Title), new SequenceLocation(b.Id, b.Folder, null) };
var groups = new[]
{
new DuplicateGroup("ab", [new DuplicateFrame("ab", Guid.NewGuid(), a.Id, 0, a.Folder + "/originals/IMG_1.CR2"),
new DuplicateFrame("ab", Guid.NewGuid(), b.Id, 4, b.Folder + "/originals/IMG_9.CR2")]),
new DuplicateGroup("cd", [new DuplicateFrame("cd", Guid.NewGuid(), a.Id, 1, a.Folder + "/originals/IMG_2.CR2")]),
};
var duplicates = MaintenanceReports.Duplicates(groups, locations);
Assert.Equal("IMG_1.CR2", duplicates.Single().Title);
Assert.Equal("2 copies", duplicates.Single().Detail);
Assert.Equal(a.Id, duplicates.Single().SequenceId);
Assert.Contains("Comet", duplicates.Single().Places[1]);
var report = new OrphanReport(
[new SequenceOrphans(a.Folder, ["IMG_7.CR2", "IMG_8.CR2"], [])],
[new SequenceLocation(Guid.NewGuid(), "2024/Gone", "Gone")],
["2026/New shoot"]);
var orphans = MaintenanceReports.Orphans(report, locations);
Assert.SequenceEqual(new[] { OrphanKind.UnlistedFiles, OrphanKind.MissingSequence, OrphanKind.NewFolder }, orphans.Select(o => o.Kind));
Assert.Equal("2 photos not in sidecar", orphans[0].Detail);
Assert.Equal(a.Id, orphans[0].SequenceId);
Assert.Null(orphans[2].SequenceId);
// Folders stay left to right inside right-to-left text.
Assert.Equal("No sidecar in " + Formatting.LeftToRight("2024/Gone"), orphans[1].Detail);
Assert.Equal(Formatting.LeftToRight("2026/New shoot"), orphans[2].Title);
Assert.True(duplicates.Single().Places[1].EndsWith(Formatting.LeftToRight(b.Folder + "/originals/IMG_9.CR2"), StringComparison.Ordinal),
"a duplicate's path stays left to right");
var now = DateTimeOffset.UtcNow;
var jobs = new[]
{
new Job { Id = Guid.NewGuid(), Kind = JobKind.Verify, State = JobState.Running, ProgressDone = 71, ProgressTotal = 100, TargetLabel = "TitanoArchive", Created = now },
new Job { Id = Guid.NewGuid(), Kind = JobKind.Scan, State = JobState.Done, Created = now.AddDays(-2), Finished = now.AddDays(-2) },
};
var recent = MaintenanceReports.Recent(jobs, 5);
Assert.Equal("Verify · TitanoArchive", recent[0].Text);
Assert.Equal("71 %", recent[0].StateText);
Assert.Equal(StatusKind.Running, recent[0].StateKind);
Assert.Equal(JobState.Done, recent[1].State);
Assert.Equal(StatusKind.Done, recent[1].StateKind, "a finished run is a green dot, a failed one a red cross");
}
[Test]
public void TheScreensWorkOnTheRememberedArchiveElseTheFirst()
{
var other = Main with { Id = Guid.CreateVersion7(), Name = "Travel", RootPath = "/media/travel" };
Assert.Null(CurrentArchive.Pick([], "/archive"));
Assert.Equal(other.Id, CurrentArchive.Pick([Main, other], "/media/travel/")!.Id);
Assert.Equal(Main.Id, CurrentArchive.Pick([Main, other], "/elsewhere")!.Id);
Assert.Equal(Main.Id, CurrentArchive.Pick([Main, other], null)!.Id);
}
[Test]
public void TheSampleReportsFillEveryKindOfReportRow()
{
var locations = Enumerable.Range(1, 6).Select(i => new SequenceLocation(Guid.CreateVersion7(), $"2026/Shoot {i}", $"Shoot {i}")).ToList();
Assert.Equal("2 copies", MaintenanceReports.Duplicates(SampleReports.Duplicates(locations), locations).Single().Detail);
Assert.SequenceEqual(new[] { OrphanKind.UnlistedFiles, OrphanKind.MissingSequence, OrphanKind.NewFolder },
MaintenanceReports.Orphans(SampleReports.Orphans(locations), locations).Select(o => o.Kind));
var folders = SampleReports.Folders(locations);
Assert.Equal(4, folders.Count(f => f.Sidecar is not null));
Assert.Equal(2, folders.Count(f => f.Sidecar is null));
}
}