using Titano.App.Sections;
using Titano.App.Server;
using Titano.App.Shell;
using Titano.App.Tests.Windows;
using Titano.Core.Model;
using Titano.Data.Sync;
using Titano.Testing;
using Titano.Ui.Controls;
namespace Titano.App.Tests.Server;
/// The Server screen's sync table on the capture sample data: comparison, transfer, 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());
var table = shell.Server.Sync;
await table.ReloadAsync();
// 36 sequences here (4 also on the server) and 2 only on the server.
Assert.True(table.IsActive);
Assert.Equal(38, table.Rows.Count);
Assert.Equal(1, Chip(table, SyncFilter.Conflicts).Count);
Assert.Equal(2, Chip(table, SyncFilter.Remote).Count);
Assert.Equal(32, 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], Row(table, "Construction cam · week 38").Actions.Select(a => a.Action));
Assert.SequenceEqual([SyncAction.OpenWithProxies, SyncAction.DownloadOriginals], Row(table, "Sunrise over Superga").Actions.Select(a => a.Action));
Assert.SequenceEqual([SyncAction.Upload], Row(table, "Clouds over the Langhe").Actions.Select(a => a.Action));
var conflict = Row(table, "Holy grail · Monviso");
Assert.Equal(StatusKind.Warning, conflict.StateKind);
Assert.SequenceEqual([SyncAction.KeepMine, SyncAction.TakeServer, SyncAction.SaveBoth], conflict.Actions.Select(a => a.Action));
Assert.Contains("41", conflict.StateToolTip);
Assert.NotNull(table.LastSyncText);
Assert.NotNull(table.ArchiveText);
// The Library shows the real sync state of each local copy.
var holyGrail = shell.Library.Items.Single(i => i.Title == "Holy grail · Monviso");
Assert.Equal(SyncState.Conflict, holyGrail.Sequence.SyncState);
Assert.Contains(holyGrail.Statuses, s => s.Kind == StatusKind.Warning);
Assert.Contains(shell.Library.Items.Single(i => i.Title == "Harbour sunset").Statuses, s => s.Kind == StatusKind.Synced);
shell.Navigate(SectionKey.Server);
await app.ExpectAsync("server");
// An upload in progress: the row shows what the server received and offers Pause.
await app.Sample.FillSyncAsync();
await table.ReloadAsync();
var comet = Row(table, "Comet over the ridge");
Assert.Equal(SyncStatus.Transferring, comet.Entry.Status);
Assert.SequenceEqual([SyncAction.Pause], comet.Actions.Select(a => a.Action));
Assert.Contains("46", comet.ServerText);
await app.ExpectAsync("server-sync");
// The conflict filter leaves the one row waiting for a choice.
Chip(table, SyncFilter.Conflicts).SelectCommand.Execute(null);
Assert.Equal("Holy grail · Monviso", Assert.Contains(table.Rows, _ => true).Title);
Assert.Count(1, table.Rows);
await app.ExpectAsync("server-conflict");
table.SearchText = "nothing like this";
Assert.False(table.HasRows);
Assert.NotNull(table.EmptyText);
});
[Test]
public Task SigningOutEmptiesTheTable() => ScreenHost.RunAsync(async () =>
{
await using var app = await ScreenSmokeTests.SampleApp.StartAsync();
var shell = app.Shell;
await app.Sample.FillAsync();
await shell.Services.Server.RestoreAsync();
await shell.Services.Sync.RefreshAsync();
await shell.Server.Sync.ReloadAsync();
Assert.True(shell.Server.Sync.HasRows);
await shell.Services.Server.SignOutAsync();
Assert.Null(await shell.Services.Sync.RefreshAsync());
await shell.Server.Sync.ReloadAsync();
Assert.False(shell.Server.Sync.IsActive);
Assert.False(shell.Server.Sync.HasRows);
shell.Navigate(SectionKey.Server);
await app.ExpectAsync("server-signedout-table");
});
private static SyncFilterChip Chip(SyncTableViewModel table, SyncFilter filter) => table.Chips.Single(c => c.Filter == filter);
private static SyncRow Row(SyncTableViewModel table, string title) => table.Rows.Single(r => r.Title == title);
}