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
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:
1 parent
9aa818ed47
commit
7e4a3b481d
416 files changed
+15304
-1572
No files matched your search
@@ -112,17 +112,24 @@ internal sealed class ArchiveFixture : IAsyncDisposable
|
||||
return job;
|
||||
}
|
||||
|
||||
/// <summary>Stages a verified upload-like file for ingest: returns its path, size and SHA-256.</summary>
|
||||
public (string Path, long Size, string Sha256) Stage(DateTime captured, int seed, string make = "Canon", string model = "Canon EOS R5")
|
||||
/// <summary>Stages a verified upload-like file for ingest (<paramref name="captured"/> null: a photo that records no time): returns its path, size and SHA-256.</summary>
|
||||
public (string Path, long Size, string Sha256) Stage(DateTime? captured, int seed, string? make = "Canon", string? model = "Canon EOS R5", string? lens = null)
|
||||
{
|
||||
var staging = Path.Combine(Paths.StagingDirectory, "uploads");
|
||||
Directory.CreateDirectory(staging);
|
||||
var bytes = Photos.Jpeg(captured, seed, make, model);
|
||||
var bytes = Photos.Jpeg(captured, seed, make, model, lens);
|
||||
var path = Path.Combine(staging, $"{Guid.NewGuid():N}.part");
|
||||
File.WriteAllBytes(path, bytes);
|
||||
return (path, bytes.Length, Convert.ToHexStringLower(SHA256.HashData(bytes)));
|
||||
}
|
||||
|
||||
/// <summary>Every file under <paramref name="folder"/> with the hash of its content, in path order: what a test compares to tell that nothing there changed.</summary>
|
||||
public static List<string> FilesOf(string folder) =>
|
||||
[
|
||||
.. Directory.EnumerateFiles(folder, "*", SearchOption.AllDirectories).Order(StringComparer.Ordinal)
|
||||
.Select(p => $"{Path.GetRelativePath(folder, p).Replace('\\', '/')} {Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(p)))}"),
|
||||
];
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
foreach (var owned in _owned) await owned.DisposeAsync();
|
||||
@@ -132,7 +139,7 @@ internal sealed class ArchiveFixture : IAsyncDisposable
|
||||
/// <summary>Small real JPEGs with Exif made by the in-house encoder; each seed gives distinct bytes.</summary>
|
||||
internal static class Photos
|
||||
{
|
||||
public static byte[] Jpeg(DateTime captured, int seed, string make = "Canon", string model = "Canon EOS R5")
|
||||
public static byte[] Jpeg(DateTime? captured, int seed, string? make = "Canon", string? model = "Canon EOS R5", string? lens = null)
|
||||
{
|
||||
using var image = new ImageBuffer(48, 32);
|
||||
var random = new Random(seed);
|
||||
@@ -142,7 +149,7 @@ internal static class Photos
|
||||
var level = (float)random.NextDouble();
|
||||
for (var i = 0; i < plane.Length; i++) plane[i] = level * (0.5f + 0.5f * (i % 48) / 48f);
|
||||
}
|
||||
var exif = ExifWriter.BuildJpegApp1(new FrameMetadata { Make = make, Model = model, BodySerial = "0431", CapturedAt = captured, Iso = 100 });
|
||||
var exif = ExifWriter.BuildJpegApp1(new FrameMetadata { Make = make, Model = model, LensModel = lens, BodySerial = "0431", CapturedAt = captured, Iso = 100 });
|
||||
return JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 85, App1Segments = [exif[4..]] });
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ using System.Text.Json.Nodes;
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Core.Codecs.Jpeg;
|
||||
using Chronos.Core.Imaging;
|
||||
using Chronos.Core.Localization;
|
||||
using Chronos.Core.Metadata;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data.Archiving;
|
||||
@@ -90,6 +91,19 @@ public sealed class ArchiveSettingsTests(TestContext context)
|
||||
var template = await Assert.ThrowsAsync<ArgumentException>(() =>
|
||||
fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Templates = s.Templates with { Folder = "{year/{date" } }, fx.Ct));
|
||||
Assert.Equal("folderTemplate", template.ParamName);
|
||||
// The rules every template is held to wherever it is typed: a folder scans never enter, a lost extension, names that repeat.
|
||||
foreach (var (folder, file, field) in new[]
|
||||
{
|
||||
("exports/{date}", "{n}", "folderTemplate"), ("{date}", "{n}{ext}", "fileTemplate"), ("{date}", "{title}", "fileTemplate"),
|
||||
})
|
||||
{
|
||||
var rule = await Assert.ThrowsAsync<ArgumentException>(() =>
|
||||
fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Templates = s.Templates with { Folder = folder, File = file } }, fx.Ct));
|
||||
Assert.Equal(field, rule.ParamName, $"{folder} · {file}");
|
||||
}
|
||||
var created = await Assert.ThrowsAsync<ArgumentException>(() =>
|
||||
fx.Service.CreateAsync(context.CreateTempDirectory("other"), "Other", new LayoutTemplates { Folder = "{date}", File = "{n}{ext}" }, null, fx.Ct));
|
||||
Assert.Equal("fileTemplate", created.ParamName);
|
||||
var proxies = await Assert.ThrowsAsync<ArgumentException>(() =>
|
||||
fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Proxies = s.Proxies with { LongEdge = 100 } }, fx.Ct));
|
||||
Assert.Equal("proxyLongEdge", proxies.ParamName);
|
||||
@@ -102,6 +116,37 @@ public sealed class ArchiveSettingsTests(TestContext context)
|
||||
Assert.Equal(before, await File.ReadAllTextAsync(fx.Paths.ManifestPath, fx.Ct));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TemplatesSavedBeforeTheRulesNeverFailAnEditOfAnotherSetting()
|
||||
{
|
||||
await using var fx = await ArchiveFixture.CreateAsync(context);
|
||||
// An archive.json of 0.1.0-rc.5 or earlier: both templates break a rule new templates are held to.
|
||||
var manifest = ManifestStore.ReadArchive(fx.Paths.ManifestPath);
|
||||
ManifestStore.WriteArchive(fx.Paths.ManifestPath, manifest with { LayoutTemplates = new LayoutTemplates { Folder = "exports/{date}", File = "{title}" } });
|
||||
|
||||
// The desktop saves one row at a time: each edit keeps the templates it does not change.
|
||||
var proxies = await fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Proxies = s.Proxies with { LongEdge = 2560 } }, fx.Ct);
|
||||
Assert.Equal(("exports/{date}", "{title}", 2560), (proxies.FolderTemplate, proxies.FileTemplate, proxies.ProxyLongEdge));
|
||||
await fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Name = "Studio" }, fx.Ct);
|
||||
var folder = await fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Templates = s.Templates with { Folder = "{year}/{date}" } }, fx.Ct);
|
||||
Assert.Equal(("{year}/{date}", "{title}"), (folder.FolderTemplate, folder.FileTemplate));
|
||||
|
||||
// A template given anew is held to every rule, and the refusal says which.
|
||||
var before = await File.ReadAllTextAsync(fx.Paths.ManifestPath, fx.Ct);
|
||||
var refused = await Assert.ThrowsAsync<TemplateRuleException>(() =>
|
||||
fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Templates = s.Templates with { File = "{n}{ext}" } }, fx.Ct));
|
||||
Assert.Equal(("fileTemplate", "Extension", Loc.L("Errors.Template.Extension")), (refused.ParamName, refused.Rule, refused.Message));
|
||||
Assert.Equal(before, await File.ReadAllTextAsync(fx.Paths.ManifestPath, fx.Ct));
|
||||
|
||||
var file = await fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Templates = s.Templates with { File = "{date}_{n:0000}" } }, fx.Ct);
|
||||
Assert.Equal(("Studio", "{year}/{date}", "{date}_{n:0000}", 2560), (file.Name, file.FolderTemplate, file.FileTemplate, file.ProxyLongEdge));
|
||||
|
||||
// A new archive has nothing to keep: its templates meet every rule.
|
||||
var created = await Assert.ThrowsAsync<TemplateRuleException>(() => fx.Service.CreateAsync(context.CreateTempDirectory("other"), "Other",
|
||||
new LayoutTemplates { Folder = "exports/{date}", File = "{date}_{n:0000}" }, null, fx.Ct));
|
||||
Assert.Equal(("folderTemplate", "ReservedFolder"), (created.ParamName, created.Rule));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnOfflineArchiveIsNotChangedAndItsIndexSettingsAreShown()
|
||||
{
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -2,9 +2,11 @@ using Chronos.Core.Archiving;
|
||||
using Chronos.Core.Codecs.Jpeg;
|
||||
using Chronos.Core.Imaging;
|
||||
using Chronos.Core.Imaging.Decoders;
|
||||
using Chronos.Core.Localization;
|
||||
using Chronos.Core.Metadata;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data.Archiving;
|
||||
using Chronos.Data.Tests.Processing;
|
||||
using Chronos.Testing;
|
||||
using static Chronos.Data.Tests.Archiving.ArchiveFixture;
|
||||
|
||||
@@ -37,11 +39,11 @@ public sealed class ProxyTests(TestContext context)
|
||||
var folder = fx.Paths.Sequence(sequence.Folder);
|
||||
var images = fx.Service.Images;
|
||||
|
||||
Assert.Equal(ProxyStoreResult.Invalid, images.StoreProxy(fx.Archive, sequence, frames[0], "not a photo"u8.ToArray()));
|
||||
Assert.Equal(ProxyStoreResult.WrongFrame, images.StoreProxy(fx.Archive, sequence, frames[0], Proxy(frames[1].CapturedAt!.Value, 1)));
|
||||
Assert.Equal(ProxyStoreResult.Invalid, await images.StoreProxyAsync(fx.Archive, sequence, frames[0], "not a photo"u8.ToArray(), fx.Ct));
|
||||
Assert.Equal(ProxyStoreResult.WrongFrame, await images.StoreProxyAsync(fx.Archive, sequence, frames[0], Proxy(frames[1].CapturedAt!.Value, 1), fx.Ct));
|
||||
Assert.FileMissing(folder.Proxy(0));
|
||||
var proxy = Proxy(frames[0].CapturedAt!.Value, 2);
|
||||
Assert.Equal(ProxyStoreResult.Stored, images.StoreProxy(fx.Archive, sequence, frames[0], proxy));
|
||||
Assert.Equal(ProxyStoreResult.Stored, await images.StoreProxyAsync(fx.Archive, sequence, frames[0], proxy, fx.Ct));
|
||||
Assert.SequenceEqual(proxy, await File.ReadAllBytesAsync(folder.Proxy(0), fx.Ct));
|
||||
|
||||
// The proxy pass keeps what was sent and makes none from the originals here, even when forced.
|
||||
@@ -62,6 +64,148 @@ public sealed class ProxyTests(TestContext context)
|
||||
Assert.Equal((0, 1, 1), (again.Built, again.Reused, again.Undecodable));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ProxiesSentOrMadeAfterTheFolderMovedGoWhereTheSequenceIsNow()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (stale, frames) = await HeifSequenceAsync(fx);
|
||||
var images = fx.Service.Images;
|
||||
var plan = await fx.Service.PlanRenameAsync(fx.Archive.Id, [stale.Id], "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 proxy upload and a proxy pass that read the sequence before the move write once they hold its lease.
|
||||
var proxy = Proxy(frames[0].CapturedAt!.Value, 2);
|
||||
Assert.Equal(ProxyStoreResult.Stored, await images.StoreProxyAsync(fx.Archive, stale, frames[0], proxy, fx.Ct));
|
||||
Assert.SequenceEqual(proxy, await File.ReadAllBytesAsync(now.Proxy(0), fx.Ct));
|
||||
var pass = await images.BuildProxiesAsync(fx.Archive, stale, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
|
||||
Assert.Equal((0, 1, 0, 1), (pass.Built, pass.Reused, pass.Undecodable, pass.Waiting));
|
||||
Assert.False(Directory.Exists(left), "the folder it left is not created again");
|
||||
|
||||
// Deleted since: nothing is written.
|
||||
Assert.True(await fx.Db.Sequences.DeleteAsync(stale.Id, fx.Ct));
|
||||
Assert.Equal(ProxyStoreResult.Gone, await images.StoreProxyAsync(fx.Archive, stale, frames[1], Proxy(frames[1].CapturedAt!.Value, 3), fx.Ct));
|
||||
Assert.FileMissing(now.Proxy(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ProxiesOfAFolderAnotherIndexMovedWaitForThisIndexsScan()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (sequence, frames) = await HeifSequenceAsync(fx);
|
||||
var images = fx.Service.Images;
|
||||
var left = fx.Paths.Sequence(sequence.Folder).Folder;
|
||||
var (server, archive) = await fx.OtherWriterAsync();
|
||||
var plan = await server.PlanRenameAsync(archive.Id, [sequence.Id], "moved/{date}", null, fx.Ct);
|
||||
await server.ApplyRenameAsync(plan, null, fx.Ct);
|
||||
var moved = fx.Paths.Sequence(plan.Steps.Single().ToFolder);
|
||||
|
||||
// This index still has the folder where it was: a proxy sent and a proxy pass write nothing, anywhere.
|
||||
var proxy = Proxy(frames[0].CapturedAt!.Value, 2);
|
||||
Assert.Equal(ProxyStoreResult.Gone, await images.StoreProxyAsync(fx.Archive, sequence, frames[0], proxy, fx.Ct));
|
||||
var refused = await Assert.ThrowsAsync<InvalidOperationException>(() =>
|
||||
images.BuildProxiesAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct));
|
||||
Assert.Equal(Loc.L("Errors.Archive.FolderMoved"), refused.Message);
|
||||
Assert.False(Directory.Exists(left), "the folder it left is not created again");
|
||||
Assert.FileMissing(moved.Proxy(0));
|
||||
|
||||
// Once this index's scan found it there, they go where it is.
|
||||
await fx.ScanAsync();
|
||||
Assert.Equal(ProxyStoreResult.Stored, await images.StoreProxyAsync(fx.Archive, sequence, frames[0], proxy, fx.Ct));
|
||||
Assert.SequenceEqual(proxy, await File.ReadAllBytesAsync(moved.Proxy(0), fx.Ct));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ImagesOfAFolderAnotherSequenceTookAreNotWrittenThere()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (sequence, frames) = await HeifSequenceAsync(fx);
|
||||
Photos.WriteSeries(Path.Combine(fx.Paths.Root, "inbox", "other"), Start.AddDays(3), 2, firstSeed: 70);
|
||||
await fx.ScanAsync();
|
||||
var stranger = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id, fx.Ct)).Single(l => l.Folder == "inbox/other").Id;
|
||||
var images = fx.Service.Images;
|
||||
var (server, archive) = await fx.OtherWriterAsync();
|
||||
await server.ApplyRenameAsync(await server.PlanRenameAsync(archive.Id, [sequence.Id], "moved/{date}", null, fx.Ct), null, fx.Ct);
|
||||
await server.ApplyRenameAsync(await server.PlanRenameAsync(archive.Id, [stranger], sequence.Folder, null, fx.Ct), null, fx.Ct);
|
||||
var folder = fx.Paths.Sequence(sequence.Folder).Folder;
|
||||
var before = FilesOf(folder);
|
||||
|
||||
// This index still has the sequence in the folder the other sequence holds now: a proxy sent, a proxy pass and a
|
||||
// thumbnail pass write nothing there.
|
||||
Assert.Equal(ProxyStoreResult.Gone, await images.StoreProxyAsync(fx.Archive, sequence, frames[0], Proxy(frames[0].CapturedAt!.Value, 2), fx.Ct));
|
||||
foreach (var pass in new Func<Task<ImagePassResult>>[]
|
||||
{
|
||||
() => images.BuildProxiesAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct),
|
||||
() => images.BuildThumbnailsAsync(fx.Archive, sequence, force: true, 0, (_, _) => ValueTask.CompletedTask, fx.Ct),
|
||||
})
|
||||
Assert.Equal(Loc.L("Errors.Archive.FolderMoved"), (await Assert.ThrowsAsync<InvalidOperationException>(pass)).Message);
|
||||
Assert.SequenceEqual(before, FilesOf(folder));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AProxyPassLetsAnotherProcessReorganizeBetweenItsBatches()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
Guid id = default;
|
||||
for (var i = 0; i < 4; i++)
|
||||
{
|
||||
var staged = fx.Stage(Start.AddSeconds(10 * i), 60 + i);
|
||||
id = (await fx.Service.Ingest.CommitAsync(new IngestRequest(fx.Archive.Id, null, null, "batches", staged.Path, $"IMG_{i + 1:0000}.JPG",
|
||||
staged.Size, staged.Sha256, null, null), fx.Ct)).SequenceId;
|
||||
}
|
||||
var sequence = (await fx.Db.Sequences.GetAsync(id, fx.Ct))!;
|
||||
var left = fx.Paths.Sequence(sequence.Folder).Folder;
|
||||
// The second photo is read as a PNG, through the decoders (JPEGs are decoded without them), and slowly.
|
||||
var sidecar = ManifestStore.ReadSequence(fx.Paths.Sequence(sequence.Folder).ManifestPath);
|
||||
ManifestStore.WriteSequence(fx.Paths.Sequence(sequence.Folder).ManifestPath, sidecar with
|
||||
{
|
||||
Frames = [.. sidecar.Frames.Select(f => f.I == 1 ? f with { Format = ImageFileFormat.Png } : f)],
|
||||
});
|
||||
var gate = new GateDecoder { Hold = Path.GetFileName(sidecar.Frames[1].File) };
|
||||
var (server, archive) = await fx.OtherWriterAsync();
|
||||
var plan = await server.PlanRenameAsync(archive.Id, [id], "moved/{date}", null, fx.Ct);
|
||||
var moved = fx.Paths.Sequence(plan.Steps.Single().ToFolder);
|
||||
|
||||
// A proxy pass here, one photo at a time, in batches of a quarter of its lease wait.
|
||||
var batch = TimeSpan.FromSeconds(TestTiming.Budget(2));
|
||||
var here = new ArchiveService(fx.Db, new ArchiveServiceOptions
|
||||
{
|
||||
Owner = "test",
|
||||
LeaseWait = batch * 4,
|
||||
Decoder = new ImageDecoder([gate]),
|
||||
Parallelism = () => 1,
|
||||
});
|
||||
var pass = Task.Run(() => here.Images.BuildProxiesAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct));
|
||||
await Task.WhenAny(gate.Holding.Task, pass).WaitAsync(TimeSpan.FromSeconds(60), fx.Ct);
|
||||
if (pass.IsCompleted) Assert.Fail($"The pass ended before the held photo: {await pass}");
|
||||
var held = DateTime.UtcNow;
|
||||
|
||||
// Another process sharing the archive reorganizes the sequence: it holds the sequence, then waits for the thumbnail lease.
|
||||
var move = Task.Run(() => server.ApplyRenameAsync(plan, null, fx.Ct));
|
||||
var until = DateTime.UtcNow + TimeSpan.FromSeconds(TestTiming.Budget(30));
|
||||
while (LeaseFile.Holder(fx.Paths.LocksDirectory, $"seq-{id:N}")?.Owner != "other")
|
||||
{
|
||||
Assert.False(move.IsCompleted, "the Reorganize waits for the pass");
|
||||
Assert.True(DateTime.UtcNow < until, "the Reorganize holds the sequence");
|
||||
await Task.Delay(20, fx.Ct);
|
||||
}
|
||||
var rest = held + batch + TimeSpan.FromSeconds(0.5) - DateTime.UtcNow;
|
||||
if (rest > TimeSpan.Zero) await Task.Delay(rest, fx.Ct);
|
||||
gate.Release();
|
||||
|
||||
// The batch ends with that photo and lets the lease go: the Reorganize moves the folder before the pass ends, and the
|
||||
// pass, finding the folder gone from where this index has it, stops there.
|
||||
await move.WaitAsync(TimeSpan.FromSeconds(TestTiming.Budget(60)), fx.Ct);
|
||||
var stopped = await Assert.ThrowsAsync<InvalidOperationException>(() => pass);
|
||||
Assert.Equal(Loc.L("Errors.Archive.FolderMoved"), stopped.Message);
|
||||
Assert.False(Directory.Exists(left), "the folder it left is not created again");
|
||||
Assert.FileExists(moved.Proxy(0));
|
||||
Assert.FileExists(moved.Proxy(1));
|
||||
Assert.FileMissing(moved.Proxy(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AProxyLeftByARenumberedFrameIsAskedForAgain()
|
||||
{
|
||||
@@ -99,8 +243,8 @@ public sealed class ProxyTests(TestContext context)
|
||||
|
||||
// A client or render node sending one that small is refused; one of the policy's size (1920 px) is kept.
|
||||
var frame = frames[0] with { Width = 4032, Height = 3024 };
|
||||
Assert.Equal(ProxyStoreResult.TooSmall, images.StoreProxy(fx.Archive, sequence, frame, Proxy(frame.CapturedAt!.Value, 6, 160, 120)));
|
||||
Assert.Equal(ProxyStoreResult.Stored, images.StoreProxy(fx.Archive, sequence, frame, Proxy(frame.CapturedAt!.Value, 7, 1920, 1440)));
|
||||
Assert.Equal(ProxyStoreResult.TooSmall, await images.StoreProxyAsync(fx.Archive, sequence, frame, Proxy(frame.CapturedAt!.Value, 6, 160, 120), fx.Ct));
|
||||
Assert.Equal(ProxyStoreResult.Stored, await images.StoreProxyAsync(fx.Archive, sequence, frame, Proxy(frame.CapturedAt!.Value, 7, 1920, 1440), fx.Ct));
|
||||
var again = await images.BuildProxiesAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
|
||||
Assert.Equal((0, 1, 0, 1), (again.Built, again.Reused, again.Undecodable, again.Waiting));
|
||||
Assert.FileExists(folder.Proxy(0));
|
||||
@@ -114,7 +258,7 @@ public sealed class ProxyTests(TestContext context)
|
||||
var manifest = ManifestStore.ReadArchive(fx.Paths.ManifestPath);
|
||||
ManifestStore.WriteArchive(fx.Paths.ManifestPath, manifest with { ProxyPolicy = new ProxyPolicy { LongEdge = ProxyPolicy.Disabled } });
|
||||
|
||||
Assert.Equal(ProxyStoreResult.Disabled, fx.Service.Images.StoreProxy(fx.Archive, sequence, frames[0], Proxy(frames[0].CapturedAt!.Value, 4)));
|
||||
Assert.Equal(ProxyStoreResult.Disabled, await fx.Service.Images.StoreProxyAsync(fx.Archive, sequence, frames[0], Proxy(frames[0].CapturedAt!.Value, 4), fx.Ct));
|
||||
Assert.FileMissing(fx.Paths.Sequence(sequence.Folder).Proxy(0));
|
||||
}
|
||||
|
||||
@@ -170,7 +314,7 @@ public sealed class ProxyTests(TestContext context)
|
||||
|
||||
// A client makes one from the index's time: it is kept, and the next pass reuses it rather than making its own.
|
||||
var sent = Proxy(frame.CapturedAt!.Value, 1);
|
||||
Assert.Equal(ProxyStoreResult.Stored, images.StoreProxy(fx.Archive, sequence, frame, sent));
|
||||
Assert.Equal(ProxyStoreResult.Stored, await images.StoreProxyAsync(fx.Archive, sequence, frame, sent, fx.Ct));
|
||||
var again = await images.BuildProxiesAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
|
||||
Assert.Equal((0, 1), (again.Built, again.Reused));
|
||||
Assert.SequenceEqual(sent, await File.ReadAllBytesAsync(folder.Proxy(0), fx.Ct));
|
||||
@@ -264,7 +408,7 @@ public sealed class ProxyTests(TestContext context)
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (sequence, frames) = await HeifSequenceAsync(fx);
|
||||
var images = fx.Service.Images;
|
||||
Assert.Equal(ProxyStoreResult.Stored, images.StoreProxy(fx.Archive, sequence, frames[1], Proxy(frames[1].CapturedAt!.Value, 1)));
|
||||
Assert.Equal(ProxyStoreResult.Stored, await images.StoreProxyAsync(fx.Archive, sequence, frames[1], Proxy(frames[1].CapturedAt!.Value, 1), fx.Ct));
|
||||
|
||||
// A pass resumed at frame 1 (paused, retried or restarted) finds that frame done, but frame 0 still waits: the
|
||||
// status must not tell a server's clients that nothing is wanted.
|
||||
@@ -272,7 +416,7 @@ public sealed class ProxyTests(TestContext context)
|
||||
Assert.Equal((0, 1, 0, 0), (resumed.Built, resumed.Reused, resumed.Undecodable, resumed.Waiting));
|
||||
Assert.False(await ProxiesBuiltAsync(fx, sequence), "frame 0 has no proxy");
|
||||
|
||||
Assert.Equal(ProxyStoreResult.Stored, images.StoreProxy(fx.Archive, sequence, frames[0], Proxy(frames[0].CapturedAt!.Value, 2)));
|
||||
Assert.Equal(ProxyStoreResult.Stored, await images.StoreProxyAsync(fx.Archive, sequence, frames[0], Proxy(frames[0].CapturedAt!.Value, 2), fx.Ct));
|
||||
await images.BuildProxiesAsync(fx.Archive, sequence, force: false, 1, (_, _) => ValueTask.CompletedTask, fx.Ct);
|
||||
Assert.True(await ProxiesBuiltAsync(fx, sequence), "every frame has its proxy");
|
||||
}
|
||||
@@ -290,7 +434,7 @@ public sealed class ProxyTests(TestContext context)
|
||||
{
|
||||
Frames = [.. sidecar.Frames.Select(f => f with { Tier = DecodeTier.EmbeddedPreview, Flags = f.Flags | FrameFlags.PreviewOnly, PreviewW = 160, PreviewH = 120 })],
|
||||
});
|
||||
Assert.Equal(ProxyStoreResult.Stored, images.StoreProxy(fx.Archive, sequence, frames[0], Proxy(frames[0].CapturedAt!.Value, 1)));
|
||||
Assert.Equal(ProxyStoreResult.Stored, await images.StoreProxyAsync(fx.Archive, sequence, frames[0], Proxy(frames[0].CapturedAt!.Value, 1), fx.Ct));
|
||||
|
||||
// Frame 0 now renders from its proxy, not from the thumbnail: it caps nothing. Its tier stays the original's.
|
||||
await images.BuildProxiesAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
|
||||
@@ -315,7 +459,7 @@ public sealed class ProxyTests(TestContext context)
|
||||
var (sequence, frames) = await HeifSequenceAsync(fx);
|
||||
var folder = fx.Paths.Sequence(sequence.Folder);
|
||||
var images = fx.Service.Images;
|
||||
Assert.Equal(ProxyStoreResult.Stored, images.StoreProxy(fx.Archive, sequence, frames[0], Proxy(frames[0].CapturedAt!.Value, 1, 640, 480)));
|
||||
Assert.Equal(ProxyStoreResult.Stored, await images.StoreProxyAsync(fx.Archive, sequence, frames[0], Proxy(frames[0].CapturedAt!.Value, 1, 640, 480), fx.Ct));
|
||||
Task<ImagePassResult> ThumbnailsAsync(bool force = false) =>
|
||||
images.BuildThumbnailsAsync(fx.Archive, sequence, force, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
|
||||
(int, int) Entry()
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user