using System.Globalization; using System.IO; using Chronos.App.Archiving; using Chronos.App.Infrastructure; using Chronos.Core.Archiving; using Chronos.Core.Localization; using Chronos.Core.Model; using Chronos.Data.Archiving; using Chronos.Testing; namespace Chronos.App.Tests.Library; /// The confirmation of Delete in the Library and the Archive manager: the engine's dry run behind a typed confirmation (DESIGN ยง4). public sealed class DeletePlansTests { private static Sequence Sequence(string title, int frames) => new() { Id = Guid.CreateVersion7(), ArchiveId = Guid.CreateVersion7(), Title = title, Folder = "2026/" + title, FrameCount = frames, }; private static DeleteStep Step(string title, int frames, int files, long bytes) => new(Sequence(title, frames), $".chronos/trash/{Guid.CreateVersion7():N}", files, bytes); [Test] public void ThePlanAsksToTypeHowManySequencesMove() { var plan = new DeletePlan( [Step("Harbour sunset", 3_600, 3_640, 96_200_000_000), Step("Comet over the ridge", 1_005, 1_020, 21_000_000_000)], [new BlockedDelete(Sequence("Fog lifting", 980), DeleteBlock.Busy)]); var request = Assert.NotNull(DeletePlans.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.Delete.Heading", 2), request.Heading); Assert.Equal(Loc.L("Archive.Delete.Message", Loc.Plural("Shell.Status.Photos", 4_605), Loc.Plural("Archive.Delete.Files", 4_660), Formatting.Bytes(117_200_000_000)), request.Message); var lines = request.Detail!.Split(Environment.NewLine); Assert.Equal(3, lines.Length); Assert.Equal(Loc.L("Archive.Delete.Line", "Harbour sunset", Loc.Plural("Shell.Status.Photos", 3_600), Formatting.Bytes(96_200_000_000)), lines[0]); Assert.Equal(Loc.L("Archive.Delete.Blocked", "Fog lifting", Loc.L("Archive.Delete.Reason.Busy")), lines[2]); Assert.Equal(Loc.L("Archive.Delete.Keep"), request.SafeText); Assert.Equal(Loc.Plural("Tooltips.Archive.DeleteAction", 2), request.ActionToolTip); } [Test] public void LongPlansAreCountedAndEmptyOnesAskNothing() { var many = Enumerable.Range(1, DeletePlans.ListedLines + 3).Select(i => Step("Sequence " + i.ToString(CultureInfo.InvariantCulture), 10, 10, 1_000)).ToList(); var request = Assert.NotNull(DeletePlans.Request(new DeletePlan(many, []))); var lines = request.Detail!.Split(Environment.NewLine); Assert.Equal(DeletePlans.ListedLines + 1, lines.Length); Assert.Equal(Loc.Plural("Archive.Delete.More", 3), lines[^1]); Assert.Equal((DeletePlans.ListedLines + 3).ToString(CultureInfo.InvariantCulture), request.TypedConfirmation); var blocked = new DeletePlan([], [new BlockedDelete(Sequence("Root", 2), DeleteBlock.ArchiveRoot)]); Assert.Null(DeletePlans.Request(blocked), "nothing can move: no confirmation"); Assert.SequenceEqual([Loc.L("Archive.Delete.Blocked", "Root", Loc.L("Archive.Delete.Reason.ArchiveRoot"))], DeletePlans.Blocked(blocked, 8)); foreach (var reason in Enum.GetValues()) Assert.NotEqual("Archive.Delete.Reason." + reason, Loc.L("Archive.Delete.Reason." + reason), "every reason has words"); } [Test] public void TheConfirmationStaysShortEnoughForASmallScreen() { var steps = Enumerable.Range(1, 20).Select(i => Step("Sequence " + i.ToString(CultureInfo.InvariantCulture), 10, 10, 1_000)).ToList(); var blocked = Enumerable.Range(1, 9).Select(i => new BlockedDelete(Sequence("Busy " + i.ToString(CultureInfo.InvariantCulture), 5), DeleteBlock.Busy)).ToList(); var request = Assert.NotNull(DeletePlans.Request(new DeletePlan(steps, blocked))); var lines = request.Detail!.Split(Environment.NewLine); Assert.Equal(DeletePlans.ListedLines + 1 + DeletePlans.BlockedLines + 1, lines.Length, "named lines plus one count for each part"); Assert.Equal(Loc.Plural("Archive.Delete.More", 14), lines[DeletePlans.ListedLines]); Assert.Equal(Loc.Plural("Archive.Delete.More", 6), lines[^1]); Assert.False(request.IsDetailMonospaced, "names read in the UI font, not the fingerprint font"); } [Test] public void ARefusedDeleteOrRestoreSaysWhyInOneSentence() { Assert.Equal(Loc.L("Archive.Error.StalePlan"), ArchiveFailures.Cause(new StaleDeletePlanException(Guid.NewGuid()))); Assert.Equal(Loc.L("Archive.Error.InLibrary", "Harbour sunset"), ArchiveFailures.Cause(new SequenceInLibraryException(Guid.NewGuid(), "Harbour sunset"))); Assert.Equal(Loc.L("Archive.Error.NewerFormat", "sequence.json", 9), ArchiveFailures.Cause(new NewerFormatException("/a/2026/x/.chronos/sequence.json", 9))); Assert.Equal(Loc.L("Archive.Failed.Files"), ArchiveFailures.Cause(new DirectoryNotFoundException("/a/.chronos/trash/0123 is not in the trash")), "the engine's own words stay in the log"); } }