fix(archive): stable Reorganize folders, settle trash restores first, retranslate Reorganize
Integrating owner review 2 phase 4a (R7) onto the reviewed phase 3 code. - RenamePlanner: a sequence already at its expanded folder, or at the " (n)" of it that filing or an earlier Reorganize gave it, keeps that folder (in the template's case, unless another sequence holds that exact name). The planner seeded its claimed folders with the sequence's own, so "2026-09-28 (2)" was planned to "(3)" and back to "(2)" by the next run; a second Reorganize with the same templates now plans nothing. - MassRenamer: each step settles what a crash or a busy trash entry left in its sequences (SequenceWriter.ReadLocked) and waits, as a sequence deletion does, while a restore from the trash is not settled (FrameTrash.EnsureSettled), before its folder or any file name changes: that restore's journal names files by the names they had. Trash records keep restoring after a Reorganize: into the sequence where it is now, under the record's own name made free (_2) with its companion following. - Jobs.Kind.Move and Admin.Job.Kind.Move now mean "Reorganize": their old "Move archive" translations are removed from the 36 other cultures so they are translated again. - Tests: a second run plans nothing (planner and archive), companions found alike by the trash and Reorganize whatever their case, a photo deleted before a Reorganize comes back beside the new names, a Reorganize waits for a busy restore and settles it first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ad72501f11
commit
950524929c
78 files changed
+183
-75
No files matched your search
@@ -1127,6 +1127,90 @@ public abstract class FrameTrashTests(TestContext context) : DatabaseTest(contex
|
||||
Assert.SequenceEqual(frames.Select(f => f.Id), (await fx.Db.Frames.ListBySequenceAsync(sequence.Id)).Select(f => f.Id));
|
||||
}
|
||||
|
||||
/// <summary>The files of a sequence as its sidecar names them, after checking that the index names the same ones and that each holds its photo.</summary>
|
||||
private static async Task<List<string>> FilesAsync(ArchiveFixture fx, Guid id)
|
||||
{
|
||||
var sequence = Assert.NotNull(await fx.Db.Sequences.GetAsync(id));
|
||||
var folder = fx.Paths.Sequence(sequence.Folder);
|
||||
var sidecar = ManifestStore.ReadSequence(folder.ManifestPath);
|
||||
var rows = await fx.Db.Frames.ListBySequenceAsync(id);
|
||||
Assert.SequenceEqual(sidecar.Frames.Select(f => (f.Id, folder.ArchiveRelative(f.File))), rows.Select(r => (r.Id, r.RelativePath)));
|
||||
foreach (var frame in sidecar.Frames) Assert.Equal(frame.Sha256, Sha(folder.File(frame.File)));
|
||||
return [.. sidecar.Frames.Select(f => f.File)];
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PhotosDeletedBeforeAReorganizeComeBackBesideTheNamesItGave()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (sequence, frames) = await UploadedAsync(fx, 4);
|
||||
var numbered = new RenameTemplates(File: "{n:000}");
|
||||
await fx.Service.ApplyRenameAsync(await fx.Service.PlanReorganizeAsync(fx.Archive.Id, [sequence.Id], numbered, fx.Ct), null, fx.Ct);
|
||||
var folder = fx.Paths.Sequence(sequence.Folder);
|
||||
File.WriteAllText(folder.File("originals/002.xmp"), "second");
|
||||
await fx.ScanAsync();
|
||||
// The record keeps the names the photo and its companion had: 002.JPG and 002.xmp.
|
||||
var deleted = await DeleteAsync(fx, (await fx.Db.Sequences.GetAsync(sequence.Id))!, frames[1]);
|
||||
Assert.SequenceEqual(["originals/002.JPG", "originals/002.xmp"], FrameTrash.ReadEntry(fx.Paths, deleted.Entry.Name)!.Photos.Single().Files.Select(f => f.From));
|
||||
|
||||
// Moved and renamed since: the photos that stay take 001-003, the deleted photo's name included.
|
||||
var moved = new RenameTemplates(Folder: "moved/{date}", File: "{n:000}");
|
||||
await fx.Service.ApplyRenameAsync(await fx.Service.PlanReorganizeAsync(fx.Archive.Id, [sequence.Id], moved, fx.Ct), null, fx.Ct);
|
||||
Assert.SequenceEqual(["originals/001.JPG", "originals/002.JPG", "originals/003.JPG"], await FilesAsync(fx, sequence.Id));
|
||||
|
||||
// Restored into the sequence where it is now, under its own name made free: its companion follows that name.
|
||||
var restored = await fx.Service.RestoreEntryAsync(fx.Archive.Id, deleted.Entry.Name, null, fx.Ct);
|
||||
Assert.Equal(1, restored.Photos);
|
||||
var row = (await fx.Db.Sequences.GetAsync(sequence.Id))!;
|
||||
Assert.Equal("moved/2026-09-28", row.Folder);
|
||||
Assert.SequenceEqual(["originals/001.JPG", "originals/002_2.JPG", "originals/002.JPG", "originals/003.JPG"], await FilesAsync(fx, sequence.Id));
|
||||
var now = fx.Paths.Sequence(row.Folder);
|
||||
Assert.Equal(frames[1].Sha256, Sha(now.File("originals/002_2.JPG")));
|
||||
Assert.Equal("second", File.ReadAllText(now.File("originals/002_2.xmp")));
|
||||
Assert.SequenceEqual(frames.Select(f => f.Id), (await fx.Db.Frames.ListBySequenceAsync(sequence.Id)).Select(f => f.Id));
|
||||
Assert.DoesNotContain(TrashKind.Photos, (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Select(e => e.Kind));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AReorganizeWaitsForARestoreItsEntryHoldsThenSettlesItFirst()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (sequence, frames) = await UploadedAsync(fx, 3);
|
||||
var folder = fx.Paths.Sequence(sequence.Folder);
|
||||
var deleted = await DeleteAsync(fx, sequence, frames[1]);
|
||||
// A restore died after moving the original back, before the sidecar; the entry is being emptied meanwhile.
|
||||
var journal = RestoreJournal(fx, deleted.Entry);
|
||||
journal.Write(folder);
|
||||
var back = journal.Photos[0].Moves[0];
|
||||
File.Move(fx.Paths.FromRelative(back.To), folder.File(back.From));
|
||||
var templates = new RenameTemplates(Folder: "moved/{date}", File: "{n:000}");
|
||||
var plan = await fx.Service.PlanReorganizeAsync(fx.Archive.Id, [sequence.Id], templates, fx.Ct);
|
||||
Assert.Equal(2, plan.Steps.Single().FileChanges);
|
||||
var before = await FilesAsync(fx, sequence.Id);
|
||||
|
||||
using (Assert.NotNull(LeaseFile.TryAcquire(fx.Paths.LocksDirectory, SequenceTrash.EntryLease(deleted.Entry.Name), "other")))
|
||||
{
|
||||
// Neither the folder nor a file name changes under that restore.
|
||||
await Assert.ThrowsAsync<LeaseUnavailableException>(() => fx.Service.ApplyRenameAsync(plan, null, fx.Ct));
|
||||
Assert.Equal(sequence.Folder, (await fx.Db.Sequences.GetAsync(sequence.Id))!.Folder);
|
||||
Assert.SequenceEqual(before, await FilesAsync(fx, sequence.Id));
|
||||
Assert.FileExists(folder.RestoreJournalPath);
|
||||
Assert.FileExists(folder.File(back.From));
|
||||
}
|
||||
|
||||
// Once the entry is free the rename resumes: the restore is settled first (the original goes back into its entry),
|
||||
// then the folder moves and the files are renamed; the photo then comes back beside them.
|
||||
Assert.Equal(1, await fx.Service.Renamer.ResumePendingAsync(fx.Archive, fx.Ct));
|
||||
Assert.Equal(frames[1].Sha256, Sha(fx.Paths.FromRelative(back.To)));
|
||||
var row = (await fx.Db.Sequences.GetAsync(sequence.Id))!;
|
||||
Assert.Equal("moved/2026-09-28", row.Folder);
|
||||
Assert.FileMissing(fx.Paths.Sequence(row.Folder).RestoreJournalPath);
|
||||
Assert.SequenceEqual(["originals/001.JPG", "originals/002.JPG"], await FilesAsync(fx, sequence.Id));
|
||||
Assert.Equal(1, (await fx.Service.RestoreEntryAsync(fx.Archive.Id, deleted.Entry.Name, null, fx.Ct)).Photos);
|
||||
Assert.SequenceEqual(frames.Select(f => f.Id), (await fx.Db.Frames.ListBySequenceAsync(sequence.Id)).Select(f => f.Id));
|
||||
Assert.Count(3, await FilesAsync(fx, sequence.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ARestoreIntoANewSequenceStoppedBetweenPhotosKeepsItsRecordTrue()
|
||||
{
|
||||
|
||||
@@ -97,6 +97,24 @@ public sealed class ReorganizeTests(TestContext context)
|
||||
Assert.False((await fx.Service.PlanReorganizeAsync(fx.Archive.Id, [id], templates, fx.Ct)).Steps.Any(s => s.Changes));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ASecondReorganizeWithTheSameTemplatesPlansNothing()
|
||||
{
|
||||
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");
|
||||
// One day for both: the second sequence's folder gets " (2)".
|
||||
var templates = new RenameTemplates(Folder: "days/{date}", File: "{date}_{n:000}");
|
||||
await fx.Service.ApplyRenameAsync(await fx.Service.PlanReorganizeAsync(fx.Archive.Id, null, templates, fx.Ct), null, fx.Ct);
|
||||
var folders = new List<string>();
|
||||
foreach (var id in new[] { first, second }) folders.Add((await fx.Db.Sequences.GetAsync(id))!.Folder);
|
||||
Assert.SequenceEqual(["days/2026-09-28", "days/2026-09-28 (2)"], folders.Order(StringComparer.Ordinal));
|
||||
|
||||
var again = await fx.Service.PlanReorganizeAsync(fx.Archive.Id, null, templates, fx.Ct);
|
||||
Assert.Count(2, again.Subjects);
|
||||
Assert.False(again.Steps.Any(s => s.Changes), string.Join(", ", again.Steps.Select(s => $"{s.FromFolder}>{s.ToFolder}")));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UploadsAndImportsAreNamedAsReorganizeNamesThem()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user