Files
Alby96andClaude Opus 5.5 7e4a3b481d
CI / Build and test (push) Successful in 9m15s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 6s
Release / Build, test and publish (push) Successful in 15m35s
CI / PostgreSQL 16 integration (push) Successful in 7m26s
CI / PostgreSQL 17 integration (push) Successful in 6m47s
CI / Docker image (push) Successful in 59s
Release / Server image (push) Successful in 1m24s
feat: owner review 2 phase 4a: Reorganize sequences after import (#12)
- Reorganize… on the web panel and the desktop: folder, file and title templates applied to existing sequences, with a dry run and an explicit Apply that runs as a job.
- One naming rule (FrameNaming) for import, upload and Reorganize.
- Crash-safe plan and file journals, held plans, stale-plan refusal; exports move with their folder.
- Writers refuse a folder another program moved or gave to another sequence.
- Server: POST /api/v1/archive/reorganize with a bounded plan cache; template rules.
- Phase 3 docs and translations merged; strings in all 38 languages.

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

211 lines
12 KiB
C#

using Chronos.Core.Archiving;
using Chronos.Core.Import;
using Chronos.Core.Metadata;
using Chronos.Core.Model;
using Chronos.Core.Naming;
using Chronos.Core.Tests.Archiving;
using Chronos.Testing;
using static Chronos.Core.Tests.Import.ImportTestKit;
namespace Chronos.Core.Tests.Naming;
/// <summary>Import, upload and Reorganize name a photo alike (owner review 2, R7).</summary>
[Category("naming")]
public sealed class FrameNamingTests(TestContext context)
{
private const string EveryToken = "{date}_{time}_{hm}_{sessionstart}_{n:0000}_{nsession}_{camera}_{lens}_{iso}_{shutter}_{aperture}_{name}_{title}";
private static FrameMetadata Metadata(DateTime captured) => new()
{
Make = "Canon",
Model = "Canon EOS R5",
LensModel = "RF15-35mm F2.8 L IS USM",
Iso = 400,
ExposureTime = 1.0 / 250,
FNumber = 8,
CapturedAt = captured,
TimestampSource = TimestampSource.ExifOriginal,
};
[Test]
public void AFrameInItsSequenceIsNamedAsTheUploadOfItsPhotoWas()
{
var start = new DateTime(2026, 9, 28, 18, 2, 11);
var template = PathTemplate.Parse(EveryToken);
var frames = Enumerable.Range(0, 3)
.Select(i => SequenceFrames.FromMetadata(Guid.NewGuid(), $"originals/x{i}.CR3", $"IMG_{4211 + i}.CR3", 100, new string('a', 64), Metadata(start.AddSeconds(5 * i))))
.ToList();
var first = Metadata(start);
var manifest = SequenceFrames.Insert(new SequenceManifest
{
Id = Guid.NewGuid(),
Camera = new SidecarCamera { Make = first.Make, Model = first.Model },
Lens = first.LensModel,
}, frames);
for (var i = 0; i < 3; i++)
{
var filed = template.ExpandFile(FrameNaming.Context(Metadata(start.AddSeconds(5 * i)), $"IMG_{4211 + i}.CR3", start.AddSeconds(5 * i), start, i + 1, "Harbour"));
Assert.Equal(filed, template.ExpandFile(FrameNaming.Context(manifest.Frames[i], manifest, "Harbour")));
}
Assert.Equal("2026-09-28_18-02-16_18-02_2026-09-28 18-02_0002_0002_Canon EOS R5_RF15-35mm F2.8 L IS USM_400_1-250_f8_IMG_4212_Harbour.CR3",
template.ExpandFile(FrameNaming.Context(manifest.Frames[1], manifest, "Harbour")));
}
[Test]
public void AFrameWithoutACaptureTimeTakesTheStartOfItsSequence()
{
var created = new DateTimeOffset(2026, 10, 1, 9, 30, 0, TimeSpan.Zero);
var manifest = new SequenceManifest
{
Id = Guid.NewGuid(),
Created = created,
Frames = [new SidecarFrame { Id = Guid.NewGuid(), File = "originals/a.jpg", Original = "DSC_0001.JPG" }],
};
var context = FrameNaming.Context(manifest.Frames[0], manifest, null);
Assert.Equal(new DateTime(2026, 10, 1, 9, 30, 0), context.CapturedAt);
Assert.Equal(context.CapturedAt, context.SessionStart);
Assert.Equal("DSC_0001", context.OriginalName);
Assert.Equal("jpg", context.Extension);
Assert.Equal(1, context.Number);
// After a first photo that has one, that photo's time.
var first = new DateTime(2026, 9, 28, 18, 0, 0);
var timed = manifest with
{
Frames = [manifest.Frames[0] with { CapturedAt = first }, new SidecarFrame { Id = Guid.NewGuid(), I = 1, File = "originals/b.jpg", Original = "DSC_0002.JPG" }],
};
var second = FrameNaming.Context(timed.Frames[1], timed, null);
Assert.Equal((first, first, 2), (second.CapturedAt, second.SessionStart, second.Number));
}
[Test]
public void TakenNamesGetNumbersBeforeTheExtensionAsFilingGivesThem()
{
var template = PathTemplate.Parse("{date}");
var context = FrameNaming.Context(Metadata(new DateTime(2026, 9, 28, 18, 0, 0)), "IMG_1.JPG", new DateTime(2026, 9, 28, 18, 0, 0),
new DateTime(2026, 9, 28, 18, 0, 0), 1, null);
var taken = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "originals/2026-09-28.jpg" };
var names = new UniqueNames(taken);
Assert.Equal("originals/2026-09-28_2.JPG", FrameNaming.FileName(template, context, names, "originals/"));
Assert.Equal("originals/2026-09-28_3.JPG", FrameNaming.FileName(template, context, names, "originals/"));
Assert.Contains("originals/2026-09-28_3.JPG", taken);
Assert.Equal("a/IMG_5_2.CR2", new UniqueNames(new HashSet<string>(["a/IMG_5.CR2"])).Claim("a/IMG_5.CR2"));
}
[Test]
public async Task AnImportedSessionIsNamedAsReorganizeNamesItsFrames()
{
var card = context.CreateTempDirectory("card");
TestPhotos.WriteSeries(Path.Combine(card, "DCIM", "100CANON"), Start, 4, TimeSpan.FromSeconds(10));
const string fileTemplate = "{date}_{time}_{n:0000}_{camera}_{name}";
var plan = await PlanAsync(card, context.CreateTempDirectory("archive"), new ImportOptions { FileTemplate = fileTemplate });
var session = plan.Sessions.Single();
var manifest = Filed(card, session);
var reorganized = FileRenames.Plan(manifest, session.Title, PathTemplate.Parse(fileTemplate), [.. manifest.Frames.Select(f => f.File)]);
Assert.Empty(reorganized.Moves);
Assert.Equal(0, reorganized.Frames);
Assert.SequenceEqual(session.Items.Select(i => i.FileName),
manifest.Frames.Select(f => PathTemplate.Parse(fileTemplate).ExpandFile(FrameNaming.Context(f, manifest, session.Title))));
}
/// <summary>
/// The sequence an import files <paramref name="session"/> of <paramref name="source"/> into: the plan's names, each
/// frame from its photo's metadata, the camera and lens of the first.
/// </summary>
private static SequenceManifest Filed(string source, PlannedSession session)
{
var frames = session.Items.Select(item => SequenceFrames.FromMetadata(Guid.NewGuid(), "originals/" + item.FileName, Path.GetFileName(item.SourcePath),
item.Size, new string('b', 64), MetadataReader.Read(Path.Combine(source, item.SourcePath)), item.CapturedAt)).ToList();
var first = MetadataReader.Read(Path.Combine(source, session.Items[0].SourcePath));
return SequenceFrames.Insert(new SequenceManifest
{
Id = Guid.NewGuid(),
Camera = new SidecarCamera { Make = first.Make, Model = first.Model },
Lens = first.LensModel,
}, frames);
}
private static List<string> Renames(SequenceManifest manifest, string? title, string template) =>
[.. FileRenames.Plan(manifest, title, PathTemplate.Parse(template), [.. manifest.Frames.Select(f => f.File)]).Moves.Select(m => $"{m.From}>{m.To}")];
[Test]
public async Task ASecondSessionOfACardIsNumberedByTheImportAndRenamedByReorganize()
{
// Two sessions of one card two hours apart: the import numbers them, while each sequence is one session of its own.
var card = context.CreateTempDirectory("card");
var folder = Path.Combine(card, "DCIM", "100CANON");
TestPhotos.WriteSeries(folder, Start, 2, TimeSpan.FromSeconds(10));
TestPhotos.WriteSeries(folder, Start.AddHours(2), 2, TimeSpan.FromSeconds(10), firstSeed: 10, firstNumber: 3);
const string fileTemplate = "{session}_{n:0000}";
var plan = await PlanAsync(card, context.CreateTempDirectory("archive"), new ImportOptions { FileTemplate = fileTemplate, MinimumSessionFrames = 1 });
Assert.SequenceEqual(["1_0001.JPG 1_0002.JPG", "2_0001.JPG 2_0002.JPG"], plan.Sessions.Select(s => string.Join(' ', s.Items.Select(i => i.FileName))));
// A divergence FrameNaming names: Reorganize calls the second sequence's session 1, and its dry run lists the renames.
Assert.Empty(Renames(Filed(card, plan.Sessions[0]), plan.Sessions[0].Title, fileTemplate));
Assert.SequenceEqual(["originals/2_0001.JPG>originals/1_0001.JPG", "originals/2_0002.JPG>originals/1_0002.JPG"],
Renames(Filed(card, plan.Sessions[1]), plan.Sessions[1].Title, fileTemplate));
}
[Test]
public async Task AWholeFolderOfTwoCamerasNamesEveryPhotoWithTheFirstOnesCamera()
{
var folder = Path.Combine(context.CreateTempDirectory("source"), "Etna");
TestPhotos.WriteSeries(folder, Start, 2, TimeSpan.FromSeconds(10), prefix: "CAN_");
TestPhotos.WriteSeries(folder, Start.AddSeconds(5), 2, TimeSpan.FromSeconds(10), firstSeed: 40, prefix: "NIK_", make: "NIKON CORPORATION", model: "NIKON Z 8");
const string fileTemplate = "{camera}_{n:0000}";
var plan = await PlanAsync(folder, context.CreateTempDirectory("archive"), new ImportOptions { Grouping = ImportGrouping.WholeFolder, FileTemplate = fileTemplate });
var session = plan.Sessions.Single();
Assert.Equal(2, session.Cameras);
Assert.SequenceEqual(["CAN_0001.JPG", "NIK_0001.JPG", "CAN_0002.JPG", "NIK_0002.JPG"], session.Items.Select(i => Path.GetFileName(i.SourcePath)));
// The sequence carries the first photo's camera, which names every photo of it, the second body's too: Reorganize agrees.
Assert.SequenceEqual(["Canon EOS R5_0001.JPG", "Canon EOS R5_0002.JPG", "Canon EOS R5_0003.JPG", "Canon EOS R5_0004.JPG"],
session.Items.Select(i => i.FileName));
Assert.Empty(Renames(Filed(folder, session), session.Title, fileTemplate));
}
[Test]
public async Task ALensChangedDuringASessionNamesItsPhotosAsTheirSequenceDoes()
{
var card = context.CreateTempDirectory("card");
var folder = Path.Combine(card, "DCIM", "100CANON");
for (var i = 0; i < 4; i++)
TestPhotos.Write(Path.Combine(folder, $"IMG_{i + 1:0000}.JPG"), Start.AddSeconds(10 * i), i + 1, lens: i < 2 ? "RF15-35mm" : "RF24-70mm");
const string fileTemplate = "{lens}_{n:0000}";
var plan = await PlanAsync(card, context.CreateTempDirectory("archive"), new ImportOptions { FileTemplate = fileTemplate });
var session = plan.Sessions.Single();
// The sequence carries the lens of its first photo, which names every photo of it.
Assert.SequenceEqual(["RF15-35mm_0001.JPG", "RF15-35mm_0002.JPG", "RF15-35mm_0003.JPG", "RF15-35mm_0004.JPG"], session.Items.Select(i => i.FileName));
var frames = session.Items.Select(item => SequenceFrames.FromMetadata(Guid.NewGuid(), "originals/" + item.FileName, Path.GetFileName(item.SourcePath),
item.Size, new string('c', 64), MetadataReader.Read(Path.Combine(card, item.SourcePath)), item.CapturedAt)).ToList();
var manifest = SequenceFrames.Insert(new SequenceManifest
{
Id = Guid.NewGuid(),
Camera = new SidecarCamera { Make = "Canon", Model = "Canon EOS R5" },
Lens = "RF15-35mm",
}, frames);
Assert.Empty(FileRenames.Plan(manifest, session.Title, PathTemplate.Parse(fileTemplate), [.. manifest.Frames.Select(f => f.File)]).Moves);
}
[Test]
public void AnUploadIsNamedWithItsSequencesCameraAsReorganizeNamesIt()
{
// The creator's hint stands for the camera the photos do not name; a second body's photo takes the sequence's camera.
var start = new DateTime(2026, 9, 28, 18, 2, 11);
var frames = new[] { Metadata(start), Metadata(start.AddSeconds(5)) with { Make = "GoPro", Model = "HERO12 Black", LensModel = null } }
.Select((m, i) => SequenceFrames.FromMetadata(Guid.NewGuid(), $"IMG_{i}.JPG", $"IMG_{i}.JPG", 100, new string('a', 64), m)).ToList();
var manifest = SequenceFrames.Insert(new SequenceManifest
{
Id = Guid.NewGuid(),
Camera = new SidecarCamera { Model = "Hinted body" },
Lens = "RF15-35mm F2.8 L IS USM",
}, frames);
var template = PathTemplate.Parse("{camera}_{lens}_{n:00}");
Assert.SequenceEqual(["Hinted body_RF15-35mm F2.8 L IS USM_01.JPG", "Hinted body_RF15-35mm F2.8 L IS USM_02.JPG"],
manifest.Frames.Select(f => template.ExpandFile(FrameNaming.Context(f, manifest, null))));
}
}