Files
Chronos/tests/Chronos.App.Tests/Archive/TrashDialogTests.cs
T
Alby96 4518ffe655 fix(archive): settle trash journals under their leases and say why restored photos stayed
Second fix round of owner review 2, phase 3 (engine, server, desktop).

- A photo never hashed whose original is missing can be deleted again: it goes
  into its trash record without a file and without a tombstone (nothing is left
  to name), stays listed until Empty erases it, and a restore keeps it with the
  reason "no file". FrameDeleteBlock.Unhashable now blocks only a present
  original that cannot be read; the API answers it as 409 photo_unreadable
  naming the photo, and every block has its own code and message.
- Deleting a whole sequence first settles an interrupted photo deletion,
  restore or ingest of it (ReadLocked under its lease), so no journal or half
  entry travels into the sequence's trash entry; it refuses while a restore
  into it waits for its busy entry.
- FrameTrash.Settle takes the photo entry's lease and leaves the journal for
  later while the entry is busy (an emptying), so it never races EraseAsync;
  meanwhile no other restore writes its journal over it and scans do not adopt
  its files.
- The hashes computed for frames that stay (to find a twin of a deleted photo)
  go into the sidecar and index, and only a frame whose first and last 64 KiB
  match a deleted photo's is read whole.
- A restore into a new sequence rewrites its record after each photo filed, so
  a crash or a database failure never leaves filed photos listed.
- An upload completion no longer has a window before the commit: the ingest
  checks the sidecar's tombstones under the sequence lease a deletion takes
  (IngestRequest.RefuseRemoved), unless the upload asked to bring it back.
- A restore reports each kept photo with its reason (no longer matches its
  record, could not be moved back, already in the sequence, had no file); the
  desktop line and the API message say so in en and it, and the tooltip holds
  the whole sentence. The desktop tells TrashMismatchException by its type.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 1858f1f450a35fb8383ede8d80ce5f8de9b7ceac)
2026-10-27 13:53:41 +01:00

328 lines
17 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.Null(TrashEmptyPlans.Request(plan with { Entries = [] }), "nothing to erase: no confirmation");
}
[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);
await UntilAsync(() => asked is not null && !dialog.IsBusy);
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);
}
}
}