Files
Chronos/tests/Chronos.Core.Tests/Naming/FrameNamingTests.cs
T
Alby96 eb2395d9b0 feat(archive): reorganize existing sequences with folder, file and title templates
Owner review 2, R7 asks for renaming and moving after import, on the web panel and the
desktop. This is the engine and the server API both UIs build on.

- FrameNaming (Core/Naming) is the one naming context and unique-name rule of a frame:
  imports, uploads and Reorganize now produce identical names (behaviour of the ingest and
  the import planner unchanged).
- RenamePlanner takes a file template: each step counts its renamed frames, shows the first
  three and the frames version they were planned at, and lists what keeps its name (photos
  in place outside originals/, never hashed, missing, unreadable sidecar). Names are given
  in index order with _2, _3 as filing does; XMP companions follow their photo.
- MassRenamer renames a step's files under the same leases after its folder moved, checks
  the frames version, journals .chronos/rename-files.json and moves files in two phases
  through temporary names so chains and swaps never meet; then sidecar, index
  (FrameRepository.RenameFilesAsync, both dialects) and journal. A failure puts every file
  back; a crash is completed by the next writer or the plan's resume, the index following.
  Plans a paused or cancelled job holds are left to that job.
- JobKind.Move gets MoveJobHandler: it plans again and applies only the confirmed plan
  (fingerprint and frames versions), with progress and checkpoints per sequence.
- POST /api/v1/archive/reorganize: dry run with the plan and its fingerprint, otherwise a
  202 move job; validated templates, audited, sequenceChanged reorganized events.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit aae1c3eb18ea932b4a6251c5e2a282b9295a5785)
2026-10-27 14:03:36 +01:00

117 lines
5.6 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))));
}
}