using Chronos.App.Server;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Data.Sync;
using Chronos.Testing;
using Chronos.Ui.Controls;
namespace Chronos.App.Tests.Server;
/// Words, glyphs and actions of the sync table rows (Server.html).
public sealed class SyncTextTests
{
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 Sequence Local = new()
{
Id = Guid.CreateVersion7(),
ArchiveId = Guid.CreateVersion7(),
Title = "Harbour sunset",
Folder = "2026/harbour",
FrameCount = 3_600,
TotalBytes = 86_000_000_000,
Status = SequenceStatus.ProxiesBuilt,
SettingsJson = """{"exposure":{"deflickerWindow":{"isAuto":false,"manual":41}}}""",
};
private static readonly SyncLink Link = new()
{
Server = "https://nas.local:8443",
RemoteId = Guid.CreateVersion7(),
Title = "Harbour sunset",
TotalBytes = 61_000_000_000,
RemoteStatus = SequenceStatus.ProxiesBuilt | SequenceStatus.ThumbsBuilt,
RenderCount = 2,
};
[Test]
public void RightsFollowTheServerPermissions()
{
Assert.Equal(new SyncRights(true, true, true), Editor);
Assert.Equal(new SyncRights(false, false, false), Viewer);
Assert.Equal(new SyncRights(false, true, false), SyncRights.Of((long)Permissions.RenderNode));
}
[Test]
public void EachStateOffersItsActions()
{
Assert.SequenceEqual([SyncAction.Upload], SyncText.Actions(Entry(SyncStatus.LocalOnly, link: null), Editor));
Assert.Empty(SyncText.Actions(Entry(SyncStatus.LocalOnly, link: null), Viewer));
// A server that keeps no projects opens its sequences with proxies; the workspace is offered disabled (owner review 2, R8).
Assert.SequenceEqual([SyncAction.OpenWithProxies, SyncAction.DownloadOriginals, SyncAction.OpenInWorkspace],
SyncText.Actions(Entry(SyncStatus.RemoteOnly, local: null), Editor));
Assert.SequenceEqual([SyncAction.OpenWithProxies, SyncAction.OpenInWorkspace], SyncText.Actions(Entry(SyncStatus.ProxiesOnly, local: null), Viewer));
Assert.SequenceEqual([SyncAction.Upload], SyncText.Actions(Entry(SyncStatus.LocalAhead, Link with { MissingRemote = 4 }), Editor));
Assert.SequenceEqual([SyncAction.DownloadOriginals], SyncText.Actions(Entry(SyncStatus.ServerAhead, Link with { MissingLocal = 2 }), Editor));
Assert.SequenceEqual([SyncAction.SyncSettings], SyncText.Actions(Entry(SyncStatus.ServerAhead), Editor));
Assert.SequenceEqual([SyncAction.KeepMine, SyncAction.TakeServer, SyncAction.SaveBoth], SyncText.Actions(Entry(SyncStatus.Conflict), Editor));
Assert.Empty(SyncText.Actions(Entry(SyncStatus.InSync), Editor));
// Without the edit right nothing of this PC is sent: no "Keep mine", no settings push.
Assert.SequenceEqual([SyncAction.TakeServer, SyncAction.SaveBoth], SyncText.Actions(Entry(SyncStatus.Conflict), Viewer));
Assert.Empty(SyncText.Actions(Entry(SyncStatus.LocalAhead), Viewer));
Assert.SequenceEqual([SyncAction.SyncSettings], SyncText.Actions(Entry(SyncStatus.ServerAhead), Viewer));
// Renders travel on request once the originals agree.
var exports = Link with { ExportsToGet = 3, ExportsToGetBytes = 1_900_000_000, ExportsToSend = 1, ExportsToSendBytes = 600_000_000 };
Assert.SequenceEqual([SyncAction.GetExports, SyncAction.SendExports], SyncText.Actions(Entry(SyncStatus.InSync, exports), Editor));
Assert.SequenceEqual([SyncAction.GetExports], SyncText.Actions(Entry(SyncStatus.InSync, exports), Viewer));
Assert.SequenceEqual([SyncAction.KeepMine, SyncAction.TakeServer, SyncAction.SaveBoth], SyncText.Actions(Entry(SyncStatus.Conflict, exports), Editor));
Assert.Equal(Loc.Plural("Tooltips.Server.Sync.GetExports", 3, Chronos.App.Infrastructure.Formatting.Bytes(1_900_000_000)),
SyncText.ToolTip(Entry(SyncStatus.InSync, exports), SyncAction.GetExports));
Assert.SequenceEqual([SyncAction.Pause], SyncText.Actions(Entry(SyncStatus.Transferring, transfer: Upload(JobState.Running)), Editor));
Assert.SequenceEqual([SyncAction.Pause], SyncText.Actions(Entry(SyncStatus.Transferring, transfer: Upload(JobState.Queued)), Editor));
Assert.SequenceEqual([SyncAction.Resume], SyncText.Actions(Entry(SyncStatus.Transferring, transfer: Upload(JobState.Paused)), Editor));
}
[Test]
public async Task AServerThatKeepsProjectsOpensItsSequencesInTheWorkspace()
{
// Owner review 2, R8, phase 5b: the workspace replaces Open with proxies, and the originals can still come down.
var projects = Editor with { KeepsProjects = true };
Assert.SequenceEqual([SyncAction.OpenInWorkspace, SyncAction.DownloadOriginals], SyncText.Actions(Entry(SyncStatus.RemoteOnly, local: null), projects));
// An account that may not edit only looks, with proxies: it is offered no workspace copy it could not push.
Assert.SequenceEqual([SyncAction.OpenWithProxies], SyncText.Actions(Entry(SyncStatus.ProxiesOnly, local: null), Viewer with { KeepsProjects = true }));
Assert.SequenceEqual([SyncAction.OpenWithProxies], SyncText.Actions(Entry(SyncStatus.RemoteOnly, local: null), Viewer with { KeepsProjects = true }));
Assert.True(SyncText.IsEnabled(SyncAction.OpenInWorkspace, projects), "a server that keeps projects opens them");
Assert.Equal(Loc.L("Tooltips.Server.Sync.OpenInWorkspace"), SyncText.ToolTip(Entry(SyncStatus.RemoteOnly, local: null), SyncAction.OpenInWorkspace, projects));
// An older server's row says why its workspace action is disabled; the other actions are not.
Assert.False(SyncText.IsEnabled(SyncAction.OpenInWorkspace, Editor), "an older server keeps no projects");
Assert.True(SyncText.IsEnabled(SyncAction.OpenWithProxies, Editor), "proxies open as before");
Assert.Equal(Loc.L("Tooltips.Server.Sync.OpenInWorkspace.NoProjects"), SyncText.ToolTip(Entry(SyncStatus.RemoteOnly, local: null), SyncAction.OpenInWorkspace, Editor));
// The row's button is disabled with its reason, and enabled once the server keeps projects.
var acted = new List();
var row = new SyncRow(Entry(SyncStatus.RemoteOnly, local: null), Editor, (_, action) =>
{
acted.Add(action);
return Task.CompletedTask;
});
var workspace = row.Actions.Single(a => a.Action == SyncAction.OpenInWorkspace);
Assert.Equal((false, Loc.L("Tooltips.Server.Sync.OpenInWorkspace.NoProjects")), (workspace.Command.CanExecute(null), workspace.ToolTip));
row.Update(Entry(SyncStatus.RemoteOnly, local: null), projects);
workspace = row.Actions.First();
Assert.Equal((SyncAction.OpenInWorkspace, true), (workspace.Action, workspace.Command.CanExecute(null)));
workspace.Command.Execute(null);
await Task.Yield();
Assert.SequenceEqual([SyncAction.OpenInWorkspace], acted);
}
[Test]
public void ColumnsSayWhatEachSideHolds()
{
var originals = Loc.L("Server.Sync.Originals");
var proxies = Loc.L("Server.Sync.Proxies");
Assert.Equal($"{originals} · {proxies}", SyncText.Local(Entry(SyncStatus.InSync)));
Assert.Equal(originals, SyncText.Local(Entry(SyncStatus.InSync, local: Local with { Status = SequenceStatus.None })));
Assert.Equal(proxies, SyncText.Local(Entry(SyncStatus.ProxiesOnly, Link with { ProxiesCached = 10 }, local: null)));
Assert.Equal(Loc.L("Server.Sync.None"), SyncText.Local(Entry(SyncStatus.RemoteOnly, local: null)));
Assert.Equal($"{originals} · {proxies} · {Loc.Plural("Server.Sync.Exports", 2)}", SyncText.Server(Entry(SyncStatus.InSync)));
Assert.Equal(Loc.L("Server.Sync.None"), SyncText.Server(Entry(SyncStatus.LocalOnly, link: null)));
var uploading = Entry(SyncStatus.Transferring, link: null, transfer: Upload(JobState.Running) with { ProgressDone = 462, ProgressTotal = 1_005 });
Assert.Equal(Loc.L("Server.Sync.Received", Loc.L("Common.Percent", "46")), SyncText.Server(uploading));
Assert.Equal(86_000_000_000, SyncText.Size(Entry(SyncStatus.LocalOnly, link: null), SyncAction.Upload));
Assert.Equal(61_000_000_000, SyncText.Size(Entry(SyncStatus.RemoteOnly, local: null), SyncAction.DownloadOriginals));
}
[Test]
public void StatesPairAGlyphWithAWord()
{
Assert.Equal((StatusKind.Done, Loc.L("Server.Sync.State.InSync")), Kind(Entry(SyncStatus.InSync)));
Assert.Equal((StatusKind.Offline, Loc.L("Server.Sync.State.LocalOnly")), Kind(Entry(SyncStatus.LocalOnly, link: null)));
Assert.Equal((StatusKind.Remote, Loc.L("Server.Sync.State.RemoteOnly")), Kind(Entry(SyncStatus.RemoteOnly, local: null)));
Assert.Equal((StatusKind.Warning, Loc.L("Server.Sync.State.Conflict")), Kind(Entry(SyncStatus.Conflict)));
Assert.Equal((StatusKind.Running, Loc.L("Server.Sync.State.Uploading")), Kind(Entry(SyncStatus.Transferring, transfer: Upload(JobState.Running))));
Assert.Equal((StatusKind.Paused, Loc.L("Server.Sync.State.Paused")), Kind(Entry(SyncStatus.Transferring, transfer: Upload(JobState.Paused))));
Assert.Equal((StatusKind.Running, Loc.L("Server.Sync.State.Downloading")),
Kind(Entry(SyncStatus.Transferring, transfer: Upload(JobState.Running) with { Kind = JobKind.Download })));
}
[Test]
public void AConflictNamesTheSettingsThatDifferInWords()
{
var theirs = new SyncPayload(Local.Title, ["boats"], 0, null, """{"exposure":{"deflickerWindow":{"isAuto":false,"manual":33},"deflicker":{"isAuto":false,"manual":true}}}""");
var conflict = Entry(SyncStatus.Conflict, Link with { ConflictRemote = theirs });
var lines = SyncText.Conflict(conflict, 10);
Assert.SequenceEqual(
[
Loc.L("Server.Conflict.Difference", Loc.L("Server.Setting.Tags"), Loc.L("Server.Sync.None"), "boats"),
Loc.L("Server.Conflict.Difference", Loc.L("Server.Setting.Deflicker"), Loc.L("Server.Setting.Auto"), Loc.L("Server.Setting.Manual")),
Loc.L("Server.Conflict.Difference", Loc.L("Server.Setting.DeflickerWindow"), "41", "33"),
], lines);
// The State tooltip is one line: the first difference and how many more.
Assert.Equal(Loc.L("Server.Conflict.More", lines[0], "2"), SyncText.State(conflict).ToolTip);
Assert.False(SyncText.State(conflict).ToolTip.Contains('\n', StringComparison.Ordinal), "one line");
Assert.Equal(Loc.L("Tooltips.Server.Sync.State.Conflict"), SyncText.ConflictSummary(Entry(SyncStatus.Conflict)));
}
[Test]
public void ASequenceThatCouldNotBeComparedSaysWhy()
{
var (kind, text, tip) = SyncText.State(Entry(SyncStatus.InSync, Link with { Error = "403 forbidden" }));
Assert.Equal((StatusKind.Warning, Loc.L("Server.Sync.State.Failed")), (kind, text));
Assert.Contains("403 forbidden", tip);
}
[Test]
public void PhotosThatFailedToMoveThreeTimesReadNotSentWithTheReason()
{
// Owner review 6 (#73), design §7.3: automatic photos of an integrated pair failed NotSentAfter times in a row.
var entry = Entry(SyncStatus.InSync);
var told = SyncText.State(entry, new SyncMark(null, null, NotSent: new AutoPhotoFailure(SyncAgent.NotSentAfter, "Quota reached")));
Assert.Equal((StatusKind.Warning, Loc.L("Server.Sync.State.NotSent")), (told.Kind, told.Text));
Assert.Equal(Loc.L("Tooltips.Server.Sync.State.NotSent", "Quota reached"), told.ToolTip);
// Fewer failures are still retried quietly; a transfer running now wins.
Assert.Equal(SyncText.State(entry), SyncText.State(entry, new SyncMark(null, null, NotSent: new AutoPhotoFailure(SyncAgent.NotSentAfter - 1, "Quota reached"))));
var moving = Entry(SyncStatus.Transferring, transfer: Upload(JobState.Running));
Assert.Equal(SyncText.State(moving), SyncText.State(moving, new SyncMark(null, null, NotSent: new AutoPhotoFailure(SyncAgent.NotSentAfter, "x"))));
}
[Test]
public void FiltersAndSearchSelectRows()
{
var rows = new[]
{
Entry(SyncStatus.LocalOnly, link: null),
Entry(SyncStatus.RemoteOnly, local: null),
Entry(SyncStatus.ProxiesOnly, local: null),
Entry(SyncStatus.Conflict),
Entry(SyncStatus.InSync, local: Local with { Title = "Comet over the ridge" }),
};
Assert.Equal(5, rows.Count(r => SyncText.Matches(r, SyncFilter.All, string.Empty)));
Assert.Equal(1, rows.Count(r => SyncText.Matches(r, SyncFilter.LocalOnly, string.Empty)));
Assert.Equal(2, rows.Count(r => SyncText.Matches(r, SyncFilter.Remote, string.Empty)));
Assert.Equal(1, rows.Count(r => SyncText.Matches(r, SyncFilter.Conflicts, string.Empty)));
Assert.Equal(1, rows.Count(r => SyncText.Matches(r, SyncFilter.All, "COMET")));
Assert.Equal(0, rows.Count(r => SyncText.Matches(r, SyncFilter.Conflicts, "comet")));
}
[Test]
public void ASettingDifferingOnSeveralPhotosIsOneLineWithTheirCount()
{
var keyframes = new SettingsDifference("pendingFrames.sha:ab12#0.keyframes", "[]", "[{\"x\":1}]");
var lines = SyncText.Differences(
[
new SettingsDifference("pendingFrames.sha:ab12#0.excluded", "true", "false"),
keyframes,
new SettingsDifference("pendingFrames.sha:cd34#0.excluded", "true", "false"),
new SettingsDifference("pendingFrames.sha:ef56#1.excluded", "false", "true"),
new SettingsDifference("pendingFrames.sha:ab12#0.excluded", "true", "false"),
], 10);
// Counted by photo, in the order the settings were met; a setting on one photo keeps its values.
Assert.SequenceEqual(
[Loc.L("Server.Conflict.Count", Loc.L("Server.Setting.ExcludedFrames"), Chronos.App.Infrastructure.Formatting.Count(3)), SyncText.Differences([keyframes], 1)[0]],
lines);
Assert.Contains(Loc.L("Server.Setting.Keyframes"), lines[1]);
Assert.Count(1, SyncText.Differences([new SettingsDifference("pendingFrames.sha:ab12#0.excluded", "true", "false")], 10));
Assert.Contains(Loc.L("Server.Setting.ExcludedFrames"), SyncText.Differences([new SettingsDifference("pendingFrames.sha:ab12#0.excluded", "true", "false")], 10)[0]);
}
[Test]
public void ATransferCountsWhereItsOriginalsAre()
{
var uploadNotOnServer = Entry(SyncStatus.Transferring, transfer: Upload(JobState.Queued)) with { Link = null };
var download = new Job { Id = Guid.CreateVersion7(), Kind = JobKind.Download, State = JobState.Running };
var downloadNotHere = Entry(SyncStatus.Transferring, transfer: download) with { Local = null };
var uploadOnBoth = Entry(SyncStatus.Transferring, transfer: Upload(JobState.Running));
Assert.True(SyncText.Matches(uploadNotOnServer, SyncFilter.LocalOnly, string.Empty));
Assert.False(SyncText.Matches(uploadNotOnServer, SyncFilter.Remote, string.Empty));
Assert.True(SyncText.Matches(downloadNotHere, SyncFilter.Remote, string.Empty));
Assert.False(SyncText.Matches(downloadNotHere, SyncFilter.LocalOnly, string.Empty));
// Partly on both sides: neither "only here" nor "only on the server".
Assert.False(SyncText.Matches(uploadOnBoth, SyncFilter.LocalOnly, string.Empty));
Assert.False(SyncText.Matches(uploadOnBoth, SyncFilter.Remote, string.Empty));
Assert.True(SyncText.Matches(uploadOnBoth, SyncFilter.All, string.Empty));
}
private static (StatusKind, string) Kind(SyncEntry entry)
{
var (kind, text, _) = SyncText.State(entry);
return (kind, text);
}
private static SyncEntry Entry(SyncStatus status, SyncLink? link = null, Sequence? local = null, Job? transfer = null) =>
new(local ?? (status is SyncStatus.RemoteOnly or SyncStatus.ProxiesOnly ? null : Local), link ?? (status == SyncStatus.LocalOnly ? null : Link), status,
transfer);
private static Job Upload(JobState state) => new() { Id = Guid.CreateVersion7(), Kind = JobKind.UploadCommit, State = state };
}