feat: owner review 2 phase 4b: mass conversion after import (#13)
CI / Build and test (push) Failing after 3m1s
CI / PostgreSQL 16 integration (push) Skipped
CI / PostgreSQL 17 integration (push) Skipped
CI / Docker image (push) Skipped
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 10s
Release / Build, test and publish (push) Successful in 15m31s
Release / Server image (push) Successful in 1m13s
CI / Build and test (push) Failing after 3m1s
CI / PostgreSQL 16 integration (push) Skipped
CI / PostgreSQL 17 integration (push) Skipped
CI / Docker image (push) Skipped
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 10s
Release / Build, test and publish (push) Successful in 15m31s
Release / Server image (push) Successful in 1m13s
Convert photos already in the archive to JPEG, TIFF-16 or Linear DNG, from the panel's Library and the desktop's Archive and Library. Each run asks Replace (same frames, originals to the trash, recoverable) or New sequence. A dry run lists what converts, what is skipped and why, size before and after, and free space; the job converts exactly the confirmed photos, resumes, and commits all or nothing per sequence. A conversion never changes what Chronos reads from a photo. Replaced originals are erased only on the archive's own word that their conversion is gone. Desktop TIFFs that WIC showed black or as noise now go to the managed reader; the Edit picker no longer spins a core. 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 #13.
This commit is contained in:
1 parent
7e4a3b481d
commit
efa87f68ce
354 files changed
+24530
-843
No files matched your search
@@ -34,10 +34,13 @@ internal sealed class ArchiveFixture : IAsyncDisposable
|
||||
public CancellationToken Ct => _context.Cancellation;
|
||||
|
||||
/// <param name="database">The index to use (a PostgreSQL schema); a temporary SQLite file when null.</param>
|
||||
public static async Task<ArchiveFixture> CreateAsync(TestContext context, string? root = null, DatabaseOptions? database = null)
|
||||
/// <param name="configure">Changes to the archive service's options (decoders, free space).</param>
|
||||
public static async Task<ArchiveFixture> CreateAsync(TestContext context, string? root = null, DatabaseOptions? database = null,
|
||||
Func<ArchiveServiceOptions, ArchiveServiceOptions>? configure = null)
|
||||
{
|
||||
var db = await ChronosDatabase.OpenAsync(database ?? DatabaseOptions.Sqlite(Path.Combine(context.CreateTempDirectory("db"), "chronos.db")), context.Cancellation);
|
||||
var service = new ArchiveService(db, new ArchiveServiceOptions { Owner = "test", LeaseWait = TimeSpan.FromSeconds(20) });
|
||||
var options = new ArchiveServiceOptions { Owner = "test", LeaseWait = TimeSpan.FromSeconds(20) };
|
||||
var service = new ArchiveService(db, configure?.Invoke(options) ?? options);
|
||||
var archive = await service.AdoptAsync(root ?? context.CreateTempDirectory("archive"), "Main", context.Cancellation);
|
||||
return new ArchiveFixture(context, db, service, archive);
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -439,21 +439,33 @@ public abstract class FrameTrashTests(TestContext context) : DatabaseTest(contex
|
||||
var (sequence, frames) = await UploadedAsync(fx, 2);
|
||||
var replacement = frames[1];
|
||||
var good = WriteReplaced(fx, sequence, "20261027100000", replacement.Sha256!);
|
||||
var bad = WriteReplaced(fx, sequence, "20261027110000", new string('a', 64));
|
||||
// A replacement no frame of the sequence holds any more, and whose deletion the sidecar records (its conversion deleted
|
||||
// and erased): the dry run says so, and the emptying confirmed with it erases the originals (owner review 2, R7).
|
||||
var deleted = new string('a', 64);
|
||||
var gone = WriteReplaced(fx, sequence, "20261027110000", deleted);
|
||||
var sidecar = fx.Paths.Sequence(sequence.Folder).ManifestPath;
|
||||
var manifest = ManifestStore.ReadSequence(sidecar);
|
||||
ManifestStore.WriteSequence(sidecar, manifest with
|
||||
{
|
||||
Removed = [.. (manifest.Removed ?? []), new RemovedPhoto { Sha256 = deleted, Reason = RemovalReason.Deleted, At = DateTimeOffset.UtcNow }],
|
||||
});
|
||||
// One that nothing in the archive says was deleted stays: the sidecar holds no frame of it and no tombstone of it, as a
|
||||
// sidecar rebuilt from the photos shows a conversion it never knew.
|
||||
var untraced = WriteReplaced(fx, sequence, "20261027113000", new string('b', 64));
|
||||
var engine = await fx.StartEngineAsync();
|
||||
var listed = await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct);
|
||||
Assert.True(listed.All(e => e.Kind == TrashKind.Replaced));
|
||||
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.True(plan.Entries.Single(e => e.Name == FrameTrash.EntryPrefix + gone).ConversionDeleted);
|
||||
Assert.False(plan.Entries.Single(e => e.Name == FrameTrash.EntryPrefix + good).ConversionDeleted);
|
||||
Assert.False(plan.Entries.Single(e => e.Name == FrameTrash.EntryPrefix + untraced).ConversionDeleted, "the archive does not say it was deleted");
|
||||
var job = await engine.EnqueueAsync(ArchiveJobs.EmptyTrash(fx.Archive, plan), cancellationToken: fx.Ct);
|
||||
var failed = await fx.WaitForAsync(job.Id, JobState.Failed);
|
||||
var kept = await fx.WaitForAsync(job.Id, JobState.Failed);
|
||||
Assert.Contains(Chronos.Core.Localization.Loc.L("Archive.Log.TrashKeptUnknown", sequence.DisplayTitle), kept.LogTail);
|
||||
Assert.Equal(FrameTrash.EntryPrefix + untraced, (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Single().Name, "the others are erased");
|
||||
|
||||
Assert.Equal(Chronos.Core.Localization.Loc.Plural("Archive.Error.TrashKept", 1), failed.Error);
|
||||
var left = Assert.Contains(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct), e => e.Name == FrameTrash.EntryPrefix + bad);
|
||||
Assert.Equal(TrashKind.Replaced, left.Kind);
|
||||
Assert.DoesNotContain(FrameTrash.EntryPrefix + good, (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Select(e => e.Name));
|
||||
|
||||
// A replacement whose file no longer hashes to it keeps its originals too.
|
||||
// A replacement whose file no longer hashes to it keeps its originals.
|
||||
var other = WriteReplaced(fx, sequence, "20261027120000", replacement.Sha256!);
|
||||
var file = fx.Paths.FromRelative(replacement.RelativePath);
|
||||
var bytes = File.ReadAllBytes(file);
|
||||
|
||||
@@ -0,0 +1,810 @@
|
||||
using System.Security.Cryptography;
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Core.Import;
|
||||
using Chronos.Core.Localization;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Core.Project;
|
||||
using Chronos.Data.Archiving;
|
||||
using Chronos.Data.Archiving.Jobs;
|
||||
using Chronos.Data.Repositories;
|
||||
using Chronos.Testing;
|
||||
using static Chronos.Data.Tests.Archiving.ArchiveFixture;
|
||||
|
||||
namespace Chronos.Data.Tests.Archiving;
|
||||
|
||||
/// <summary>
|
||||
/// Frames whose files a conversion replaced (owner review 2, R7) and everything that meets them afterwards: a replacement
|
||||
/// that could not be undone, a second conversion, the conversion deleted, the trash emptied, the job that made an entry,
|
||||
/// project positions, and the original filed again.
|
||||
/// </summary>
|
||||
[Category("archive")]
|
||||
public sealed class ReplacedTrashTests(TestContext context)
|
||||
{
|
||||
/// <summary>A sequence of <paramref name="count"/> uploaded photos ten seconds apart, and their bytes.</summary>
|
||||
private static async Task<(Guid Id, List<byte[]> Photos)> UploadedAsync(ArchiveFixture fx, int count, string session = "upload-1")
|
||||
{
|
||||
Guid? id = null;
|
||||
var photos = new List<byte[]>();
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var bytes = Photos.Jpeg(Start.AddSeconds(10 * i), 100 + i);
|
||||
photos.Add(bytes);
|
||||
id = (await CommitAsync(fx, id, id is null ? session : null, bytes, $"IMG_{i + 1:0000}.JPG")).SequenceId;
|
||||
}
|
||||
return (id!.Value, photos);
|
||||
}
|
||||
|
||||
private static Task<IngestResult> CommitAsync(ArchiveFixture fx, Guid? sequenceId, string? session, byte[] bytes, string name)
|
||||
{
|
||||
var staging = Path.Combine(fx.Paths.StagingDirectory, "uploads");
|
||||
Directory.CreateDirectory(staging);
|
||||
var path = Path.Combine(staging, $"{Guid.NewGuid():N}.part");
|
||||
File.WriteAllBytes(path, bytes);
|
||||
return fx.Service.Ingest.CommitAsync(new IngestRequest(fx.Archive.Id, sequenceId, null, sequenceId is null ? session : null, path, name, bytes.Length,
|
||||
Sha(bytes), null, null), fx.Ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replaces the files of the frames at <paramref name="indices"/> (in the sidecar now) with other photos, as a
|
||||
/// conversion's stage B does (<see cref="FrameTrash.ReplaceAsync"/>), on this host or on <paramref name="by"/> (another
|
||||
/// host sharing the archive, whose index this one does not read); returns the trash entry of the originals.
|
||||
/// </summary>
|
||||
private static async Task<TrashEntry> ReplaceAsync(ArchiveFixture fx, Guid sequenceId, IReadOnlyList<int> indices, int seed, Guid? job = null,
|
||||
(ArchiveService Service, Archive Archive)? by = null)
|
||||
{
|
||||
var sequence = (await fx.Db.Sequences.GetAsync(sequenceId))!;
|
||||
var manifest = Sidecar(fx, sequence);
|
||||
var staging = Path.Combine(fx.Paths.StagingDirectory, "replace-test");
|
||||
Directory.CreateDirectory(staging);
|
||||
var swaps = new List<FrameSwap>();
|
||||
foreach (var i in indices)
|
||||
{
|
||||
var frame = manifest.Frames[i];
|
||||
var bytes = Photos.Jpeg(frame.CapturedAt, seed + i);
|
||||
var path = Path.Combine(staging, $"{Guid.NewGuid():N}.jpeg");
|
||||
File.WriteAllBytes(path, bytes);
|
||||
var sha = Sha(bytes);
|
||||
swaps.Add(new FrameSwap(frame.Id, fx.Paths.ToRelative(path), [], (current, _) => Path.ChangeExtension(current.File, ".jpeg"), current => current with
|
||||
{
|
||||
Sha256 = sha,
|
||||
Size = bytes.Length,
|
||||
Source = new FrameSource { Sha256 = current.Sha256, Format = current.Format, File = current.File, Size = current.Size },
|
||||
}));
|
||||
}
|
||||
var (service, archive) = by ?? (fx.Service, fx.Archive);
|
||||
var result = Assert.NotNull(await service.FrameTrash.ReplaceAsync(archive, sequenceId, null, swaps, null, job, null, null, fx.Ct));
|
||||
return Assert.NotNull(result.Entry);
|
||||
}
|
||||
|
||||
/// <summary>Flips a byte near the end of <paramref name="path"/>; returns its bytes before.</summary>
|
||||
private static byte[] Damage(string path)
|
||||
{
|
||||
var bytes = File.ReadAllBytes(path);
|
||||
var damaged = (byte[])bytes.Clone();
|
||||
damaged[^10] ^= 0x01;
|
||||
File.WriteAllBytes(path, damaged);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
private static SequenceManifest Sidecar(ArchiveFixture fx, Sequence sequence) => ManifestStore.ReadSequence(fx.Paths.Sequence(sequence.Folder).ManifestPath);
|
||||
|
||||
private static string Sha(byte[] bytes) => Convert.ToHexStringLower(SHA256.HashData(bytes));
|
||||
|
||||
private static string Sha(string path) => Sha(File.ReadAllBytes(path));
|
||||
|
||||
private static async Task<Job> EmptyAsync(ArchiveFixture fx, Chronos.Data.Jobs.JobEngine engine, TrashEmptyPlan plan, JobState state)
|
||||
{
|
||||
var job = await engine.EnqueueAsync(ArchiveJobs.EmptyTrash(fx.Archive, plan), cancellationToken: fx.Ct);
|
||||
return await fx.WaitForAsync(job.Id, state);
|
||||
}
|
||||
|
||||
/// <summary>What the emptying job of <paramref name="plan"/> is given, for a test that runs the emptying itself.</summary>
|
||||
private static EmptyTrashParameters Parameters(TrashEmptyPlan plan) =>
|
||||
new([.. plan.Entries.Select(e => new EmptyTrashItem(e.Name, SequenceTrash.EntryFingerprint(e)))], plan.Fingerprint);
|
||||
|
||||
[Test]
|
||||
public async Task AReplacementThatCouldNotBeUndoneIsUndoneBeforeAnythingElseIsFiled()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, _) = await UploadedAsync(fx, 2);
|
||||
var sequence = (await fx.Db.Sequences.GetAsync(id))!;
|
||||
var folder = fx.Paths.Sequence(sequence.Folder);
|
||||
var manifest = Sidecar(fx, sequence);
|
||||
var frame = manifest.Frames[0];
|
||||
// A replacement that failed and whose undo failed too: the original in its entry, the converted file in the sequence,
|
||||
// the record pending, the sidecar as it was, and the folder the converted file must go back to taken by a file.
|
||||
var staging = Path.Combine(fx.Paths.StagingDirectory, "convert-test");
|
||||
var staged = Path.Combine(staging, "converted.jpeg");
|
||||
var converted = Photos.Jpeg(frame.CapturedAt, 900);
|
||||
var entryFolder = Path.Combine(FrameTrash.Directory(fx.Paths), $"{id:N}-20261027120000");
|
||||
var record = entryFolder + ".json";
|
||||
var trashed = Path.Combine(entryFolder, "originals", Path.GetFileName(frame.File));
|
||||
var placed = Path.ChangeExtension(frame.File, ".jpeg");
|
||||
new FrameReplaceJournal(manifest.Versions.Frames, fx.Paths.ToRelative(record), fx.Paths.ToRelative(entryFolder),
|
||||
[new SwapMove(frame.File, fx.Paths.ToRelative(trashed))], [new SwapMove(placed, fx.Paths.ToRelative(staged))], false, null)
|
||||
{
|
||||
Names = new Dictionary<Guid, string> { [frame.Id] = placed },
|
||||
}.Write(folder);
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(trashed)!);
|
||||
File.WriteAllText(FrameRemovalJournal.PendingRecord(record), "{}");
|
||||
File.Move(folder.File(frame.File), trashed);
|
||||
File.WriteAllBytes(folder.File(placed), converted);
|
||||
Directory.CreateDirectory(fx.Paths.StagingDirectory);
|
||||
File.WriteAllText(staging, "in the way");
|
||||
|
||||
// An upload into the sequence meanwhile: the undo fails again, and nothing is filed.
|
||||
await Assert.ThrowsAsync<IOException>(() => CommitAsync(fx, id, null, Photos.Jpeg(Start.AddSeconds(30), 300), "IMG_0003.JPG"));
|
||||
Assert.Equal(manifest.Versions.Frames, Sidecar(fx, sequence).Versions.Frames);
|
||||
Assert.Equal(ReplacePhase.Undoing, Assert.NotNull(FrameReplaceJournal.Read(folder)).Phase);
|
||||
Assert.Empty(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct));
|
||||
|
||||
// Another writer of an earlier build moved the frames version meanwhile: the replacement is undone all the same, never
|
||||
// taken for one whose sidecar was written.
|
||||
ManifestStore.WriteSequence(folder.ManifestPath, manifest with { Versions = manifest.Versions with { Frames = manifest.Versions.Frames + 1 } });
|
||||
File.Delete(staging);
|
||||
await fx.Service.EditSequenceInfoAsync(id, info => info with { Rating = 2 }, fx.Ct);
|
||||
Assert.FileMissing(folder.ReplaceJournalPath);
|
||||
Assert.Equal(frame.Sha256, Sha(folder.File(frame.File)));
|
||||
Assert.FileMissing(folder.File(placed));
|
||||
Assert.Equal(Sha(converted), Sha(staged));
|
||||
Assert.False(Directory.Exists(entryFolder));
|
||||
Assert.FileMissing(FrameRemovalJournal.PendingRecord(record));
|
||||
Assert.Empty(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct));
|
||||
Assert.SequenceEqual(manifest.Frames.Select(f => (f.Id, f.File, f.Sha256)), Sidecar(fx, sequence).Frames.Select(f => (f.Id, f.File, f.Sha256)));
|
||||
|
||||
// Settled, the upload goes in.
|
||||
await CommitAsync(fx, id, null, Photos.Jpeg(Start.AddSeconds(30), 300), "IMG_0003.JPG");
|
||||
Assert.Count(3, Sidecar(fx, sequence).Frames);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnInterruptedReplacementIsSettledByWhatItsSidecarLists()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, _) = await UploadedAsync(fx, 2);
|
||||
var entry = await ReplaceAsync(fx, id, [0], 500);
|
||||
var sequence = (await fx.Db.Sequences.GetAsync(id))!;
|
||||
var folder = fx.Paths.Sequence(sequence.Folder);
|
||||
var manifest = Sidecar(fx, sequence);
|
||||
var (entryFolder, record) = FrameTrash.Location(fx.Paths, entry.Name);
|
||||
// As a crash right after the sidecar was written leaves it (at the version an ingest would give too): completed.
|
||||
File.Move(record, FrameRemovalJournal.PendingRecord(record));
|
||||
new FrameReplaceJournal(manifest.Versions.Frames - 1, fx.Paths.ToRelative(record), fx.Paths.ToRelative(entryFolder), [], [], false, null)
|
||||
{
|
||||
Names = new Dictionary<Guid, string> { [manifest.Frames[0].Id] = manifest.Frames[0].File },
|
||||
}.Write(folder);
|
||||
Assert.Empty(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct));
|
||||
await fx.Service.EditSequenceInfoAsync(id, info => info with { Rating = 2 }, fx.Ct);
|
||||
Assert.FileMissing(folder.ReplaceJournalPath);
|
||||
Assert.Equal(entry.Name, (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Single().Name);
|
||||
|
||||
// As a crash before the sidecar was written leaves it, with the frames version moved by another writer since: undone.
|
||||
manifest = Sidecar(fx, sequence);
|
||||
var frame = manifest.Frames[1];
|
||||
var staged = Path.Combine(fx.Paths.StagingDirectory, "convert-test", "converted.jpeg");
|
||||
var converted = Photos.Jpeg(frame.CapturedAt, 901);
|
||||
var second = Path.Combine(FrameTrash.Directory(fx.Paths), $"{id:N}-20261027130000");
|
||||
var trashed = Path.Combine(second, "originals", Path.GetFileName(frame.File));
|
||||
var placed = Path.ChangeExtension(frame.File, ".jpeg");
|
||||
new FrameReplaceJournal(manifest.Versions.Frames, fx.Paths.ToRelative(second + ".json"), fx.Paths.ToRelative(second),
|
||||
[new SwapMove(frame.File, fx.Paths.ToRelative(trashed))], [new SwapMove(placed, fx.Paths.ToRelative(staged))], false, null)
|
||||
{
|
||||
Names = new Dictionary<Guid, string> { [frame.Id] = placed },
|
||||
}.Write(folder);
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(trashed)!);
|
||||
File.WriteAllText(FrameRemovalJournal.PendingRecord(second + ".json"), "{}");
|
||||
File.Move(folder.File(frame.File), trashed);
|
||||
File.WriteAllBytes(folder.File(placed), converted);
|
||||
ManifestStore.WriteSequence(folder.ManifestPath, manifest with { Versions = manifest.Versions with { Frames = manifest.Versions.Frames + 1 } });
|
||||
|
||||
await fx.Service.EditSequenceInfoAsync(id, info => info with { Rating = 3 }, fx.Ct);
|
||||
Assert.FileMissing(folder.ReplaceJournalPath);
|
||||
Assert.Equal(frame.Sha256, Sha(folder.File(frame.File)));
|
||||
Assert.FileMissing(folder.File(placed));
|
||||
Assert.Equal(Sha(converted), Sha(staged));
|
||||
Assert.False(Directory.Exists(second));
|
||||
Assert.Equal(entry.Name, (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Single().Name);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task OriginalsConvertedTwiceAreErasedWhileTheLastConversionHolds()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, _) = await UploadedAsync(fx, 2);
|
||||
var engine = await fx.StartEngineAsync();
|
||||
await ReplaceAsync(fx, id, [0, 1], 500);
|
||||
await ReplaceAsync(fx, id, [0, 1], 600);
|
||||
var sequence = (await fx.Db.Sequences.GetAsync(id))!;
|
||||
var current = fx.Paths.Sequence(sequence.Folder).File(Sidecar(fx, sequence).Frames[0].File);
|
||||
var bytes = File.ReadAllBytes(current);
|
||||
|
||||
// The last conversion damaged: neither the originals nor the first conversion go.
|
||||
var damaged = (byte[])bytes.Clone();
|
||||
damaged[^10] ^= 0x01;
|
||||
File.WriteAllBytes(current, damaged);
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.Count(2, plan.Entries);
|
||||
Assert.True(plan.Entries.All(e => !e.ConversionDeleted), "the conversions are in the sequence");
|
||||
await EmptyAsync(fx, engine, plan, JobState.Failed);
|
||||
Assert.Count(2, await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct));
|
||||
|
||||
// Whole again: the first originals are held by the second conversion, through the first one's tombstone.
|
||||
File.WriteAllBytes(current, bytes);
|
||||
await EmptyAsync(fx, engine, await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct), JobState.Done);
|
||||
Assert.Empty(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task OriginalsWhoseConversionWasDeletedAreErasedAsTheDryRunSays()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, _) = await UploadedAsync(fx, 3);
|
||||
var engine = await fx.StartEngineAsync();
|
||||
var replaced = await ReplaceAsync(fx, id, [0], 500);
|
||||
var before = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.False(before.Entries.Single().ConversionDeleted);
|
||||
|
||||
// The user deletes the converted photo.
|
||||
var frames = await fx.Db.Frames.ListBySequenceAsync(id);
|
||||
var sequence = (await fx.Db.Sequences.GetAsync(id))!;
|
||||
var deletion = await fx.Service.FrameTrash.DeleteAsync(await fx.Service.FrameTrash.PlanDeleteAsync(id, [frames[0].Id], sequence.FramesVersion, fx.Ct),
|
||||
null, null, fx.Ct);
|
||||
|
||||
// The plan made before no longer holds: nothing is erased.
|
||||
await EmptyAsync(fx, engine, before, JobState.Failed);
|
||||
Assert.Count(2, await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct));
|
||||
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.True(plan.Entries.Single(e => e.Name == replaced.Name).ConversionDeleted, "the dry run says the conversion was deleted");
|
||||
Assert.False(plan.Entries.Single(e => e.Name == deletion.Entry.Name).ConversionDeleted);
|
||||
Assert.NotEqual(SequenceTrash.EntryFingerprint(plan.Entries.Single(e => e.Name == replaced.Name) with { ConversionDeleted = false }),
|
||||
SequenceTrash.EntryFingerprint(plan.Entries.Single(e => e.Name == replaced.Name)));
|
||||
|
||||
// The deleted conversion is still in the trash, damaged: the originals stay.
|
||||
var record = Assert.NotNull(FrameTrash.ReadEntry(fx.Paths, deletion.Entry.Name));
|
||||
var copy = Path.Combine(FrameTrash.Location(fx.Paths, deletion.Entry.Name).Folder, record.Photos[0].Files[0].Path);
|
||||
var bytes = File.ReadAllBytes(copy);
|
||||
var damaged = (byte[])bytes.Clone();
|
||||
damaged[^10] ^= 0x01;
|
||||
File.WriteAllBytes(copy, damaged);
|
||||
await EmptyAsync(fx, engine, await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, [replaced.Name], fx.Ct), JobState.Failed);
|
||||
Assert.Contains(replaced.Name, (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Select(e => e.Name));
|
||||
|
||||
// Whole again, both go; and once the conversion's entry is erased, the originals go with nothing left to check.
|
||||
File.WriteAllBytes(copy, bytes);
|
||||
await EmptyAsync(fx, engine, await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct), JobState.Done);
|
||||
Assert.Empty(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task OriginalsWhoseConvertedSequenceWasDeletedAreErasedWithIt()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, _) = await UploadedAsync(fx, 2);
|
||||
var engine = await fx.StartEngineAsync();
|
||||
var replaced = await ReplaceAsync(fx, id, [0, 1], 500);
|
||||
await fx.Service.Trash.DeleteAsync(await fx.Service.Trash.PlanAsync([id], null, fx.Ct), null, null, fx.Ct);
|
||||
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.Count(2, plan.Entries);
|
||||
Assert.True(plan.Entries.Single(e => e.Name == replaced.Name).ConversionDeleted);
|
||||
Assert.False(plan.Entries.Single(e => e.Kind == TrashKind.Sequence).ConversionDeleted);
|
||||
// Only the originals: the sequence's entry still holds the conversions, which hash to their records.
|
||||
await EmptyAsync(fx, engine, await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, [replaced.Name], fx.Ct), JobState.Done);
|
||||
Assert.Equal(TrashKind.Sequence, (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Single().Kind);
|
||||
|
||||
await fx.Service.RestoreAsync(fx.Archive.Id, (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Single().Name, fx.Ct);
|
||||
Assert.Count(2, await fx.Db.Frames.ListBySequenceAsync(id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AConversionAnotherHostMadeIsHashedWhereTheArchiveHoldsIt()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, photos) = await UploadedAsync(fx, 2);
|
||||
var engine = await fx.StartEngineAsync();
|
||||
// A desktop sharing the archive converts the first photo; this index still holds the original there.
|
||||
var replaced = await ReplaceAsync(fx, id, [0], 500, by: await fx.OtherWriterAsync());
|
||||
Assert.Equal(Sha(photos[0]), (await fx.Db.Frames.ListBySequenceAsync(id))[0].Sha256);
|
||||
var sequence = (await fx.Db.Sequences.GetAsync(id))!;
|
||||
var converted = fx.Paths.Sequence(sequence.Folder).File(Sidecar(fx, sequence).Frames[0].File);
|
||||
|
||||
// The conversion is damaged since: the dry run finds it in the sequence, and the emptying keeps the originals.
|
||||
var bytes = Damage(converted);
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.False(plan.Entries.Single().ConversionDeleted, "the archive holds the conversion, whatever this index has");
|
||||
var kept = await EmptyAsync(fx, engine, plan, JobState.Failed);
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKeptMismatch", replaced.Title), kept.LogTail);
|
||||
Assert.Equal(replaced.Name, (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Single().Name);
|
||||
|
||||
// Whole again: the originals go.
|
||||
File.WriteAllBytes(converted, bytes);
|
||||
await EmptyAsync(fx, engine, await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct), JobState.Done);
|
||||
Assert.Empty(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AConversionAnotherIndexMovedIsHashedWhereItIsNow()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, _) = await UploadedAsync(fx, 2);
|
||||
var engine = await fx.StartEngineAsync();
|
||||
var replaced = await ReplaceAsync(fx, id, [0], 500);
|
||||
// Another index moves the sequence into another folder; this one has not scanned since.
|
||||
var before = (await fx.Db.Sequences.GetAsync(id))!.Folder;
|
||||
var (other, archive) = await fx.OtherWriterAsync();
|
||||
await other.ApplyRenameAsync(await other.PlanRenameAsync(archive.Id, [id], "moved/{title}", null, fx.Ct), null, fx.Ct);
|
||||
Assert.Equal(before, (await fx.Db.Sequences.GetAsync(id))!.Folder);
|
||||
var folder = ArchiveScanner.FindSequenceFolders(fx.Paths, fx.Ct).Select(s => fx.Paths.Sequence(s.RelativeFolder)).Single(s => s.RelativeFolder != before);
|
||||
Assert.True(folder.RelativeFolder.StartsWith("moved", StringComparison.Ordinal), folder.RelativeFolder);
|
||||
var converted = folder.File(ManifestStore.ReadSequence(folder.ManifestPath).Frames[0].File);
|
||||
|
||||
var bytes = Damage(converted);
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.False(plan.Entries.Single().ConversionDeleted, "found in the folder the sequence is in now");
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKeptMismatch", replaced.Title), (await EmptyAsync(fx, engine, plan, JobState.Failed)).LogTail);
|
||||
|
||||
File.WriteAllBytes(converted, bytes);
|
||||
await EmptyAsync(fx, engine, await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct), JobState.Done);
|
||||
Assert.Empty(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task OriginalsStayWhileTheArchiveCannotTellWhereTheirConversionIs()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, photos) = await UploadedAsync(fx, 2);
|
||||
var engine = await fx.StartEngineAsync();
|
||||
var replaced = await ReplaceAsync(fx, id, [0], 500);
|
||||
var sequence = (await fx.Db.Sequences.GetAsync(id))!;
|
||||
var folder = fx.Paths.Sequence(sequence.Folder);
|
||||
var manifest = Sidecar(fx, sequence);
|
||||
|
||||
// A sidecar that cannot be read: a scan rebuilds it, as the line says.
|
||||
File.WriteAllText(folder.ManifestPath, "{ \"damaged");
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.False(plan.Entries.Single().ConversionDeleted);
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKeptUnreadable", replaced.Title), (await EmptyAsync(fx, engine, plan, JobState.Failed)).LogTail);
|
||||
|
||||
// A sidecar rebuilt from the photos, as a scan adopts a folder that lost its own: new frame ids, nothing hashed, no
|
||||
// tombstones. A verify of the sequence, as the line says, may find the conversion in one of its frames.
|
||||
var rebuilt = manifest with { Frames = [.. manifest.Frames.Select(f => f with { Id = Guid.CreateVersion7(), Sha256 = null })], Removed = null };
|
||||
ManifestStore.WriteSequence(folder.ManifestPath, rebuilt);
|
||||
plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.False(plan.Entries.Single().ConversionDeleted);
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKeptUnhashed", replaced.Title), (await EmptyAsync(fx, engine, plan, JobState.Failed)).LogTail);
|
||||
// Unless their files are missing: there is nothing a verify could hash.
|
||||
ManifestStore.WriteSequence(folder.ManifestPath, rebuilt with { Frames = [.. rebuilt.Frames.Select(f => f with { Flags = f.Flags | FrameFlags.Missing })] });
|
||||
plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKeptUnknown", replaced.Title), (await EmptyAsync(fx, engine, plan, JobState.Failed)).LogTail);
|
||||
// Nor when they are flagged corrupt: a verify does not hash a file whose size differs from its record, so it would
|
||||
// never settle the entry.
|
||||
ManifestStore.WriteSequence(folder.ManifestPath, rebuilt with { Frames = [.. rebuilt.Frames.Select(f => f with { Flags = f.Flags | FrameFlags.Corrupt })] });
|
||||
plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKeptUnknown", replaced.Title), (await EmptyAsync(fx, engine, plan, JobState.Failed)).LogTail);
|
||||
|
||||
// Hashed, the conversion damaged meanwhile: no frame holds it and no tombstone says it was deleted. The originals stay,
|
||||
// and the dry run never calls them the last copy: only the archive's own word makes a conversion deleted.
|
||||
Damage(folder.File(manifest.Frames[0].File));
|
||||
ManifestStore.WriteSequence(folder.ManifestPath, rebuilt with { Frames = [.. rebuilt.Frames.Select(f => f with { Sha256 = Sha(folder.File(f.File)) })] });
|
||||
plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.False(plan.Entries.Single().ConversionDeleted, "nothing in the archive says the conversion was deleted");
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKeptUnknown", replaced.Title), (await EmptyAsync(fx, engine, plan, JobState.Failed)).LogTail);
|
||||
|
||||
// Restored, as the line says, the original comes back whole, as a sequence of its own.
|
||||
var restored = await fx.Service.RestoreEntryAsync(fx.Archive.Id, replaced.Name, null, fx.Ct);
|
||||
Assert.True(restored.NewSequence);
|
||||
Assert.SequenceEqual([Sha(photos[0])], (await fx.Db.Frames.ListBySequenceAsync(restored.Sequence.Id)).Select(f => f.Sha256!));
|
||||
Assert.Empty(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AConversionAnotherHostMadeIsNotTakenForDeletedOnceItsSidecarIsRebuilt()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, photos) = await UploadedAsync(fx, 2);
|
||||
var engine = await fx.StartEngineAsync();
|
||||
// A desktop sharing the archive converts the first photo: this index never sees the converted file.
|
||||
var replaced = await ReplaceAsync(fx, id, [0], 500, by: await fx.OtherWriterAsync());
|
||||
var sequence = (await fx.Db.Sequences.GetAsync(id))!;
|
||||
var folder = fx.Paths.Sequence(sequence.Folder);
|
||||
// The conversion is damaged and the sidecar lost since: this index's scan rebuilds the sidecar from the photos with what
|
||||
// it knew, which is nothing of the conversion (the converted file gets a new frame id, no tombstone names it).
|
||||
Damage(folder.File(Sidecar(fx, sequence).Frames[0].File));
|
||||
File.Delete(folder.ManifestPath);
|
||||
await fx.ScanAsync();
|
||||
Assert.Null(Sidecar(fx, sequence).Removed);
|
||||
var before = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.False(before.Entries.Single().ConversionDeleted);
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKeptUnhashed", replaced.Title), (await EmptyAsync(fx, engine, before, JobState.Failed)).LogTail);
|
||||
|
||||
// A routine verify hashes every photo, the damaged conversion too: still nothing says the conversion was deleted, so the
|
||||
// originals, the only good copy of that photo, are neither called the last copy nor erased.
|
||||
await fx.Service.Verifier.VerifyAsync(fx.Archive, [id], default, (_, _, _) => ValueTask.CompletedTask, cancellationToken: fx.Ct);
|
||||
Assert.True(Sidecar(fx, sequence).Frames.All(f => f.Sha256 is not null), "verified");
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.False(plan.Entries.Single().ConversionDeleted, "the archive does not say the conversion was deleted");
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKeptUnknown", replaced.Title), (await EmptyAsync(fx, engine, plan, JobState.Failed)).LogTail);
|
||||
var record = Assert.NotNull(FrameTrash.ReadEntry(fx.Paths, replaced.Name));
|
||||
Assert.Equal(Sha(photos[0]), Sha(Path.Combine(FrameTrash.Location(fx.Paths, replaced.Name).Folder, record.Photos[0].Files[0].Path)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task OriginalsOfASequenceAnotherHostDeletedAndErasedGoAsTheDryRunSays()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, _) = await UploadedAsync(fx, 2);
|
||||
var engine = await fx.StartEngineAsync();
|
||||
var replaced = await ReplaceAsync(fx, id, [0, 1], 500);
|
||||
// A desktop sharing the archive deletes the converted sequence and erases its entry alone; this index still lists it.
|
||||
var (other, archive) = await fx.OtherWriterAsync();
|
||||
await other.Trash.DeleteAsync(await other.Trash.PlanAsync([id], null, fx.Ct), null, null, fx.Ct);
|
||||
var deleted = (await other.ListTrashAsync(archive.Id, fx.Ct)).Single(e => e.Kind == TrashKind.Sequence);
|
||||
var erased = await other.Trash.EmptyAsync(archive, Parameters(await other.Trash.PlanEmptyAsync(archive, [deleted.Name], fx.Ct)), 0,
|
||||
(_, _) => ValueTask.CompletedTask, fx.Ct);
|
||||
Assert.Equal((1, 0), (erased.Erased, erased.Refused.Count));
|
||||
Assert.NotNull(await fx.Db.Sequences.GetAsync(id));
|
||||
var erasures = Path.Combine(SequenceTrash.TrashDirectory(fx.Paths), ".erased");
|
||||
Assert.FileExists(Path.Combine(erasures, $"{id:N}.json"));
|
||||
|
||||
// The trash's record of that erasing is the archive's word that the conversions went with the sequence, whatever this
|
||||
// index still lists: the dry run says so, and the emptying confirmed with it erases.
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.True(plan.Entries.Single().ConversionDeleted, "the trash erased the sequence that held the conversions");
|
||||
await EmptyAsync(fx, engine, plan, JobState.Done);
|
||||
Assert.Empty(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct));
|
||||
Assert.False(Directory.Exists(erasures), "no entry needs the record any more");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ASequenceThatAlsoLivesLeavesNoRecordOfItsErasing()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, _) = await UploadedAsync(fx, 2);
|
||||
var engine = await fx.StartEngineAsync();
|
||||
var replaced = await ReplaceAsync(fx, id, [0, 1], 500);
|
||||
var sequence = (await fx.Db.Sequences.GetAsync(id))!;
|
||||
// A copy of the converted sequence's folder made outside Chronos: its sidecar names the same sequence.
|
||||
var source = fx.Paths.Sequence(sequence.Folder).Folder;
|
||||
var copy = Path.Combine(fx.Paths.Root, "copy", Path.GetFileName(source));
|
||||
foreach (var file in Directory.EnumerateFiles(source, "*", SearchOption.AllDirectories))
|
||||
{
|
||||
var target = Path.Combine(copy, Path.GetRelativePath(source, file));
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(target)!);
|
||||
File.Copy(file, target);
|
||||
}
|
||||
await fx.Service.Trash.DeleteAsync(await fx.Service.Trash.PlanAsync([id], null, fx.Ct), null, null, fx.Ct);
|
||||
var deleted = (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Single(e => e.Kind == TrashKind.Sequence);
|
||||
|
||||
// Erasing the deleted sequence while a folder still names it leaves no record that its conversions went with it.
|
||||
var erased = await fx.Service.Trash.EmptyAsync(fx.Archive, Parameters(await fx.Service.Trash.PlanEmptyAsync(fx.Archive, [deleted.Name], fx.Ct)), 0,
|
||||
(_, _) => ValueTask.CompletedTask, fx.Ct);
|
||||
Assert.Equal((1, 0), (erased.Erased, erased.Refused.Count));
|
||||
Assert.FileMissing(Path.Combine(SequenceTrash.TrashDirectory(fx.Paths), ".erased", $"{id:N}.json"));
|
||||
|
||||
// So once the copy's sidecar is lost, the walk missing it, the originals stay: its conversions are still there.
|
||||
File.Delete(Path.Combine(copy, Path.GetRelativePath(source, fx.Paths.Sequence(sequence.Folder).ManifestPath)));
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.False(plan.Entries.Single().ConversionDeleted);
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKeptUnknown", replaced.Title), (await EmptyAsync(fx, engine, plan, JobState.Failed)).LogTail);
|
||||
Assert.Equal(replaced.Name, (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Single().Name);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task OriginalsOfASequenceGoneWithoutTheTrashErasingItStay()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, photos) = await UploadedAsync(fx, 2);
|
||||
var engine = await fx.StartEngineAsync();
|
||||
var replaced = await ReplaceAsync(fx, id, [0], 500);
|
||||
var sequence = (await fx.Db.Sequences.GetAsync(id))!;
|
||||
|
||||
// The sequence's folder removed outside Chronos while this index still lists it: kept, and the line says what drops the row.
|
||||
Directory.Delete(fx.Paths.Sequence(sequence.Folder).Folder, recursive: true);
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.False(plan.Entries.Single().ConversionDeleted);
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKeptStaleIndex", replaced.Title), (await EmptyAsync(fx, engine, plan, JobState.Failed)).LogTail);
|
||||
|
||||
// Rebuilt, the index lists it no more; still nothing in the archive says the conversion was deleted, so the originals,
|
||||
// the only copy of that photo there is, stay.
|
||||
await fx.ScanAsync(ScanMode.Rebuild);
|
||||
Assert.Null(await fx.Db.Sequences.GetAsync(id));
|
||||
plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.False(plan.Entries.Single().ConversionDeleted, "no sequence of that id anywhere, and no record of erasing it");
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKeptUnknown", replaced.Title), (await EmptyAsync(fx, engine, plan, JobState.Failed)).LogTail);
|
||||
|
||||
// Restored, as the line says, it comes back as a sequence of its own.
|
||||
var restored = await fx.Service.RestoreEntryAsync(fx.Archive.Id, replaced.Name, null, fx.Ct);
|
||||
Assert.True(restored.NewSequence);
|
||||
Assert.SequenceEqual([Sha(photos[0])], (await fx.Db.Frames.ListBySequenceAsync(restored.Sequence.Id)).Select(f => f.Sha256!));
|
||||
Assert.Empty(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task OriginalsStayWhileTheirSequenceIsInAFolderWithoutSidecar()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, _) = await UploadedAsync(fx, 2);
|
||||
var engine = await fx.StartEngineAsync();
|
||||
var replaced = await ReplaceAsync(fx, id, [0], 500);
|
||||
// Another index moves the sequence, its sidecar is lost there, and this index, which never saw the move, lists it no more.
|
||||
var (other, archive) = await fx.OtherWriterAsync();
|
||||
await other.ApplyRenameAsync(await other.PlanRenameAsync(archive.Id, [id], "moved/{title}", null, fx.Ct), null, fx.Ct);
|
||||
var moved = ArchiveScanner.FindSequenceFolders(fx.Paths, fx.Ct).Select(s => fx.Paths.Sequence(s.RelativeFolder)).Single();
|
||||
Assert.True(moved.RelativeFolder.StartsWith("moved", StringComparison.Ordinal), moved.RelativeFolder);
|
||||
File.Delete(moved.ManifestPath);
|
||||
await fx.Db.Sequences.DeleteAsync(id, fx.Ct);
|
||||
|
||||
// The walk of the archive cannot tell whose photos those are, and the trash never erased the sequence: its conversion,
|
||||
// alive there, is not taken for deleted, and the originals stay without it being hashed.
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.False(plan.Entries.Single().ConversionDeleted);
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKeptUnknown", replaced.Title), (await EmptyAsync(fx, engine, plan, JobState.Failed)).LogTail);
|
||||
Assert.Equal(replaced.Name, (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Single().Name);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task OriginalsOfAConvertedSequenceErasedBeforeThemGoAsTheDryRunSays()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, _) = await UploadedAsync(fx, 2);
|
||||
var (second, _) = await UploadedAsync(fx, 2, "upload-2");
|
||||
var engine = await fx.StartEngineAsync();
|
||||
var replaced = await ReplaceAsync(fx, id, [0, 1], 500);
|
||||
var together = await ReplaceAsync(fx, second, [0], 600);
|
||||
await fx.Service.Trash.DeleteAsync(await fx.Service.Trash.PlanAsync([id, second], null, fx.Ct), null, null, fx.Ct);
|
||||
var erasures = Path.Combine(SequenceTrash.TrashDirectory(fx.Paths), ".erased");
|
||||
|
||||
// The first sequence's entry erased alone: the trash records erasing it, for the originals it leaves there.
|
||||
var deleted = (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Single(e => e.Kind == TrashKind.Sequence && e.SequenceId == id);
|
||||
await EmptyAsync(fx, engine, await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, [deleted.Name], fx.Ct), JobState.Done);
|
||||
Assert.Equal($"{id:N}.json", Path.GetFileName(Directory.EnumerateFiles(erasures).Single()));
|
||||
|
||||
// Then everything in one run: the first one's originals as that record says, the second sequence, then its originals
|
||||
// as the record its erasing leaves says. No record is left once no entry needs one.
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.Count(3, plan.Entries);
|
||||
Assert.True(plan.Entries.Single(e => e.Name == replaced.Name).ConversionDeleted, "the trash erased the sequence that held the conversions");
|
||||
Assert.True(plan.Entries.Single(e => e.Name == together.Name).ConversionDeleted, "its sequence is in the trash");
|
||||
await EmptyAsync(fx, engine, plan, JobState.Done);
|
||||
Assert.Empty(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct));
|
||||
Assert.False(Directory.Exists(erasures), "no entry needs a record of erasing any more");
|
||||
Assert.Empty(Directory.EnumerateFiles(SequenceTrash.TrashDirectory(fx.Paths), "*", SearchOption.AllDirectories));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnEmptyingWalksTheArchiveAgainForASequenceThatMovedSinceItsWalk()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, _) = await UploadedAsync(fx, 2);
|
||||
var (second, _) = await UploadedAsync(fx, 3, "upload-2");
|
||||
var replaced = await ReplaceAsync(fx, id, [0], 500);
|
||||
// Photos deleted from another sequence since: the newer entry, erased first.
|
||||
var row = (await fx.Db.Sequences.GetAsync(second))!;
|
||||
var deletion = await fx.Service.FrameTrash.DeleteAsync(
|
||||
await fx.Service.FrameTrash.PlanDeleteAsync(second, [(await fx.Db.Frames.ListBySequenceAsync(second))[0].Id], row.FramesVersion, fx.Ct), null, null, fx.Ct);
|
||||
// Another index moved the converted sequence: this one finds it by walking the archive, once for the whole plan.
|
||||
var (other, archive) = await fx.OtherWriterAsync();
|
||||
await other.ApplyRenameAsync(await other.PlanRenameAsync(archive.Id, [id], "moved/{title}", null, fx.Ct), null, fx.Ct);
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.SequenceEqual([deletion.Entry.Name, replaced.Name], plan.Entries.Select(e => e.Name));
|
||||
Assert.False(plan.Entries.Single(e => e.Name == replaced.Name).ConversionDeleted, "found where it is now");
|
||||
|
||||
// It moves again while the emptying erases the other entry: the folder that walk found no longer holds it, so the archive
|
||||
// is walked again, and the conversion is hashed where it is now.
|
||||
var result = await fx.Service.Trash.EmptyAsync(fx.Archive, Parameters(plan), 0, async (done, _) =>
|
||||
{
|
||||
if (done == 1) await other.ApplyRenameAsync(await other.PlanRenameAsync(archive.Id, [id], "again/{title}", null, fx.Ct), null, fx.Ct);
|
||||
}, fx.Ct);
|
||||
Assert.Equal((2, 0), (result.Erased, result.Refused.Count));
|
||||
Assert.Empty(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task OriginalsOfABusySequenceStayInTheTrash()
|
||||
{
|
||||
await using var fx = await CreateAsync(context, configure: o => o with { LeaseWait = TimeSpan.FromMilliseconds(300) });
|
||||
var (id, _) = await UploadedAsync(fx, 2);
|
||||
var replaced = await ReplaceAsync(fx, id, [0], 500);
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
var parameters = new EmptyTrashParameters([.. plan.Entries.Select(e => new EmptyTrashItem(e.Name, SequenceTrash.EntryFingerprint(e)))], plan.Fingerprint);
|
||||
|
||||
// Another host writes the sequence meanwhile: the conversion is not weighed under it.
|
||||
using (Assert.NotNull(LeaseFile.TryAcquire(fx.Paths.LocksDirectory, $"seq-{id:N}", "another host")))
|
||||
{
|
||||
var busy = await fx.Service.Trash.EmptyAsync(fx.Archive, parameters, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
|
||||
Assert.Equal((0, TrashKeptReason.Busy), (busy.Erased, busy.Refused.Single().Reason));
|
||||
Assert.Equal(Loc.L("Archive.Log.TrashKeptBusy", replaced.Title), busy.Refused.Single().LogLine);
|
||||
}
|
||||
Assert.Equal(replaced.Name, (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Single().Name);
|
||||
|
||||
var free = await fx.Service.Trash.EmptyAsync(fx.Archive, parameters, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
|
||||
Assert.Equal((1, 0), (free.Erased, free.Refused.Count));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AConversionAnotherHostDeletedIsReadFromTheArchiveNotThisIndex()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, _) = await UploadedAsync(fx, 3);
|
||||
var engine = await fx.StartEngineAsync();
|
||||
var replaced = await ReplaceAsync(fx, id, [0], 500);
|
||||
var frames = await fx.Db.Frames.ListBySequenceAsync(id);
|
||||
// A desktop sharing the archive deletes the converted photo; this index still holds it.
|
||||
var (other, _) = await fx.OtherWriterAsync();
|
||||
var deletion = await other.FrameTrash.DeleteAsync(await other.FrameTrash.PlanDeleteAsync(id, [frames[0].Id], null, fx.Ct), null, null, fx.Ct);
|
||||
Assert.Count(3, await fx.Db.Frames.ListBySequenceAsync(id));
|
||||
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.True(plan.Entries.Single(e => e.Name == replaced.Name).ConversionDeleted, "the sidecar's tombstone says so, whatever this index holds");
|
||||
Assert.False(plan.Entries.Single(e => e.Name == deletion.Entry.Name).ConversionDeleted);
|
||||
|
||||
// The deleted conversion is in the trash, damaged: the originals stay.
|
||||
var record = Assert.NotNull(FrameTrash.ReadEntry(fx.Paths, deletion.Entry.Name));
|
||||
var copy = Path.Combine(FrameTrash.Location(fx.Paths, deletion.Entry.Name).Folder, record.Photos[0].Files[0].Path);
|
||||
var bytes = Damage(copy);
|
||||
var kept = await EmptyAsync(fx, engine, await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, [replaced.Name], fx.Ct), JobState.Failed);
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKeptMismatch", replaced.Title), kept.LogTail);
|
||||
|
||||
// Whole again: the originals go, the deleted conversion stays where it is.
|
||||
File.WriteAllBytes(copy, bytes);
|
||||
await EmptyAsync(fx, engine, await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, [replaced.Name], fx.Ct), JobState.Done);
|
||||
Assert.Equal(deletion.Entry.Name, (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Single().Name);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task OriginalsStayWhenTheirConversionChangedSince()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, _) = await UploadedAsync(fx, 2);
|
||||
var engine = await fx.StartEngineAsync();
|
||||
var replaced = await ReplaceAsync(fx, id, [0], 500);
|
||||
var sequence = (await fx.Db.Sequences.GetAsync(id))!;
|
||||
var folder = fx.Paths.Sequence(sequence.Folder);
|
||||
var manifest = Sidecar(fx, sequence);
|
||||
// The converted file was edited in place and hashed again: its frame holds other content, and nothing says what
|
||||
// became of the conversion.
|
||||
var edited = Photos.Jpeg(manifest.Frames[0].CapturedAt, 700);
|
||||
File.WriteAllBytes(folder.File(manifest.Frames[0].File), edited);
|
||||
ManifestStore.WriteSequence(folder.ManifestPath, manifest with
|
||||
{
|
||||
Frames = [.. manifest.Frames.Select((f, i) => i == 0 ? f with { Sha256 = Sha(edited), Size = edited.Length } : f)],
|
||||
});
|
||||
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.False(plan.Entries.Single().ConversionDeleted, "the frame is there");
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKeptMismatch", replaced.Title), (await EmptyAsync(fx, engine, plan, JobState.Failed)).LogTail);
|
||||
Assert.Equal(replaced.Name, (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Single().Name);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnEmptyingReadsAnEntryOnlyOnceAnUndoWaitingForItIsSettled()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, photos) = await UploadedAsync(fx, 2);
|
||||
var engine = await fx.StartEngineAsync();
|
||||
var replaced = await ReplaceAsync(fx, id, [0], 500);
|
||||
var (entryFolder, recordPath) = FrameTrash.Location(fx.Paths, replaced.Name);
|
||||
var record = await File.ReadAllTextAsync(recordPath, fx.Ct);
|
||||
// Undone: the original is back in its frame, the conversion in an entry of its own.
|
||||
await fx.Service.RestoreEntryAsync(fx.Archive.Id, replaced.Name, null, fx.Ct);
|
||||
var undo = (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Single();
|
||||
// As a crash right after the undo's sidecar was written leaves it: the entry its original came from still lists it,
|
||||
// and the undo's completion waits for that entry's lease.
|
||||
Directory.CreateDirectory(entryFolder);
|
||||
await File.WriteAllTextAsync(recordPath, record, fx.Ct);
|
||||
var sequence = (await fx.Db.Sequences.GetAsync(id))!;
|
||||
var folder = fx.Paths.Sequence(sequence.Folder);
|
||||
var manifest = Sidecar(fx, sequence);
|
||||
var (undoFolder, undoRecord) = FrameTrash.Location(fx.Paths, undo.Name);
|
||||
new FrameReplaceJournal(manifest.Versions.Frames - 1, fx.Paths.ToRelative(undoRecord), fx.Paths.ToRelative(undoFolder), [], [], false,
|
||||
new ConsumedEntry(fx.Paths.ToRelative(recordPath), fx.Paths.ToRelative(entryFolder), SequenceTrash.EntryLease(replaced.Name), [manifest.Frames[0].Id],
|
||||
null))
|
||||
{
|
||||
Phase = ReplacePhase.Written,
|
||||
}.Write(folder);
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, [replaced.Name], fx.Ct);
|
||||
Assert.Equal(replaced.Name, plan.Entries.Single().Name);
|
||||
|
||||
// The emptying settles the undo under the entry's lease before it reads the entry again, which takes the photo out of
|
||||
// it: the entry is not erased as planned, and the sequence keeps its original.
|
||||
var kept = await EmptyAsync(fx, engine, plan, JobState.Failed);
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKept", replaced.Title), kept.LogTail);
|
||||
Assert.FileMissing(folder.ReplaceJournalPath);
|
||||
Assert.Equal(undo.Name, (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Single().Name);
|
||||
Assert.Equal(Sha(photos[0]), Sha(folder.File(Sidecar(fx, sequence).Frames[0].File)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnEntryNamesTheJobThatReplacedItsPhotos()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, photos) = await UploadedAsync(fx, 2);
|
||||
var job = Guid.CreateVersion7();
|
||||
var entry = await ReplaceAsync(fx, id, [0, 1], 500, job);
|
||||
Assert.Equal(job, entry.JobId);
|
||||
Assert.Equal(job, (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Single().JobId);
|
||||
|
||||
// Its undo swaps the originals back; the entry of the converted files it makes names no job.
|
||||
var undone = await fx.Service.RestoreEntryAsync(fx.Archive.Id, entry.Name, null, fx.Ct);
|
||||
Assert.False(undone.NewSequence);
|
||||
Assert.SequenceEqual(photos.Select(Sha), (await fx.Db.Frames.ListBySequenceAsync(id)).Select(f => f.Sha256!));
|
||||
var swapped = (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Single();
|
||||
Assert.Equal(TrashKind.Replaced, swapped.Kind);
|
||||
Assert.Null(swapped.JobId);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PositionsOnAConvertedFrameStayWhenAFrameWentBehindTheIndex()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, _) = await UploadedAsync(fx, 3);
|
||||
var frames = await fx.Db.Frames.ListBySequenceAsync(id);
|
||||
var project = new ProjectSettings { ExcludedFrames = [2], HeroFrame = 2 }.ToJson();
|
||||
await fx.Db.Sequences.EditAsync(id, new SequenceEdit { SettingsJson = project, HeroFrameId = frames[2].Id }, null, fx.Ct);
|
||||
// A desktop sharing the archive deletes the first photo; this index has not read the sidecar since.
|
||||
var (other, _) = await fx.OtherWriterAsync();
|
||||
await other.FrameTrash.DeleteAsync(await other.FrameTrash.PlanDeleteAsync(id, [frames[0].Id], null, fx.Ct), null, null, fx.Ct);
|
||||
|
||||
// This host converts the photo the positions are on (second in the sidecar now).
|
||||
await ReplaceAsync(fx, id, [1], 500);
|
||||
|
||||
var row = (await fx.Db.Sequences.GetAsync(id))!;
|
||||
var settings = ProjectSettings.FromJson(row.SettingsJson!);
|
||||
Assert.SequenceEqual([1], settings.ExcludedFrames);
|
||||
Assert.Equal((1, frames[2].Id), (settings.HeroFrame, row.HeroFrameId));
|
||||
Assert.True(settings.PendingFrames is not { Count: > 0 }, "nothing waits for the photo the conversion replaced");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheOriginalOfAConvertedPhotoFiledAgainIsADuplicateOfItsConversion()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, photos) = await UploadedAsync(fx, 2, "card-1");
|
||||
await ReplaceAsync(fx, id, [0], 500);
|
||||
var sequence = (await fx.Db.Sequences.GetAsync(id))!;
|
||||
var converted = Sidecar(fx, sequence).Frames[0];
|
||||
|
||||
// The card imported again on a PC without its history (same session, so the same sequence).
|
||||
var again = await CommitAsync(fx, null, "card-1", photos[0], "IMG_0001.JPG");
|
||||
Assert.Equal((id, converted.Id, true), (again.SequenceId, again.FrameId, again.Duplicate));
|
||||
var after = Sidecar(fx, sequence);
|
||||
Assert.Count(2, after.Frames);
|
||||
Assert.Contains(after.Removed!, r => r.Sha256 == Sha(photos[0]) && r.Replacement == converted.Sha256 && r.Reason == RemovalReason.Replaced);
|
||||
Assert.True(await fx.Db.FrameRemovals.IsRemovedAsync(id, Sha(photos[0]), fx.Ct), "the alias stays in the index");
|
||||
|
||||
// Converted once more: the original is a duplicate of the last conversion.
|
||||
await ReplaceAsync(fx, id, [0], 600);
|
||||
var last = Sidecar(fx, sequence).Frames[0];
|
||||
var third = await CommitAsync(fx, null, "card-1", photos[0], "IMG_0001.JPG");
|
||||
Assert.Equal((last.Id, true), (third.FrameId, third.Duplicate));
|
||||
Assert.Count(2, Sidecar(fx, sequence).Frames);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AFolderImportedAgainDoesNotFileTheOriginalsOfItsConvertedPhotos()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var source = Path.Combine(context.CreateTempDirectory("source"), "Etna eruption");
|
||||
Photos.WriteSeries(source, Start, 3);
|
||||
var whole = new ImportOptions { Grouping = ImportGrouping.WholeFolder };
|
||||
var plan = await fx.Service.PlanImportAsync(fx.Archive.Id, source, whole, null, null, fx.Ct);
|
||||
await fx.Service.Imports.ExecuteAsync(new ImportJobParameters(fx.Archive.Id, plan, null, true, null), Guid.NewGuid(), 0, _ => ValueTask.CompletedTask, null,
|
||||
fx.Ct);
|
||||
var id = (await fx.Db.Sequences.FindBySessionKeyAsync(fx.Archive.Id, plan.Sessions.Single().SessionKey))!.Value;
|
||||
await ReplaceAsync(fx, id, [0, 1, 2], 500);
|
||||
var sequence = (await fx.Db.Sequences.GetAsync(id))!;
|
||||
var aliases = Sidecar(fx, sequence).Removed!.Count;
|
||||
|
||||
// A new shot in the folder, imported with the rest of it.
|
||||
Photos.WriteSeries(source, Start.AddMinutes(5), 1, firstSeed: 40, prefix: "LATE_");
|
||||
var later = await fx.Service.PlanImportAsync(fx.Archive.Id, source, whole, null, null, fx.Ct);
|
||||
await fx.Service.Imports.ExecuteAsync(new ImportJobParameters(fx.Archive.Id, later, null, true, null), Guid.NewGuid(), 0, _ => ValueTask.CompletedTask, null,
|
||||
fx.Ct);
|
||||
|
||||
var after = Sidecar(fx, sequence);
|
||||
Assert.Count(4, after.Frames);
|
||||
Assert.Equal(aliases, after.Removed!.Count);
|
||||
Assert.Equal(3, after.Frames.Count(f => f.Source is not null));
|
||||
}
|
||||
}
|
||||
@@ -252,6 +252,51 @@ public sealed class JobEngineTests(TestContext context)
|
||||
Assert.Empty((await db.Jobs.ListAsync(null, JobKind.LightAnalyse)).Items);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A follow-up of work already done (a sync download's frame swapped to another encoding, whose thumbnails it cleared) is
|
||||
/// queued whatever stops its job meanwhile: a cancel arriving right after the work leaves it queued, and it runs.
|
||||
/// </summary>
|
||||
[Test]
|
||||
public async Task AFollowUpQueuedWhateverStopsItsJobOutlivesACancel()
|
||||
{
|
||||
await using var db = await OpenAsync();
|
||||
var target = Guid.CreateVersion7();
|
||||
var started = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var queued = new TaskCompletionSource<Job?>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var download = new CallbackHandler(JobKind.Scan, async c =>
|
||||
{
|
||||
started.TrySetResult();
|
||||
try
|
||||
{
|
||||
await Task.Delay(Timeout.InfiniteTimeSpan, c.Cancellation);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// The work is done; the cancel came before its follow-up was queued.
|
||||
}
|
||||
try
|
||||
{
|
||||
queued.TrySetResult(await c.EnqueueFollowUpAsync(JobKind.Thumbs, [target], "Roof", null, JobPriority.Background, CancellationToken.None));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
queued.TrySetException(ex);
|
||||
}
|
||||
c.Cancellation.ThrowIfCancellationRequested();
|
||||
});
|
||||
await using var engine = new JobEngine(db, [download, new CountingHandler(JobKind.Thumbs, 1)],
|
||||
new JobEngineOptions { Workers = 1, WorkerId = "t", PollInterval = TimeSpan.FromMilliseconds(50) });
|
||||
await engine.StartAsync();
|
||||
var job = await engine.EnqueueAsync(JobKind.Scan, []);
|
||||
await started.Task.WaitAsync(TimeSpan.FromSeconds(10));
|
||||
|
||||
await engine.CancelAsync(job.Id);
|
||||
|
||||
var followUp = Assert.NotNull(await queued.Task.WaitAsync(TimeSpan.FromSeconds(10)), "queued although its job was cancelled");
|
||||
await WaitForStateAsync(db, job.Id, JobState.Cancelled, TimeSpan.FromSeconds(5));
|
||||
Assert.SequenceEqual([target], (await WaitForStateAsync(db, followUp.Id, JobState.Done, TimeSpan.FromSeconds(10))).TargetIds);
|
||||
}
|
||||
|
||||
/// <summary>Waits until <paramref name="condition"/> holds, failing with <paramref name="what"/> after ten seconds.</summary>
|
||||
private static async Task UntilAsync(Func<bool> condition, string what)
|
||||
{
|
||||
|
||||
@@ -103,6 +103,56 @@ public abstract class JobRepositoryTests(TestContext context) : RepositoryTest(c
|
||||
return job;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task HoldsNameTheTargetsOfRunningAndPausedJobsAndOnlyAVerifysParameters()
|
||||
{
|
||||
// What a dry run counts as busy, read at every one and at every sequence a conversion comes to: never the parameters
|
||||
// a conversion or an import keeps, nor the row of the job asking.
|
||||
var db = await OpenChronosAsync();
|
||||
Guid a = Guid.CreateVersion7(), b = Guid.CreateVersion7(), archive = Guid.CreateVersion7();
|
||||
var convert = await EnqueueAsync(db, TestData.Job(JobKind.Convert) with { TargetIds = [a, b], ParametersJson = "{\"fingerprint\": \"f\"}", State = JobState.Running });
|
||||
var verify = await EnqueueAsync(db, TestData.Job(JobKind.Verify) with { TargetIds = [archive], ParametersJson = "{\"sequenceIds\": []}", State = JobState.Paused });
|
||||
await EnqueueAsync(db, TestData.Job(JobKind.Thumbs) with { TargetIds = [a] });
|
||||
await EnqueueAsync(db, TestData.Job(JobKind.Scan) with { TargetIds = [archive], State = JobState.Done, Finished = TestData.Now });
|
||||
|
||||
var holds = await db.Jobs.ListHoldsAsync(cancellationToken: Ct);
|
||||
Assert.SequenceEqual(new[] { convert.Id, verify.Id }.Order(), holds.Select(h => h.Id).Order());
|
||||
var running = holds.Single(h => h.Id == convert.Id);
|
||||
Assert.Equal((JobKind.Convert, (string?)null), (running.Kind, running.ParametersJson));
|
||||
Assert.SequenceEqual([a, b], running.TargetIds);
|
||||
var paused = holds.Single(h => h.Id == verify.Id);
|
||||
Assert.Equal((JobKind.Verify, verify.ParametersJson), (paused.Kind, paused.ParametersJson));
|
||||
Assert.SequenceEqual([verify.Id], (await db.Jobs.ListHoldsAsync(convert.Id, Ct)).Select(h => h.Id));
|
||||
|
||||
Assert.Equal(convert.Created, await db.Jobs.GetCreatedAsync(convert.Id, Ct));
|
||||
Assert.Null(await db.Jobs.GetCreatedAsync(Guid.CreateVersion7(), Ct));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task BriefListsReadEveryFieldButTheParameters()
|
||||
{
|
||||
var db = await OpenChronosAsync();
|
||||
var job = TestData.Job(JobKind.Convert, JobPriority.User) with
|
||||
{
|
||||
TargetIds = [Guid.CreateVersion7(), Guid.CreateVersion7()],
|
||||
TargetLabel = "Harbour sunset",
|
||||
ParametersJson = "{\"fingerprint\": \"f\"}",
|
||||
LogTail = "line 1",
|
||||
CreatedBy = Guid.CreateVersion7(),
|
||||
ScheduleId = Guid.CreateVersion7(),
|
||||
};
|
||||
await db.Jobs.EnqueueAsync(job, Ct);
|
||||
|
||||
var brief = Assert.NotNull((await db.Jobs.ListBriefAsync(JobState.Queued, JobKind.Convert, 10, null, Ct)).Items.SingleOrDefault());
|
||||
Assert.SequenceEqual(job.TargetIds, brief.TargetIds);
|
||||
Assert.Equal(job with { TargetIds = brief.TargetIds, ParametersJson = null }, brief);
|
||||
Assert.Equal(job.ParametersJson, (await db.Jobs.ListAsync(JobState.Queued, JobKind.Convert, 10, null, Ct)).Items.Single().ParametersJson);
|
||||
Assert.Empty((await db.Jobs.ListBriefAsync(JobState.Running, null, 10, null, Ct)).Items);
|
||||
var next = Assert.NotNull((await db.Jobs.ListQueuedBriefAsync([JobKind.Convert], 10, Chronos.Data.Jobs.JobPriorityQueue.Off, Ct)).SingleOrDefault());
|
||||
Assert.Equal((job.Id, (string?)null), (next.Id, next.ParametersJson));
|
||||
Assert.Empty(await db.Jobs.ListQueuedBriefAsync([], 10, Chronos.Data.Jobs.JobPriorityQueue.Off, Ct));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ThePriorityQueueClaimsTheHigherKindsFirst()
|
||||
{
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using Chronos.Core.Imaging;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data.Repositories;
|
||||
using Chronos.Data.Tests.Infrastructure;
|
||||
@@ -6,8 +7,9 @@ using Chronos.Testing;
|
||||
namespace Chronos.Data.Tests.Repositories;
|
||||
|
||||
/// <summary>
|
||||
/// The stamp of what a Reorganize plans from in an archive's index (<see cref="SequenceRepository.GetStampAsync"/>) on both
|
||||
/// dialects: the server makes a kept Reorganize plan again once it is another one, and only then (owner review 2, R7).
|
||||
/// The stamps of what a Reorganize and a conversion plan from in an archive's index (<see cref="SequenceRepository.GetStampAsync"/>,
|
||||
/// <see cref="SequenceRepository.GetConvertStampAsync"/>) on both dialects: the server makes a kept plan again once its
|
||||
/// stamp is another one, and only then (owner review 2, R7).
|
||||
/// </summary>
|
||||
public abstract class SequenceStampTests(TestContext context) : RepositoryTest(context)
|
||||
{
|
||||
@@ -77,6 +79,53 @@ public abstract class SequenceStampTests(TestContext context) : RepositoryTest(c
|
||||
Assert.False(verified == all || verified == uploaded, "a photo hashed");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheConvertStampFollowsThePhotosAConversionPlansFrom()
|
||||
{
|
||||
var db = await OpenChronosAsync();
|
||||
var archive = TestData.Archive();
|
||||
await db.Archives.UpsertAsync(archive, Ct);
|
||||
var roof = TestData.Sequence(archive.Id, new DateTime(2026, 9, 28, 18, 0, 0), "Roof");
|
||||
var ridge = TestData.Sequence(archive.Id, new DateTime(2026, 9, 29, 7, 0, 0), "Ridge");
|
||||
foreach (var sequence in new[] { roof, ridge }) await db.Sequences.UpsertAsync(sequence, Ct);
|
||||
var photo = await PhotoAsync(db, roof, 0);
|
||||
var elsewhere = await PhotoAsync(db, ridge, 0);
|
||||
IReadOnlyList<Guid> planned = [roof.Id];
|
||||
var stamp = await db.Sequences.GetConvertStampAsync(archive.Id, planned, Ct);
|
||||
Assert.Equal(stamp, await db.Sequences.GetConvertStampAsync(archive.Id, planned, Ct));
|
||||
Assert.Equal(stamp, await db.Sequences.GetConvertStampAsync(archive.Id, [roof.Id, roof.Id], Ct));
|
||||
Assert.NotEqual(stamp, await db.Sequences.GetConvertStampAsync(archive.Id, null, Ct));
|
||||
|
||||
// What a conversion does not read: the photos of a sequence not planned, light statistics, tags, a verify time.
|
||||
Assert.Equal(1, await db.Frames.UpdateFlagsAsync([elsewhere.Id], FrameFlags.Missing, FrameFlags.None, Ct));
|
||||
Assert.Equal(1, await db.Frames.UpdateLightStatsAsync([new FrameLightStats(photo.Id, -2.5, 0.01, 0.02)], Ct));
|
||||
Assert.True(await db.Sequences.SetTagsAsync(roof.Id, ["sky"], Ct));
|
||||
Assert.True(await db.Sequences.RecordVerifiedAsync(roof.Id, SequenceStatus.Verified, SequenceStatus.None, TestData.Now, Ct));
|
||||
Assert.Equal(stamp, await db.Sequences.GetConvertStampAsync(archive.Id, planned, Ct));
|
||||
|
||||
// What it reads, without another frames version: each flag that keeps a photo as it is (a scan's, a verify's, a
|
||||
// decode's), the format a decode records, the hash a verify records. Cleared again, the stamp is the one before.
|
||||
var seen = new HashSet<string>(StringComparer.Ordinal) { stamp };
|
||||
foreach (var flag in new[] { FrameFlags.Missing, FrameFlags.Corrupt, FrameFlags.PreviewOnly, FrameFlags.Undecodable })
|
||||
{
|
||||
Assert.Equal(1, await db.Frames.UpdateFlagsAsync([photo.Id], flag, FrameFlags.None, Ct));
|
||||
Assert.True(seen.Add(await db.Sequences.GetConvertStampAsync(archive.Id, planned, Ct)), flag.ToString());
|
||||
Assert.Equal(1, await db.Frames.UpdateFlagsAsync([photo.Id], FrameFlags.None, flag, Ct));
|
||||
Assert.Equal(stamp, await db.Sequences.GetConvertStampAsync(archive.Id, planned, Ct));
|
||||
}
|
||||
Assert.Equal(1, await db.Frames.UpdateTiersAsync([new FrameTierFields(photo.Id, FrameFlags.None, 0, 0, ImageFileFormat.Dng)], Ct));
|
||||
Assert.True(seen.Add(await db.Sequences.GetConvertStampAsync(archive.Id, planned, Ct)), "format");
|
||||
var unhashed = await PhotoAsync(db, roof, 1, hashed: false);
|
||||
Assert.True(seen.Add(await db.Sequences.GetConvertStampAsync(archive.Id, planned, Ct)), "a photo added");
|
||||
await VerifyAsync(db, unhashed);
|
||||
Assert.True(seen.Add(await db.Sequences.GetConvertStampAsync(archive.Id, planned, Ct)), "hashed by a verify");
|
||||
|
||||
// With every sequence planned, a flag on any photo changes it.
|
||||
var all = await db.Sequences.GetConvertStampAsync(archive.Id, null, Ct);
|
||||
Assert.Equal(1, await db.Frames.UpdateFlagsAsync([elsewhere.Id], FrameFlags.None, FrameFlags.Missing, Ct));
|
||||
Assert.NotEqual(all, await db.Sequences.GetConvertStampAsync(archive.Id, null, Ct));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A photo indexed into a sequence, as an upload does, or without its hash, as a scan does: the frame, then the row's
|
||||
/// counts and frames version.
|
||||
|
||||
@@ -86,6 +86,28 @@ internal sealed class SyncFixture : IAsyncDisposable
|
||||
null, null), Ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replaces the file of the local frame at <paramref name="index"/> with <paramref name="converted"/>, as a conversion's
|
||||
/// Replace does on this PC (owner review 2, R7): the same frame, a tombstone naming the new content.
|
||||
/// </summary>
|
||||
public async Task ReplaceLocalAsync(Guid sequenceId, int index, byte[] converted)
|
||||
{
|
||||
var sequence = (await Db.Sequences.GetAsync(sequenceId, Ct))!;
|
||||
var frame = Chronos.Core.Archiving.ManifestStore.ReadSequence(Archive.Paths.Sequence(sequence.Folder).ManifestPath).Frames[index];
|
||||
var staging = Path.Combine(Archive.Paths.StagingDirectory, "convert-test");
|
||||
Directory.CreateDirectory(staging);
|
||||
var path = Path.Combine(staging, $"{Guid.NewGuid():N}.jpeg");
|
||||
await File.WriteAllBytesAsync(path, converted, Ct);
|
||||
var sha = Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(converted));
|
||||
var swap = new FrameSwap(frame.Id, Archive.Paths.ToRelative(path), [], (current, _) => Path.ChangeExtension(current.File, ".jpeg"), current => current with
|
||||
{
|
||||
Sha256 = sha,
|
||||
Size = converted.Length,
|
||||
Source = new Chronos.Core.Archiving.FrameSource { Sha256 = current.Sha256, Format = current.Format, File = current.File, Size = current.Size },
|
||||
});
|
||||
Assert.NotNull(await Archive.Service.FrameTrash.ReplaceAsync(Archive.Archive, sequenceId, null, [swap], null, null, null, null, Ct));
|
||||
}
|
||||
|
||||
/// <summary>An edit on this PC, as the editor makes it (project file and index, settings version + 1).</summary>
|
||||
public async Task<Sequence> EditLocalSettingsAsync(Guid id, string settingsJson)
|
||||
{
|
||||
|
||||
@@ -84,7 +84,8 @@ public sealed class SyncRulesTests
|
||||
|
||||
// A photo replaced by a converted file on either side is the same photo as its replacement, and so is a chain of them.
|
||||
var replaced = new SyncTombstones([], [new RemovedPhotoDto("aa", "a2", "replaced", at), new RemovedPhotoDto("a2", "a3", "replaced", at)]);
|
||||
Assert.Equal("a3", replaced.Canonical("AA"));
|
||||
Assert.Equal(replaced.Canonical("a3"), replaced.Canonical("AA"));
|
||||
Assert.Equal(replaced.Canonical("a3"), replaced.Canonical("a2"));
|
||||
Assert.Equal((0, 0), SyncRules.CompareOriginals([Photo("aa"), Photo("bb")], [Photo("a3"), Photo("bb")], replaced));
|
||||
Assert.False(replaced.DeletedRemotely("aa"), "a replaced photo is there as its replacement");
|
||||
var replacedHere = new SyncTombstones([new FrameRemoval(localId, "bb", "b2", RemovalReason.Replaced, at)], null);
|
||||
@@ -97,6 +98,43 @@ public sealed class SyncRulesTests
|
||||
new FrameRemoval(localId, "bb", "b2", RemovalReason.Replaced, at)]).Select(r => r.Reason));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void APhotoEachSideConvertedOnItsOwnIsStillOnePhoto()
|
||||
{
|
||||
static FramePhoto Photo(string sha) => new(sha, 100, null);
|
||||
var at = new DateTimeOffset(2026, 10, 27, 10, 0, 0, TimeSpan.Zero);
|
||||
var localId = Guid.CreateVersion7();
|
||||
// This PC converted "oo" to "aa", the server converted its copy to "bb" (another decoder, other bytes).
|
||||
var both = new SyncTombstones([new FrameRemoval(localId, "OO", "aa", RemovalReason.Replaced, at)], [new RemovedPhotoDto("oo", "BB", "replaced", at)]);
|
||||
Assert.Equal(both.Canonical("oo"), both.Canonical("aa"));
|
||||
Assert.Equal(both.Canonical("oo"), both.Canonical("bb"));
|
||||
Assert.Equal((0, 0), SyncRules.CompareOriginals([Photo("aa"), Photo("cc")], [Photo("bb"), Photo("cc")], both));
|
||||
// The same whichever side's links come first, and with an undo (a link back) in the chain.
|
||||
var reversed = new SyncTombstones([new FrameRemoval(localId, "bb", "oo", RemovalReason.Replaced, at)], [new RemovedPhotoDto("aa", "oo", "replaced", at)]);
|
||||
Assert.Equal(both.Canonical("aa"), reversed.Canonical("bb"));
|
||||
Assert.Equal("cc", both.Canonical("CC"));
|
||||
// A deletion covers every encoding of the photo: the server deleted its conversion, so this PC's is not sent.
|
||||
var deleted = new SyncTombstones([new FrameRemoval(localId, "oo", "aa", RemovalReason.Replaced, at)],
|
||||
[new RemovedPhotoDto("oo", "bb", "replaced", at), new RemovedPhotoDto("bb", null, "deleted", at)]);
|
||||
Assert.True(deleted.DeletedRemotely("aa"));
|
||||
Assert.False(deleted.DeletedLocally("bb"));
|
||||
Assert.False(both.DeletedRemotely("aa"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AProxyIsMadeFromThePhotoThisPcConverted()
|
||||
{
|
||||
var heif = new ImageDecoder([new NativeHeif()]);
|
||||
var at = new DateTimeOffset(2026, 10, 27, 10, 0, 0, TimeSpan.Zero);
|
||||
// The server holds the HEIC original "oo" and cannot decode it; this PC converted its copy to a JPEG ("dd").
|
||||
List<Frame> local = [Original("dd", 100, ImageFileFormat.Jpeg)];
|
||||
var manifest = new SequenceManifest(Guid.CreateVersion7(), 1, 1, null, [], [Wanted("oo", 500, "IMG_0001.HEIC")], [], new ProxySpec(1920, 90));
|
||||
var tombstones = new SyncTombstones([new FrameRemoval(Local.Id, "oo", "dd", RemovalReason.Replaced, at)], null);
|
||||
|
||||
Assert.Equal(1, SyncRules.ProxiesToSend(manifest, local, heif, null, tombstones));
|
||||
Assert.Equal(0, SyncRules.ProxiesToSend(manifest, local, heif));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ATombstoneBesideAPhotoItsSideStillHoldsHidesNothing()
|
||||
{
|
||||
|
||||
@@ -5,6 +5,7 @@ using Chronos.Core.Localization;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Core.Project;
|
||||
using Chronos.Core.Render;
|
||||
using Chronos.Data.Archiving;
|
||||
using Chronos.Data.Sync;
|
||||
using Chronos.Data.Tests.Archiving;
|
||||
using Chronos.Protocol;
|
||||
@@ -180,7 +181,7 @@ public sealed class SyncTransferTests(TestContext context)
|
||||
var kept = await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct);
|
||||
Assert.True(kept.Select(k => k.Sha256).ToHashSet().SetEquals(photos[1..].Select(Sha)), "the photo deleted here stays deleted");
|
||||
Assert.True(await f.Db.FrameRemovals.IsRemovedAsync(local.Id, Sha(photos[0])), "and keeps its tombstone");
|
||||
Assert.Contains("deleted on this PC, not downloaded", done.LogTail);
|
||||
Assert.Contains(Loc.L("Archive.Log.SyncDeletedHere", remote.Frames.Single(fr => fr.Sha256 == Sha(photos[0])).FileName), done.LogTail);
|
||||
Assert.False(Directory.Exists(Path.Combine(f.Archive.Paths.StagingDirectory, "sync-" + remote.Id.ToString("N"))), "staging cleaned up");
|
||||
}
|
||||
|
||||
@@ -207,7 +208,7 @@ public sealed class SyncTransferTests(TestContext context)
|
||||
var done = await f.Archive.WaitForAsync(job.Id, JobState.Done);
|
||||
|
||||
Assert.Equal(3, remote.Frames.Count);
|
||||
Assert.Contains("deleted on the server, not uploaded", done.LogTail);
|
||||
Assert.Contains(Loc.L("Archive.Log.SyncDeletedThere", "DSC_0004.JPG"), done.LogTail);
|
||||
// Its hash is in the index now: the comparison leaves it out, so the row settles instead of offering Upload for good.
|
||||
Assert.True((await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct)).All(fr => fr.Sha256 is not null), "hashed by the verify");
|
||||
var entry = await f.EntryAsync(local.DisplayTitle);
|
||||
@@ -793,6 +794,228 @@ public sealed class SyncTransferTests(TestContext context)
|
||||
Assert.Empty(f.Remote.Sequences);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task APhotoEachSideConvertedIsNeitherSentNorFetchedAgain()
|
||||
{
|
||||
await using var f = await SyncFixture.CreateAsync(context);
|
||||
var photos = f.NewPhotos(2);
|
||||
var local = await f.LocalAsync("Comet", photos);
|
||||
// Each side converted its own copy of the first photo, to other bytes (another decoder, another quality).
|
||||
var (mine, theirs) = (f.NewPhotos(1)[0], f.NewPhotos(1)[0]);
|
||||
await f.ReplaceLocalAsync(local.Id, 0, mine);
|
||||
var remote = f.Remote.Add("Comet", [theirs, photos[1]]);
|
||||
remote.Removed.Add(new RemovedPhotoDto(Sha(photos[0]), Sha(theirs), "replaced", DateTimeOffset.UtcNow));
|
||||
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
|
||||
await f.RefreshAsync();
|
||||
var entry = await f.EntryAsync("Comet");
|
||||
Assert.Equal((SyncStatus.InSync, 0, 0), (entry.Status, entry.Link!.MissingRemote, entry.Link.MissingLocal));
|
||||
var engine = await f.StartEngineAsync();
|
||||
|
||||
await f.Archive.WaitForAsync((await f.Sync.EnqueueUploadAsync(engine, f.Server, local)).Id, JobState.Done);
|
||||
await f.Archive.WaitForAsync((await f.Sync.EnqueueDownloadAsync(engine, f.Server, remote.Id, "Comet", SyncDownloadMode.Originals, null)).Id,
|
||||
JobState.Done);
|
||||
Assert.SequenceEqual([Sha(theirs), Sha(photos[1])], remote.Frames.Select(r => r.Sha256));
|
||||
Assert.SequenceEqual([Sha(mine), Sha(photos[1])], (await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct)).Select(r => r.Sha256!));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task CopiesConvertedOnOneSideBeforeTheyWereLinkedAreLinkedByContent()
|
||||
{
|
||||
await using var f = await SyncFixture.CreateAsync(context);
|
||||
// The Uploader sent the card to the server, which converted it before this PC compared the two.
|
||||
var photos = f.NewPhotos(3);
|
||||
var local = await f.LocalAsync("Comet", photos);
|
||||
var converted = f.NewPhotos(3);
|
||||
var remote = f.Remote.Add("2026-09-28 Canon", converted);
|
||||
foreach (var (original, replacement) in photos.Zip(converted))
|
||||
remote.Removed.Add(new RemovedPhotoDto(Sha(original), Sha(replacement), "replaced", DateTimeOffset.UtcNow));
|
||||
remote.FirstCapture = local.FirstCapture;
|
||||
remote.CameraModel = local.CameraModel;
|
||||
// And the other way round: this PC converted its copy of another card the server holds as it came.
|
||||
var cards = f.NewPhotos(2);
|
||||
var mine = await f.LocalAsync("Harbour", cards);
|
||||
foreach (var (index, replacement) in f.NewPhotos(2).Index()) await f.ReplaceLocalAsync(mine.Id, index, replacement);
|
||||
mine = (await f.Db.Sequences.GetAsync(mine.Id, f.Ct))!;
|
||||
var theirs = f.Remote.Add("2026-09-28 Canon (2)", cards);
|
||||
theirs.FirstCapture = mine.FirstCapture;
|
||||
theirs.CameraModel = mine.CameraModel;
|
||||
|
||||
await f.RefreshAsync();
|
||||
|
||||
var comet = await f.EntryAsync("Comet");
|
||||
Assert.Equal(remote.Id, comet.Link?.RemoteId);
|
||||
Assert.Equal((0, 0), (comet.Link!.MissingRemote, comet.Link.MissingLocal));
|
||||
var harbour = await f.EntryAsync("Harbour");
|
||||
Assert.Equal(theirs.Id, harbour.Link?.RemoteId);
|
||||
Assert.Equal((0, 0), (harbour.Link!.MissingRemote, harbour.Link.MissingLocal));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AFrameMissingHereTakesBackItsPhotoAsTheServerConvertedIt()
|
||||
{
|
||||
await using var f = await SyncFixture.CreateAsync(context);
|
||||
var photos = f.NewPhotos(3);
|
||||
var local = await f.LocalAsync("Harbour", photos);
|
||||
var lost = (await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct))[1];
|
||||
File.Delete(f.Archive.Paths.FromRelative(lost.RelativePath));
|
||||
await f.Archive.Service.Verifier.VerifyAsync(f.Archive.Archive, [local.Id], default, (_, _, _) => ValueTask.CompletedTask, cancellationToken: f.Ct);
|
||||
// The server converted that photo.
|
||||
var converted = f.NewPhotos(1)[0];
|
||||
var remote = f.Remote.Add("Harbour", [photos[0], converted, photos[2]]);
|
||||
remote.Removed.Add(new RemovedPhotoDto(Sha(photos[1]), Sha(converted), "replaced", DateTimeOffset.UtcNow));
|
||||
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
|
||||
await f.RefreshAsync();
|
||||
var behind = await f.EntryAsync("Harbour");
|
||||
Assert.Equal((SyncStatus.ServerAhead, 1), (behind.Status, behind.Link!.MissingLocal));
|
||||
var engine = await f.StartEngineAsync();
|
||||
|
||||
var job = await f.Sync.EnqueueDownloadAsync(engine, f.Server, remote.Id, "Harbour", SyncDownloadMode.Originals, null);
|
||||
await f.Archive.WaitForAsync(job.Id, JobState.Done);
|
||||
|
||||
// The same frame, holding the server's conversion, and no frame beside it.
|
||||
var frames = await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct);
|
||||
Assert.Equal(3, frames.Count);
|
||||
Assert.Equal((lost.Id, Sha(converted)), (frames[1].Id, frames[1].Sha256));
|
||||
Assert.True(frames.All(fr => (fr.Flags & FrameFlags.Missing) == 0), "the frame is no longer missing");
|
||||
Assert.Equal(Sha(converted), Sha(await File.ReadAllBytesAsync(f.Archive.Paths.FromRelative(frames[1].RelativePath), f.Ct)));
|
||||
Assert.True(await f.Db.FrameRemovals.IsRemovedAsync(local.Id, Sha(photos[1]), f.Ct), "the original is an alias of its conversion here too");
|
||||
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Harbour")).Status);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AFrameThatTookBackItsPhotoInAnotherEncodingIsDrawnAgain()
|
||||
{
|
||||
await using var f = await SyncFixture.CreateAsync(context);
|
||||
var photos = f.NewPhotos(3);
|
||||
var local = await f.LocalAsync("Harbour", photos);
|
||||
var engine = await f.StartEngineAsync();
|
||||
var drawn = await engine.EnqueueAsync(ArchiveJobs.Thumbnails([local.Id], "Harbour"), cancellationToken: f.Ct);
|
||||
await f.Archive.WaitForAsync(drawn.Id, JobState.Done);
|
||||
var lost = (await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct))[1];
|
||||
File.Delete(f.Archive.Paths.FromRelative(lost.RelativePath));
|
||||
await f.Archive.Service.Verifier.VerifyAsync(f.Archive.Archive, [local.Id], default, (_, _, _) => ValueTask.CompletedTask, cancellationToken: f.Ct);
|
||||
Assert.True(((await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.Status & SequenceStatus.ThumbsBuilt) != 0, "drawn before");
|
||||
// The server converted that photo.
|
||||
var converted = f.NewPhotos(1)[0];
|
||||
var remote = f.Remote.Add("Harbour", [photos[0], converted, photos[2]]);
|
||||
remote.Removed.Add(new RemovedPhotoDto(Sha(photos[1]), Sha(converted), "replaced", DateTimeOffset.UtcNow));
|
||||
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
|
||||
await f.RefreshAsync();
|
||||
|
||||
var job = await f.Sync.EnqueueDownloadAsync(engine, f.Server, remote.Id, "Harbour", SyncDownloadMode.Originals, null);
|
||||
await f.Archive.WaitForAsync(job.Id, JobState.Done);
|
||||
|
||||
// Nothing was filed, only a frame swapped, which cleared what covered the sequence: the thumbnails it had are drawn again.
|
||||
Assert.Equal(lost.Id, (await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct))[1].Id);
|
||||
var again = Assert.Contains((await f.Db.Jobs.ListAsync(null, JobKind.Thumbs, 50, null, f.Ct)).Items, j => j.Id != drawn.Id && j.TargetIds.Contains(local.Id));
|
||||
await f.Archive.WaitForAsync(again.Id, JobState.Done);
|
||||
Assert.True(((await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.Status & SequenceStatus.ThumbsBuilt) != 0, "drawn again");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AFrameTakesBackItsPhotoInAnotherEncodingOnlyWhereTheSequenceIsNow()
|
||||
{
|
||||
// The folder the sequence left holds another sequence now, or nothing.
|
||||
foreach (var taken in new[] { true, false })
|
||||
{
|
||||
await using var f = await SyncFixture.CreateAsync(context);
|
||||
var photos = f.NewPhotos(3);
|
||||
var local = await f.LocalAsync("Harbour", photos);
|
||||
var stranger = taken ? await f.LocalAsync("Stranger", f.NewPhotos(2)) : null;
|
||||
var lost = (await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct))[1];
|
||||
File.Delete(f.Archive.Paths.FromRelative(lost.RelativePath));
|
||||
await f.Archive.Service.Verifier.VerifyAsync(f.Archive.Archive, [local.Id], default, (_, _, _) => ValueTask.CompletedTask, cancellationToken: f.Ct);
|
||||
// The server converted that photo.
|
||||
var converted = f.NewPhotos(1)[0];
|
||||
var remote = f.Remote.Add("Harbour", [photos[0], converted, photos[2]]);
|
||||
remote.Removed.Add(new RemovedPhotoDto(Sha(photos[1]), Sha(converted), "replaced", DateTimeOffset.UtcNow));
|
||||
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
|
||||
await f.RefreshAsync();
|
||||
var engine = await f.StartEngineAsync();
|
||||
|
||||
// Another index moves the folder away, then the other sequence into the folder it left; this index has scanned neither.
|
||||
var folder = f.Archive.Paths.Sequence(local.Folder).Folder;
|
||||
var (other, archive) = await f.Archive.OtherWriterAsync();
|
||||
var moves = stranger is null ? [(local.Id, "moved/{title}")] : new[] { (local.Id, "moved/{title}"), (stranger.Id, "parked/{title}"), (stranger.Id, local.Folder) };
|
||||
foreach (var (id, template) in moves)
|
||||
await other.ApplyRenameAsync(await other.PlanRenameAsync(archive.Id, [id], template, null, f.Ct), null, f.Ct);
|
||||
var before = taken ? ArchiveFixture.FilesOf(folder) : null;
|
||||
|
||||
var job = await f.Sync.EnqueueDownloadAsync(engine, f.Server, remote.Id, "Harbour", SyncDownloadMode.Originals, null);
|
||||
var failed = await f.Archive.WaitForAsync(job.Id, JobState.Failed);
|
||||
Assert.Equal(Loc.L("Errors.Archive.FolderMoved"), failed.Error, taken.ToString());
|
||||
if (taken) Assert.SequenceEqual(before!, ArchiveFixture.FilesOf(folder), "the other sequence's files are untouched");
|
||||
else Assert.False(Directory.Exists(folder), "the folder it left is not created again");
|
||||
Assert.Equal(lost.Sha256, (await f.Db.Frames.GetAsync(lost.Id, f.Ct))!.Sha256);
|
||||
|
||||
// Once this index's scan found it there, the converted photo takes the frame's place in the folder the sequence is in now.
|
||||
await f.Archive.ScanAsync();
|
||||
job = await f.Sync.EnqueueDownloadAsync(engine, f.Server, remote.Id, "Harbour", SyncDownloadMode.Originals, null);
|
||||
await f.Archive.WaitForAsync(job.Id, JobState.Done);
|
||||
var frames = await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct);
|
||||
Assert.Equal(3, frames.Count);
|
||||
Assert.Equal((lost.Id, Sha(converted)), (frames[1].Id, frames[1].Sha256));
|
||||
Assert.True(frames[1].RelativePath.StartsWith("moved/Harbour/", StringComparison.Ordinal), frames[1].RelativePath);
|
||||
Assert.Equal(Sha(converted), Sha(await File.ReadAllBytesAsync(f.Archive.Paths.FromRelative(frames[1].RelativePath), f.Ct)));
|
||||
Assert.True(frames.All(fr => (fr.Flags & FrameFlags.Missing) == 0), "nothing missing");
|
||||
if (taken) Assert.SequenceEqual(before!, ArchiveFixture.FilesOf(folder), "the other sequence's files are still untouched");
|
||||
else Assert.False(Directory.Exists(folder));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AConversionMissingHereTakesBackTheOriginalTheServerHolds()
|
||||
{
|
||||
await using var f = await SyncFixture.CreateAsync(context);
|
||||
var photos = f.NewPhotos(3);
|
||||
var local = await f.LocalAsync("Harbour", photos);
|
||||
var remote = f.Remote.Add("Harbour", photos);
|
||||
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
|
||||
// This PC converted the second photo, and that file is gone since.
|
||||
var converted = f.NewPhotos(1)[0];
|
||||
await f.ReplaceLocalAsync(local.Id, 1, converted);
|
||||
var lost = (await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct))[1];
|
||||
File.Delete(f.Archive.Paths.FromRelative(lost.RelativePath));
|
||||
await f.Archive.Service.Verifier.VerifyAsync(f.Archive.Archive, [local.Id], default, (_, _, _) => ValueTask.CompletedTask, cancellationToken: f.Ct);
|
||||
await f.RefreshAsync();
|
||||
Assert.Equal(1, (await f.EntryAsync("Harbour")).Link!.MissingLocal);
|
||||
var engine = await f.StartEngineAsync();
|
||||
|
||||
var job = await f.Sync.EnqueueDownloadAsync(engine, f.Server, remote.Id, "Harbour", SyncDownloadMode.Originals, null);
|
||||
await f.Archive.WaitForAsync(job.Id, JobState.Done);
|
||||
|
||||
var frames = await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct);
|
||||
Assert.Equal(3, frames.Count);
|
||||
Assert.Equal((lost.Id, Sha(photos[1])), (frames[1].Id, frames[1].Sha256));
|
||||
Assert.True(await f.Db.FrameRemovals.IsRemovedAsync(local.Id, Sha(converted), f.Ct), "the conversion is an alias of the original");
|
||||
Assert.False(await f.Db.FrameRemovals.IsRemovedAsync(local.Id, Sha(photos[1]), f.Ct));
|
||||
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Harbour")).Status);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnUploadSendsTheProxyOfAPhotoThisPcConverted()
|
||||
{
|
||||
await using var f = await SyncFixture.CreateAsync(context);
|
||||
var photos = f.NewPhotos(3);
|
||||
var local = await f.LocalAsync("Holy grail", photos);
|
||||
// The server holds the originals and cannot decode them (HEIF, BRIEF §2.3); this PC converted its copy of the first.
|
||||
var remote = f.Remote.Add("Holy grail", photos);
|
||||
remote.Undecodable = true;
|
||||
await f.ReplaceLocalAsync(local.Id, 0, f.NewPhotos(1)[0]);
|
||||
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
|
||||
await f.RefreshAsync();
|
||||
var ahead = await f.EntryAsync("Holy grail");
|
||||
Assert.Equal((SyncStatus.LocalAhead, 3), (ahead.Status, ahead.Link!.MissingRemote));
|
||||
var engine = await f.StartEngineAsync();
|
||||
|
||||
var job = await f.Sync.EnqueueUploadAsync(engine, f.Server, local);
|
||||
var done = await f.Archive.WaitForAsync(job.Id, JobState.Done);
|
||||
|
||||
Assert.True(f.Remote.SentProxies.Keys.ToHashSet().SetEquals(remote.Frames.Select(r => r.Id)), "a proxy of every frame");
|
||||
Assert.Equal(3, remote.Frames.Count);
|
||||
Assert.Contains(Loc.L("Archive.Log.ProxiesSent", 3, 0), done.LogTail);
|
||||
}
|
||||
|
||||
private static async Task<Render> AddRenderAsync(SyncFixture f, Sequence local, string name, long bytes)
|
||||
{
|
||||
var render = new Render
|
||||
|
||||
Reference in new issue
Block a user