using System.Net.Http; using Febe.App; using Febe.Core.Domain; using Febe.Desktop.Browser; using Febe.Tests.Windows.Support; namespace Febe.Tests.Windows; /// /// One real collection session of the shared composition () running on WebView2, as Febe.exe /// composes it, against the local test site, with files arriving in a temporary library. The Linux suite has the /// equivalent with Chromium (ServerEndToEndTests). Never Instagram: the browser only sees 127.0.0.1. /// [TestClass] public sealed class WebView2CollectionTests { public TestContext TestContext { get; set; } = null!; [TestInitialize] public void RequireWebView2() => WebView2Runtime.Require(); [TestMethod] [Timeout(420_000, CooperativeCancellation = true)] public async Task ARealSessionOfTheCompositionRunsOnWebView2AndTheFilesArriveInTheLibrary() { var ct = TestContext.CancellationToken; await using var bed = await DesktopBed.StartAsync("raccolta", autoLogin: true, ct); var app = bed.App; Assert.IsEmpty(app.Problems, string.Join("; ", app.Problems.Select(p => p.Title))); // The browser starts from a thread of the composition (the path that failed on Alberto's PC), the pipeline and // the engine follow. await app.Running.WaitAsync(TimeSpan.FromSeconds(120), ct); Assert.IsInstanceOfType(app.Browser); Assert.IsTrue(bed.Lines.Any("Browser di Febe pronto: WebView2 "), bed.Lines.Text); Assert.IsFalse(bed.Lines.Any("non parte"), bed.Lines.Text); Assert.AreEqual(Febe.Web.Connection.ConnectionState.Connected, (await app.Connection!.GetStatusAsync(ct)).State, "the test site's automatic login"); // Accounts and «Controlla ora» through the interface, as from the main window. var added = await bed.JsonAsync(HttpMethod.Post, "/api/accounts", """{"usernames":["marta.prova","luca.prova"],"start":"everything"}""", ct); Assert.AreEqual(2, added.GetProperty("added").GetInt32()); var marta = await app.Database.Accounts.FindByUsernameAsync("marta.prova", ct) ?? throw new AssertFailedException("marta.prova manca"); var check = await bed.JsonAsync(HttpMethod.Post, $"/api/accounts/{marta.Id}/check-now", null, ct); if (check.GetProperty("status").GetString() == "needsConfirmation") { check = await bed.JsonAsync(HttpMethod.Post, $"/api/accounts/{marta.Id}/check-now", $$"""{"token":"{{check.GetProperty("token").GetString()}}"}""", ct); } Assert.AreEqual("queued", check.GetProperty("status").GetString(), check.GetProperty("message").GetString()); // A session of the engine acts on the site through WebView2: every action has its Governor's permit. await bed.WaitUntilAsync( async () => (await app.Database.ActionLog.ListAsync(DateTimeOffset.UnixEpoch, bed.Clock.Now.AddDays(1), ct)).Any(a => a.AccountId == marta.Id), TimeSpan.FromSeconds(120), "un'azione del motore su marta.prova nel registro delle azioni", ct); // Files arrive in the library, complete, downloaded through the browser from the test site's CDN. var library = app.Library ?? throw new AssertFailedException("nessuna libreria"); List stored = []; await bed.WaitUntilAsync( async () => { stored = await StoredAsync(app, marta.Id, ct); return stored.Count >= 3; }, TimeSpan.FromSeconds(180), "tre file di marta.prova nella libreria", ct); foreach (var media in stored) { var path = library.FullPath(marta, media.RelativePath!); Assert.IsTrue(File.Exists(path), path); Assert.AreEqual(media.Bytes, new FileInfo(path).Length, path); StringAssert.StartsWith(path, bed.Folder.Path, "inside the temporary library"); } var actions = await app.Database.ActionLog.ListAsync(DateTimeOffset.UnixEpoch, bed.Clock.Now.AddDays(1), ct); Assert.IsTrue(actions.All(a => a.SessionId is not null), "every counted action belongs to a session"); TestContext.WriteLine($"File di marta.prova: {stored.Count}; azioni: {actions.Count}; CDN: {bed.Site.Control.CdnFilesServed} file serviti."); TestContext.WriteLine(bed.Lines.Text); Assert.IsFalse(bed.Lines.Any("non parte"), "the browser never failed to start"); Assert.IsFalse(bed.Lines.Any("firma="), "no signed address in the log"); } private static async Task> StoredAsync(FebeApp app, long accountId, CancellationToken ct) { var list = new List(); foreach (var entry in await app.Database.Entries.ListByAccountAsync(accountId, null, ct)) { foreach (var media in await app.Database.Media.ListByEntryAsync(entry.Id, ct)) { if (media.State == MediaState.Downloaded && media.RelativePath is not null && media.Sha256 is not null && entry.Kind != EntryKind.ProfilePicture) { list.Add(media); } } } return list; } }