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)
This commit is contained in:
Alby96 committed 2026-10-27 18:08:08 +01:00
1 parent 9e1f109b58
commit 765f0ad36b
20 files changed
+446 -141

No files matched your search

@@ -203,6 +203,8 @@ public sealed class ReorganizeTests(TestContext context)
Assert.Equal("fileTemplate", new RenameTemplates(File: "{nope}").Problem());
Assert.Equal("fileTemplate", new RenameTemplates(File: "{date:%}").Problem());
Assert.Equal("titleTemplate", new RenameTemplates(Title: "{title").Problem());
// Not valid, blank or written with an unusable format: no rule to name.
Assert.Equal(new TemplateProblem("fileTemplate", null), new RenameTemplates(File: "{date:%}").Check());
Assert.True(new RenameTemplates().IsEmpty);
}
@@ -221,6 +223,7 @@ public sealed class ReorganizeTests(TestContext context)
var folder = field == "folderTemplate";
var templates = folder ? new RenameTemplates(Folder: template) : new RenameTemplates(File: template);
Assert.Equal(field, templates.Problem());
Assert.Equal(new TemplateProblem(field, rule), templates.Check());
Assert.Equal(rule, folder ? TemplateRules.FolderRule(PathTemplate.Parse(template)) : TemplateRules.FileRule(PathTemplate.Parse(template)));
}
@@ -362,6 +365,59 @@ public sealed class ReorganizeTests(TestContext context)
}
}
[Test]
public void AChangeOfCaseIsNeverPlannedOntoAnotherFolderOfThatExactName()
{
// A case-sensitive server: "Trip (2)" is a folder of its own beside the sequence at "trip (2)", and no sequence's.
var subject = new RenameSubject(Guid.NewGuid(), "trip (2)", "Trip", Start, null, null, null);
var onDisk = new HashSet<string>(["trip (2)", "Trip (2)"], StringComparer.Ordinal);
var plan = RenamePlanner.Plan(Guid.NewGuid(), [subject], "{title}", null, ["trip (2)"], onDisk.Contains, existsExactly: onDisk.Contains);
Assert.Equal("Trip", plan.Steps.Single().ToFolder);
// Case-insensitive, the only folder of that name is its own: it keeps its number, in the template's case.
var own = RenamePlanner.Plan(Guid.NewGuid(), [subject], "{title}", null, ["trip (2)"], f => f.Equals("trip (2)", StringComparison.OrdinalIgnoreCase),
existsExactly: f => f == "trip (2)");
Assert.Equal("Trip (2)", own.Steps.Single().ToFolder);
}
[Test]
public void AFolderExistsExactlyOnlyInTheCaseOfEveryLevel()
{
var archive = new ArchivePaths(context.CreateTempDirectory("archive"));
Directory.CreateDirectory(archive.FromRelative("trip/day"));
File.WriteAllText(Path.Combine(archive.FromRelative("trip/day"), "a.txt"), "a");
Assert.True(RenamePlanner.ExistsExactly(archive, "trip/day"));
Assert.False(RenamePlanner.ExistsExactly(archive, "Trip/day"));
Assert.False(RenamePlanner.ExistsExactly(archive, "trip/Day"));
Assert.False(RenamePlanner.ExistsExactly(archive, "trip/night"));
// An outer level's case alone: the folder moves on any file system (a case-insensitive one keeps the outer name as it was).
var step = new RenameStep(Guid.NewGuid(), "trip/day", "Trip/day", "T", "T");
RenamePlanner.MoveFolder(archive, step);
RenamePlanner.MoveFolder(archive, step);
Assert.Equal("a", File.ReadAllText(Path.Combine(archive.FromRelative("Trip/day"), "a.txt")));
}
[Test]
public void PhotosOfALongTitleKeepTheirExtension()
{
// As long as a title goes, less the room the number and the extension take: the name gives way before ".{ext}".
var manifest = Sequence("originals/a.JPG", "originals/b.JPG", "originals/c.JPG");
var title = new string('t', 186) + " harbour";
const string Template = "{title}_{n:0000}.{ext}";
var plan = Plan(manifest, Template, title);
Assert.Equal(3, plan.Frames);
foreach (var move in plan.Moves)
{
Assert.True(move.To.EndsWith(".JPG", StringComparison.Ordinal), move.To);
Assert.True(Path.GetFileName(move.To).Length <= PathTemplate.MaxSegmentLength + 2, move.To);
}
Assert.Equal(3, plan.Moves.Select(m => m.To).Distinct(StringComparer.OrdinalIgnoreCase).Count());
// Renamed so, the template names them again as they are.
var renamed = FileRenameJournal.Applied(manifest, FileRenameJournal.Create(manifest.Versions.Frames, plan.Moves, "t"))!;
Assert.Empty(Plan(renamed, Template, title).Moves);
}
[Test]
public void ExtensionsAPhotoLostComeBackFromItsName()
{
@@ -384,7 +440,8 @@ public sealed class ReorganizeTests(TestContext context)
{
// Every third photo has an XMP companion: "x_4" is free for the others, not for it.
Func<string, bool> companion = i % 3 == 1 ? c => !reference.Contains(Path.ChangeExtension(c, ".xmp")) : _ => true;
var expected = FrameNaming.Unique("a/x.JPG", c => !reference.Contains(c) && companion(c));
var expected = "a/x.JPG";
for (var n = 2; reference.Contains(expected) || !companion(expected); n++) expected = FrameNaming.Numbered("a/x.JPG", n);
reference.Add(expected);
Assert.Equal(expected, names.Claim("a/x.JPG", companion), $"photo {i}");
}