feat: owner review 2 phase 4a: Reorganize sequences after import (#12)
CI / Build and test (push) Successful in 9m15s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 6s
Release / Build, test and publish (push) Successful in 15m35s
CI / PostgreSQL 16 integration (push) Successful in 7m26s
CI / PostgreSQL 17 integration (push) Successful in 6m47s
CI / Docker image (push) Successful in 59s
Release / Server image (push) Successful in 1m24s

- Reorganize… on the web panel and the desktop: folder, file and title templates applied to existing sequences, with a dry run and an explicit Apply that runs as a job.
- One naming rule (FrameNaming) for import, upload and Reorganize.
- Crash-safe plan and file journals, held plans, stale-plan refusal; exports move with their folder.
- Writers refuse a folder another program moved or gave to another sequence.
- Server: POST /api/v1/archive/reorganize with a bounded plan cache; template rules.
- Phase 3 docs and translations merged; strings in all 38 languages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
This commit was merged in pull request #12.
This commit is contained in:
Alby96andClaude Opus 5.5 authored and Alby96 committed 2026-10-27 21:58:39 +01:00
1 parent 9aa818ed47
commit 7e4a3b481d
416 files changed
+15304 -1572

No files matched your search

@@ -1,5 +1,8 @@
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;
@@ -233,6 +236,331 @@ public sealed class MassRenameTests(TestContext context)
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 AnExportWrittenWhereItsFolderWasStaysWithItsFile()
{
await using var fx = await CreateAsync(context);
var ids = await PopulateAsync(fx);
List<Guid> renders = [await RenderAsync(fx, ids[0], "inbox/a/exports/A.mp4")];
var (server, archive) = await fx.OtherWriterAsync();
// The other index moves "inbox/a"; a writer here that read the folder before files an export where it was, creating
// it again. Another export of the sequence is missing from both places.
var plan = await server.PlanRenameAsync(archive.Id, [ids[0]], "moved/{date}", null, fx.Ct);
await server.ApplyRenameAsync(plan, null, fx.Ct);
renders.Add(await RenderAsync(fx, ids[0], "inbox/a/exports/Late.mp4"));
renders.Add(await RenderAsync(fx, ids[0], "inbox/a/exports/Gone.mp4", write: false));
// The scan here moves the rows whose file went with the folder or is nowhere, and keeps the one whose file stayed.
await fx.ScanAsync();
Assert.Equal("moved/2026-09-28", (await fx.Db.Sequences.GetAsync(ids[0], fx.Ct))!.Folder);
string[] expected = ["moved/2026-09-28/exports/A.mp4", "inbox/a/exports/Late.mp4", "moved/2026-09-28/exports/Gone.mp4"];
Assert.SequenceEqual(expected, await OutputsAsync(fx, renders));
foreach (var path in expected[..2]) Assert.FileExists(fx.Paths.FromRelative(path));
}
[Test]
public async Task AnImageSequenceExportIsAFolderThatFollowsAsAFileDoes()
{
await using var fx = await CreateAsync(context);
var ids = await PopulateAsync(fx);
// Image-sequence exports: of "inbox/a" in its folder, and of "inbox/b" saved in the exports of "inbox/a".
List<Guid> renders =
[
await RenderAsync(fx, ids[0], "inbox/a/exports/A frames", frames: true),
await RenderAsync(fx, ids[1], "inbox/a/exports/B frames", frames: true),
];
var (server, archive) = await fx.OtherWriterAsync();
// The other index moves "inbox/a"; a writer here that read the folder before files one where it was, creating it again.
var plan = await server.PlanRenameAsync(archive.Id, [ids[0]], "moved/{date}", null, fx.Ct);
await server.ApplyRenameAsync(plan, null, fx.Ct);
renders.Add(await RenderAsync(fx, ids[0], "inbox/a/exports/Late frames", frames: true));
// Each folder follows, or stays, as a file would: both that moved with the folder, not the one written where it was.
await fx.ScanAsync();
string[] expected = ["moved/2026-09-28/exports/A frames", "moved/2026-09-28/exports/B frames", "inbox/a/exports/Late frames"];
Assert.SequenceEqual(expected, await OutputsAsync(fx, renders));
foreach (var path in expected) Assert.FileExists(Path.Combine(fx.Paths.FromRelative(path), "000001.png"));
}
[Test]
public async Task AnExportNeverNamesTheOneAnotherSequenceBroughtIntoItsOldFolder()
{
// The new folder is scanned after "inbox/a", then before it. "inbox/a" and "inbox/c" each have an export of the
// same name: the one of "inbox/a" gone, both there, or the one of "inbox/c" gone.
var cases = from template in new[] { "moved/{date}", "done/{date}" }
from files in new[] { (A: false, C: true), (A: true, C: true), (A: true, C: false) }
select (template, files.A, files.C);
foreach (var (template, a, c) in cases)
{
var label = $"{template}, a {(a ? "there" : "gone")}, c {(c ? "there" : "gone")}";
await using var fx = await CreateAsync(context);
var ids = await PopulateAsync(fx);
List<Guid> renders =
[
await RenderAsync(fx, ids[0], "inbox/a/exports/Clip.mp4", write: a),
await RenderAsync(fx, ids[2], "inbox/c/exports/Clip.mp4", write: c),
];
if (a) await File.WriteAllTextAsync(fx.Paths.FromRelative("inbox/a/exports/Clip.mp4"), "a", fx.Ct);
if (c) await File.WriteAllTextAsync(fx.Paths.FromRelative("inbox/c/exports/Clip.mp4"), "c", fx.Ct);
var (server, archive) = await fx.OtherWriterAsync();
// The other index moves "inbox/a", then "inbox/c" into the folder it left, their exports along.
var first = await server.PlanRenameAsync(archive.Id, [ids[0]], template, null, fx.Ct);
await server.ApplyRenameAsync(first, null, fx.Ct);
await server.ApplyRenameAsync(await server.PlanRenameAsync(archive.Id, [ids[2]], "inbox/a", null, fx.Ct), null, fx.Ct);
// Each row follows its own sequence's folder, its file there or missing there too, and never names the other one's.
await fx.ScanAsync();
string[] expected = [first.Steps.Single().ToFolder + "/exports/Clip.mp4", "inbox/a/exports/Clip.mp4"];
Assert.SequenceEqual(expected, await OutputsAsync(fx, renders), label);
if (a) Assert.Equal("a", await File.ReadAllTextAsync(fx.Paths.FromRelative(expected[0]), fx.Ct), label);
if (c) Assert.Equal("c", await File.ReadAllTextAsync(fx.Paths.FromRelative(expected[1]), fx.Ct), label);
Assert.Equal(0, (await fx.ScanAsync()).Indexed);
Assert.SequenceEqual(expected, await OutputsAsync(fx, renders), label);
}
}
[Test]
public async Task AnExportSavedInAnotherSequencesFolderNeverNamesTheOneBroughtIntoIt()
{
// The new folder is scanned after "inbox/a", then before it.
foreach (var template in new[] { "moved/{date}", "done/{date}" })
{
await using var fx = await CreateAsync(context);
var ids = await PopulateAsync(fx);
// "inbox/b" saved an export in the exports of "inbox/a" (the Export tab's folder picker); "inbox/c" has one of the same name.
List<Guid> renders =
[
await RenderAsync(fx, ids[1], "inbox/a/exports/Clip.mp4"),
await RenderAsync(fx, ids[2], "inbox/c/exports/Clip.mp4"),
];
await File.WriteAllTextAsync(fx.Paths.FromRelative("inbox/a/exports/Clip.mp4"), "b", fx.Ct);
await File.WriteAllTextAsync(fx.Paths.FromRelative("inbox/c/exports/Clip.mp4"), "c", fx.Ct);
var (server, archive) = await fx.OtherWriterAsync();
// The other index moves "inbox/a", the export of "inbox/b" along, then "inbox/c" into the folder it left, its export along.
var first = await server.PlanRenameAsync(archive.Id, [ids[0]], template, null, fx.Ct);
await server.ApplyRenameAsync(first, null, fx.Ct);
await server.ApplyRenameAsync(await server.PlanRenameAsync(archive.Id, [ids[2]], "inbox/a", null, fx.Ct), null, fx.Ct);
// The export of "inbox/b" follows the folder its file moved with and never names the one of "inbox/c".
await fx.ScanAsync();
string[] expected = [first.Steps.Single().ToFolder + "/exports/Clip.mp4", "inbox/a/exports/Clip.mp4"];
Assert.SequenceEqual(expected, await OutputsAsync(fx, renders), template);
Assert.Equal("b", await File.ReadAllTextAsync(fx.Paths.FromRelative(expected[0]), fx.Ct), template);
Assert.Equal("c", await File.ReadAllTextAsync(fx.Paths.FromRelative(expected[1]), fx.Ct), template);
Assert.Equal(0, (await fx.ScanAsync()).Indexed);
Assert.SequenceEqual(expected, await OutputsAsync(fx, renders), template);
}
}
[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; with <paramref name="frames"/> an image sequence's export, a folder of frames.
/// </summary>
private static async Task<Guid> RenderAsync(ArchiveFixture fx, Guid sequenceId, string output, bool write = true, bool frames = false)
{
if (write)
{
var file = Path.IsPathRooted(output) ? output : fx.Paths.FromRelative(output);
if (frames) file = Path.Combine(file, "000001.png");
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()
{