Files
Chronos/tests/Chronos.Core.Tests/Naming/FrameNamingTests.cs
T
Alby96 b253cac8d4 fix(archive): Reorganize names as filing does and refuses templates that lose or hide photos
Reorganize with the archive's own templates planned renames and moves for
photos just filed, and some templates could strip extensions, hide sequences
from scans or stall a dry run. Owner review 2, phase 4a findings.

- Filing and Reorganize share one context: uploads and imports name every
  photo with the sequence's camera and lens (its first photo's, or the
  creator's hint), an upload's folder from its first frame in the new
  sequence, and a missing capture time from the sequence start instead of
  the current time. The planner expands folder and title templates from the
  first frame in the sidecar ({name}, {ext}, {iso}...), never today's date.
- Titles expand as text (ExpandText): "Paris: night" stays as typed.
- A sequence whose sidecar cannot be read (torn, another sequence's) keeps
  its folder, title and names, listed with why, and so does every folder
  around it; the dry run reads every sidecar, nested ones included.
- Kept never gives one folder to two sequences of a plan whatever the case,
  and a case-only move onto another folder is refused instead of taken as
  done.
- Shared template rules (TemplateRules) for the server's Reorganize and
  archive settings and the desktop's fields: a file template must keep the
  photo's extension ("{n}{ext}" refused) and tell a session's photos apart;
  a folder template may not write a level scans skip (a leading dot,
  originals, proxies, exports). Levels a title gives are escaped with "_".
- {ext} comes from the file, or from the original name when the file lost
  a photo's extension, so a Reorganize can put it back.
- Unique names cost linear time (UniqueNames) with the same names as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 6c734777e01476881a3f34b3188571cc3f7150ab)
2026-10-27 16:29:31 +01:00

159 lines
8.2 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);
}
[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" };
Assert.Equal("originals/2026-09-28_2.JPG", FrameNaming.FileName(template, context, taken, "originals/"));
Assert.Equal("originals/2026-09-28_3.JPG", FrameNaming.FileName(template, context, taken, "originals/"));
Assert.Contains("originals/2026-09-28_3.JPG", taken);
Assert.Equal("a/IMG_5_2.CR2", FrameNaming.Unique("a/IMG_5.CR2", f => f != "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();
// The sequence the import files: the plan's names, each frame from its photo's metadata, the camera of the first.
var frames = session.Items.Select(item =>
{
var metadata = MetadataReader.Read(Path.Combine(card, item.SourcePath));
return SequenceFrames.FromMetadata(Guid.NewGuid(), "originals/" + item.FileName, Path.GetFileName(item.SourcePath), item.Size, new string('b', 64),
metadata, item.CapturedAt);
}).ToList();
var first = MetadataReader.Read(Path.Combine(card, session.Items[0].SourcePath));
var manifest = SequenceFrames.Insert(new SequenceManifest
{
Id = Guid.NewGuid(),
Camera = new SidecarCamera { Make = first.Make, Model = first.Model },
Lens = first.LensModel,
}, frames);
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))));
}
[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))));
}
}