using System.Reflection;
using Chronos.App.Infrastructure;
using Chronos.App.Library;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Data.Archiving;
using Chronos.Data.Copies;
using Chronos.Testing;
namespace Chronos.App.Tests.Library;
/// Two versions… (owner Q3, #73): a drop whose dry run cannot go says why and asks nothing.
[Serial]
public sealed class VersionsDialogTests
{
private static readonly Sequence Comet = new()
{
Id = Guid.CreateVersion7(),
ArchiveId = Guid.CreateVersion7(),
Title = "Comet",
Folder = "2026/comet",
};
[Test]
public async Task ABlockedDropSaysWhyAndNeverAsksOrDrops()
{
foreach (var (block, expected) in new[]
{
(FrameDeleteBlock.Offline, Loc.L("Errors.Copies.TrashBlocked", "IMG_0002.JPG", Loc.L("Archive.Delete.Reason.Offline"))),
(FrameDeleteBlock.Busy, Loc.L("Errors.Copies.TrashBlocked", "IMG_0002.JPG", Loc.L("Archive.Delete.Reason.Busy"))),
(FrameDeleteBlock.FramesChanged, Loc.L("Archive.Convert.Blocked.FramesChanged")),
})
{
var engine = FakeEngine.Create(new OwnVersionDropPlan(new FrameDeletePlan(Comet, [], 0, [block]), Guid.NewGuid(), "IMG_0003.JPG", "b", []));
var asked = 0;
var dialog = new VersionsDialogViewModel((ICopyEngine)engine, Comet.Id, Comet.DisplayTitle, _ =>
{
asked++;
return true;
}, _ => { });
var row = new VersionRow(dialog, new PhotoVersions(Guid.NewGuid(), "IMG_0002.JPG", "a", Guid.NewGuid(), "IMG_0003.JPG", "b"), Guid.NewGuid(), "IMG_0002.JPG", 10, "IMG_0003.JPG", inMain: true);
await dialog.DropAsync(row);
Assert.Equal(0, asked);
Assert.Equal(0, ((FakeEngine)engine).Dropped);
Assert.Equal(expected, dialog.ErrorText);
Assert.True(row.InMain);
}
}
/// An that plans for every drop and counts the drops applied.
public class FakeEngine : DispatchProxy
{
internal OwnVersionDropPlan? Plan { get; set; }
internal int Dropped { get; private set; }
internal static object Create(OwnVersionDropPlan plan)
{
var proxy = Create();
((FakeEngine)(object)proxy).Plan = plan;
return proxy;
}
protected override object? Invoke(MethodInfo? targetMethod, object?[]? args) => targetMethod?.Name switch
{
nameof(ICopyEngine.PlanDropOwnVersionAsync) => Task.FromResult(Plan!),
nameof(ICopyEngine.DropOwnVersionAsync) => Drop(),
nameof(ICopyEngine.VersionsAsync) => Task.FromResult>([]),
nameof(IDisposable.Dispose) => null,
_ => throw new NotSupportedException(targetMethod?.Name),
};
private Task Drop()
{
Dropped++;
return Task.FromResult(1);
}
}
}