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)
141 lines
5.3 KiB
C#
141 lines
5.3 KiB
C#
using Chronos.Core.Naming;
|
|
using Chronos.Testing;
|
|
|
|
namespace Chronos.Core.Tests.Naming;
|
|
|
|
public sealed class PathTemplateTests
|
|
{
|
|
private static readonly NamingContext Frame = new()
|
|
{
|
|
CapturedAt = new DateTime(2026, 9, 28, 18, 2, 11, 250),
|
|
SessionStart = new DateTime(2026, 9, 28, 18, 2, 11),
|
|
Session = 2,
|
|
Number = 1,
|
|
NumberInSession = 1,
|
|
CameraMake = "Canon",
|
|
CameraModel = "Canon EOS R5",
|
|
Lens = "RF24-70mm F2.8 L IS USM",
|
|
Iso = 400,
|
|
ExposureTime = 1.0 / 8,
|
|
FNumber = 8,
|
|
OriginalName = "IMG_0001",
|
|
Extension = "CR2",
|
|
Title = "Harbour sunset",
|
|
};
|
|
|
|
[Test]
|
|
public void DefaultTemplatesMatchTheBriefExample()
|
|
{
|
|
Assert.Equal("2026/2026-09-28 Canon EOS R5", PathTemplate.Parse(TemplateDefaults.Folder).ExpandFolder(Frame));
|
|
Assert.Equal("2026-09-28_18-02-11_0001.CR2", PathTemplate.Parse(TemplateDefaults.File).ExpandFile(Frame));
|
|
}
|
|
|
|
[Theory]
|
|
[Case("{n:000000}", "000001.CR2")]
|
|
[Case("{date:yyyyMMdd}_{name}", "20260928_IMG_0001.CR2")]
|
|
[Case("{iso}_{shutter}_{aperture}", "400_1-8_f8.CR2")]
|
|
[Case("{name}.{ext}", "IMG_0001.CR2")]
|
|
[Case("{{literal}}_{n}", "{literal}_0001.CR2")]
|
|
[Case("{session}-{nsession:00}", "2-01.CR2")]
|
|
public void FileTokens(string template, string expected) =>
|
|
Assert.Equal(expected, PathTemplate.Parse(template).ExpandFile(Frame));
|
|
|
|
[Test]
|
|
public void IllegalCharactersAreReplaced()
|
|
{
|
|
var context = Frame with { Title = "a:b*c?<d>|e\"f" };
|
|
Assert.Equal("a_b_c__d__e_f.CR2", PathTemplate.Parse("{title}").ExpandFile(context));
|
|
}
|
|
|
|
[Test]
|
|
public void TokenValuesNeverCreateFolders()
|
|
{
|
|
var context = Frame with { Lens = "18/55 mm" };
|
|
Assert.Equal("2026/18-55 mm", PathTemplate.Parse("{year}/{lens}").ExpandFolder(context));
|
|
}
|
|
|
|
[Test]
|
|
public void ReservedWindowsNamesAreEscaped() =>
|
|
Assert.Equal("_CON.CR2", PathTemplate.Parse("{title}").ExpandFile(Frame with { Title = "CON" }));
|
|
|
|
[Theory]
|
|
[Case(".NET Conf", "2026/_.NET Conf")]
|
|
[Case("Exports", "2026/_Exports")]
|
|
[Case("originals", "2026/_originals")]
|
|
[Case("PROXIES", "2026/_PROXIES")]
|
|
[Case(".chronos", "2026/_.chronos")]
|
|
[Case("Harbour", "2026/Harbour")]
|
|
public void FolderLevelsScansNeverEnterAreEscaped(string title, string expected)
|
|
{
|
|
var template = PathTemplate.Parse("{year}/{title}");
|
|
Assert.Equal(expected, template.ExpandFolder(Frame with { Title = title }));
|
|
Assert.Equal(title != "Harbour", template.WritesHiddenFolder(Frame with { Title = title }));
|
|
Assert.Equal(title != "Harbour", PathTemplate.IsHiddenFolder(title));
|
|
}
|
|
|
|
[Theory]
|
|
[Case("{n}", true)]
|
|
[Case("{name}.{ext}", true)]
|
|
[Case("{n} .{ext}", true)]
|
|
[Case("{n}.CR3", true)]
|
|
[Case("{n}{ext}", false)]
|
|
[Case("{n}_{ext}", false)]
|
|
[Case("{n}. {ext}", false)]
|
|
[Case("{n}.{ext}{ext}", false)]
|
|
public void AFileTemplateKeepsTheExtensionOnlyWithItsDot(string template, bool keeps)
|
|
{
|
|
var parsed = PathTemplate.Parse(template);
|
|
Assert.Equal(keeps, parsed.KeepsExtension, template);
|
|
Assert.Equal(keeps, parsed.ExpandFile(Frame).EndsWith(".CR2", StringComparison.Ordinal), parsed.ExpandFile(Frame));
|
|
}
|
|
|
|
[Theory]
|
|
[Case("{title} 2026", "Paris: night", "Paris: night 2026")]
|
|
[Case("{title} 2026", "24/7 roof", "24/7 roof 2026")]
|
|
[Case("{title}", "Aux. trip", "Aux. trip")]
|
|
[Case("{title}", "What?", "What?")]
|
|
[Case("{title}", "Sunset...", "Sunset...")]
|
|
[Case(" {title}\t", "Dawn", "Dawn")]
|
|
public void TitlesAreTextNotFileNames(string template, string title, string expected) =>
|
|
Assert.Equal(expected, PathTemplate.Parse(template).ExpandText(Frame with { Title = title }));
|
|
|
|
[Test]
|
|
public void TextKeepsToTheLongestTitle()
|
|
{
|
|
var text = PathTemplate.Parse("{title}{title}").ExpandText(Frame with { Title = new string('a', 150) });
|
|
Assert.Equal(PathTemplate.MaxTextLength, text.Length);
|
|
// A character outside the basic plane is never cut in two.
|
|
var emoji = PathTemplate.Parse("{title}").ExpandText(Frame with { Title = new string('a', 199) + "\U0001F305" });
|
|
Assert.Equal(new string('a', 199), emoji);
|
|
}
|
|
|
|
[Test]
|
|
public void ErrorsCarryPositionAndToken()
|
|
{
|
|
var t = PathTemplate.Parse("{date}_{bogus}_{n");
|
|
Assert.False(t.IsValid);
|
|
Assert.Contains(new TemplateError(7, "UnknownToken", "bogus"), t.Errors);
|
|
Assert.Contains(t.Errors, e => e.Code == "UnclosedBrace");
|
|
}
|
|
|
|
[Test]
|
|
public void EmptyExpansionFallsBackToOriginalName() =>
|
|
Assert.Equal("IMG_0001.CR2", PathTemplate.Parse("{title}").ExpandFile(Frame with { Title = null }));
|
|
|
|
[Theory]
|
|
[Case("NIKON CORPORATION", "NIKON Z 8", "Nikon Z 8")]
|
|
[Case("SONY", "ILCE-7M4", "SONY ILCE-7M4")]
|
|
[Case("Canon", "Canon EOS R5", "Canon EOS R5")]
|
|
[Case(null, "HERO12 Black", "HERO12 Black")]
|
|
public void CameraNameAvoidsDuplicatedBrand(string? make, string model, string expected) =>
|
|
Assert.Equal(expected, PathTemplate.CameraName(make, model));
|
|
|
|
[Test]
|
|
public void ShutterFormatting()
|
|
{
|
|
Assert.Equal("1-250", PathTemplate.Shutter(0.004));
|
|
Assert.Equal("2s", PathTemplate.Shutter(2));
|
|
Assert.Equal(string.Empty, PathTemplate.Shutter(null));
|
|
}
|
|
}
|