Files
Chronos/tests/Chronos.App.Tests/Archive/TrashDialogTests.cs
T
Alby96 341fb77507 fix: conversions name, lock and settle as phase 4a's writers do
Phase 4b was built on phase 4a before its review fixes. Integrated on the reviewed 4a, the
conversion now follows the same rules as filing and the other writers.

- Names: a converted file is named as filing names a photo of its sequence (FrameNaming: the
  archive's file template, the frame's context in the sequence with its camera, lens, number
  and title, {name} the card's, {ext} the target's, a long name keeping its extension).
  Replace through ConvertOptions.FileName; New sequence through the ingest itself
  (StagedFrame.NamedAs), which numbers and names the copies in the new sequence. Names stay
  unique against every file present, those leaving included, so a Replace never moves onto a
  file; a test pins a stray file that keeps its bytes.
- Leases: stage A starts from the sidecar settled under the sequence's lease where the index
  has it then (SequenceWriter.ReadLocked), and FrameTrash.ReplaceAsync reads the row again
  under the lease, so a folder a Reorganize moved meanwhile is followed, never made again.
  A Reorganize the archive waits for keeps the conversion off its sequences (pinned).
- Journals: the replace journal is settled with the others. MassRenamer steps, restores into
  a sequence and sequence deletions wait for an unsettled replacement as they wait for a
  restore, and so do photo deletions, whose frames version the recovery reads
  (FrameTrash.EnsureReplaced); a scan settles it and reconciles nothing meanwhile; a restore
  settles its sequence's replace journal first, at the folder the index has under the lease.
- The convert job tells each sequence converted or made as it completes, as a Reorganize
  tells each step, so an editor open on it reads it again while the rest of the job runs.
- Desktop: JobKind.Convert gets its row in the settings' job priority list. A click on Apply
  that commits the quality no longer starts the same dry run again under the click (as 4a
  fixed for Reorganize).
- Panel: a dry run answered during the confirmation with the same plan no longer drops the
  confirmed Apply; a different plan is shown with Admin.Convert.PlanChanged (en, it).
- Tests: TrashDialogTests.EmptyQueuesTheJobOfTheConfirmedPlanOnly waits for the command to
  end before running it again (a running AsyncRelayCommand ignored the second Execute).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 2e0d1d6dcc10575734970bb9c77749f00777c859)
2026-10-27 22:03:07 +01:00

329 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.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);
// 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);
}
}
}