fix(reorganize): a sequence still receiving photos keeps its file names and the job goes on
A Reorganize with a file template failed its job at the step of a sequence
a camera was still uploading into, and its retry gave the rest up. Photos
changed since the plan now never refuse a step: that sequence keeps its file
names (FilesKept), its folder and title change as planned, the other steps go
on and the job ends with ReorganizeKept. The fingerprint, the job's
confirmation and the check under the leases pin file renames by their digest
instead of the frames version, so photos filed under the names the plan gives
them no longer make a dry run or a queued job stale.
- A step whose folder or files a template names after {title} checks the
title as a retitle does (RenameStep.NamedByTitle): no name comes from a
title edited since the plan.
- Recovery sends a photo that already took its new name back together with a
companion whose new name was taken, so the two never end up apart.
- ArchiveService.OpenAsync logs a NewerFormatException from resuming a plan
and keeps its journal, so a server still boots.
- Once a step's folder moved, every index row of the step follows it even
when a sidecar cannot be read or written; the step then fails for a retry.
- Job log lines and errors give the real reason for files kept busy (in use
or a new name taken) and for a sidecar of a newer format (new Newer
outcome; strings en and it).
- A plan an archive open completes tells sequenceChanged; the docs that
promised more than that are corrected.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit f8554b8af0828ab9b0ff128e694abacdd53e6fd7)
This commit is contained in:
1 parent
59ee982e4d
commit
9e1f109b58
16 files changed
+560
-195
No files matched your search
@@ -171,7 +171,27 @@ public sealed class ReorganizeTests(TestContext context)
|
||||
var plan = Make(manifest);
|
||||
Assert.Equal(64, plan.Fingerprint().Length);
|
||||
Assert.Equal(plan.Fingerprint(), Make(manifest).Fingerprint());
|
||||
Assert.NotEqual(plan.Fingerprint(), Make(manifest with { Versions = new ManifestVersions { Frames = 5 } }).Fingerprint());
|
||||
// A photo received since under the name the plan gives it raises the frames version only: the plan is the one shown.
|
||||
var received = manifest with
|
||||
{
|
||||
Versions = new ManifestVersions { Frames = 5 },
|
||||
Frames = [.. manifest.Frames, manifest.Frames[0] with { Id = Guid.NewGuid(), I = 1, File = "originals/002.JPG", CapturedAt = Start.AddSeconds(10) }],
|
||||
};
|
||||
Assert.Equal(plan.Fingerprint(), Make(received).Fingerprint());
|
||||
// One the plan would rename too, or a file renamed otherwise, is another plan.
|
||||
Assert.NotEqual(plan.Fingerprint(), Make(received with { Frames = [.. received.Frames.SkipLast(1), received.Frames[1] with { File = "originals/b.JPG" }] }).Fingerprint());
|
||||
Assert.NotEqual(plan.Fingerprint(), Make(manifest with { Frames = [manifest.Frames[0] with { File = "originals/c.JPG" }] }).Fingerprint());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AStepNamedAfterTheTitleSaysSo()
|
||||
{
|
||||
var manifest = Sequence("originals/a.JPG");
|
||||
RenameStep Step(string? folder, string? file) => RenamePlanner.Plan(Guid.NewGuid(), [new RenameSubject(manifest.Id, "s", "Roof", Start, null, null, null)
|
||||
{ Files = new SequenceFiles(manifest, [.. manifest.Frames.Select(f => f.File)]) }], folder, null, ["s"], _ => false, fileTemplate: file).Steps.Single();
|
||||
Assert.True(Step("moved/{TITLE}", null) is { MovesFolder: true, NamedByTitle: true, UsesTitle: true, Retitles: false });
|
||||
Assert.True(Step(null, "{title}_{n:000}") is { FileChanges: 1, NamedByTitle: true, UsesTitle: true });
|
||||
Assert.True(Step("moved/{date}", "{n:000}") is { MovesFolder: true, FileChanges: 1, NamedByTitle: false, UsesTitle: false });
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -560,4 +580,34 @@ public sealed class ReorganizeTests(TestContext context)
|
||||
Assert.FileMissing(sequence.File("originals/0001.CR2"));
|
||||
Assert.Empty(Directory.EnumerateFiles(sequence.Originals, ".chronos-rename-*"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void APhotoThatTookItsNewNameGoesBackWithACompanionWhoseNewNameIsTaken()
|
||||
{
|
||||
var sequence = Folder(out _);
|
||||
var manifest = Sequence("originals/A.CR2");
|
||||
Write(sequence, "originals/A.CR2", "photo");
|
||||
Write(sequence, "originals/A.xmp", "sidecar");
|
||||
var id = manifest.Frames[0].Id;
|
||||
FileRename[] moves = [new(id, "originals/A.CR2", "originals/0001.CR2"), new(id, "originals/A.xmp", "originals/0001.xmp", Companion: true)];
|
||||
var journal = FileRenameJournal.Create(manifest.Versions.Frames, moves, "t6") with { Phase = 2 };
|
||||
FileRenameJournal.Write(sequence, journal);
|
||||
foreach (var f in journal.Files) File.Move(sequence.File(f.From), sequence.File(f.Temp));
|
||||
// Stopped once the photo took its new name, before its companion did; then something took the companion's, and the
|
||||
// photo's old name too.
|
||||
File.Move(sequence.File(journal.Files[0].Temp), sequence.File(journal.Files[0].To));
|
||||
Write(sequence, "originals/0001.xmp", "other");
|
||||
Write(sequence, "originals/A.CR2", "another");
|
||||
|
||||
// The photo goes back with its companion, both to the first free number of its old name; the frame names the photo.
|
||||
var recovered = Assert.NotNull(FileRenameJournal.Recover(sequence, manifest));
|
||||
Assert.Equal("originals/A_2.CR2", recovered.Frames.Single().File);
|
||||
Assert.Equal(("photo", "sidecar"), (Read(sequence, "originals/A_2.CR2"), Read(sequence, "originals/A_2.xmp")));
|
||||
Assert.Equal(("other", "another"), (Read(sequence, "originals/0001.xmp"), Read(sequence, "originals/A.CR2")));
|
||||
Assert.FileMissing(sequence.File("originals/0001.CR2"));
|
||||
Assert.Empty(Directory.EnumerateFiles(sequence.Originals, ".chronos-rename-*"));
|
||||
// Run again, every file stays where it is.
|
||||
Assert.Equal("originals/A_2.CR2", Assert.NotNull(FileRenameJournal.Recover(sequence, manifest)).Frames.Single().File);
|
||||
Assert.Equal(("photo", "sidecar"), (Read(sequence, "originals/A_2.CR2"), Read(sequence, "originals/A_2.xmp")));
|
||||
}
|
||||
}
|
||||
@@ -252,29 +252,52 @@ public sealed class ReorganizeTests(TestContext context)
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task APlanWhosePhotosChangedIsRefusedOrResumedWithoutTheirNames()
|
||||
public async Task APlanWhosePhotosChangedAppliesWithoutTheirNames()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var id = await UploadAsync(fx, 2);
|
||||
var plan = await fx.Service.PlanReorganizeAsync(fx.Archive.Id, [id], new RenameTemplates(File: "{n:000}", Title: "Roof"), fx.Ct);
|
||||
// A photo arrives after the dry run: the names shown are no longer the ones the template gives.
|
||||
// A photo arrives after the dry run, filed between them: the names shown are no longer the ones the template gives.
|
||||
await UploadAsync(fx, 1, firstSeed: 9, firstSecond: 5);
|
||||
var before = await FilesAsync(fx, id);
|
||||
|
||||
var stale = await Assert.ThrowsAsync<StaleRenamePlanException>(() => fx.Service.ApplyRenameAsync(plan, null, fx.Ct));
|
||||
Assert.Equal(id, stale.SequenceId);
|
||||
Assert.SequenceEqual(before, await FilesAsync(fx, id));
|
||||
Assert.Null((await fx.Db.Sequences.GetAsync(id))!.Title);
|
||||
Assert.Empty(RenameJournal.ListPending(fx.Paths));
|
||||
|
||||
// Interrupted before its step began, the plan resumes: the title changes, the files keep their names.
|
||||
RenameJournal.Write(fx.Paths, new RenameJournal(plan, []));
|
||||
Assert.Equal(1, await fx.Service.Renamer.ResumePendingAsync(fx.Archive, fx.Ct));
|
||||
// The title changes, the files keep their names.
|
||||
var result = await fx.Service.ApplyRenameAsync(plan, null, fx.Ct);
|
||||
Assert.SequenceEqual([id], result.FilesKept);
|
||||
Assert.SequenceEqual([id], result.Changed);
|
||||
Assert.Equal(0, result.Files);
|
||||
Assert.SequenceEqual(before, await FilesAsync(fx, id));
|
||||
Assert.Equal("Roof", (await fx.Db.Sequences.GetAsync(id))!.Title);
|
||||
Assert.Empty(RenameJournal.ListPending(fx.Paths));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PhotosReceivedUnderTheNamesThePlanGivesLeaveItAsItWas()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var id = await UploadAsync(fx, 2);
|
||||
// The archive files new photos as the Reorganize names them, which its photos do not follow yet.
|
||||
const string Template = "{n:000}";
|
||||
await fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Templates = s.Templates with { File = Template } }, fx.Ct);
|
||||
var templates = new RenameTemplates(File: Template);
|
||||
var plan = await fx.Service.PlanReorganizeAsync(fx.Archive.Id, [id], templates, fx.Ct);
|
||||
Assert.Equal(2, plan.Steps.Single().FileChanges);
|
||||
var request = ArchiveJobs.Reorganize(fx.Archive, plan, templates);
|
||||
|
||||
// A camera still uploads into it: its photo is filed under the name the plan leaves it, which changes nothing of the plan.
|
||||
await UploadAsync(fx, 1, firstSeed: 3, firstSecond: 20);
|
||||
Assert.Equal("originals/003.JPG", (await FilesAsync(fx, id))[2]);
|
||||
var now = await fx.Service.PlanReorganizeAsync(fx.Archive.Id, [id], templates, fx.Ct);
|
||||
Assert.NotEqual(plan.Steps.Single().FramesVersion, now.Steps.Single().FramesVersion);
|
||||
Assert.Equal(plan.Fingerprint(), now.Fingerprint());
|
||||
|
||||
// The job confirmed before it applies it as it was shown.
|
||||
var engine = await fx.StartEngineAsync();
|
||||
var done = await fx.WaitForAsync((await engine.EnqueueAsync(request, cancellationToken: fx.Ct)).Id, JobState.Done);
|
||||
Assert.Contains(Loc.L("Archive.Log.Reorganized", 1, 2), done.LogTail);
|
||||
Assert.SequenceEqual(["originals/001.JPG", "originals/002.JPG", "originals/003.JPG"], await FilesAsync(fx, id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task CompanionsMoveWithTheirPhoto()
|
||||
{
|
||||
@@ -403,26 +426,72 @@ public sealed class ReorganizeTests(TestContext context)
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AStepWhosePhotosChangedIsRefusedUnderItsLeasesBeforeAnythingMoves()
|
||||
public async Task AStepWhosePhotosChangedUnderItsLeasesMovesWithoutRenamingThem()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (parent, nested, plan) = await CarrierAsync(fx);
|
||||
var from = await FolderAsync(fx, parent);
|
||||
|
||||
// A photo uploaded once the plan's own checks passed, before its step: nothing of that step moves.
|
||||
var stale = await Assert.ThrowsAsync<StaleRenamePlanException>(() => fx.Service.Renamer.ApplyAsync(fx.Archive, plan,
|
||||
async _ => await UploadAsync(fx, 1, firstSeed: 9, firstSecond: 5), null, fx.Ct));
|
||||
Assert.Equal(parent, stale.SequenceId);
|
||||
var before = await FilesAsync(fx, parent);
|
||||
Assert.Equal((from, from + "/extra"), (await FolderAsync(fx, parent), await FolderAsync(fx, nested)));
|
||||
Assert.Empty(Assert.NotNull(RenameJournal.Read(fx.Paths, plan.Id)).Completed);
|
||||
|
||||
// Resumed after an interruption, the folder moves with the one inside it, whose index row follows, and the files keep their names.
|
||||
var result = Assert.NotNull(await fx.Service.Renamer.ResumeAsync(fx.Archive, plan.Id, null, fx.Ct));
|
||||
// A photo uploaded once the plan's own checks passed, before its step: the folder moves with the one inside it,
|
||||
// whose index row follows, and the files keep their names.
|
||||
var result = await fx.Service.Renamer.ApplyAsync(fx.Archive, plan, async _ => await UploadAsync(fx, 1, firstSeed: 9, firstSecond: 5), null, fx.Ct);
|
||||
Assert.SequenceEqual([parent], result.FilesKept);
|
||||
Assert.SequenceEqual([parent, nested], result.Changed);
|
||||
Assert.Equal(("moved/2026-09-28", "moved/2026-09-28/extra"), (await FolderAsync(fx, parent), await FolderAsync(fx, nested)));
|
||||
Assert.SequenceEqual(before, await FilesAsync(fx, parent));
|
||||
var files = await FilesAsync(fx, parent);
|
||||
Assert.Count(3, files);
|
||||
Assert.Empty(files.Intersect(plan.Steps[0].FileSamples.Select(s => s.To)));
|
||||
Assert.Empty(RenameJournal.ListPending(fx.Paths));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ASequenceStillReceivingPhotosKeepsItsFileNamesWhileTheJobGoesOn()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var first = await UploadAsync(fx, 2);
|
||||
var second = await UploadAsync(fx, 2, firstSeed: 20, firstSecond: 3600, key: "upload-2");
|
||||
var third = await UploadAsync(fx, 2, firstSeed: 40, firstSecond: 7200, key: "upload-3");
|
||||
var templates = new RenameTemplates(Folder: "moved/{date} {hm}", File: "{n:000}");
|
||||
var plan = await fx.Service.PlanReorganizeAsync(fx.Archive.Id, [first, second, third], templates, fx.Ct);
|
||||
var told = new ConcurrentQueue<Guid>();
|
||||
var service = new ArchiveService(fx.Db, new ArchiveServiceOptions
|
||||
{
|
||||
Owner = "jobs",
|
||||
LeaseWait = TimeSpan.FromSeconds(TestTiming.Budget(20)),
|
||||
SequenceChanged = (id, _, _) =>
|
||||
{
|
||||
told.Enqueue(id);
|
||||
return Task.CompletedTask;
|
||||
},
|
||||
});
|
||||
await using var engine = new JobEngine(fx.Db, service.CreateJobHandlers(),
|
||||
new JobEngineOptions { Workers = 1, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) });
|
||||
await engine.StartAsync(fx.Ct);
|
||||
|
||||
// A camera uploads into the second sequence while the job waits for the first.
|
||||
Job job;
|
||||
using (await LeaseFile.AcquireAsync(fx.Paths.LocksDirectory, $"seq-{first:N}", "editor", TimeSpan.FromSeconds(5), cancellationToken: fx.Ct))
|
||||
{
|
||||
job = await engine.EnqueueAsync(ArchiveJobs.Reorganize(fx.Archive, plan, templates), cancellationToken: fx.Ct);
|
||||
var until = DateTime.UtcNow.AddSeconds(TestTiming.Budget(15));
|
||||
while (RenameJournal.ListPending(fx.Paths).Count == 0)
|
||||
{
|
||||
Assert.True(DateTime.UtcNow < until, "the job never started its plan");
|
||||
await Task.Delay(20, fx.Ct);
|
||||
}
|
||||
await UploadAsync(fx, 1, firstSeed: 30, firstSecond: 3605, key: "upload-2");
|
||||
}
|
||||
var received = await FilesAsync(fx, second);
|
||||
|
||||
// Its folder moves and its files keep their names; the sequence after it is reorganized; the job then says why it failed.
|
||||
var failed = await fx.WaitForAsync(job.Id, JobState.Failed, (int)TestTiming.Budget(60));
|
||||
Assert.Equal(Loc.Plural("Archive.Error.ReorganizeKept", 1), failed.Error);
|
||||
Assert.Contains(Loc.L("Archive.Log.Reorganized", 3, 4), failed.LogTail);
|
||||
Assert.Contains(Loc.L("Archive.Log.ReorganizeFilesKept", plan.Steps.Single(s => s.SequenceId == second).FromFolder), failed.LogTail);
|
||||
Assert.Equal(("moved/2026-09-28 18-00", "moved/2026-09-28 19-00", "moved/2026-09-28 20-00"),
|
||||
(await FolderAsync(fx, first), await FolderAsync(fx, second), await FolderAsync(fx, third)));
|
||||
Assert.SequenceEqual(received, await FilesAsync(fx, second));
|
||||
foreach (var id in new[] { first, third }) Assert.SequenceEqual(["originals/001.JPG", "originals/002.JPG"], await FilesAsync(fx, id));
|
||||
Assert.SequenceEqual([first, second, third], told);
|
||||
Assert.Empty(RenameJournal.ListPending(fx.Paths));
|
||||
}
|
||||
|
||||
@@ -452,7 +521,7 @@ public sealed class ReorganizeTests(TestContext context)
|
||||
await using var fx = await CreateAsync(context);
|
||||
var first = await UploadAsync(fx, 2);
|
||||
var second = await UploadAsync(fx, 2, firstSeed: 20, firstSecond: 3600, key: "upload-2");
|
||||
var templates = new RenameTemplates(File: "{n:000}");
|
||||
var templates = new RenameTemplates(File: "{n:000}", Title: "Roof");
|
||||
var plan = await fx.Service.PlanReorganizeAsync(fx.Archive.Id, [first, second], templates, fx.Ct);
|
||||
var told = new ConcurrentQueue<(Guid, string)>();
|
||||
var service = new ArchiveService(fx.Db, new ArchiveServiceOptions
|
||||
@@ -469,7 +538,7 @@ public sealed class ReorganizeTests(TestContext context)
|
||||
new JobEngineOptions { Workers = 1, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) });
|
||||
await engine.StartAsync(fx.Ct);
|
||||
|
||||
// An editor holds the first sequence while the job starts its plan (planned again: its own id); a photo reaches the second meanwhile.
|
||||
// An editor holds the first sequence while the job starts its plan (planned again: its own id); the second is retitled meanwhile.
|
||||
Job job;
|
||||
using (await LeaseFile.AcquireAsync(fx.Paths.LocksDirectory, $"seq-{first:N}", "editor", TimeSpan.FromSeconds(5), cancellationToken: fx.Ct))
|
||||
{
|
||||
@@ -480,7 +549,7 @@ public sealed class ReorganizeTests(TestContext context)
|
||||
Assert.True(DateTime.UtcNow < until, "the job never started its plan");
|
||||
await Task.Delay(20, fx.Ct);
|
||||
}
|
||||
await UploadAsync(fx, 1, firstSeed: 30, firstSecond: 3605, key: "upload-2");
|
||||
await fx.Service.SetSequenceInfoAsync(second, "Roof north", [], 0, null, fx.Ct);
|
||||
}
|
||||
|
||||
var failed = await fx.WaitForAsync(job.Id, JobState.Failed, (int)TestTiming.Budget(60));
|
||||
@@ -492,7 +561,7 @@ public sealed class ReorganizeTests(TestContext context)
|
||||
Assert.SequenceEqual([first], journal.Completed);
|
||||
Assert.SequenceEqual(["originals/001.JPG", "originals/002.JPG"], await FilesAsync(fx, first));
|
||||
var untouched = await FilesAsync(fx, second);
|
||||
Assert.Count(3, untouched);
|
||||
Assert.Equal("Roof north", (await fx.Db.Sequences.GetAsync(second))!.Title);
|
||||
|
||||
// An archive open leaves it alone; the retry checks the rest as a plan applied anew and gives it up.
|
||||
Assert.Equal(0, await fx.Service.Renamer.ResumePendingAsync(fx.Archive, fx.Ct));
|
||||
@@ -617,8 +686,12 @@ public sealed class ReorganizeTests(TestContext context)
|
||||
Assert.SequenceEqual(before, await FilesAsync(fx, id));
|
||||
Assert.Null(FileRenameJournal.Read(folder));
|
||||
|
||||
// Interrupted there, the plan resumes at the next open without its files; a new dry run renames none of them.
|
||||
await fx.Service.OpenAsync(fx.Archive.RootPath, fx.Ct);
|
||||
// Interrupted there, its job resumes the plan without its files and says why; a new dry run renames none of them.
|
||||
var engine = await fx.StartEngineAsync();
|
||||
var failed = await fx.WaitForAsync((await fx.EnqueueWithCheckpointAsync(engine, ArchiveJobs.Reorganize(fx.Archive, plan, templates),
|
||||
"plan:" + plan.Id.ToString("N"))).Id, JobState.Failed);
|
||||
Assert.Equal(Loc.Plural("Archive.Error.ReorganizeNewer", 1), failed.Error);
|
||||
Assert.Contains(Loc.L("Archive.Log.ReorganizeNewerKept", plan.Steps.Single().FromFolder), failed.LogTail);
|
||||
Assert.Empty(RenameJournal.ListPending(fx.Paths));
|
||||
Assert.SequenceEqual(before, await FilesAsync(fx, id));
|
||||
var again = (await fx.Service.PlanReorganizeAsync(fx.Archive.Id, [id], templates, fx.Ct)).Steps.Single();
|
||||
@@ -669,16 +742,114 @@ public sealed class ReorganizeTests(TestContext context)
|
||||
var plan = await fx.Service.PlanReorganizeAsync(fx.Archive.Id, [id], new RenameTemplates(File: "{n:000}"), fx.Ct);
|
||||
Assert.Equal((2, RenameBlock.Unhashed), (plan.Steps.Single().FileChanges, plan.Steps.Single().Blocked.Single().Reason));
|
||||
|
||||
// A verify hashes it afterwards, which leaves the frames version as it is: the plan would now rename it too.
|
||||
// A verify hashes it afterwards, which leaves the frames version as it is: the plan would now rename it too, so none is.
|
||||
ManifestStore.WriteSequence(folder.ManifestPath, manifest);
|
||||
var before = await FilesAsync(fx, id);
|
||||
await Assert.ThrowsAsync<StaleRenamePlanException>(() => fx.Service.ApplyRenameAsync(plan, null, fx.Ct));
|
||||
var result = await fx.Service.ApplyRenameAsync(plan, null, fx.Ct);
|
||||
Assert.SequenceEqual([id], result.FilesKept);
|
||||
Assert.Empty(result.Changed);
|
||||
Assert.SequenceEqual(before, await FilesAsync(fx, id));
|
||||
|
||||
// Interrupted before its step, it resumes without renaming any of them.
|
||||
// So it is when resumed after an interruption before its step.
|
||||
RenameJournal.Write(fx.Paths, new RenameJournal(plan, []));
|
||||
var result = Assert.NotNull(await fx.Service.Renamer.ResumeAsync(fx.Archive, plan.Id, null, fx.Ct));
|
||||
result = Assert.NotNull(await fx.Service.Renamer.ResumeAsync(fx.Archive, plan.Id, null, fx.Ct));
|
||||
Assert.SequenceEqual([id], result.FilesKept);
|
||||
Assert.SequenceEqual(before, await FilesAsync(fx, id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task NamesComeFromATitleEditedAfterThePlanNever()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var id = await UploadAsync(fx, 2);
|
||||
await fx.Service.SetSequenceInfoAsync(id, "Roof", [], 0, null, fx.Ct);
|
||||
var from = await FolderAsync(fx, id);
|
||||
// The folder and the files are named after the title, which the plan does not change.
|
||||
var plan = await fx.Service.PlanReorganizeAsync(fx.Archive.Id, [id], new RenameTemplates(Folder: "moved/{title}", File: "{title}_{n:000}"), fx.Ct);
|
||||
Assert.True(plan.Steps.Single() is { ToFolder: "moved/Roof", FileChanges: 2, Retitles: false, NamedByTitle: true });
|
||||
var before = await FilesAsync(fx, id);
|
||||
|
||||
// Retitled once the plan's own checks passed: refused before anything of it moves.
|
||||
await Assert.ThrowsAsync<StaleRenamePlanException>(() => fx.Service.Renamer.ApplyAsync(fx.Archive, plan,
|
||||
async _ => await fx.Service.SetSequenceInfoAsync(id, "Roof north", [], 0, null, fx.Ct), null, fx.Ct));
|
||||
Assert.Equal(from, await FolderAsync(fx, id));
|
||||
Assert.SequenceEqual(before, await FilesAsync(fx, id));
|
||||
|
||||
// Held by its job, the rest is refused as a plan applied anew; interrupted, it is dropped.
|
||||
Assert.True(MassRenamer.Hold(fx.Archive, plan.Id));
|
||||
await Assert.ThrowsAsync<StaleRenamePlanException>(() => fx.Service.Renamer.ResumeAsync(fx.Archive, plan.Id, null, fx.Ct));
|
||||
RenameJournal.Write(fx.Paths, new RenameJournal(plan, []));
|
||||
var result = Assert.NotNull(await fx.Service.Renamer.ResumeAsync(fx.Archive, plan.Id, null, fx.Ct));
|
||||
Assert.SequenceEqual([id], result.Dropped);
|
||||
Assert.Equal(from, await FolderAsync(fx, id));
|
||||
Assert.SequenceEqual(before, await FilesAsync(fx, id));
|
||||
Assert.Equal("Roof north", (await fx.Db.Sequences.GetAsync(id))!.Title);
|
||||
Assert.Empty(RenameJournal.ListPending(fx.Paths));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task APlanAnArchiveOpenCompletesTellsTheSequencesItChanged()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var id = await UploadAsync(fx, 1);
|
||||
var plan = await fx.Service.PlanReorganizeAsync(fx.Archive.Id, [id], new RenameTemplates(Folder: "moved/{date}"), fx.Ct);
|
||||
// Its job yielded to another, or its host stopped, before the step: the plan is not held.
|
||||
RenameJournal.Write(fx.Paths, new RenameJournal(plan, []));
|
||||
var told = new ConcurrentQueue<(Guid, string)>();
|
||||
var service = new ArchiveService(fx.Db, new ArchiveServiceOptions
|
||||
{
|
||||
Owner = "test",
|
||||
SequenceChanged = (sequence, change, _) =>
|
||||
{
|
||||
told.Enqueue((sequence, change));
|
||||
return Task.CompletedTask;
|
||||
},
|
||||
});
|
||||
|
||||
await service.OpenAsync(fx.Archive.RootPath, fx.Ct);
|
||||
Assert.SequenceEqual([(id, MassRenamer.Change)], told);
|
||||
Assert.Equal("moved/2026-09-28", await FolderAsync(fx, id));
|
||||
Assert.Empty(RenameJournal.ListPending(fx.Paths));
|
||||
}
|
||||
|
||||
[Test]
|
||||
[WindowsOnly]
|
||||
public async Task AMovedFolderWhoseSidecarCannotBeWrittenHasEveryIndexRowFollowIt()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var parent = await UploadAsync(fx, 2);
|
||||
var folder = (await fx.Db.Sequences.GetAsync(parent))!.Folder;
|
||||
Photos.WriteSeries(Path.Combine(fx.Paths.FromRelative(folder), "extra"), Start.AddDays(3), 2, firstSeed: 50);
|
||||
await fx.ScanAsync();
|
||||
var nested = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).Single(l => l.Folder == folder + "/extra").Id;
|
||||
var plan = await fx.Service.PlanReorganizeAsync(fx.Archive.Id, [parent], new RenameTemplates(Folder: "moved/{date}", Title: "Roof"), fx.Ct);
|
||||
Assert.SequenceEqual([(parent, false), (nested, true)], plan.Steps.Select(s => (s.SequenceId, s.Carried)));
|
||||
string[] sidecars = [fx.Paths.Sequence(folder).ManifestPath, fx.Paths.Sequence("moved/2026-09-28").ManifestPath];
|
||||
void Writable(bool writable)
|
||||
{
|
||||
foreach (var sidecar in sidecars.Where(File.Exists))
|
||||
File.SetAttributes(sidecar, writable ? File.GetAttributes(sidecar) & ~FileAttributes.ReadOnly : File.GetAttributes(sidecar) | FileAttributes.ReadOnly);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Its sidecar can be read, not written: the new title fails once the folder moved.
|
||||
Writable(false);
|
||||
await Assert.ThrowsAsync<UnauthorizedAccessException>(() => fx.Service.ApplyRenameAsync(plan, null, fx.Ct));
|
||||
// Both index rows followed the folder all the same; the title waits for the rest of the step.
|
||||
Assert.Equal(("moved/2026-09-28", "moved/2026-09-28/extra"), (await FolderAsync(fx, parent), await FolderAsync(fx, nested)));
|
||||
Assert.Null((await fx.Db.Sequences.GetAsync(parent))!.Title);
|
||||
Assert.Empty(Assert.NotNull(RenameJournal.Read(fx.Paths, plan.Id)).Completed);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Writable(true);
|
||||
}
|
||||
|
||||
var result = Assert.NotNull(await fx.Service.Renamer.ResumeAsync(fx.Archive, plan.Id, null, fx.Ct));
|
||||
Assert.SequenceEqual([parent, nested], result.Changed);
|
||||
Assert.Equal("Roof", (await fx.Db.Sequences.GetAsync(parent))!.Title);
|
||||
await FilesAsync(fx, parent);
|
||||
Assert.Empty(RenameJournal.ListPending(fx.Paths));
|
||||
}
|
||||
}
|
||||
@@ -284,6 +284,35 @@ public sealed class ReorganizeTests(TestContext context)
|
||||
Assert.SequenceEqual(["001.JPG", "002.JPG", "003.JPG"], (await server.Database.Frames.ListBySequenceAsync(id)).Select(f => f.FileName));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnUploadFiledUnderTheNameThePlanGivesLeavesThePlanToConfirm()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
||||
var admin = await server.AdminAsync();
|
||||
Guid id = default;
|
||||
for (var i = 0; i < 2; i++) id = (await Uploads.UploadAsync(editor, Photo(i), $"IMG_{i + 1:0000}.JPG", sessionKey: "roof")).SequenceId;
|
||||
await UntilIdleAsync(server);
|
||||
// The archive files new photos as the Reorganize names them; the two filed before do not follow it yet.
|
||||
using (var read = await admin.GetAsync("/api/v1/archive/settings"))
|
||||
{
|
||||
var settings = await read.ReadAsync(ProtocolJson.Default.ArchiveSettingsDto);
|
||||
using var put = await admin.PutAsJsonAsync("/api/v1/archive/settings", settings with { FileTemplate = "{n:000}" }, ProtocolJson.Default.ArchiveSettingsDto);
|
||||
await put.ExpectAsync(HttpStatusCode.OK);
|
||||
}
|
||||
var request = new ReorganizeRequest([id], false, null, "{n:000}", null, DryRun: true);
|
||||
var shown = await DryRunAsync(admin, request);
|
||||
Assert.Equal(2, shown.Files);
|
||||
|
||||
// The camera still uploads before Apply, a photo after the others: it takes the name the plan leaves it, so the plan
|
||||
// confirmed is the one now.
|
||||
await Uploads.UploadAsync(editor, Photo(2), "IMG_0003.JPG", sessionKey: "roof", capturedAt: new DateTime(2026, 9, 28, 18, 3, 0));
|
||||
await UntilIdleAsync(server);
|
||||
Assert.Equal("003.JPG", (await server.Database.Frames.ListBySequenceAsync(id))[^1].FileName);
|
||||
await UntilJobAsync(admin, await ConfirmAsync(admin, request with { DryRun = false, Fingerprint = shown.Fingerprint }), "done");
|
||||
Assert.SequenceEqual(["001.JPG", "002.JPG", "003.JPG"], (await server.Database.Frames.ListBySequenceAsync(id)).Select(f => f.FileName));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AMoveJobPausedBetweenTwoSequencesCompletesTheRestWhenResumed()
|
||||
{
|
||||
|
||||
@@ -19,11 +19,13 @@ internal static class Uploads
|
||||
|
||||
public static string Sha(byte[] data) => Convert.ToHexStringLower(SHA256.HashData(data));
|
||||
|
||||
/// <param name="capturedAt">The capture time the client sends; null: 2026-09-28 18:02:11.</param>
|
||||
public static async Task<StartUploadResponse> StartAsync(HttpClient client, byte[] data, string fileName = "IMG_0001.CR3", Guid? sequenceId = null,
|
||||
string? sessionKey = null, HttpStatusCode expected = HttpStatusCode.Created)
|
||||
string? sessionKey = null, HttpStatusCode expected = HttpStatusCode.Created, DateTime? capturedAt = null)
|
||||
{
|
||||
using var response = await client.PostAsJsonAsync("/api/v1/uploads",
|
||||
new StartUploadRequest(sequenceId, fileName, data.Length, Sha(data), new DateTime(2026, 9, 28, 18, 2, 11), sessionKey), ProtocolJson.Default.StartUploadRequest);
|
||||
new StartUploadRequest(sequenceId, fileName, data.Length, Sha(data), capturedAt ?? new DateTime(2026, 9, 28, 18, 2, 11), sessionKey),
|
||||
ProtocolJson.Default.StartUploadRequest);
|
||||
await response.ExpectAsync(expected);
|
||||
return await response.ReadAsync(ProtocolJson.Default.StartUploadResponse);
|
||||
}
|
||||
@@ -40,9 +42,9 @@ internal static class Uploads
|
||||
|
||||
/// <summary>Start, every chunk, complete; returns the committed frame.</summary>
|
||||
public static async Task<CompleteUploadResponse> UploadAsync(HttpClient client, byte[] data, string fileName = "IMG_0001.CR3", Guid? sequenceId = null,
|
||||
string? sessionKey = null)
|
||||
string? sessionKey = null, DateTime? capturedAt = null)
|
||||
{
|
||||
var start = await StartAsync(client, data, fileName, sequenceId, sessionKey);
|
||||
var start = await StartAsync(client, data, fileName, sequenceId, sessionKey, capturedAt: capturedAt);
|
||||
var id = Assert.NotNull(start.UploadId);
|
||||
var chunks = (data.Length + start.ChunkSize - 1) / start.ChunkSize;
|
||||
for (var i = 0; i < chunks; i++)
|
||||
|
||||
Reference in new issue
Block a user