Files
Chronos/tests/Chronos.Core.Tests/Naming/PathTemplateTests.cs
T
Alby96 765f0ad36b fix(archive): Reorganize never plans a move onto a folder of the exact name, keeps long names' extensions, says why a template is refused
Second fix round of owner review 2, phase 4a (planner findings).

- A sequence keeps a folder that differs from its own in case only while no
  other folder holds that exact name (RenamePlanner.ExistsExactly, every level
  compared exactly). On a case-sensitive file system a stray "Trip (2)" beside
  the sequence at "trip (2)" no longer gives a case-only step that MoveFolder
  refuses at every archive open: the plan gives "Trip", and a plan whose
  target appeared since is refused under the step's leases before anything
  moves (or dropped when it resumes). A change of case in an outer level no
  longer looks like two folders on Windows.
- A file name longer than 200 characters is shortened before the ".{ext}" the
  template ends with, never through it, so a long title never strips the
  photo's extension.
- The archive engine's own settings check applies the template rules the
  server and the desktop apply (a folder scans skip, a lost extension, names
  that repeat), so archive.json can no longer take such a template.
- The server's invalid_template answer names the rule after the field
  ("fileTemplate:extension") with the desktop's sentence as its message; both
  admin panels show it on the field.
- FrameNaming keeps only the naming overloads production uses.
- Comments about Unreadable say the whole sequence stays as it is.
- Tests pin filing against Reorganize: an upload without a capture time, a
  card's second session with {session}, a duplicate skipped mid-session, and a
  whole folder of two cameras named with the first one's camera.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 50fe59aec1e180daa1733bcb951892a79dceba6c)
2026-10-27 18:08:08 +01:00

155 lines
6.1 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));
}
[Test]
public void ALongNameIsShortenedBeforeItsExtension()
{
// A title as long as titles go: the number gives way, the extension never does.
var title = new string('a', 195);
var name = PathTemplate.Parse("{title}_{n:0000}.{ext}").ExpandFile(Frame with { Title = title });
Assert.Equal(title + "_.CR2", name);
Assert.Equal(PathTemplate.MaxSegmentLength, name.Length);
// Appended, the extension comes after the longest name.
Assert.Equal(title + "_0001.CR2", PathTemplate.Parse("{title}_{n:0000}").ExpandFile(Frame with { Title = title }));
// Short names are as they were.
Assert.Equal("Harbour sunset_0001.CR2", PathTemplate.Parse("{title}_{n:0000}.{ext}").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));
}
}