Pagine aperte da Alberto non sono segnali (regole-1); cookie perso fuori sessione = disconnessione (regole-2); pagina a riposo quando Alberto lascia il browser (regole-6); la lease di «Accedi»/«Usa il browser» finisce da sola dopo 2 minuti senza vista remota né finestra (websec-1); ping con scadenza sulla vista remota (websec-5); aperture della vista remota serializzate (websec-6); orologio virtuale senza fine anticipata (engine-1); la sincronizzazione della coda non duplica il compito in corso (engine-2); raccolte in evidenza programmate (sched-1). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
189 lines
9.8 KiB
C#
189 lines
9.8 KiB
C#
using System.Net;
|
|
using Febe.App;
|
|
using Febe.Core.Abstractions;
|
|
using Febe.Core.Collection;
|
|
using Febe.Core.Engine;
|
|
using Febe.Core.Storage;
|
|
using Febe.Tests.Desktop.Support;
|
|
using Febe.Tests.Support;
|
|
|
|
namespace Febe.Tests.Desktop;
|
|
|
|
/// <summary>
|
|
/// The link to Instagram in the shared composition (spec §4.1, §7.6, rule 7 of §2), with the real Instagram rules of
|
|
/// <c>segnali.json</c> and a fake browser that only answers by itself: the pages Alberto opens are not stop signals, a
|
|
/// session lost outside a session is a disconnection, and the «driving» lease of the connection page does not outlive
|
|
/// the browser window. Never Instagram.
|
|
/// </summary>
|
|
[TestClass]
|
|
public sealed class ConnectionLifecycleTests
|
|
{
|
|
public TestContext TestContext { get; set; } = null!;
|
|
|
|
[TestMethod]
|
|
[Timeout(60_000, CooperativeCancellation = true)]
|
|
public async Task TheLoginPageAlbertoOpensIsNotADisconnectionButTheEnginesPageGoingBackToItIs()
|
|
{
|
|
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 breaker = bed.App.Engine!.Services.Breaker;
|
|
|
|
// «Accedi a Instagram»: the real login page, which segnali.json calls «login-url» (a disconnection).
|
|
using (var begin = await bed.SendAsync(HttpMethod.Post, "/api/connection/begin", """{"openLogin":true}"""))
|
|
{
|
|
Assert.AreEqual(HttpStatusCode.NoContent, begin.StatusCode);
|
|
}
|
|
|
|
Assert.AreEqual("https://www.instagram.com/accounts/login/", browser.FakeCdp.Url);
|
|
|
|
// Instagram asks the new device to confirm during his login: his to complete, not the engine's (rule 7).
|
|
browser.FakeCdp.Navigated("https://www.instagram.com/challenge/AbC123/");
|
|
await breaker.FlushAsync();
|
|
Assert.AreEqual(BreakerStatus.Closed, breaker.State.Status, "the pages Alberto opens are not stop signals");
|
|
Assert.IsEmpty(await bed.App.Database.Incidents.ListOpenAsync(ct));
|
|
|
|
browser.FakeCdp.LoggedIn = true;
|
|
var finish = await bed.JsonAsync(HttpMethod.Post, "/api/connection/finish");
|
|
Assert.IsTrue(finish.GetProperty("connected").GetBoolean());
|
|
Assert.DoesNotContain("Riprendi", finish.GetProperty("message").GetString()!, "nothing to resume after a plain login");
|
|
|
|
// Without Alberto at the browser, the same address is the page going back to the login (§7.6).
|
|
browser.FakeCdp.Navigated("https://www.instagram.com/accounts/login/");
|
|
await breaker.FlushAsync();
|
|
Assert.AreEqual(BreakerStatus.Disconnected, breaker.State.Status);
|
|
Assert.AreEqual("login-url", (await bed.App.Database.Incidents.ListOpenAsync(ct)).Single().Signal);
|
|
}
|
|
|
|
[TestMethod]
|
|
[Timeout(90_000, CooperativeCancellation = true)]
|
|
public async Task ASessionLostOutsideASessionStopsTheEngineUntilResumeButScollegaDoesNot()
|
|
{
|
|
var ct = TestContext.CancellationToken;
|
|
using var temp = new TempFolder("collegamento");
|
|
var locations = new MemoryLocationStore();
|
|
await using (var bed = await DesktopTestBed.StartAsync(temp: temp, locations: locations))
|
|
{
|
|
await bed.App.Running.WaitAsync(TimeSpan.FromSeconds(20), ct);
|
|
var engine = bed.App.Engine!;
|
|
var breaker = engine.Services.Breaker;
|
|
await breaker.FlushAsync();
|
|
Assert.AreEqual(BreakerStatus.Closed, breaker.State.Status, "never connected: no session is no news");
|
|
|
|
await LoginAsync(bed);
|
|
|
|
// The page at rest, Alberto away: the session cookie goes (expired, dropped by Instagram). The background
|
|
// check notices: a disconnection, with its incident and its line in Attività (the alert follows it).
|
|
bed.Browser!.FakeCdp.LoggedIn = false;
|
|
await bed.App.Connection!.RefreshAsync(ct);
|
|
await breaker.FlushAsync();
|
|
Assert.AreEqual(BreakerStatus.Disconnected, breaker.State.Status);
|
|
Assert.AreEqual(SignalRules.SessionCookieMissingCode, (await bed.App.Database.Incidents.ListOpenAsync(ct)).Single().Signal);
|
|
var events = await bed.App.Database.Events.ListAsync(new EventQuery(), ct);
|
|
Assert.IsTrue(events.Any(e => e.Code == EngineEventCodes.DisconnectStop), "the «disconnessione» line that the alerts follow");
|
|
Assert.AreEqual("verificationRequired", (await bed.JsonAsync(HttpMethod.Get, "/api/connection")).GetProperty("state").GetString());
|
|
|
|
// A new login is not enough: «Riprendi», with the gradual start, restarts the engine.
|
|
var again = await LoginAsync(bed);
|
|
StringAssert.Contains(again, "premi «Riprendi»");
|
|
Assert.IsTrue((await engine.ResumeAsync(ct)).Resumed);
|
|
Assert.AreEqual(BreakerStatus.Closed, breaker.State.Status);
|
|
await bed.StopAsync();
|
|
}
|
|
|
|
// Next start: the profile lost the session meanwhile (a crash, S3). It had been connected: a disconnection.
|
|
await using (var bed = await DesktopTestBed.StartAsync(temp: temp, locations: locations))
|
|
{
|
|
await bed.App.Running.WaitAsync(TimeSpan.FromSeconds(20), ct);
|
|
var engine = bed.App.Engine!;
|
|
await engine.Services.Breaker.FlushAsync();
|
|
Assert.AreEqual(BreakerStatus.Disconnected, engine.Services.Breaker.State.Status, "missing at start-up after a connection");
|
|
Assert.IsTrue((await engine.ResumeAsync(ct)).Resumed);
|
|
|
|
// «Scollega»: the session goes because Alberto wants so. Not a disconnection, now or at the next start.
|
|
await LoginAsync(bed);
|
|
using (var disconnect = await bed.SendAsync(HttpMethod.Post, "/api/connection/disconnect"))
|
|
{
|
|
Assert.AreEqual(HttpStatusCode.NoContent, disconnect.StatusCode);
|
|
}
|
|
|
|
await bed.App.Connection!.RefreshAsync(ct);
|
|
await engine.Services.Breaker.FlushAsync();
|
|
Assert.AreEqual(BreakerStatus.Closed, engine.Services.Breaker.State.Status);
|
|
Assert.AreEqual("notConnected", (await bed.JsonAsync(HttpMethod.Get, "/api/connection")).GetProperty("state").GetString());
|
|
await bed.StopAsync();
|
|
}
|
|
|
|
await using (var bed = await DesktopTestBed.StartAsync(temp: temp, locations: locations))
|
|
{
|
|
await bed.App.Running.WaitAsync(TimeSpan.FromSeconds(20), ct);
|
|
await bed.App.Engine!.Services.Breaker.FlushAsync();
|
|
Assert.AreEqual(BreakerStatus.Closed, bed.App.Engine.Services.Breaker.State.Status, "after «Scollega» a missing session is expected");
|
|
Assert.IsEmpty(await bed.App.Database.Incidents.ListOpenAsync(ct));
|
|
await bed.StopAsync();
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
[Timeout(60_000, CooperativeCancellation = true)]
|
|
public async Task TheConnectionLeaseEndsSoonAfterTheBrowserWindowWithoutHoFinito()
|
|
{
|
|
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.DrivingGrace = TimeSpan.FromMilliseconds(500);
|
|
await LoginAsync(bed);
|
|
|
|
// «Usa il browser»: the window comes to the front, Alberto is driving.
|
|
using (var begin = await bed.SendAsync(HttpMethod.Post, "/api/connection/begin", """{"openLogin":false}"""))
|
|
{
|
|
Assert.AreEqual(HttpStatusCode.NoContent, begin.StatusCode);
|
|
}
|
|
|
|
Assert.IsTrue(browser.UserIsDriving);
|
|
Assert.IsTrue(bed.App.Engine!.Services.Runtime.UserIsDriving);
|
|
|
|
// While the window is in front the lease holds, however long he takes.
|
|
await Task.Delay(TimeSpan.FromSeconds(1.5), ct);
|
|
Assert.IsTrue(browser.UserIsDriving, "the window is in front: Alberto is still driving");
|
|
|
|
// He closes the window with its X (WebView2Host.Done: the window's own lease ends), no «Ho finito».
|
|
int Navigations() => browser.FakeCdp.Methods.Count(m => m == "Page.navigate");
|
|
var navigations = Navigations();
|
|
await browser.HideFromUserAsync(ct);
|
|
await DesktopTestBed.WaitUntilAsync(() => !browser.UserIsDriving, TimeSpan.FromSeconds(10), "la fine della guida dopo la chiusura della finestra");
|
|
Assert.IsFalse(bed.App.Engine.Services.Runtime.UserIsDriving, "the engine may work again");
|
|
|
|
// The engine puts back to rest whatever he left open (§6.1).
|
|
await DesktopTestBed.WaitUntilAsync(() => Navigations() > navigations, TimeSpan.FromSeconds(10), "la pagina di riposo");
|
|
Assert.AreEqual("about:blank", browser.FakeCdp.Url);
|
|
|
|
// A late «Annulla» from a page left open does nothing to the page, which is the engine's again.
|
|
navigations = Navigations();
|
|
using (var cancel = await bed.SendAsync(HttpMethod.Post, "/api/connection/cancel"))
|
|
{
|
|
Assert.AreEqual(HttpStatusCode.NoContent, cancel.StatusCode);
|
|
}
|
|
|
|
Assert.AreEqual(navigations, Navigations());
|
|
Assert.IsFalse(browser.UserIsDriving);
|
|
}
|
|
|
|
/// <summary>«Accedi a Instagram», Alberto logs in, «Ho finito». Returns the message of «Ho finito».</summary>
|
|
private static async Task<string> LoginAsync(DesktopTestBed bed)
|
|
{
|
|
using (var begin = await bed.SendAsync(HttpMethod.Post, "/api/connection/begin", """{"openLogin":true}"""))
|
|
{
|
|
Assert.AreEqual(HttpStatusCode.NoContent, begin.StatusCode);
|
|
}
|
|
|
|
bed.Browser!.FakeCdp.LoggedIn = true;
|
|
var finish = await bed.JsonAsync(HttpMethod.Post, "/api/connection/finish");
|
|
Assert.IsTrue(finish.GetProperty("connected").GetBoolean(), finish.GetProperty("message").GetString());
|
|
return finish.GetProperty("message").GetString()!;
|
|
}
|
|
}
|