using Chronos.App.Library;
using Chronos.App.Server;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Data.Sync;
using Chronos.Testing;
namespace Chronos.App.Tests.Library;
/// Which selected sequences each button of the mass actions bar works on, and why a button is off (BRIEF §12.2, DESIGN §4).
public sealed class MassActionsTests
{
private const string Server = "https://nas.local:8443";
private static readonly SyncRights Editor = SyncRights.Of((long)RolePermissions.Default(UserRole.Editor));
private static readonly SyncRights Viewer = SyncRights.Of((long)RolePermissions.Default(UserRole.Viewer));
private static readonly Guid MainArchive = Guid.CreateVersion7();
private static readonly SelectionContext Offline = new(HasEngine: true, IsSignedIn: false, default);
private static readonly SelectionContext SignedIn = new(HasEngine: true, IsSignedIn: true, Editor);
private static Sequence Local(string title, Guid? archive = null) => new()
{
Id = Guid.CreateVersion7(),
ArchiveId = archive ?? MainArchive,
Title = title,
Folder = "2026/" + title,
FrameCount = 120,
};
private static SyncLink Link(Sequence sequence) => new() { Server = Server, RemoteId = Guid.CreateVersion7(), Title = sequence.DisplayTitle };
private static SelectedSequence Remote(string title)
{
var link = new SyncLink { Server = Server, RemoteId = Guid.CreateVersion7(), Title = title, FrameCount = 300 };
var card = RemoteCards.FromLink(link);
return new SelectedSequence(card, new SyncEntry(null, link, SyncStatus.RemoteOnly));
}
private static SelectedSequence Linked(string title, SyncStatus status, Func? change = null)
{
var local = Local(title);
var link = Link(local);
return new SelectedSequence(local, new SyncEntry(local, change?.Invoke(link) ?? link, status));
}
private static SelectedSequence Only(string title) => new(Local(title), null);
[Test]
public void NothingRunsWithoutASelectionOrAnIndexButTheListExport()
{
foreach (var action in Enum.GetValues())
Assert.Equal(Loc.L("Library.Blocked.NoSelection"), MassActions.Blocker(action, [], SignedIn));
SelectedSequence[] one = [Only("Harbour sunset")];
var noIndex = new SelectionContext(HasEngine: false, IsSignedIn: false, default);
Assert.Equal(Loc.L("Library.Blocked.NoIndex"), MassActions.Blocker(MassAction.Analyse, one, noIndex));
Assert.Equal(Loc.L("Library.Blocked.NoIndex"), MassActions.Blocker(MassAction.Delete, one, noIndex));
Assert.Null(MassActions.Blocker(MassAction.ExportList, one, noIndex), "a CSV needs only the rows on screen");
}
[Test]
public void ArchiveActionsWorkOnTheSequencesOfThisPc()
{
var harbour = Only("Harbour sunset");
var remote = Remote("Sunrise over Superga");
SelectedSequence[] mixed = [harbour, remote];
foreach (var action in new[] { MassAction.Analyse, MassAction.Proxies, MassAction.Verify, MassAction.Tag, MassAction.Rate, MassAction.Move, MassAction.Delete })
{
Assert.Null(MassActions.Blocker(action, mixed, SignedIn), action.ToString());
Assert.SequenceEqual([harbour], MassActions.Targets(action, mixed, SignedIn), action.ToString());
Assert.Equal(Loc.L("Library.Blocked.ServerOnly"), MassActions.Blocker(action, [remote], SignedIn));
}
Assert.SequenceEqual(mixed, MassActions.Targets(MassAction.ExportList, mixed, SignedIn), "the CSV lists remote cards too");
Assert.Equal(Loc.Plural("Tooltips.Library.Mass.Delete", 1), MassActions.ToolTip(MassAction.Delete, 1));
}
[Test]
public void MoveStaysInsideOneArchive()
{
SelectedSequence[] two = [Only("Harbour sunset"), new(Local("Aurora", Guid.CreateVersion7()), null)];
Assert.Equal(Loc.L("Library.Blocked.Archives"), MassActions.Blocker(MassAction.Move, two, Offline));
Assert.Null(MassActions.Blocker(MassAction.Delete, two, Offline), "the trash of each archive takes its own sequences");
}
[Test]
public void TransfersNeedASessionAndTheRight()
{
SelectedSequence[] one = [Only("Harbour sunset")];
Assert.Equal(Loc.L("Library.Blocked.NotConnected"), MassActions.Blocker(MassAction.Upload, one, Offline));
Assert.Equal(Loc.L("Library.Blocked.NotConnected"), MassActions.Blocker(MassAction.Download, one, Offline));
Assert.Equal(Loc.L("Library.Blocked.NoUploadRight"), MassActions.Blocker(MassAction.Upload, one, SignedIn with { Rights = Viewer }));
Assert.Equal(Loc.L("Library.Blocked.NoDownloadRight"), MassActions.Blocker(MassAction.Download, one, SignedIn with { Rights = Viewer }));
Assert.Null(MassActions.Blocker(MassAction.Upload, one, SignedIn), "a sequence only here uploads before the table compares it");
Assert.Equal(Loc.L("Library.Blocked.NothingToDownload"), MassActions.Blocker(MassAction.Download, one, SignedIn));
}
[Test]
public void EachTransferTakesWhatItsSyncRowOffers()
{
var only = Only("Harbour sunset");
var ahead = Linked("Comet over the ridge", SyncStatus.LocalAhead, l => l with { MissingRemote = 4 });
var renders = Linked("Star trails", SyncStatus.InSync, l => l with { ExportsToSend = 1, ExportsToGet = 2 });
var same = Linked("Aurora, Tromsø", SyncStatus.InSync);
var behind = Linked("Fog lifting", SyncStatus.ServerAhead, l => l with { MissingLocal = 12 });
var remote = Remote("Sunrise over Superga");
SelectedSequence[] all = [only, ahead, renders, same, behind, remote];
Assert.SequenceEqual([only, ahead, renders], MassActions.Targets(MassAction.Upload, all, SignedIn));
Assert.Equal(SyncAction.Upload, MassActions.UploadStep(only, Editor));
Assert.Equal(SyncAction.Upload, MassActions.UploadStep(ahead, Editor));
Assert.Equal(SyncAction.SendExports, MassActions.UploadStep(renders, Editor));
Assert.Null(MassActions.UploadStep(same, Editor));
Assert.Null(MassActions.UploadStep(remote, Editor), "a remote card has nothing here to send");
Assert.SequenceEqual([renders, behind, remote], MassActions.Targets(MassAction.Download, all, SignedIn));
Assert.Equal(SyncAction.GetExports, MassActions.DownloadStep(renders, Editor));
Assert.Equal(SyncAction.DownloadOriginals, MassActions.DownloadStep(behind, Editor));
Assert.Equal(SyncAction.DownloadOriginals, MassActions.DownloadStep(remote, Editor));
Assert.Null(MassActions.DownloadStep(only, Editor));
Assert.Equal(Loc.L("Library.Blocked.NothingToUpload"), MassActions.Blocker(MassAction.Upload, [same, remote], SignedIn));
}
[Test]
public void TheCardsSyncDoesTheFirstThingItsRowOffers()
{
Assert.Null(MassActions.QuickSync(Only("Harbour sunset"), Offline, out var why));
Assert.Equal(Loc.L("Library.Blocked.NotConnected"), why);
Assert.Equal(SyncAction.Upload, MassActions.QuickSync(Only("Harbour sunset"), SignedIn, out why));
Assert.Null(why);
Assert.Null(MassActions.QuickSync(Only("Harbour sunset"), SignedIn with { Rights = Viewer }, out why));
Assert.Equal(Loc.L("Library.Blocked.NoUploadRight"), why);
Assert.Equal(SyncAction.DownloadOriginals, MassActions.QuickSync(Remote("Sunrise over Superga"), SignedIn, out _));
Assert.Equal(SyncAction.SyncSettings, MassActions.QuickSync(Linked("Holy grail", SyncStatus.ServerAhead), SignedIn, out _));
Assert.Equal(SyncAction.KeepMine, MassActions.QuickSync(Linked("Holy grail", SyncStatus.Conflict), SignedIn, out _), "a conflict goes to the Server screen");
Assert.Null(MassActions.QuickSync(Linked("Aurora, Tromsø", SyncStatus.InSync), SignedIn, out why));
Assert.Equal(Loc.L("Library.Blocked.InSync"), why);
var moving = Linked("Comet over the ridge", SyncStatus.LocalAhead);
var transferring = moving with { Sync = moving.Sync! with { Transfer = new Job { Id = Guid.CreateVersion7(), Kind = JobKind.UploadCommit, State = JobState.Running } } };
Assert.Null(MassActions.QuickSync(transferring, SignedIn, out why));
Assert.Equal(Loc.L("Library.Blocked.Transferring"), why);
}
}