Files
Chronos/tests/Chronos.Core.Tests/Import/ImportPlannerTests.cs
T
Alby96andClaude Opus 5.5 1f089888e8
CI / Build and test (push) Successful in 5m11s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 6s
Release / Build, test and publish (push) Successful in 8m57s
CI / PostgreSQL 16 integration (push) Successful in 3m10s
CI / PostgreSQL 17 integration (push) Successful in 3m30s
CI / Docker image (push) Successful in 49s
Release / Server image (push) Successful in 1m4s
feat: owner review 2 phase 1: :latest, Unraid array space, Settings with About, whole-folder import (#9)
- :latest follows the highest stable release, or the highest pre-release while none is stable (Chronos.Tools release-channels); bad tags are refused before anything is built.
- Unraid: CHRONOS_SPACE_PATH and the template's Array view show the array's free space; the write volume (cache) is shown apart; completions check the archive volume (507).
- Settings is a gear, docked at the foot of the admin sidebar; About is the last part of Settings on the desktop and the panel.
- Import > Folder > Whole folder: one sequence per format (desktop only).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-27 01:09:59 +01:00

299 lines
17 KiB
C#

using Chronos.Core.Archiving;
using Chronos.Core.Imaging;
using Chronos.Core.Import;
using Chronos.Core.Localization;
using Chronos.Core.Metadata;
using Chronos.Core.Tests.Archiving;
using Chronos.Testing;
using static Chronos.Core.Tests.Import.ImportTestKit;
namespace Chronos.Core.Tests.Import;
[Category("import")]
public sealed class ImportPlannerTests(TestContext context)
{
private static readonly TimeSpan Interval = TimeSpan.FromSeconds(10);
[Test]
public async Task ADcimCardIsPlannedWithTheDefaultTemplates()
{
var card = context.CreateTempDirectory("card");
TestPhotos.WriteSeries(Path.Combine(card, "DCIM", "100CANON"), Start, 10, Interval);
File.WriteAllBytes(Path.Combine(card, "DCIM", "100CANON", "._IMG_0001.JPG"), [0, 5, 22]);
File.WriteAllText(Path.Combine(card, "DCIM", "100CANON", "notes.txt"), "x");
TestPhotos.Write(Path.Combine(card, "MISC", "IMG_9999.JPG"), Start, 99);
var plan = await PlanAsync(card, context.CreateTempDirectory("archive"));
var session = plan.Sessions.Single();
Assert.Equal("2026/2026-09-28 Canon EOS R5", session.Folder);
Assert.Equal(10, plan.FileCount);
Assert.Equal(10, plan.CopyCount);
Assert.SequenceEqual(
["2026-09-28_18-00-00_0001.JPG", "2026-09-28_18-00-10_0002.JPG", "2026-09-28_18-00-20_0003.JPG"],
session.Items.Take(3).Select(i => i.FileName));
Assert.Equal("DCIM/100CANON/IMG_0001.JPG", session.Items[0].SourcePath);
Assert.Equal(plan.Items.Sum(i => i.Size), plan.CopyBytes);
Assert.NotNull(plan.AvailableBytes);
Assert.True(plan.HasEnoughSpace);
Assert.Empty(plan.Problems);
}
[Test]
public void AFileThatCannotBeReadIsAProblemWithOneLocalizedSentence()
{
var card = context.CreateTempDirectory("card");
TestPhotos.WriteSeries(Path.Combine(card, "DCIM", "100CANON"), Start, 3, Interval);
var files = CardScanner.Find(card, includeSubfolders: true);
// A photo leaves the card between listing and reading; another name is a folder the system refuses to read as a file.
File.Delete(Path.Combine(card, files[1].RelativePath));
Directory.CreateDirectory(Path.Combine(card, "DCIM", "100CANON", "IMG_9999.JPG"));
var problems = new List<ImportProblem>();
var photos = CardScanner.ReadMetadata(card, [.. files, new CardFile("DCIM/100CANON/IMG_9999.JPG", 0, Start)], problems);
Assert.Equal(2, photos.Count);
Assert.SequenceEqual([files[1].RelativePath, "DCIM/100CANON/IMG_9999.JPG"], problems.Select(p => p.RelativePath));
Assert.Equal(Loc.L("Import.Failed.Photo"), problems[0].Message);
Assert.Contains(problems[1].Message, new[] { Loc.L("Import.Failed.Photo"), Loc.L("Import.Failed.PhotoAccess") });
Assert.False(problems.Any(p => p.Message.Contains(card, StringComparison.Ordinal)), "the system's text, with the card's path, stays in the log");
}
[Test]
public async Task AFlatFolderIncludesSubfoldersOnlyWhenAsked()
{
var card = context.CreateTempDirectory("card");
TestPhotos.WriteSeries(card, Start, 8, Interval);
TestPhotos.WriteSeries(Path.Combine(card, "more"), Start.AddSeconds(80), 8, Interval, firstSeed: 50, firstNumber: 9);
var destination = context.CreateTempDirectory("archive");
Assert.Equal(16, (await PlanAsync(card, destination)).FileCount);
Assert.Equal(8, (await PlanAsync(card, destination, new ImportOptions { IncludeSubfolders = false })).FileCount);
}
[Test]
public async Task SessionsAreSplitPerCameraBodyAndNumberedFromOne()
{
var card = context.CreateTempDirectory("card");
var dcim = Path.Combine(card, "DCIM", "100MEDIA");
TestPhotos.WriteSeries(dcim, Start, 10, Interval, prefix: "CAN_");
TestPhotos.WriteSeries(dcim, Start.AddHours(2), 10, Interval, firstSeed: 20, prefix: "CAN_", firstNumber: 11);
TestPhotos.WriteSeries(dcim, Start.AddSeconds(5), 9, Interval, firstSeed: 40, prefix: "NIK_", make: "NIKON CORPORATION", model: "NIKON Z 8");
var plan = await PlanAsync(card, context.CreateTempDirectory("archive"));
Assert.SequenceEqual(
["2026/2026-09-28 Canon EOS R5", "2026/2026-09-28 Nikon Z 8", "2026/2026-09-28 Canon EOS R5 (2)"],
plan.Sessions.Select(s => s.Folder));
Assert.SequenceEqual([1, 2, 3], plan.Sessions.Select(s => s.Number));
Assert.Equal("2026-09-28_20-00-00_0001.JPG", plan.Sessions[2].Items[0].FileName);
Assert.Equal(3, plan.Sessions.Select(s => s.SessionKey).Distinct().Count());
Assert.True(plan.Sessions.All(s => s.Items.All(i => i.SourcePath.Contains(s.CameraModel == "NIKON Z 8" ? "NIK_" : "CAN_"))));
}
[Test]
public async Task NamesTakenTwiceGetASuffix()
{
var card = context.CreateTempDirectory("card");
TestPhotos.WriteSeries(card, Start, 3, Interval);
var plan = await PlanAsync(card, context.CreateTempDirectory("archive"), new ImportOptions { FileTemplate = "{date}", MinimumSessionFrames = 1 });
Assert.SequenceEqual(["2026-09-28.JPG", "2026-09-28_2.JPG", "2026-09-28_3.JPG"], plan.Sessions[0].Items.Select(i => i.FileName));
}
[Test]
public async Task ImportedAndArchivedPhotosAreSkippedAfterAHashMatch()
{
var card = context.CreateTempDirectory("card");
var files = TestPhotos.WriteSeries(card, Start, 10, Interval);
var lookup = new FakeLookup();
lookup.Imported.Add("IMG_0001.JPG");
var archived = await ContentHash.Sha256Async(files[1]);
lookup.Archive.Add((new FileInfo(files[1]).Length, Start + Interval, new ArchiveMatch(Guid.NewGuid(), archived)));
lookup.Archive.Add((new FileInfo(files[2]).Length, Start + 2 * Interval, new ArchiveMatch(Guid.NewGuid(), new string('0', 64))));
var plan = await PlanAsync(card, context.CreateTempDirectory("archive"), lookup: lookup);
var items = plan.Sessions[0].Items;
Assert.Equal(ImportAction.SkipImported, items[0].Action);
Assert.Equal(ImportAction.SkipInArchive, items[1].Action);
Assert.Equal(lookup.Archive[0].Match.FrameId, items[1].ExistingFrameId);
Assert.Equal(ImportAction.Copy, items[2].Action);
Assert.Equal(await ContentHash.Sha256Async(files[2]), items[2].Sha256);
Assert.Null(items[3].Sha256);
Assert.Equal(8, plan.CopyCount);
Assert.Equal("2026-09-28_18-00-10_0002.JPG", items[1].FileName);
var again = await PlanAsync(card, context.CreateTempDirectory("archive"), new ImportOptions { Duplicates = DuplicatePolicy.ImportAgain }, lookup);
Assert.Equal(ImportAction.Copy, again.Sessions[0].Items[1].Action);
Assert.Equal(lookup.Archive[0].Match.FrameId, again.Sessions[0].Items[1].ExistingFrameId);
}
[Test]
public async Task ReplanningGivesStableKeysThatTitlesAndEarlierFoldersAttachTo()
{
var card = context.CreateTempDirectory("card");
TestPhotos.WriteSeries(card, Start, 10, Interval);
var destination = context.CreateTempDirectory("archive");
var first = await PlanAsync(card, destination);
var key = first.Sessions[0].SessionKey;
Assert.Equal(key, (await PlanAsync(card, destination)).Sessions[0].SessionKey);
var titled = await PlanAsync(card, destination, new ImportOptions
{
FolderTemplate = "{year}/{date} {title}",
Titles = new Dictionary<string, string> { [key] = "Harbour sunset" },
});
Assert.Equal("2026/2026-09-28 Harbour sunset", titled.Sessions[0].Folder);
Assert.Equal("Harbour sunset", titled.Sessions[0].Title);
var lookup = new FakeLookup();
lookup.SessionFolders[key] = "2026/earlier import";
Assert.Equal("2026/earlier import", (await PlanAsync(card, destination, lookup: lookup)).Sessions[0].Folder);
var taken = new FakeLookup();
taken.TakenFolders.Add("2026/2026-09-28 Canon EOS R5");
Assert.Equal("2026/2026-09-28 Canon EOS R5 (2)", (await PlanAsync(card, destination, lookup: taken)).Sessions[0].Folder);
}
[Test]
public async Task UserMergeAndSplitReshapeSessions()
{
var card = context.CreateTempDirectory("card");
TestPhotos.WriteSeries(card, Start, 10, Interval);
TestPhotos.WriteSeries(card, Start.AddHours(3), 10, Interval, firstSeed: 30, firstNumber: 11);
var destination = context.CreateTempDirectory("archive");
Assert.Equal(2, (await PlanAsync(card, destination)).Sessions.Count);
var merged = await PlanAsync(card, destination, new ImportOptions { MergeAt = new HashSet<string> { "IMG_0011.JPG" } });
Assert.Equal(20, merged.Sessions.Single().Items.Count);
Assert.Equal("2026-09-28_21-00-00_0011.JPG", merged.Sessions[0].Items[10].FileName);
var split = await PlanAsync(card, destination, new ImportOptions { SplitBefore = new HashSet<string> { "IMG_0006.JPG" } });
Assert.SequenceEqual([5, 5, 10], split.Sessions.Select(s => s.Items.Count));
}
[Test]
public async Task AWholeFolderIsOneSequenceWhateverTheCameraBodiesAndPauses()
{
var folder = Path.Combine(context.CreateTempDirectory("source"), "Etna eruption");
TestPhotos.WriteSeries(folder, Start, 10, Interval, prefix: "CAN_");
TestPhotos.WriteSeries(folder, Start.AddHours(3), 10, Interval, firstSeed: 20, prefix: "CAN_", firstNumber: 11);
TestPhotos.WriteSeries(folder, Start.AddSeconds(5), 3, Interval, firstSeed: 40, prefix: "NIK_", make: "NIKON CORPORATION", model: "NIKON Z 8");
var destination = context.CreateTempDirectory("archive");
Assert.Equal(3, (await PlanAsync(folder, destination)).Sessions.Count, "by sessions: two Canon sessions and the Nikon");
var plan = await PlanAsync(folder, destination, new ImportOptions { Grouping = ImportGrouping.WholeFolder, FolderTemplate = "{year}/{title}" });
Assert.Equal(ImportGrouping.WholeFolder, plan.Grouping);
var session = plan.Sessions.Single();
Assert.Equal(23, session.Items.Count);
Assert.Equal(2, session.Cameras);
Assert.Equal("Etna eruption", session.Title, "the folder's name is the default title");
Assert.Equal("Etna eruption", session.DefaultTitle);
Assert.Equal("2026/Etna eruption", session.Folder);
Assert.SequenceEqual(session.Items.OrderBy(i => i.CapturedAt).Select(i => i.SourcePath), session.Items.Select(i => i.SourcePath),
"both bodies in capture order");
Assert.Equal("2026-09-28_21-01-30_0023.JPG", session.Items[^1].FileName);
}
[Test]
public async Task AWholeFolderOfRawAndJpegIsTwoSequencesSoNoFrameIsDoubled()
{
var photos = new List<CardPhoto>();
for (var i = 0; i < 6; i++)
{
// An hour between shots: far beyond the session gap, ignored for a whole folder.
var at = Start.AddHours(i);
photos.Add(Photo($"IMG_{i:0000}.JPG", at, ImageFileFormat.Jpeg));
photos.Add(Photo($"IMG_{i:0000}.CR3", at, ImageFileFormat.Cr3));
}
var plan = await ImportPlanner.PlanAsync(context.CreateTempDirectory("raw+jpeg"), context.CreateTempDirectory("archive"), photos, [],
new ImportOptions { Grouping = ImportGrouping.WholeFolder }, new FakeLookup());
Assert.Equal(2, plan.Sessions.Count, "one sequence per format");
Assert.SequenceEqual([ImageFileFormat.Jpeg, ImageFileFormat.Cr3], plan.Sessions.Select(s => s.Format).OrderBy(f => f));
foreach (var session in plan.Sessions)
{
Assert.Equal(6, session.Items.Count);
Assert.Equal(1, session.Items.Select(i => Path.GetExtension(i.SourcePath)).Distinct().Count(), "one format per sequence");
}
Assert.Equal(12, plan.Items.Select(i => i.SourcePath).Distinct().Count(), "every photo is planned once");
Assert.NotEqual(plan.Sessions[0].SessionKey, plan.Sessions[1].SessionKey);
Assert.NotEqual(plan.Sessions[0].Folder, plan.Sessions[1].Folder);
}
[Test]
public async Task AWholeFolderTakesItsSubfoldersOnlyWhenAsked()
{
var folder = Path.Combine(context.CreateTempDirectory("source"), "Construction");
TestPhotos.WriteSeries(folder, Start, 4, Interval);
TestPhotos.WriteSeries(Path.Combine(folder, "week 2"), Start.AddDays(7), 4, Interval, firstSeed: 50, firstNumber: 5);
TestPhotos.WriteSeries(Path.Combine(folder, "week 2", "monday"), Start.AddDays(8), 4, Interval, firstSeed: 70, firstNumber: 9);
var destination = context.CreateTempDirectory("archive");
var whole = new ImportOptions { Grouping = ImportGrouping.WholeFolder };
var all = (await PlanAsync(folder, destination, whole)).Sessions.Single();
Assert.Equal(12, all.Items.Count);
Assert.Equal("week 2/monday/IMG_0012.JPG", all.Items[^1].SourcePath);
var top = (await PlanAsync(folder, destination, whole with { IncludeSubfolders = false })).Sessions.Single();
Assert.Equal(4, top.Items.Count);
Assert.Equal(all.SessionKey, top.SessionKey, "the same folder's sequence, whatever is scanned in it");
}
[Test]
public async Task AWholeFolderKeepsItsKeySoAnInterruptedImportResumesIntoItsSequence()
{
var folder = Path.Combine(context.CreateTempDirectory("source"), "Etna");
TestPhotos.WriteSeries(folder, Start, 10, Interval);
var destination = context.CreateTempDirectory("archive");
var whole = new ImportOptions { Grouping = ImportGrouping.WholeFolder };
var first = (await PlanAsync(folder, destination, whole)).Sessions.Single();
Assert.True(first.SessionKey.StartsWith("folder-", StringComparison.Ordinal), first.SessionKey);
Assert.Equal(first.SessionKey, ImportPlanner.FolderKey(folder + Path.DirectorySeparatorChar, ImageFileFormat.Jpeg),
"a trailing separator names the same folder");
Assert.NotEqual(first.SessionKey, ImportPlanner.FolderKey(folder, ImageFileFormat.Cr3));
Assert.NotEqual(first.SessionKey, ImportPlanner.FolderKey(Path.Combine(folder, "other"), ImageFileFormat.Jpeg));
// Photos added later, earlier ones too, keep the folder's key.
TestPhotos.WriteSeries(folder, Start.AddDays(-1), 2, Interval, firstSeed: 90, prefix: "OLD_");
Assert.Equal(first.SessionKey, (await PlanAsync(folder, destination, whole)).Sessions.Single().SessionKey);
var lookup = new FakeLookup();
lookup.SessionFolders[first.SessionKey] = "2026/Etna";
Assert.Equal("2026/Etna", (await PlanAsync(folder, destination, whole, lookup)).Sessions.Single().Folder, "the sequence of the interrupted import");
var typed = whole with { Titles = new Dictionary<string, string> { [first.SessionKey] = "Etna at night" } };
var titled = (await PlanAsync(folder, destination, typed)).Sessions.Single();
Assert.Equal("Etna at night", titled.Title);
Assert.Equal("Etna", titled.DefaultTitle);
}
[Test]
public async Task AWholeFolderSplitsOnlyWhereTheUserSplitsIt()
{
var folder = Path.Combine(context.CreateTempDirectory("source"), "Harbour");
TestPhotos.WriteSeries(folder, Start, 10, Interval);
var destination = context.CreateTempDirectory("archive");
var split = new ImportOptions { Grouping = ImportGrouping.WholeFolder, SplitBefore = new HashSet<string> { "IMG_0009.JPG" } };
var plan = await PlanAsync(folder, destination, split);
Assert.SequenceEqual([8, 2], plan.Sessions.Select(s => s.Items.Count), "a part the user split off stays, however short");
Assert.Equal(ImportPlanner.FolderKey(folder, ImageFileFormat.Jpeg), plan.Sessions[0].SessionKey);
Assert.NotEqual(plan.Sessions[0].SessionKey, plan.Sessions[1].SessionKey);
Assert.Equal(plan.Sessions[1].SessionKey, (await PlanAsync(folder, destination, split)).Sessions[1].SessionKey);
var merged = split with { SplitBefore = new HashSet<string>(), MergeAt = new HashSet<string> { "IMG_0009.JPG" } };
Assert.Equal(10, (await PlanAsync(folder, destination, merged)).Sessions.Single().Items.Count, "merging the part back undoes the split");
}
[Test]
public async Task APhotoWithoutExifIsPlannedAtItsFileTime()
{
var card = context.CreateTempDirectory("card");
TestPhotos.WriteSeries(card, Start, 8, Interval);
var bare = Path.Combine(card, "BARE.JPG");
File.WriteAllBytes(bare, [0xFF, 0xD8, 0xFF, 0xD9]);
File.SetLastWriteTime(bare, Start.AddSeconds(85));
var plan = await PlanAsync(card, context.CreateTempDirectory("archive"));
var item = plan.Items.Single(i => i.SourcePath == "BARE.JPG");
Assert.Equal(Start.AddSeconds(85), item.CapturedAt);
// No camera identity: it forms its own body group rather than joining the Canon session.
Assert.SequenceEqual([8, 1], plan.Sessions.Select(s => s.Items.Count));
}
/// <summary>A photo as the scanner reads it, without a file behind it (the planner hashes only duplicate candidates).</summary>
private static CardPhoto Photo(string path, DateTime captured, ImageFileFormat format) => new(new CardFile(path, 1_000, captured),
new FrameMetadata { Format = format, Make = "Canon", Model = "Canon EOS R5", BodySerial = "0431", CapturedAt = captured });
}