Second review of owner review 2, phase 4a (Reorganize), cross-cutting findings. - A second index of the archive (a desktop sharing a server's archive, or a folder moved by hand) moved the sequence row at its next scan but left renders.output_path at the old folder: the export answered 404 there. The scan now rebases the render rows in the same transaction when it finds a sequence in another folder. - The rebase covers every render of the archive under the old folder: the sequence's own exports always, and another sequence's export saved inside it (through the Export folder picker) when its file moved with the folder. That check keeps the exports of a sequence that took the old folder since (a later Reorganize on the other index) where they are. A change of case only counts as a move. - The desktop now tells an open editor after each Reorganize step (ArchiveServiceOptions.SequenceChanged -> AppServices.SequenceReorganized), and the editor reads its sequence again where it is now, so frames no longer decode blank until the next save. LocalSequenceSource.LoadAsync also requires the same folder on both row reads, so a move that commits during the frame listing is never taken as current. - Writers that wrote without the lease, or trusted a row another index had moved, could create the old folder again. A proxy sent by a client is now filed under the sequence's lease into the folder the index has then, and a gone sequence or folder answers 404 (sequence_busy 503 after the lease wait). The server files a render a client or render node sends under the lease (ArchiveService.UnderLeaseAsync). "Save both" keeps its copy under the lease. The proxy pass holds the thumbnail lease, as the thumbnail pass does. The deep analysis opens its checkpoint under the sequence lock only while the folder is still there, and a pass whose folder another process moved records nothing (the job logs Advisor.Log.FramesChangedElsewhere). Editor saves and sync applies refuse when the folder is gone. - ARCHITECTURE.md no longer claims more than this: a render of another process whose folder moves while it runs is still not stopped. - ArchiveVerifier.FlushAsync: corrected the comment. A move keeps each file's stamp, so only a check that found the file missing no longer applies. Tests cover the scan path with a later swap into the vacated folder, another sequence's export inside a moving folder, a photo deletion refused when the folder moved while it waited for the lease, proxies sent and made after a move, Save both during a move, a deep analysis whose folder another index moved, a render filed after a move on the server, and an open editor reloaded by a Move job. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit c857b98f795b22956c060f1c5acf4d94b6d1c994)
460 lines
26 KiB
C#
460 lines
26 KiB
C#
using Chronos.Core.Archiving;
|
|
using Chronos.Core.Model;
|
|
using Chronos.Core.Project;
|
|
using Chronos.Data.Archiving;
|
|
using Chronos.Data.Processing;
|
|
using Chronos.Testing;
|
|
using static Chronos.Data.Tests.Archiving.ArchiveFixture;
|
|
|
|
namespace Chronos.Data.Tests.Archiving;
|
|
|
|
[Category("archive")]
|
|
public sealed class MassRenameTests(TestContext context)
|
|
{
|
|
private static async Task<List<Guid>> PopulateAsync(ArchiveFixture fx)
|
|
{
|
|
Photos.WriteSeries(Path.Combine(fx.Paths.Root, "inbox", "a"), Start, 3);
|
|
Photos.WriteSeries(Path.Combine(fx.Paths.Root, "inbox", "b"), Start.AddDays(1), 3, firstSeed: 10);
|
|
Photos.WriteSeries(Path.Combine(fx.Paths.Root, "inbox", "c"), Start.AddDays(2), 3, firstSeed: 20);
|
|
await fx.ScanAsync();
|
|
return (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).OrderBy(l => l.Folder).Select(l => l.Id).ToList();
|
|
}
|
|
|
|
/// <summary>Folder, title and every frame path of each sequence as the index and the disk see them.</summary>
|
|
private static async Task<List<string>> StateAsync(ArchiveFixture fx, IEnumerable<Guid> ids)
|
|
{
|
|
var state = new List<string>();
|
|
foreach (var id in ids)
|
|
{
|
|
var s = (await fx.Db.Sequences.GetAsync(id))!;
|
|
var sidecar = ManifestStore.ReadSequence(fx.Paths.Sequence(s.Folder).ManifestPath);
|
|
Assert.Equal(id, sidecar.Id);
|
|
Assert.Equal(s.Title, sidecar.Title);
|
|
foreach (var f in await fx.Db.Frames.ListBySequenceAsync(id)) Assert.FileExists(fx.Paths.FromRelative(f.RelativePath));
|
|
state.Add($"{s.Folder}|{s.Title}|{string.Join(',', (await fx.Db.Frames.ListBySequenceAsync(id)).Select(f => f.RelativePath))}");
|
|
}
|
|
return state;
|
|
}
|
|
|
|
private static List<string> Expected(RenamePlan plan, IEnumerable<string> before) =>
|
|
before.Select(line =>
|
|
{
|
|
var folder = line.Split('|')[0];
|
|
var step = plan.Steps.SingleOrDefault(s => s.FromFolder == folder);
|
|
return step is null ? line : $"{step.ToFolder}|{step.ToTitle}|{line.Split('|')[2].Replace(step.FromFolder + "/", step.ToFolder + "/")}";
|
|
}).ToList();
|
|
|
|
[Test]
|
|
public async Task ApplyingAPlanDoesExactlyWhatTheDryRunSaid()
|
|
{
|
|
await using var fx = await CreateAsync(context);
|
|
var ids = await PopulateAsync(fx);
|
|
var before = await StateAsync(fx, ids);
|
|
var plan = await fx.Service.PlanRenameAsync(fx.Archive.Id, ids, "{year}/{date} {title}", "Trip", fx.Ct);
|
|
Assert.SequenceEqual(["2026/2026-09-28 Trip", "2026/2026-09-29 Trip", "2026/2026-09-30 Trip"], plan.Steps.Select(s => s.ToFolder));
|
|
Assert.SequenceEqual(before, await StateAsync(fx, ids));
|
|
|
|
await fx.Service.ApplyRenameAsync(plan, null, fx.Ct);
|
|
Assert.SequenceEqual(Expected(plan, before), await StateAsync(fx, ids));
|
|
Assert.Empty(RenameJournal.ListPending(fx.Paths));
|
|
Assert.False(Directory.Exists(Path.Combine(fx.Paths.Root, "inbox", "a")));
|
|
Assert.Equal(0, (await fx.ScanAsync()).Indexed);
|
|
}
|
|
|
|
[Test]
|
|
public async Task AnInterruptedRenameResumesFromItsJournal()
|
|
{
|
|
await using var fx = await CreateAsync(context);
|
|
var ids = await PopulateAsync(fx);
|
|
var before = await StateAsync(fx, ids);
|
|
var plan = await fx.Service.PlanRenameAsync(fx.Archive.Id, ids, "renamed/{date}", null, fx.Ct);
|
|
using var cts = new CancellationTokenSource();
|
|
await Assert.ThrowsAsync<OperationCanceledException>(() => fx.Service.ApplyRenameAsync(plan, new SyncProgress(_ => cts.Cancel()), cts.Token));
|
|
var journal = RenameJournal.ListPending(fx.Paths).Single();
|
|
Assert.Count(1, journal.Completed);
|
|
|
|
// Crash in the middle of the next step: its folder moved, the index not yet updated.
|
|
var next = plan.Steps[1];
|
|
Directory.CreateDirectory(Path.GetDirectoryName(fx.Paths.FromRelative(next.ToFolder))!);
|
|
Directory.Move(fx.Paths.FromRelative(next.FromFolder), fx.Paths.FromRelative(next.ToFolder));
|
|
|
|
await fx.Service.OpenAsync(fx.Archive.RootPath, fx.Ct); // the next start finds the journal
|
|
Assert.Empty(RenameJournal.ListPending(fx.Paths));
|
|
Assert.SequenceEqual(Expected(plan, before), await StateAsync(fx, ids));
|
|
}
|
|
|
|
[Test]
|
|
public async Task AResumedRenameOnTheSameIndexMatchesThePlan()
|
|
{
|
|
await using var fx = await CreateAsync(context);
|
|
var ids = await PopulateAsync(fx);
|
|
var before = await StateAsync(fx, ids);
|
|
var plan = await fx.Service.PlanRenameAsync(fx.Archive.Id, ids, "renamed/{date}", "{title} 2026", fx.Ct);
|
|
using var cts = new CancellationTokenSource();
|
|
await Assert.ThrowsAsync<OperationCanceledException>(() => fx.Service.ApplyRenameAsync(plan, new SyncProgress(_ => cts.Cancel()), cts.Token));
|
|
Assert.Equal(1, await fx.Service.Renamer.ResumePendingAsync(fx.Archive, fx.Ct));
|
|
Assert.SequenceEqual(Expected(plan, before), await StateAsync(fx, ids));
|
|
}
|
|
|
|
[Test]
|
|
public async Task AStalePlanIsRefused()
|
|
{
|
|
await using var fx = await CreateAsync(context);
|
|
var ids = await PopulateAsync(fx);
|
|
var plan = await fx.Service.PlanRenameAsync(fx.Archive.Id, [ids[0]], "x/{date}", null, fx.Ct);
|
|
var other = await fx.Service.PlanRenameAsync(fx.Archive.Id, [ids[0]], "y/{date}", null, fx.Ct);
|
|
await fx.Service.ApplyRenameAsync(other, null, fx.Ct);
|
|
await Assert.ThrowsAsync<StaleRenamePlanException>(() => fx.Service.ApplyRenameAsync(plan, null, fx.Ct));
|
|
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)
|
|
{
|
|
Photos.WriteSeries(Path.Combine(fx.Paths.Root, "trips"), Start, 2);
|
|
Photos.WriteSeries(Path.Combine(fx.Paths.Root, "trips", "day2"), Start.AddDays(1), 2, firstSeed: 10);
|
|
Photos.WriteSeries(Path.Combine(fx.Paths.Root, "trips", "day2", "dawn"), Start.AddDays(2), 2, firstSeed: 20);
|
|
await fx.ScanAsync();
|
|
var locations = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).OrderBy(l => l.Folder, StringComparer.Ordinal).ToList();
|
|
Assert.SequenceEqual(["trips", "trips/day2", "trips/day2/dawn"], locations.Select(l => l.Folder));
|
|
return [.. locations.Select(l => l.Id)];
|
|
}
|
|
|
|
[Test]
|
|
public async Task SequencesInsideAMovedFolderAreInTheDryRunAndMoveWithIt()
|
|
{
|
|
await using var fx = await CreateAsync(context);
|
|
var ids = await NestedAsync(fx);
|
|
var before = await StateAsync(fx, ids);
|
|
|
|
var plan = await fx.Service.PlanRenameAsync(fx.Archive.Id, [ids[0]], "moved/{date}", null, fx.Ct);
|
|
Assert.SequenceEqual(["moved/2026-09-28", "moved/2026-09-28/day2", "moved/2026-09-28/day2/dawn"], plan.Steps.Select(s => s.ToFolder));
|
|
Assert.SequenceEqual([false, true, true], plan.Steps.Select(s => s.Carried));
|
|
Assert.SequenceEqual(before, await StateAsync(fx, ids));
|
|
|
|
await fx.Service.ApplyRenameAsync(plan, null, fx.Ct);
|
|
Assert.SequenceEqual(Expected(plan, before), await StateAsync(fx, ids));
|
|
Assert.Empty(RenameJournal.ListPending(fx.Paths));
|
|
Assert.Equal(0, (await fx.ScanAsync()).Indexed);
|
|
Assert.Empty((await fx.Service.FindOrphansAsync(fx.Archive.Id, fx.Ct)).MissingSequences);
|
|
}
|
|
|
|
[Test]
|
|
public async Task AFolderMovesOnceTheWritersOfTheSequencesInsideAreDone()
|
|
{
|
|
await using var fx = await CreateAsync(context);
|
|
var ids = await NestedAsync(fx);
|
|
var before = await StateAsync(fx, ids);
|
|
var plan = await fx.Service.PlanRenameAsync(fx.Archive.Id, [ids[0]], "moved/{date}", null, fx.Ct);
|
|
|
|
// An ingest into trips/day2 holds that sequence's lease; a thumbnail pass of trips/day2/dawn holds its pack's.
|
|
Task apply;
|
|
using (await LeaseFile.AcquireAsync(fx.Paths.LocksDirectory, $"seq-{ids[1]:N}", "ingest", TimeSpan.FromSeconds(5), cancellationToken: fx.Ct))
|
|
using (await LeaseFile.AcquireAsync(fx.Paths.LocksDirectory, $"thumbs-{ids[2]:N}", "thumbnails", TimeSpan.FromSeconds(5), cancellationToken: fx.Ct))
|
|
{
|
|
apply = fx.Service.ApplyRenameAsync(plan, null, fx.Ct);
|
|
await Task.Delay(500, fx.Ct);
|
|
Assert.False(apply.IsCompleted);
|
|
Assert.True(Directory.Exists(Path.Combine(fx.Paths.Root, "trips", "day2", "dawn")), "nothing moved while they write");
|
|
}
|
|
|
|
await apply;
|
|
Assert.SequenceEqual(Expected(plan, before), await StateAsync(fx, ids));
|
|
Assert.Empty(RenameJournal.ListPending(fx.Paths));
|
|
}
|
|
|
|
[Test]
|
|
public async Task NestedSequencesMovedTogetherLeaveTheirFolderFirst()
|
|
{
|
|
await using var fx = await CreateAsync(context);
|
|
var ids = await NestedAsync(fx);
|
|
var before = await StateAsync(fx, ids);
|
|
|
|
var plan = await fx.Service.PlanRenameAsync(fx.Archive.Id, ids, "moved/{date}", null, fx.Ct);
|
|
Assert.SequenceEqual([ids[2], ids[1], ids[0]], plan.Steps.Select(s => s.SequenceId));
|
|
Assert.True(plan.Steps.All(s => !s.Carried));
|
|
|
|
await fx.Service.ApplyRenameAsync(plan, null, fx.Ct);
|
|
Assert.SequenceEqual(["moved/2026-09-28", "moved/2026-09-29", "moved/2026-09-30"], (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id))
|
|
.Select(l => l.Folder).Order(StringComparer.Ordinal));
|
|
Assert.SequenceEqual(Expected(plan, before), await StateAsync(fx, ids));
|
|
// The folders a nested sequence moved out of changed after their rows were written, as a folder another process
|
|
// changed would: the next scan reads those two again, and finds the index as it is.
|
|
Assert.SequenceEqual([ids[0], ids[1]], (await fx.ScanAsync()).Sequences);
|
|
Assert.SequenceEqual(Expected(plan, before), await StateAsync(fx, ids));
|
|
Assert.Equal(0, (await fx.ScanAsync()).Indexed);
|
|
}
|
|
|
|
[Test]
|
|
public async Task ExportsInsideAMovedFolderAreStillReachable()
|
|
{
|
|
await using var fx = await CreateAsync(context);
|
|
var ids = await NestedAsync(fx);
|
|
// Exports of "trips" and of "trips/day2" in their folders, one of "trips/day2/dawn" saved in the exports of "trips"
|
|
// (the Export tab's folder picker), one of "trips" outside the archive, a row whose path only starts with the same
|
|
// letters, and one of "trips/day2" in "trips" whose file is gone.
|
|
var outside = Path.Combine(context.CreateTempDirectory("exports"), "Trips elsewhere.mp4");
|
|
List<Guid> renders =
|
|
[
|
|
await RenderAsync(fx, ids[0], "trips/exports/Trips.mp4"),
|
|
await RenderAsync(fx, ids[1], "trips/day2/exports/Day 2.mp4"),
|
|
await RenderAsync(fx, ids[2], "trips/exports/Dawn.mp4"),
|
|
await RenderAsync(fx, ids[0], outside),
|
|
await RenderAsync(fx, ids[0], "trips 2/exports/Other.mp4", write: false),
|
|
await RenderAsync(fx, ids[1], "trips/exports/Gone.mp4", write: false),
|
|
];
|
|
var plan = await fx.Service.PlanRenameAsync(fx.Archive.Id, [ids[0]], "moved/{date}", null, fx.Ct);
|
|
Assert.SequenceEqual([false, true, true], plan.Steps.Select(s => s.Carried));
|
|
|
|
await fx.Service.ApplyRenameAsync(plan, null, fx.Ct);
|
|
string[] expected =
|
|
[
|
|
"moved/2026-09-28/exports/Trips.mp4", "moved/2026-09-28/day2/exports/Day 2.mp4", "moved/2026-09-28/exports/Dawn.mp4", outside,
|
|
"trips 2/exports/Other.mp4", "trips/exports/Gone.mp4",
|
|
];
|
|
Assert.SequenceEqual(expected, await OutputsAsync(fx, renders));
|
|
foreach (var path in expected[..4]) Assert.FileExists(Path.IsPathRooted(path) ? path : fx.Paths.FromRelative(path));
|
|
|
|
// A crash after the steps were recorded but before the journal said so: completed again, and again, nothing moves twice.
|
|
RenameJournal.Write(fx.Paths, new RenameJournal(plan, []));
|
|
Assert.Equal(1, await fx.Service.Renamer.ResumePendingAsync(fx.Archive, fx.Ct));
|
|
Assert.Equal(0, await fx.Service.Renamer.ResumePendingAsync(fx.Archive, fx.Ct));
|
|
Assert.SequenceEqual(expected, await OutputsAsync(fx, renders));
|
|
}
|
|
|
|
[Test]
|
|
public async Task AnotherIndexFindsTheExportsWhereAReorganizeMovedTheirFolder()
|
|
{
|
|
await using var fx = await CreateAsync(context);
|
|
var ids = await PopulateAsync(fx);
|
|
// This index's exports: of "inbox/a" in its folder, of "inbox/b" saved in the exports of "inbox/a", of "inbox/c" in its own.
|
|
List<Guid> renders =
|
|
[
|
|
await RenderAsync(fx, ids[0], "inbox/a/exports/A.mp4"),
|
|
await RenderAsync(fx, ids[1], "inbox/a/exports/B.mp4"),
|
|
await RenderAsync(fx, ids[2], "inbox/c/exports/C.mp4"),
|
|
];
|
|
var (server, archive) = await fx.OtherWriterAsync();
|
|
|
|
// The other index (a server sharing this desktop's archive) moves "inbox/a", then "inbox/c" into the folder it left.
|
|
var first = await server.PlanRenameAsync(archive.Id, [ids[0]], "moved/{date}", null, fx.Ct);
|
|
await server.ApplyRenameAsync(first, null, fx.Ct);
|
|
var second = await server.PlanRenameAsync(archive.Id, [ids[2]], "inbox/a", null, fx.Ct);
|
|
Assert.Equal("inbox/a", second.Steps.Single().ToFolder);
|
|
await server.ApplyRenameAsync(second, null, fx.Ct);
|
|
|
|
// A scan here finds both where they are now (the new "inbox/a" first): each export follows the folder its file moved with.
|
|
await fx.ScanAsync();
|
|
Assert.Equal("moved/2026-09-28", (await fx.Db.Sequences.GetAsync(ids[0], fx.Ct))!.Folder);
|
|
Assert.Equal("inbox/a", (await fx.Db.Sequences.GetAsync(ids[2], fx.Ct))!.Folder);
|
|
string[] expected = ["moved/2026-09-28/exports/A.mp4", "moved/2026-09-28/exports/B.mp4", "inbox/a/exports/C.mp4"];
|
|
Assert.SequenceEqual(expected, await OutputsAsync(fx, renders));
|
|
foreach (var path in expected) Assert.FileExists(fx.Paths.FromRelative(path));
|
|
Assert.Equal(0, (await fx.ScanAsync()).Indexed);
|
|
Assert.SequenceEqual(expected, await OutputsAsync(fx, renders));
|
|
}
|
|
|
|
[Test]
|
|
public async Task APhotoDeletionWhoseFolderMovedWhileItWaitedForTheLeaseIsRefused()
|
|
{
|
|
await using var fx = await CreateAsync(context);
|
|
var ids = await PopulateAsync(fx);
|
|
var frames = await fx.Db.Frames.ListBySequenceAsync(ids[0], cancellationToken: fx.Ct);
|
|
var deletion = await fx.Service.FrameTrash.PlanDeleteAsync(ids[0], [frames[1].Id], cancellationToken: fx.Ct);
|
|
Assert.True(deletion.CanDelete);
|
|
var pause = TimeSpan.FromSeconds(TestTiming.Budget(0.5));
|
|
|
|
// Another process holds the sequence and moves its folder (a Reorganize), while the deletion waits for the lease.
|
|
Task<FrameDeleteResult> delete;
|
|
using (await LeaseFile.AcquireAsync(fx.Paths.LocksDirectory, $"seq-{ids[0]:N}", "other", TimeSpan.FromSeconds(5), cancellationToken: fx.Ct))
|
|
{
|
|
delete = fx.Service.FrameTrash.DeleteAsync(deletion, cancellationToken: fx.Ct);
|
|
await Task.Delay(pause, fx.Ct);
|
|
Assert.False(delete.IsCompleted, "the deletion waits for the other process");
|
|
Directory.CreateDirectory(fx.Paths.FromRelative("moved"));
|
|
Directory.Move(fx.Paths.FromRelative("inbox/a"), fx.Paths.FromRelative("moved/a"));
|
|
await using var tx = await fx.Db.BeginAsync(fx.Ct);
|
|
await tx.Sequences.RelocateAsync(ids[0], "moved/a", null, fx.Ct);
|
|
await tx.Frames.RebaseFolderAsync(ids[0], "inbox/a", "moved/a", fx.Ct);
|
|
await tx.CommitAsync(fx.Ct);
|
|
}
|
|
|
|
await Assert.ThrowsAsync<StaleDeletePlanException>(() => delete);
|
|
Assert.Count(3, await fx.Db.Frames.ListBySequenceAsync(ids[0], cancellationToken: fx.Ct));
|
|
Assert.Count(3, ManifestStore.ReadSequence(fx.Paths.Sequence("moved/a").ManifestPath).Frames);
|
|
foreach (var f in frames) Assert.FileExists(fx.Paths.FromRelative(f.RelativePath.Replace("inbox/a/", "moved/a/", StringComparison.Ordinal)));
|
|
Assert.Empty(await fx.Service.Trash.ListAsync(fx.Archive, fx.Ct));
|
|
Assert.False(Directory.Exists(fx.Paths.FromRelative("inbox/a")), "the folder it left is not created again");
|
|
}
|
|
|
|
[Test]
|
|
public async Task AnInterruptedMoveTakesTheExportsAlongWhenItResumes()
|
|
{
|
|
await using var fx = await CreateAsync(context);
|
|
var ids = await PopulateAsync(fx);
|
|
List<Guid> renders = [];
|
|
foreach (var (id, folder) in ids.Zip(["inbox/a", "inbox/b", "inbox/c"])) renders.Add(await RenderAsync(fx, id, $"{folder}/exports/Clip.mp4"));
|
|
var plan = await fx.Service.PlanRenameAsync(fx.Archive.Id, ids, "renamed/{date}", null, fx.Ct);
|
|
using var cts = new CancellationTokenSource();
|
|
await Assert.ThrowsAsync<OperationCanceledException>(() => fx.Service.ApplyRenameAsync(plan, new SyncProgress(_ => cts.Cancel()), cts.Token));
|
|
Assert.Count(1, RenameJournal.ListPending(fx.Paths).Single().Completed);
|
|
|
|
// Crash in the middle of the next step: its folder moved with its export, the index not yet updated.
|
|
var next = plan.Steps[1];
|
|
Directory.CreateDirectory(Path.GetDirectoryName(fx.Paths.FromRelative(next.ToFolder))!);
|
|
Directory.Move(fx.Paths.FromRelative(next.FromFolder), fx.Paths.FromRelative(next.ToFolder));
|
|
|
|
await fx.Service.OpenAsync(fx.Archive.RootPath, fx.Ct); // the next start finds the journal
|
|
Assert.Empty(RenameJournal.ListPending(fx.Paths));
|
|
Assert.Equal(0, await fx.Service.Renamer.ResumePendingAsync(fx.Archive, fx.Ct));
|
|
var outputs = await OutputsAsync(fx, renders);
|
|
Assert.SequenceEqual(ids.Select(id => $"{plan.Steps.Single(s => s.SequenceId == id).ToFolder}/exports/Clip.mp4"), outputs);
|
|
foreach (var path in outputs) Assert.FileExists(fx.Paths.FromRelative(path));
|
|
}
|
|
|
|
[Test]
|
|
public async Task WritersThatReadTheSequenceBeforeItsFolderMovedWriteWhereItIsNow()
|
|
{
|
|
await using var fx = await CreateAsync(context);
|
|
var ids = await PopulateAsync(fx);
|
|
var stale = (await fx.Db.Sequences.GetAsync(ids[0], fx.Ct))!;
|
|
var plan = await fx.Service.PlanRenameAsync(fx.Archive.Id, [ids[0]], "moved/{date}", null, fx.Ct);
|
|
await fx.Service.ApplyRenameAsync(plan, null, fx.Ct);
|
|
var now = fx.Paths.Sequence(plan.Steps.Single().ToFolder);
|
|
var left = fx.Paths.Sequence(stale.Folder).Folder;
|
|
Assert.False(Directory.Exists(left));
|
|
|
|
// A thumbnail pass and an analysis's project update that read the row before the move take their leases after it.
|
|
await fx.Service.Images.BuildThumbnailsAsync(fx.Archive, stale, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
|
|
Assert.FileExists(now.ThumbsPackPath);
|
|
Assert.True(((await fx.Db.Sequences.GetAsync(ids[0], fx.Ct))!.Status & SequenceStatus.ThumbsBuilt) != 0, "the pass is recorded");
|
|
var projects = new ProjectStore(fx.Db, new SequenceWriter(new ArchiveServiceOptions { Owner = "analysis" }));
|
|
Assert.True(await projects.UpdateAsync(fx.Paths, stale, p => p with { HeroFrame = 1 }, fx.Ct));
|
|
Assert.Equal(1, ProjectSettings.FromJson(await File.ReadAllTextAsync(now.ProjectPath, fx.Ct)).HeroFrame);
|
|
Assert.False(Directory.Exists(left), "the folder it left is not created again");
|
|
}
|
|
|
|
[Test]
|
|
public async Task APhotoFiledWhileTheFolderMovesGoesWhereTheSequenceIsNow()
|
|
{
|
|
await using var fx = await CreateAsync(context);
|
|
var ids = await PopulateAsync(fx);
|
|
var plan = await fx.Service.PlanRenameAsync(fx.Archive.Id, [ids[0]], "moved/{date}", null, fx.Ct);
|
|
var staged = fx.Stage(Start.AddMinutes(30), 99);
|
|
var pause = TimeSpan.FromSeconds(TestTiming.Budget(0.5));
|
|
|
|
// Another process writes the sequence: the move waits for it, and an upload that read the row meanwhile waits behind the move.
|
|
Task apply;
|
|
Task<IngestResult> upload;
|
|
using (await LeaseFile.AcquireAsync(fx.Paths.LocksDirectory, $"seq-{ids[0]:N}", "other", TimeSpan.FromSeconds(5), cancellationToken: fx.Ct))
|
|
{
|
|
apply = fx.Service.ApplyRenameAsync(plan, null, fx.Ct);
|
|
await Task.Delay(pause, fx.Ct);
|
|
upload = fx.Service.Ingest.CommitAsync(new IngestRequest(fx.Archive.Id, ids[0], null, null, staged.Path, "LATE.JPG", staged.Size, staged.Sha256,
|
|
null, null), fx.Ct);
|
|
await Task.Delay(pause, fx.Ct);
|
|
Assert.False(apply.IsCompleted || upload.IsCompleted, "both wait for the other process");
|
|
}
|
|
|
|
await apply;
|
|
var filed = await upload;
|
|
var now = plan.Steps.Single().ToFolder;
|
|
Assert.True(filed.RelativePath.StartsWith(now + "/", StringComparison.Ordinal), filed.RelativePath);
|
|
Assert.FileExists(fx.Paths.FromRelative(filed.RelativePath));
|
|
Assert.Count(4, await fx.Db.Frames.ListBySequenceAsync(ids[0], cancellationToken: fx.Ct));
|
|
Assert.False(Directory.Exists(fx.Paths.FromRelative("inbox/a")), "the folder it left is not created again");
|
|
}
|
|
|
|
/// <summary>A render row of <paramref name="sequenceId"/> at <paramref name="output"/> (archive-relative or absolute), with its file unless told otherwise.</summary>
|
|
private static async Task<Guid> RenderAsync(ArchiveFixture fx, Guid sequenceId, string output, bool write = true)
|
|
{
|
|
if (write)
|
|
{
|
|
var file = Path.IsPathRooted(output) ? output : fx.Paths.FromRelative(output);
|
|
Directory.CreateDirectory(Path.GetDirectoryName(file)!);
|
|
await File.WriteAllBytesAsync(file, [1, 2, 3], fx.Ct);
|
|
}
|
|
var render = new Render { Id = Guid.CreateVersion7(), SequenceId = sequenceId, PresetName = "1080p", OutputPath = output, Bytes = 3, Created = DateTimeOffset.UtcNow };
|
|
await fx.Db.Renders.InsertAsync(render, fx.Ct);
|
|
return render.Id;
|
|
}
|
|
|
|
private static async Task<List<string>> OutputsAsync(ArchiveFixture fx, IEnumerable<Guid> renders)
|
|
{
|
|
var outputs = new List<string>();
|
|
foreach (var id in renders) outputs.Add((await fx.Db.Renders.GetAsync(id, fx.Ct))!.OutputPath);
|
|
return outputs;
|
|
}
|
|
|
|
[Test]
|
|
public async Task AFolderInsideASequenceFolderIsRefused()
|
|
{
|
|
await using var fx = await CreateAsync(context);
|
|
var ids = await PopulateAsync(fx);
|
|
var before = await StateAsync(fx, ids);
|
|
|
|
var into = await Assert.ThrowsAsync<NestedRenameException>(() => fx.Service.PlanRenameAsync(fx.Archive.Id, [ids[1]], "inbox/a/{date}", null, fx.Ct));
|
|
Assert.Equal("inbox/a", into.Folder);
|
|
var itself = await Assert.ThrowsAsync<NestedRenameException>(() => fx.Service.PlanRenameAsync(fx.Archive.Id, [ids[0]], "inbox/a/{date}", null, fx.Ct));
|
|
Assert.Equal("inbox/a/2026-09-28", itself.Target);
|
|
Assert.SequenceEqual(before, await StateAsync(fx, ids));
|
|
}
|
|
|
|
/// <summary>Reports synchronously (Progress<T> would post to the thread pool).</summary>
|
|
private sealed class SyncProgress(Action<int> report) : IProgress<int>
|
|
{
|
|
public void Report(int value) => report(value);
|
|
}
|
|
}
|