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)
This commit is contained in:
1 parent
13278710a2
commit
a133306fb0
77 files changed
+195
-115
No files matched your search
@@ -1,17 +1,24 @@
|
||||
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, and Empty… behind a dry run and a typed confirmation.</summary>
|
||||
/// <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);
|
||||
@@ -111,6 +118,167 @@ public sealed class TrashDialogTests(TestContext context)
|
||||
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));
|
||||
|
||||
Reference in new issue
Block a user