using Febe.Browser.Cdp; 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); } [TestMethod] [Timeout(240_000, CooperativeCancellation = true)] public async Task WithRichiesteClosingTheWindowCopiesTheSessionBeforeThePageGoesBack() { // Issue #36, S4b (answer 5 of 18 November 2026): the window «Browser di Febe» closed with its X, without // «Ho finito», copies the session from the site's page into the saved session, under Alberto's lease. var ct = TestContext.CancellationToken; await using var bed = await DesktopBed.StartAsync("finestra-copia", autoLogin: false, ct, ["--raccolta", "richieste"]); var app = bed.App; await app.Running.WaitAsync(TimeSpan.FromSeconds(120), ct); var connection = app.Connection!; var browser = bed.Browser; var savedSession = app.SavedSession ?? throw new AssertFailedException("the test site's catalog has configured operations"); var window = await bed.Ui.InvokeAsync(() => browser.Host.Window) ?? throw new AssertFailedException("nessuna finestra"); // «Accedi» (the welcome page starts the test site's session), then Alberto goes to the home of the site. await connection.BeginUserDrivingAsync(openLogin: true, ct); await browser.Cdp.NavigateAsync(bed.Site.Url("/"), ct); await connection.RefreshAsync(ct); Assert.AreEqual(ConnectionState.SessionCopyMissing, (await connection.GetStatusAsync(ct)).State, "the cookie alone is not «Collegato» with «richieste»"); await bed.Ui.InvokeAsync(() => { window.Close(); return true; }); await bed.WaitUntilAsync(() => Task.FromResult(savedSession.Read() is not null), TimeSpan.FromSeconds(30), "la copia alla chiusura della finestra", ct); var saved = savedSession.Read()!; Assert.AreEqual(Febe.SitoDiProva.TestSite.SpinBranch, saved.Fields["spin_b"]); Assert.AreEqual(Febe.SitoDiProva.TestSite.EntryNonce, saved.Fields["entry_nonce"]); Assert.IsFalse(await bed.Ui.InvokeAsync(() => window.IsOnScreen)); Assert.AreEqual(ConnectionState.Connected, (await connection.GetStatusAsync(ct)).State); Assert.IsTrue(bed.Lines.Any("Finestra del browser chiusa: sessione copiata"), bed.Lines.Text); Assert.IsFalse(bed.Lines.Any(Febe.SitoDiProva.TestSite.Lsd), "no value in the log"); Assert.IsFalse(bed.Lines.Any("ERRORE"), bed.Lines.Text); await connection.CancelAsync(ct); } }