Files
Chronos/tests/Titano.App.Tests/Archive/ArchiveLogicTests.cs
T
Claude f7affe7840 feat(app): import and archive manager logic (plans, card pool, card gate, server sink, tree, charts, reports)
Plain classes behind the Import and Archive screens: plan totals, session
rows and the Show plan listing; duplicate and title choices without a new
scan; folder and server destinations with the protocol client's chunked,
hash-verified uploads; the after-import gate that allows deleting or
formatting a card only after a verified archive import and a dry run; the
archive tree, chart aggregation, mass rename/retag and maintenance reports.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 17:39:03 +00:00

244 lines
12 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 Titano.App.Archiving;
using Titano.Core.Archiving;
using Titano.Core.Imaging;
using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Data.Archiving;
using Titano.Data.Repositories;
using Titano.Testing;
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 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.Equal(12, ArchiveFigures.MonthLabels().Count);
Assert.Equal("J", ArchiveFigures.MonthLabels()[0]);
Assert.Empty(ArchiveFigures.PhotosPerMonth(Stats() with { ByMonth = [] }, DateTime.Today));
}
[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("2024 – 2026", tiles[4].Value);
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);
}
[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);
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(JobState.Done, recent[1].State);
}
}