Files
Chronos/tests/Chronos.App.Tests/Archive/TrashDialogTests.cs
T
Alby96 d0bbd77c92 fix(archive): settle conversions by content and keep their aliases through trash, undo, sync and ingest
Owner review 2, phase 4b, fix round 1 (trash, undo, recovery, sync and ingest).

- Replace journal: records a phase (moving, written, undoing) and the name each swapped
  frame is filed under. Recovery decides "sidecar written" by what the sidecar lists, not
  by the frames version any writer moves; a failed rollback stays undoing and its failure
  now stops the writer (as a photo deletion's recovery does). The ingest settles restores
  and replacements first, so an upload can no longer complete a swap that never happened.
- Emptying a Replaced entry re-verifies each replacement where it is now: in the sequence
  through later conversions (a second conversion no longer strands the first originals),
  or, when the conversion was deleted, its copy in the trash. The dry run marks such
  entries "conversion deleted" (fingerprinted, shown on the desktop and the panel), and
  the confirmed emptying may then erase them instead of keeping them forever.
- Undo of a conversion takes exactly the entries its job made (the job id is recorded on
  the entry), never entries found by time window.
- Project positions translated by photo use the sidecar's photo of every frame still
  there, so positions on a converted frame stay put when another frame went meanwhile.
- Sync aliases are equivalence classes: an original converted on both sides is one photo;
  link-by-content, proxy counting and proxy upload follow aliases; a download into a
  frame missing here swaps the other encoding into that frame instead of adding a frame.
- The ingest answers a converted photo's original (a card or folder imported again, a
  download) as a duplicate of its conversion and keeps the alias tombstone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 3341a2f845753fc4c5bcba51a3b645f126c95eaf)
2026-10-27 22:13:36 +01:00

336 lines
18 KiB
C#

using System.Globalization;
using System.IO;
using System.Security.Cryptography;
using Chronos.App.Archiving;
using Chronos.App.Infrastructure;
using Chronos.App.Tests.Import;
using Chronos.Core.Archiving;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Data;
using Chronos.Data.Archiving;
using Chronos.Data.Archiving.Jobs;
using Chronos.Testing;
using Chronos.Ui.Mvvm;
namespace Chronos.App.Tests.Archiving;
/// <summary>
/// The trash dialog (owner review 2, R6): photo entries next to sequences, restores that say in their own words what stayed
/// in the trash, and Empty… behind a dry run and a typed confirmation.
/// </summary>
public sealed class TrashDialogTests(TestContext context)
{
private static readonly DateTimeOffset Deleted = new(2026, 10, 27, 9, 30, 0, TimeSpan.Zero);
private static TrashEntry Entry(string title, TrashKind kind, int photos, long bytes) =>
new(Guid.CreateVersion7(), (kind == TrashKind.Sequence ? string.Empty : FrameTrash.EntryPrefix) + Guid.CreateVersion7().ToString("N"),
Guid.CreateVersion7(), title, "2026/" + title, photos, photos, bytes, Deleted, "alberto", kind);
[Test]
public void EveryKindOfEntryHasWordsAndTheRowSaysWhatItHolds()
{
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
foreach (var kind in Enum.GetValues<TrashKind>())
Assert.NotEqual("Archive.Trash.Kind." + kind, TrashEmptyPlans.KindText(kind), "every kind has words");
var row = new TrashRowViewModel(Entry("Harbour sunset", TrashKind.Photos, 12, 340_000_000), new Chronos.Ui.Mvvm.RelayCommand(() => { }));
Assert.Equal(Loc.L("Archive.Trash.Kind.Photos"), row.KindText);
Assert.True(row.Detail.StartsWith(row.KindText, StringComparison.Ordinal), row.Detail);
Assert.Contains(Loc.Plural("Shell.Status.Photos", 12), row.Detail);
}
[Test]
public void EmptyingAsksToTypeHowManyEntriesAreErased()
{
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
var entries = new[] { Entry("Harbour sunset", TrashKind.Photos, 12, 340_000_000), Entry("Comet", TrashKind.Sequence, 1_005, 21_000_000_000) };
var plan = new TrashEmptyPlan(Guid.CreateVersion7(), entries, 21_340_000_000, "f1", []);
var request = Assert.NotNull(TrashEmptyPlans.Request(plan));
Assert.True(request.IsDestructive, "the action is outlined, never primary");
Assert.Equal("2", request.TypedConfirmation);
Assert.Equal(Loc.L("Archive.Delete.Type", "2"), request.TypedPrompt);
Assert.Equal(Loc.Plural("Archive.Trash.EmptyHeading", 2), request.Heading);
Assert.Equal(Loc.L("Archive.Trash.EmptyMessage", Loc.Plural("Shell.Status.Photos", 1_017), Loc.Plural("Archive.Delete.Files", 1_017),
Formatting.Bytes(21_340_000_000)), request.Message);
Assert.Equal(Loc.L("Archive.Trash.Erase"), request.ActionText);
Assert.Equal(Loc.L("Archive.Delete.Keep"), request.SafeText);
var lines = request.Detail!.Split(Environment.NewLine);
Assert.Equal(2, lines.Length);
Assert.Contains(Loc.L("Archive.Trash.Kind.Photos"), lines[0]);
Assert.False(request.Detail!.Contains(Loc.L("Archive.Trash.ConversionDeleted"), StringComparison.Ordinal), "no conversion was deleted");
Assert.Null(TrashEmptyPlans.Request(plan with { Entries = [] }), "nothing to erase: no confirmation");
// Replaced originals whose conversion was deleted since: the line says so, as the dry run found it.
var replaced = Entry("Etna", TrashKind.Replaced, 3, 90_000_000) with { ConversionDeleted = true };
var lone = Assert.NotNull(TrashEmptyPlans.Request(plan with { Entries = [replaced] }));
Assert.Contains(Loc.L("Archive.Trash.Kind.Replaced") + Loc.Separator + Loc.Plural("Shell.Status.Photos", 3) + Loc.Separator
+ Loc.L("Archive.Trash.ConversionDeleted"), lone.Detail!);
}
[Test]
public async Task EmptyQueuesTheJobOfTheConfirmedPlanOnly()
{
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
var directory = context.CreateTempDirectory("trash-dialog");
await using var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(directory, "index.db")));
var archives = new ArchiveService(db, new ArchiveServiceOptions { Owner = "test", LeaseWait = TimeSpan.FromSeconds(TestTiming.Budget(20)) });
var archive = await archives.CreateAsync(Path.Combine(directory, "archive"), "Main");
// A sequence deleted earlier: its folder and its record in the archive's trash.
var id = Guid.CreateVersion7();
var trash = SequenceTrash.TrashDirectory(new Core.Archiving.ArchivePaths(archive.RootPath));
Directory.CreateDirectory(Path.Combine(trash, id.ToString("N")));
await File.WriteAllBytesAsync(Path.Combine(trash, id.ToString("N"), "IMG_0001.JPG"), [1, 2, 3]);
await File.WriteAllTextAsync(Path.Combine(trash, id.ToString("N") + ".json"),
$$"""{"sequence":{"id":"{{id}}","archiveId":"{{archive.Id}}","title":"Harbour sunset","folder":"2026/Harbour","frameCount":1},"renders":[],"files":1,"bytes":3,"deletedAt":"2026-10-27T09:30:00+00:00","deletedBy":"alberto"}""");
var plain = new TrashDialogViewModel(archives, archive, () => Task.CompletedTask, _ => { });
await plain.LoadAsync();
Assert.Count(1, plain.Rows);
Assert.False(plain.CanEmpty, "a host without a job engine offers no emptying");
ConfirmRequest? asked = null;
var answer = false;
var queued = new TaskCompletionSource<ArchiveJobRequest>(TaskCreationOptions.RunContinuationsAsynchronously);
var closed = false;
var dialog = new TrashDialogViewModel(archives, archive, () => Task.CompletedTask, _ => closed = true, r =>
{
asked = r;
return answer;
}, r =>
{
queued.TrySetResult(r);
return Task.CompletedTask;
});
await dialog.LoadAsync();
Assert.True(dialog.CanEmpty);
Assert.Equal(Loc.Plural("Archive.Trash.Entries", 1) + Loc.Separator + Formatting.Bytes(3), dialog.SummaryText);
// Kept: the confirmation was dismissed, nothing is queued.
dialog.EmptyCommand.Execute(null);
// Until the command has ended, not only the dialog's work: a command still running ignores the next Execute.
await UntilAsync(() => asked is not null && !dialog.IsBusy && dialog.EmptyCommand.CanExecute(null));
Assert.Equal("1", asked!.TypedConfirmation);
Assert.False(queued.Task.IsCompleted);
Assert.False(closed);
answer = true;
dialog.EmptyCommand.Execute(null);
var request = await queued.Task.WaitAsync(TimeSpan.FromSeconds(TestTiming.Budget(20)));
Assert.Equal(JobKind.EmptyTrash, request.Kind);
Assert.SequenceEqual([archive.Id], request.Targets);
Assert.Contains(id.ToString("N"), request.ParametersJson);
await UntilAsync(() => closed);
Assert.Count(1, await archives.ListTrashAsync(archive.Id), "the job erases, not the dialog");
}
[Test]
public async Task RestoringASequenceAndPhotosTakesTheirRowsAway()
{
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
await using var fx = await TrashFixture.CreateAsync(context);
var (sequence, frames) = await fx.UploadedAsync("upload-1", 4, new DateTime(2026, 9, 28, 18, 0, 0), 100);
var photos = await fx.DeletePhotosAsync(sequence, frames[1], frames[2]);
var (whole, _) = await fx.UploadedAsync("upload-2", 2, new DateTime(2026, 9, 29, 7, 0, 0), 200);
await fx.Archives.DeleteAsync(await fx.Archives.PlanDeleteAsync([whole.Id]), "alberto");
var restored = 0;
var dialog = new TrashDialogViewModel(fx.Archives, fx.Archive, () =>
{
restored++;
return Task.CompletedTask;
}, _ => { });
await dialog.LoadAsync();
Assert.Count(2, dialog.Rows);
await RestoreAsync(Assert.Contains(dialog.Rows, r => r.Entry.Name == photos.Name));
Assert.Null(dialog.ErrorText);
Assert.Equal(1, restored);
Assert.SequenceEqual([whole.Id], dialog.Rows.Select(r => r.Entry.SequenceId));
Assert.Equal(4, (await fx.Db.Sequences.GetAsync(sequence.Id))!.FrameCount);
await RestoreAsync(Only(dialog.Rows));
Assert.Null(dialog.ErrorText);
Assert.Equal(2, restored);
Assert.True(dialog.IsEmpty);
Assert.Equal(Loc.L("Archive.Trash.Empty"), dialog.SummaryText);
// The sequence is back in the index.
Assert.NotNull(await fx.Db.Sequences.GetAsync(whole.Id));
}
[Test]
public async Task APhotoThatNoLongerMatchesItsRecordStaysAndTheLineSaysSo()
{
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
await using var fx = await TrashFixture.CreateAsync(context);
var (sequence, frames) = await fx.UploadedAsync("upload-1", 4, new DateTime(2026, 9, 28, 18, 0, 0), 100);
var entry = await fx.DeletePhotosAsync(sequence, frames[1], frames[2]);
await File.WriteAllBytesAsync(fx.InEntry(entry, frames[2]), [1, 2, 3]);
var restored = 0;
var dialog = new TrashDialogViewModel(fx.Archives, fx.Archive, () =>
{
restored++;
return Task.CompletedTask;
}, _ => { });
await dialog.LoadAsync();
await RestoreAsync(Only(dialog.Rows));
Assert.Equal(Loc.Plural("Archive.Trash.Kept.Mismatch", 1), dialog.ErrorText, "the sentence itself, not a generic file error");
// The line may cut the sentence: the tooltip holds it whole, then the photo's name.
Assert.True(dialog.ErrorToolTip!.StartsWith(dialog.ErrorText + Environment.NewLine, StringComparison.Ordinal), dialog.ErrorToolTip);
Assert.Contains(frames[2].OriginalFileName ?? Path.GetFileName(frames[2].RelativePath), dialog.ErrorToolTip);
Assert.Equal(1, restored, "the other photo came back");
var left = Only(dialog.Rows);
Assert.Equal(entry.Name, left.Entry.Name);
Assert.Equal(1, left.Entry.Frames);
Assert.Equal(3, (await fx.Db.Sequences.GetAsync(sequence.Id))!.FrameCount);
}
[Test]
public async Task AnEntryWithNoMatchingFileStaysAndTheLineSaysSo()
{
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
await using var fx = await TrashFixture.CreateAsync(context);
var (sequence, frames) = await fx.UploadedAsync("upload-1", 4, new DateTime(2026, 9, 28, 18, 0, 0), 100);
var entry = await fx.DeletePhotosAsync(sequence, frames[1], frames[2]);
await File.WriteAllBytesAsync(fx.InEntry(entry, frames[1]), [1, 2, 3]);
await File.WriteAllBytesAsync(fx.InEntry(entry, frames[2]), [4, 5, 6]);
var restored = 0;
var dialog = new TrashDialogViewModel(fx.Archives, fx.Archive, () =>
{
restored++;
return Task.CompletedTask;
}, _ => { });
await dialog.LoadAsync();
await RestoreAsync(Only(dialog.Rows));
Assert.Equal(Loc.L("Archive.Error.TrashMismatch"), dialog.ErrorText, "the sentence itself, not a generic file error");
Assert.Equal(dialog.ErrorText, dialog.ErrorToolTip, "the whole sentence, once");
Assert.Equal(0, restored);
Assert.Equal(2, Only(dialog.Rows).Entry.Frames);
Assert.Equal(2, (await fx.Db.Sequences.GetAsync(sequence.Id))!.FrameCount);
// Told by its type: an IOException that merely carries the same words, or any other invalid data (a sidecar of
// another sequence, say), still reads as a file error.
Assert.Equal(Loc.L("Archive.Error.TrashMismatch"), ArchiveFailures.Cause(new TrashMismatchException()));
Assert.Equal(Loc.L("Archive.Failed.Files"), ArchiveFailures.Cause(new IOException(Loc.L("Archive.Error.TrashMismatch"))));
Assert.Equal(Loc.L("Archive.Failed.Files"), ArchiveFailures.Cause(new InvalidDataException("sequence.json belongs to sequence 42, not 43.")));
}
[Test]
public void WhatARestoreKeptSaysWhyForEachReasonAndNamesThePhotos()
{
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
// One reason: its sentence on the line, repeated whole in the tooltip above the names.
var (one, oneTip) = TrashDialogViewModel.KeptText([new KeptPhoto("IMG_0001.CR3", KeptReason.NotMoved), new KeptPhoto("IMG_0002.CR3", KeptReason.NotMoved)]);
Assert.Equal(Loc.Plural("Archive.Trash.Kept.NotMoved", 2), one);
Assert.Equal(string.Join(Environment.NewLine, one, Formatting.LeftToRight("IMG_0001.CR3"), Formatting.LeftToRight("IMG_0002.CR3")), oneTip);
// Several reasons: how many stayed on the line; each reason's sentence in the tooltip, names counted past the list.
var kept = Enumerable.Range(0, DeletePlans.ListedLines + 3)
.Select(i => new KeptPhoto($"IMG_{i:0000}.JPG", i % 3 == 0 ? KeptReason.InSequence : i % 3 == 1 ? KeptReason.Mismatch : KeptReason.NoFile)).ToList();
var (several, tip) = TrashDialogViewModel.KeptText(kept);
Assert.Equal(Loc.Plural("Archive.Trash.KeptPhotos", kept.Count), several);
var lines = tip.Split(Environment.NewLine);
Assert.Equal(several, lines[0]);
foreach (var reason in new[] { KeptReason.Mismatch, KeptReason.InSequence, KeptReason.NoFile })
Assert.Contains(KeptPhoto.Sentence(reason, kept.Count(k => k.Reason == reason), Loc.Culture), lines);
Assert.DoesNotContain(Loc.Plural("Archive.Trash.Kept.NotMoved", 1), lines);
Assert.Equal(Loc.Plural("Archive.Trash.KeptMore", 3), lines[^1]);
Assert.Equal(DeletePlans.ListedLines, lines.Count(l => l.Contains("IMG_", StringComparison.Ordinal)));
// Italian counts photos, which are feminine.
var italian = CultureInfo.GetCultureInfo("it");
Assert.Equal("1 foto è rimasta nel cestino: non corrisponde più al suo registro.", KeptPhoto.Sentence(KeptReason.Mismatch, 1, italian));
Assert.Equal("3 foto sono rimaste nel cestino: la sequenza le contiene già.", KeptPhoto.Sentence(KeptReason.InSequence, 3, italian));
Assert.Equal("+ altre 3", Loc.Text("Archive.Trash.KeptMore", [3], italian));
Assert.Equal("+ un'altra", Loc.Text("Archive.Trash.KeptMore", [1], italian));
}
private static TrashRowViewModel Only(IReadOnlyList<TrashRowViewModel> rows)
{
Assert.Count(1, rows);
return rows[0];
}
/// <summary>Restores <paramref name="row"/> as its button does and waits for the restore to end.</summary>
private static async Task RestoreAsync(TrashRowViewModel row)
{
var command = (AsyncRelayCommand)row.RestoreCommand;
command.Execute(null);
await UntilAsync(() => !command.IsRunning);
}
/// <summary>An archive with its own SQLite index, holding uploaded photos (real JPEGs, hashed) to delete and restore.</summary>
private sealed class TrashFixture : IAsyncDisposable
{
private readonly TestContext _context;
private TrashFixture(TestContext context, ChronosDatabase db, ArchiveService archives, Archive archive)
{
_context = context;
Db = db;
Archives = archives;
Archive = archive;
}
public ChronosDatabase Db { get; }
public ArchiveService Archives { get; }
public Archive Archive { get; }
public static async Task<TrashFixture> CreateAsync(TestContext context)
{
var directory = context.CreateTempDirectory("trash-dialog");
var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(directory, "index.db")));
var archives = new ArchiveService(db, new ArchiveServiceOptions { Owner = "test", LeaseWait = TimeSpan.FromSeconds(TestTiming.Budget(20)) });
return new TrashFixture(context, db, archives, await archives.CreateAsync(Path.Combine(directory, "archive"), "Main"));
}
/// <summary>A sequence of <paramref name="count"/> photos uploaded in one session, ten seconds apart.</summary>
public async Task<(Sequence Sequence, IReadOnlyList<Frame> Frames)> UploadedAsync(string session, int count, DateTime start, int seed)
{
var staging = Path.Combine(new ArchivePaths(Archive.RootPath).StagingDirectory, "uploads");
Directory.CreateDirectory(staging);
Guid? id = null;
for (var i = 0; i < count; i++)
{
var bytes = ImportTestData.Jpeg(start.AddSeconds(10 * i), seed + i);
var path = Path.Combine(staging, Guid.NewGuid().ToString("N") + ".part");
await File.WriteAllBytesAsync(path, bytes);
var result = await Archives.Ingest.CommitAsync(new IngestRequest(Archive.Id, id, null, id is null ? session : null, path,
$"IMG_{seed + i:0000}.JPG", bytes.Length, Convert.ToHexStringLower(SHA256.HashData(bytes)), null, null), _context.Cancellation);
id = result.SequenceId;
}
return ((await Db.Sequences.GetAsync(id!.Value))!, await Db.Frames.ListBySequenceAsync(id.Value));
}
/// <summary>Moves <paramref name="frames"/> of <paramref name="sequence"/> to the trash; returns their entry.</summary>
public async Task<TrashEntry> DeletePhotosAsync(Sequence sequence, params Frame[] frames)
{
var plan = await Archives.FrameTrash.PlanDeleteAsync(sequence.Id, [.. frames.Select(f => f.Id)], sequence.FramesVersion, _context.Cancellation);
Assert.True(plan.CanDelete, string.Join(", ", plan.Blocked));
return (await Archives.FrameTrash.DeleteAsync(plan, "alberto", null, _context.Cancellation)).Entry;
}
/// <summary>Where the original of <paramref name="frame"/> lies in the photo entry <paramref name="entry"/>.</summary>
public string InEntry(TrashEntry entry, Frame frame) => Path.Combine(FrameTrash.Directory(new ArchivePaths(Archive.RootPath)),
entry.Name[FrameTrash.EntryPrefix.Length..], "originals", Path.GetFileName(frame.RelativePath));
public ValueTask DisposeAsync() => Db.DisposeAsync();
}
private static async Task UntilAsync(Func<bool> condition)
{
var until = DateTime.UtcNow + TimeSpan.FromSeconds(TestTiming.Budget(20));
while (!condition())
{
Assert.True(DateTime.UtcNow < until, "timed out");
await Task.Delay(20);
}
}
}