using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Input;
using Chronos.App.Infrastructure;
using Chronos.App.Sections;
using Chronos.App.Server;
using Chronos.App.Shell;
using Chronos.App.Tests.Windows;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Data.Sync;
using Chronos.Testing;
using Chronos.Ui.Controls;
namespace Chronos.App.Tests.Server;
/// The Server screen's sync table on the capture sample data: comparison, transfers, conflict and the Library's glyphs.
[WindowsOnly]
[Serial]
[Timeout(300_000)]
public sealed class ServerScreenSmokeTests
{
[Test]
public Task TheSyncTableShowsEveryStateOfTheSample() => ScreenHost.RunAsync(async () =>
{
await using var app = await ScreenSmokeTests.SampleApp.StartAsync();
var shell = app.Shell;
await app.Sample.FillAsync();
await shell.Library.RefreshAsync();
await shell.Services.Server.RestoreAsync();
Assert.NotNull(await shell.Services.Sync.RefreshAsync());
await app.Sample.CacheProxiesAsync();
var table = shell.Server.Sync;
await table.ReloadAsync();
// 36 sequences here (6 also on the server) and 2 only on the server; the conflict is listed first.
Assert.True(table.IsActive);
Assert.Equal(38, table.Rows.Count);
Assert.Equal("Holy grail · Monviso", table.Rows[0].Title);
Assert.True(table.Rows[0].IsConflict);
Assert.Equal(1, Chip(table, SyncFilter.Conflicts).Count);
Assert.Equal(2, Chip(table, SyncFilter.Remote).Count);
Assert.Equal(30, Chip(table, SyncFilter.LocalOnly).Count);
Assert.Equal(SyncStatus.InSync, Row(table, "Harbour sunset").Entry.Status);
Assert.Equal(SyncStatus.ServerAhead, Row(table, "Construction cam · week 38").Entry.Status);
Assert.SequenceEqual([SyncAction.DownloadOriginals], Actions(Row(table, "Construction cam · week 38")));
Assert.SequenceEqual([SyncAction.Upload], Actions(Row(table, "Milan skyline dusk")));
Assert.SequenceEqual([SyncAction.GetExports], Actions(Row(table, "Star trails, Colle del Nivolet")));
var sunrise = Row(table, "Sunrise over Superga");
Assert.Equal(SyncStatus.ProxiesOnly, sunrise.Entry.Status);
Assert.Equal(Loc.L("Server.Sync.Proxies"), sunrise.LocalText);
// A server without projects: open with proxies; the workspace is offered, disabled with the reason (phase 5b).
Assert.SequenceEqual([SyncAction.OpenWithProxies, SyncAction.DownloadOriginals, SyncAction.OpenInWorkspace], Actions(sunrise));
Assert.SequenceEqual([SyncAction.Upload], Actions(Row(table, "Clouds over the Langhe")));
var conflict = Row(table, "Holy grail · Monviso");
Assert.Equal(StatusKind.Warning, conflict.StateKind);
Assert.SequenceEqual([SyncAction.KeepMine, SyncAction.TakeServer, SyncAction.SaveBoth], Actions(conflict));
// The state whole first (its fixed column cuts longer ones), then what differs.
Assert.Equal(string.Join(Loc.Separator, conflict.StateText, Loc.L("Server.Conflict.Difference", Loc.L("Server.Setting.DeflickerWindow"), "41", "33")),
conflict.StateToolTip);
Assert.NotNull(table.LastSyncText);
Assert.NotNull(table.ArchiveText);
Assert.True(table.UploadAllCommand.CanExecute(null), "the table filled after the view bound");
// Resolving shows the plan first: what differs and which side is overwritten.
var keepMine = SyncTableViewModel.ConflictRequest(conflict.Entry, SyncAction.KeepMine);
Assert.True(keepMine.IsDestructive);
Assert.Contains(Loc.L("Server.Setting.DeflickerWindow"), keepMine.Detail);
Assert.False(SyncTableViewModel.ConflictRequest(conflict.Entry, SyncAction.SaveBoth).IsDestructive);
// The Library shows the real sync state of each local copy, in one word.
var holyGrail = shell.Library.Items.Single(i => i.Title == "Holy grail · Monviso");
Assert.Equal(SyncState.Conflict, holyGrail.Sequence.SyncState);
Assert.Equal(StatusKind.Warning, holyGrail.Statuses[^1].Kind);
Assert.Contains(shell.Library.Items.Single(i => i.Title == "Harbour sunset").Statuses, s => s.Kind == StatusKind.Synced);
var milan = shell.Library.Items.Single(i => i.Title == "Milan skyline dusk").Statuses[^1];
Assert.Equal((StatusKind.Synced, Loc.L("Library.Status.Synced")), (milan.Kind, milan.Text));
shell.Navigate(SectionKey.Server);
await app.ExpectAsync("server");
// Tab walks the row buttons of every row: the rows take no focus, the list passes Tab on to their content.
var list = app.FindAll().Single(l => l.Name == "SyncList");
Assert.Equal(KeyboardNavigationMode.Continue, KeyboardNavigation.GetTabNavigation(list));
var rowButtons = app.FindAll