using Febe.Core.Storage; using Febe.Tests.Windows.Support; using Febe.Web.Connection; namespace Febe.Tests.Windows; /// /// The login on Windows through the real connection service of the composition (spec §4.1, docs/windows.md «Il primo /// collegamento a Instagram»), on WebView2 and the test site: «Accedi a Instagram» opens the login page in the window /// «Browser di Febe», which comes on screen while the engine is still; «Ho finito» checks the session (the cookie's name /// only) and puts the window back out of view; «Scollega» deletes the profile with the session and the browser comes /// back empty. The window comes on screen for a moment, as for Alberto. /// [TestClass] public sealed class WebView2ConnectionTests { public TestContext TestContext { get; set; } = null!; [TestInitialize] public void RequireWebView2() => WebView2Runtime.Require(); [TestMethod] [Timeout(240_000, CooperativeCancellation = true)] public async Task TheLoginBringsTheWindowAndScollegaEmptiesTheProfile() { var ct = TestContext.CancellationToken; await using var bed = await DesktopBed.StartAsync("collegamento", autoLogin: false, ct); var app = bed.App; await app.Running.WaitAsync(TimeSpan.FromSeconds(120), ct); var connection = app.Connection!; var browser = bed.Browser; Assert.AreEqual(ConnectionState.NotConnected, (await connection.GetStatusAsync(ct)).State, "a new profile is not connected"); var window = await bed.Ui.InvokeAsync(() => browser.Host.Window) ?? throw new AssertFailedException("nessuna finestra"); Assert.IsFalse(await bed.Ui.InvokeAsync(() => window.IsOnScreen)); // «Accedi a Instagram» (here the test site's welcome page, whose first visit starts its session). await connection.BeginUserDrivingAsync(openLogin: true, ct); var driving = await connection.GetStatusAsync(ct); Assert.IsTrue(driving.UserIsDriving); Assert.IsTrue(browser.UserIsDriving, "the engine is still while Alberto is at the browser"); Assert.IsTrue(await bed.Ui.InvokeAsync(() => window.IsOnScreen), "the window «Browser di Febe» came on screen"); StringAssert.StartsWith(driving.CurrentUrl, bed.Site.SiteUrl, "the login page of the configured site, never Instagram in tests"); // «Ho finito». var finish = await connection.FinishAsync(ct); Assert.IsTrue(finish.Connected, finish.Message); Assert.IsFalse(browser.UserIsDriving, "«Ho finito» ends the driving"); Assert.IsFalse(await bed.Ui.InvokeAsync(() => window.IsOnScreen), "and puts the window back out of view"); Assert.AreEqual(ConnectionState.Connected, (await connection.GetStatusAsync(ct)).State); Assert.IsNotNull(await app.Database.Settings.GetInt64Async(Febe.App.BrowserConnectionService.FirstConnectionKey, ct), "first connection recorded"); // «Scollega»: engine stopped, WebView2 closed, profile deleted, browser started again empty, engine started again. var marker = Path.Combine(browser.ProfileDirectory, "segno-della-prova"); await File.WriteAllTextAsync(marker, "x", ct); await connection.DisconnectAsync(ct); Assert.IsFalse(File.Exists(marker), "the profile folder was deleted"); Assert.IsTrue(browser.IsAvailable, "the browser runs again, with an empty profile"); Assert.AreEqual(ConnectionState.NotConnected, (await connection.GetStatusAsync(ct)).State); Assert.IsFalse(browser.UserIsDriving); await bed.WaitUntilAsync(() => Task.FromResult(app.Engine!.Services.Runtime.IsRunning), TimeSpan.FromSeconds(10), "il motore di nuovo in funzione", ct); TestContext.WriteLine(bed.Lines.Text); Assert.IsTrue(bed.Lines.Any("«Scollega»: motore fermo, profilo del browser cancellato, browser ripartito vuoto."), bed.Lines.Text); Assert.IsFalse(bed.Lines.Any("non parte"), bed.Lines.Text); Assert.IsFalse(bed.Lines.Any("ERRORE"), bed.Lines.Text); } }