From 8edb706e0651384a405402efc82c285c15a05776 Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Sun, 29 Nov 2026 11:57:57 +0100 Subject: [PATCH] Correzione: il browser che non risponde ancora si aspetta, poi si apre l'accesso (#67) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Subito dopo «Scollega» il browser riparte con il profilo vuoto e, sotto carico, può non dire ancora quale pagina mostra. Febe prendeva l'indirizzo sconosciuto per una pagina di Alberto e lasciava la finestra bianca (about:blank), contro la #52. Decisione di Alberto del 29 novembre 2026: quando l'indirizzo è sconosciuto si richiede al browser ogni 200 ms, al massimo per 10 secondi (AddressWait); se resta muto la pagina conta come quella di riposo e si apre la pagina iniziale (l'accesso, senza sessione). Una pagina nota che Alberto usa non si tocca mai; l'attesa finisce con la richiesta. Il finto browser delle prove può restare muto (Silent): quattro prove nuove per la risposta tardiva, il silenzio oltre l'attesa, la pagina di Alberto lasciata com'è e l'annullamento durante l'attesa. Co-Authored-By: Claude Opus 5.5 --- src/Febe.App/BrowserConnectionService.cs | 68 ++++++++++++- .../Desktop/BrowserStartPageTests.cs | 95 +++++++++++++++++++ .../Desktop/Support/FakeDesktopShell.cs | 21 ++++ 3 files changed, 179 insertions(+), 5 deletions(-) diff --git a/src/Febe.App/BrowserConnectionService.cs b/src/Febe.App/BrowserConnectionService.cs index ab14d3d..9d24cf9 100644 --- a/src/Febe.App/BrowserConnectionService.cs +++ b/src/Febe.App/BrowserConnectionService.cs @@ -189,6 +189,17 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa /// public TimeSpan PageTurnWait { get; set; } = TimeSpan.FromSeconds(15); + /// + /// How long Alberto's first navigation waits, at most, for the browser to say which page it shows when it does not + /// answer (real time): right after «Scollega» it has just restarted with an empty profile and may not answer yet. + /// After it the page counts as the rest page, and the start page opens (issue #67, decision of 29 November 2026): + /// never a white window (issue #52). Read at every wait; tests shorten it. + /// + public TimeSpan AddressWait { get; set; } = TimeSpan.FromSeconds(10); + + /// How often the wait of asks the browser again. + private static readonly TimeSpan AddressPoll = TimeSpan.FromMilliseconds(200); + /// The engine, once it exists (it is built after the browser). public void AttachEngine(EngineHost engine) => _engine = engine ?? throw new ArgumentNullException(nameof(engine)); @@ -1083,14 +1094,23 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa /// /// Alberto's own first navigation (rule 7: not an engine action, no Governor), under his lease: the login page when /// asked for, and the start page when the browser rests on the empty page (issue #52). Any other page stays as it is - /// — the one he was using, or the verification page of a stop. + /// — the one he was using, or the verification page of a stop. A browser that does not say its page is waited for, + /// up to ; still silent, its page counts as the rest page (issue #67). /// private async Task OpenForAlbertoAsync(bool openLogin, CancellationToken ct) { - if (!openLogin && !IsRest(await FullUrlAsync(ct).ConfigureAwait(false))) + // A known address that is not the rest page is a page Alberto uses: never touched. An unknown one (the browser + // still silent after the wait) is the rest page. + var answered = true; + if (!openLogin) { - _log.Info("collegamento", "Alberto usa il browser."); - return; + var first = await AwaitFullUrlAsync(ct).ConfigureAwait(false); + answered = first is not null; + if (answered && !IsRest(first)) + { + _log.Info("collegamento", "Alberto usa il browser."); + return; + } } // The engine ends its session when the lease starts; wait for it before taking the page. A search step in @@ -1104,7 +1124,7 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa target = _loginUrl; what = "pagina di accesso"; } - else if (IsRest(await FullUrlAsync(ct).ConfigureAwait(false))) + else if (await IsAtRestAsync(wait: answered, ct).ConfigureAwait(false)) { target = StartPageUrl; what = target == _loginUrl ? "era a riposo, pagina di accesso" : "era a riposo, pagina iniziale del sito"; @@ -1267,6 +1287,44 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa } } + /// + /// The address of the main frame as gives it, but a browser that does not answer is asked + /// again every until it does, for at most (real time, issue #67); + /// null when it stayed silent. Cancelled with . + /// + private async Task AwaitFullUrlAsync(CancellationToken ct) + { + using var limit = CancellationTokenSource.CreateLinkedTokenSource(ct); + limit.CancelAfter(AddressWait); + try + { + while (true) + { + if (await FullUrlAsync(limit.Token).ConfigureAwait(false) is { } url) + { + return url; + } + + await _clock.Delay(AddressPoll, limit.Token).ConfigureAwait(false); + } + } + catch (OperationCanceledException) when (!ct.IsCancellationRequested) + { + _log.Info("collegamento", "Il browser non ha indicato la pagina in tempo: la pagina conta come quella di riposo."); + return null; + } + } + + /// + /// After the engine's session ended: whether the page is the rest page (or still unknown). A browser that already let + /// pass is asked once more ( false), not waited for again. + /// + private async Task IsAtRestAsync(bool wait, CancellationToken ct) + { + var url = wait ? await AwaitFullUrlAsync(ct).ConfigureAwait(false) : await FullUrlAsync(ct).ConfigureAwait(false); + return url is null || IsRest(url); + } + private async Task WaitForSessionEndAsync(CancellationToken ct) { if (_engine is not { } engine) diff --git a/tests/Febe.Tests/Desktop/BrowserStartPageTests.cs b/tests/Febe.Tests/Desktop/BrowserStartPageTests.cs index c34947f..5ae871f 100644 --- a/tests/Febe.Tests/Desktop/BrowserStartPageTests.cs +++ b/tests/Febe.Tests/Desktop/BrowserStartPageTests.cs @@ -54,6 +54,101 @@ public sealed class BrowserStartPageTests Assert.IsTrue(status.GetProperty("userIsDriving").GetBoolean()); } + [TestMethod] + [Timeout(60_000, CooperativeCancellation = true)] + public async Task ABrowserThatDoesNotAnswerYetIsWaitedForAndThenOpensTheLogin() + { + var ct = TestContext.CancellationToken; + await using var bed = await DesktopTestBed.StartAsync(); + await bed.App.Running.WaitAsync(TimeSpan.FromSeconds(20), ct); + var browser = bed.Browser!; + + // Just restarted (after «Scollega», issue #67): the browser does not say its page yet. Febe asks again and waits. + browser.FakeCdp.Silent = true; + var asked = browser.FakeCdp.SilentAnswers; + var begin = BeginAsync(bed, openLogin: false); + await DesktopTestBed.WaitUntilAsync(() => browser.FakeCdp.SilentAnswers >= asked + 3, TimeSpan.FromSeconds(5), "le domande ripetute al browser che non risponde"); + Assert.IsFalse(begin.IsCompleted, "still waiting for the browser"); + Assert.DoesNotContain(Login, browser.FakeCdp.Visited); + + // It answers, on the empty rest page: the login page, never a white window (issue #52). + browser.FakeCdp.Silent = false; + await begin.WaitAsync(TimeSpan.FromSeconds(10), ct); + Assert.AreEqual(Login, browser.FakeCdp.Url); + } + + [TestMethod] + [Timeout(60_000, CooperativeCancellation = true)] + public async Task ABrowserSilentBeyondTheWaitCountsAsAtRestAndOpensTheLogin() + { + var ct = TestContext.CancellationToken; + await using var bed = await DesktopTestBed.StartAsync(); + await bed.App.Running.WaitAsync(TimeSpan.FromSeconds(20), ct); + var browser = bed.Browser!; + var connection = bed.App.Connection!; + connection.AddressWait = TimeSpan.FromMilliseconds(400); + + browser.FakeCdp.Silent = true; + var watch = System.Diagnostics.Stopwatch.StartNew(); + await BeginAsync(bed, openLogin: false); + watch.Stop(); + + Assert.IsGreaterThanOrEqualTo(TimeSpan.FromMilliseconds(350), watch.Elapsed, "it waited for the browser first"); + Assert.AreEqual(Login, browser.FakeCdp.Url, "still silent: the rest page, and the login opens"); + Assert.AreEqual(1, browser.FakeCdp.Visited.Count(v => v == Login)); + } + + [TestMethod] + [Timeout(60_000, CooperativeCancellation = true)] + public async Task ABrowserThatAnswersLateWithAPageOfAlbertosLeavesItAlone() + { + var ct = TestContext.CancellationToken; + await using var bed = await DesktopTestBed.StartAsync(); + await bed.App.Running.WaitAsync(TimeSpan.FromSeconds(20), ct); + var browser = bed.Browser!; + await BeginAsync(bed, openLogin: false); + browser.FakeCdp.Navigated("https://www.instagram.com/marta.prova/"); + var navigations = Navigations(browser); + + browser.FakeCdp.Silent = true; + var asked = browser.FakeCdp.SilentAnswers; + var begin = BeginAsync(bed, openLogin: false); + await DesktopTestBed.WaitUntilAsync(() => browser.FakeCdp.SilentAnswers >= asked + 2, TimeSpan.FromSeconds(5), "le domande ripetute al browser che non risponde"); + browser.FakeCdp.Silent = false; + await begin.WaitAsync(TimeSpan.FromSeconds(10), ct); + + Assert.AreEqual("https://www.instagram.com/marta.prova/", browser.FakeCdp.Url); + Assert.AreEqual(navigations, Navigations(browser), "no navigation of his page"); + } + + [TestMethod] + [Timeout(60_000, CooperativeCancellation = true)] + public async Task TheWaitForASilentBrowserEndsWithTheRequest() + { + var ct = TestContext.CancellationToken; + await using var bed = await DesktopTestBed.StartAsync(); + await bed.App.Running.WaitAsync(TimeSpan.FromSeconds(20), ct); + var browser = bed.Browser!; + var connection = bed.App.Connection!; + var navigations = Navigations(browser); + + browser.FakeCdp.Silent = true; + var asked = browser.FakeCdp.SilentAnswers; + using var request = new CancellationTokenSource(); + var begin = connection.BeginUserDrivingAsync(openLogin: false, request.Token); + await DesktopTestBed.WaitUntilAsync(() => browser.FakeCdp.SilentAnswers >= asked + 2, TimeSpan.FromSeconds(5), "le domande ripetute al browser che non risponde"); + await request.CancelAsync(); + + await Assert.ThrowsAsync(() => begin.WaitAsync(TimeSpan.FromSeconds(10), ct)); + Assert.AreEqual(navigations, Navigations(browser), "nothing opened after the request went away"); + Assert.AreEqual("about:blank", browser.FakeCdp.Url); + + // The commands of the connection are free again: the next request, with the browser answering, opens the login. + browser.FakeCdp.Silent = false; + await BeginAsync(bed, openLogin: false); + Assert.AreEqual(Login, browser.FakeCdp.Url); + } + [TestMethod] [Timeout(60_000, CooperativeCancellation = true)] public async Task ApriIlBrowserBringsTheWindowWithItsStartPageAndTheLoginAfterADisconnection() diff --git a/tests/Febe.Tests/Desktop/Support/FakeDesktopShell.cs b/tests/Febe.Tests/Desktop/Support/FakeDesktopShell.cs index f4fca2c..1313672 100644 --- a/tests/Febe.Tests/Desktop/Support/FakeDesktopShell.cs +++ b/tests/Febe.Tests/Desktop/Support/FakeDesktopShell.cs @@ -263,6 +263,8 @@ internal sealed class FakeCdp : ICdpSession private readonly Dictionary>> _handlers = new(StringComparer.Ordinal); private int _captureReads; private int _cookieReads; + private bool _silent; + private int _silentAnswers; public bool LoggedIn { get; set; } @@ -313,6 +315,19 @@ internal sealed class FakeCdp : ICdpSession public string Url { get; private set; } = "about:blank"; + /// + /// While true the page does not say where it is, like a browser that has just restarted and does not answer yet + /// (issue #67): Page.getFrameTree fails as a closed connection, and is counted in . + /// + public bool Silent + { + get => Volatile.Read(ref _silent); + set => Volatile.Write(ref _silent, value); + } + + /// How many times Page.getFrameTree was asked while . + public int SilentAnswers => Volatile.Read(ref _silentAnswers); + public ConcurrentQueue Methods { get; } = new(); /// Every address a Page.navigate asked for, in order. @@ -329,6 +344,12 @@ internal sealed class FakeCdp : ICdpSession { CdpCommandPolicy.EnsureAllowed(method); Methods.Enqueue(method); + if (method == "Page.getFrameTree" && Silent) + { + Interlocked.Increment(ref _silentAnswers); + return Task.FromException(new CdpDisconnectedException()); + } + JsonNode result = method switch { "Page.getFrameTree" => new JsonObject { ["frameTree"] = new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1", ["url"] = Url } } },