fix(archive): mass rename refuses a folder that gained a sequence after the dry run

A scan can index a sequence inside a folder the plan moves after the dry run was
shown (photos copied into a subfolder). The folder would then move with it while
its index row stayed behind. Apply now lists the index again before anything moves
and under each moving step's leases just before the move, and throws
StaleRenamePlanException naming the newcomer. A resumed journal drops such a step
while its folder has not moved yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 15:58:47 +00:00
1 parent fba05c2840
commit e01887ef51
2 files changed
+94 -3

No files matched your search

@@ -105,6 +105,57 @@ public sealed class MassRenameTests(TestContext context)
Assert.True(Directory.Exists(fx.Paths.FromRelative(other.Steps[0].ToFolder)));
}
[Test]
public async Task ASequenceIndexedInsideAMovingFolderAfterTheDryRunMakesThePlanStale()
{
await using var fx = await CreateAsync(context);
Photos.WriteSeries(Path.Combine(fx.Paths.Root, "trips"), Start, 2);
await fx.ScanAsync();
var trips = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).Single();
var plan = await fx.Service.PlanRenameAsync(fx.Archive.Id, [trips.Id], "moved/{date}", null, fx.Ct);
Assert.Count(1, plan.Steps);
// Photos copied into a subfolder and indexed by the next scan, after the dry run was shown.
Photos.WriteSeries(Path.Combine(fx.Paths.Root, "trips", "day2"), Start.AddDays(1), 2, firstSeed: 10);
await fx.ScanAsync();
var day2 = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).Single(l => l.Folder == "trips/day2");
var stale = await Assert.ThrowsAsync<StaleRenamePlanException>(() => fx.Service.ApplyRenameAsync(plan, null, fx.Ct));
Assert.Equal(day2.Id, stale.SequenceId);
Assert.Empty(RenameJournal.ListPending(fx.Paths));
Assert.SequenceEqual(["trips|trips|trips/IMG_0001.JPG,trips/IMG_0002.JPG", "trips/day2|day2|trips/day2/IMG_0001.JPG,trips/day2/IMG_0002.JPG"],
await StateAsync(fx, [trips.Id, day2.Id]));
}
[Test]
public async Task AFolderThatGainedASequenceWhileTheRenameRanStaysWhereItIs()
{
await using var fx = await CreateAsync(context);
var ids = await PopulateAsync(fx);
var plan = await fx.Service.PlanRenameAsync(fx.Archive.Id, [ids[0], ids[1]], "renamed/{date}", null, fx.Ct);
Assert.SequenceEqual(["inbox/a", "inbox/b"], plan.Steps.Select(s => s.FromFolder));
// Once the first folder moved, a scan indexes photos copied into the second one.
var late = Path.Combine(fx.Paths.Root, "inbox", "b", "late");
var progress = new SyncProgress(done =>
{
if (done != 1) return;
Photos.WriteSeries(late, Start.AddDays(5), 2, firstSeed: 40);
fx.ScanAsync().GetAwaiter().GetResult();
});
await Assert.ThrowsAsync<StaleRenamePlanException>(() => fx.Service.ApplyRenameAsync(plan, progress, fx.Ct));
var newcomer = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).Single(l => l.Folder == "inbox/b/late");
Assert.True(Directory.Exists(late), "the folder did not move");
Assert.Count(1, RenameJournal.ListPending(fx.Paths).Single().Completed);
// Resumed, the rest of the plan runs but that step is dropped: every index row still names its files.
Assert.Equal(1, await fx.Service.Renamer.ResumePendingAsync(fx.Archive, fx.Ct));
Assert.Empty(RenameJournal.ListPending(fx.Paths));
var state = await StateAsync(fx, [ids[0], ids[1], newcomer.Id]);
Assert.SequenceEqual(["renamed/2026-09-28", "inbox/b", "inbox/b/late"], state.Select(s => s.Split('|')[0]));
Assert.Equal(0, (await fx.ScanAsync()).Indexed);
}
/// <summary>"trips" with photos of its own, "trips/day2" inside it and "trips/day2/dawn" inside that: three nested sequences.</summary>
private static async Task<List<Guid>> NestedAsync(ArchiveFixture fx)
{