Files
Chronos/tests/Chronos.App.Tests/Archive/TrashDialogTests.cs
T
Alby96 a133306fb0 fix(app): show what a trash restore kept, and drop stale trash translations
The desktop trash dialog sent the "N files stayed in the trash" sentence
through ShowError. ArchiveFailures.Cause has no arm for it, so after a
partial restore the red line and Narrator said "A file of the archive could
not be read or written." The sentence itself appeared only in the tooltip.
The same thing happened to an entry none of whose files matches its record.

- ArchiveDialogViewModel.ShowMessage shows a sentence that is already
  localized. The trash dialog uses it for kept files, and its tooltip names
  the files kept (six of them, then "+ N more").
- ArchiveFailures.Cause passes FrameTrash's Archive.Error.TrashMismatch
  sentence through. It matches the sentence's text because the engine throws
  a plain InvalidDataException. Any other InvalidDataException, such as a
  sidecar that belongs to another sequence, still reads as a file error.
- Archive.Trash.List, Tooltips.Archive.Trash and Tooltips.Archive.TrashRestore
  changed meaning in English: they now cover photo entries as well as
  sequences. They are removed from the 36 cultures other than en and it, so
  those cultures fall back to English and `i18n check` lists the keys as
  untranslated instead of showing the old sequence-only wording.
- The XAML comments describe what the trash dialog and its error line show
  now.

Tests: the dialog restores a photo entry and a sequence entry and checks
which rows are left. A photo entry with one tampered file shows the
KeptFiles sentence and keeps its row. An entry with every file tampered
shows the TrashMismatch sentence.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 2524a11445c87d37625b01095ef7c2a84a2ee422)
2026-10-27 12:46:45 +01:00

292 lines
15 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.KeptFiles", 1), dialog.ErrorText, "the sentence itself, not a generic file error");
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(0, restored);
Assert.Equal(2, Only(dialog.Rows).Entry.Frames);
Assert.Equal(2, (await fx.Db.Sequences.GetAsync(sequence.Id))!.FrameCount);
// Any other invalid data (a sidecar of another sequence, say) still reads as a file error.
Assert.Equal(Loc.L("Archive.Failed.Files"), ArchiveFailures.Cause(new InvalidDataException("sequence.json belongs to sequence 42, not 43.")));
}
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);
}
}
}