Files
Febe/tests/Febe.Tests/Server/ServerEndToEndTests.cs
T
Alby96andClaude Opus 5.5 c49df4ca7b Browser mai bianco né muto: pagina iniziale, finestra che resta, fotogrammi che ripartono (#52)
Il browser di Febe a volte era bianco o non mostrava nulla. Le cause erano
diverse e si correggono insieme.

- Pagina iniziale. Se il browser è a riposo su about:blank e Alberto lo apre
  («Usa il browser», «Apri il browser»), Febe lo porta sulla home del sito
  quando è collegato, altrimenti sulla pagina di accesso. È un'azione di
  Alberto, sotto il suo permesso di guida, non del motore: Febe aspetta che
  la sessione finisca e prende il turno della pagina.
  Il nuovo pulsante «Pagina iniziale» sta nella barra della finestra di
  Windows e nella vista remota. Su about:blank la barra dice «Il browser è a
  riposo su una pagina vuota». «Indietro» non torna più sulla pagina di
  riposo.
- Disconnessione. «Apri il browser» nella striscia rossa apre la pagina di
  accesso. Con --accesso-di-prova il primo controllo all'avvio non segnala
  più una disconnessione finta.
- Windows: la finestra resta.
  - Lasciare una pagina di Febe non nasconde più la finestra «Browser di
    Febe» e non la rimette a riposo: finisce solo il permesso tenuto dalla
    pagina (cancel con pageLeft, IConnectionService.ReleaseAsync).
  - Sullo schermo la finestra ha il suo pulsante nella barra delle
    applicazioni, così ridurla a icona non la fa sparire. Dopo 2 minuti
    ridotta a icona torna fuori vista (S2).
  - Se lo schermo è troppo piccolo, la finestra si restringe all'area di
    lavoro e la pagina si rimpicciolisce con ZoomFactor, con le stesse misure
    CSS.
  - I bordi della pagina si riapplicano, con NotifyParentWindowPositionChanged,
    quando la finestra si mostra, si sposta o riparte il processo GPU.
  - Una pagina che non risponde mentre Alberto guida resta com'è.
  - IBrowserWindow.ShowAsync lascia il posto a
    IConnectionService.ShowBrowserAsync.
- Docker: fotogrammi che ripartono. La vista remota ha un watchdog. Riavvia
  i fotogrammi dopo una pagina nuova caricata senza fotogrammi e dopo circa
  2 s di silenzio. L'attesa raddoppia fino a 30 s finché la pagina resta
  ferma. Se il riavvio non porta nulla, la vista riceve un'immagine ferma
  (Page.captureScreenshot, già fra i comandi ammessi).
  RemoteViewSession ha ora un registro vero: avvii, riavvii e silenzi vanno
  a livello Info, categoria «vista», senza indirizzi.
  Se dopo 8 s non arriva nessuna immagine, l'interfaccia mostra «Nessuna
  immagine dal browser» con «Riprova».

Nessun comando CDP nuovo e nessuna dipendenza nuova. Aggiornati PROTOCOLLO.md
e la guida (Primo collegamento, Attività e arresti, Installazione Windows,
Problemi comuni).

Prove nuove:
- ScreencastWatchdogTests, RemoteViewFramesTests (Chromium vero, solo CI
  Linux)
- Home e Indietro in RemoteViewStartTests
- RemoteViewSocketTests
- ConnectionApiTests
- BrowserStartPageTests
- BrowserWindowDecisionTests e BrowserWindowTests (WebView2 vero)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 12:51:12 +01:00

426 lines
22 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Net;
using System.Net.WebSockets;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using Febe.Core.Domain;
using Febe.Core.Library;
using Febe.Core.Storage;
using Febe.SitoDiProva;
using Febe.Tests.Alerts.Support;
using Febe.Tests.Server.Support;
using Febe.Tests.Support;
using Febe.Web.Connection;
namespace Febe.Tests.Server;
/// <summary>
/// The real composition of the server (milestone M5) against the local test site, with Xvfb and a real Chromium: the
/// password, the connection through the login page, accounts added through the API, «Controlla ora», a real session of
/// the engine with the Governor's permits, files and thumbnails in the library, the stop on the verification page and
/// the remote view. Never Instagram: the browser only sees 127.0.0.1.
/// </summary>
[TestClass]
[TestCategory(SyntheticSite.Category)]
[DoNotParallelize]
public sealed partial class ServerEndToEndTests
{
public TestContext TestContext { get; set; } = null!;
[TestInitialize]
public void RequireBrowser()
{
if (ServerTestBed.UnavailableReason() is { } reason)
{
Assert.Inconclusive(reason);
}
}
[TestMethod]
[Timeout(540_000, CooperativeCancellation = true)]
public async Task TheRealServerCollectsFromTheTestSiteStopsAtTheVerificationAndShowsTheBrowser()
{
var ct = TestContext.CancellationToken;
var telegram = new FakeTelegramHandler();
var disk = new FakeFreeSpaceProbe();
await using var bed = await ServerTestBed.StartAsync(telegram: telegram, freeSpace: disk);
var server = bed.Server;
Assert.IsEmpty(server.Problems);
await bed.WaitRunningAsync(ct);
var connection = server.Connection!;
// The health check answers without a session; everything else wants the password.
using (var health = await bed.Client.GetAsync("/salute", ct))
{
Assert.AreEqual(HttpStatusCode.OK, health.StatusCode);
Assert.AreEqual("""{"stato":"ok"}""", await health.Content.ReadAsStringAsync(ct));
}
using (var anonymous = await bed.Client.GetAsync("/api/accounts", ct))
{
Assert.AreEqual(HttpStatusCode.Unauthorized, anonymous.StatusCode);
}
await bed.LoginAsync();
// A fresh profile: not connected, the engine waits.
Assert.AreEqual(ConnectionState.NotConnected, (await connection.GetStatusAsync(ct)).State);
// Accounts added through the API, as from the «Aggiungi account» dialog.
var added = await bed.JsonAsync(HttpMethod.Post, "/api/accounts", """{"usernames":["marta.prova","luca.prova"],"start":"everything"}""");
Assert.AreEqual(2, added.GetProperty("added").GetInt32());
var marta = await server.Database.Accounts.FindByUsernameAsync("marta.prova", ct) ?? throw new AssertFailedException("marta.prova manca");
// «Accedi a Instagram» opens the login page (here: the test site, whose first visit starts its session);
// meanwhile Alberto is driving and the engine is still.
await connection.BeginUserDrivingAsync(openLogin: true, ct);
var driving = await connection.GetStatusAsync(ct);
Assert.IsTrue(driving.UserIsDriving);
Assert.IsTrue(server.Browser!.UserIsDriving);
StringAssert.StartsWith(driving.CurrentUrl, bed.Site.SiteUrl, "the login page of the configured site, never Instagram in tests");
var finish = await connection.FinishAsync(ct);
Assert.IsTrue(finish.Connected, finish.Message);
Assert.IsFalse(server.Browser.UserIsDriving, "«Ho finito» ends the driving");
Assert.IsNotNull(await server.Database.Settings.GetInt64Async(Febe.App.BrowserConnectionService.FirstConnectionKey, ct), "first connection recorded");
Assert.AreEqual(ConnectionState.Connected, (await connection.GetStatusAsync(ct)).State);
// «Controlla ora».
var check = await bed.JsonAsync(HttpMethod.Post, $"/api/accounts/{marta.Id}/check-now");
if (check.GetProperty("status").GetString() == "needsConfirmation")
{
check = await bed.JsonAsync(HttpMethod.Post, $"/api/accounts/{marta.Id}/check-now", $$"""{"token":"{{check.GetProperty("token").GetString()}}"}""");
}
Assert.AreEqual("queued", check.GetProperty("status").GetString(), check.GetProperty("message").GetString());
// The engine runs a session: every action toward the site has its Governor's permit in action_log.
await ServerTestBed.WaitUntilAsync(
async () => (await server.Database.ActionLog.ListAsync(DateTimeOffset.UnixEpoch, bed.Clock.Now.AddDays(1), ct)).Any(a => a.AccountId == marta.Id),
TimeSpan.FromSeconds(90),
"un'azione del motore su marta.prova nel registro delle azioni");
var actions = await server.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");
Assert.IsNotNull(await server.Database.Sessions.GetLastAsync(ct));
// Files arrive in the library with the names of the scheme, and their thumbnails.
var library = bed.Library;
var thumbnails = bed.Thumbnails;
List<(Entry Entry, Media Media)> stored = [];
await ServerTestBed.WaitUntilAsync(
async () =>
{
stored = await StoredAsync(server, marta.Id, ct);
return stored.Count >= 3;
},
TimeSpan.FromSeconds(90),
"tre file di marta.prova scaricati");
foreach (var (entry, 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);
Assert.MatchesRegex(NamePattern(), media.RelativePath!, "naming scheme {tipo}/{anno}/{data}_{ora}_{codice}_{n}");
if (media.Kind == MediaKind.Photo)
{
Assert.IsTrue(thumbnails.Exists(media.Sha256!), $"thumbnail of {media.RelativePath}");
}
}
Assert.IsTrue(File.Exists(Path.Combine(library.AccountFolder(marta), AccountJournal.FileName)), "_febe.jsonl");
var recipes = (await server.Database.Events.ListAsync(new EventQuery(), ct)).Where(e => e.Code.StartsWith("ricetta.", StringComparison.Ordinal)).ToList();
Assert.IsNotEmpty(recipes, "one event line per recipe run");
TestContext.WriteLine($"Ricette: {string.Join(", ", recipes.Select(r => r.Code).Distinct())}; file di marta.prova: {stored.Count}.");
// The site asks for a verification: at its next action toward the site the engine stops and the banner appears
// in /api/engine. Usually still in the first session (two accounts to read in full); if that session already
// ended, in the next one, after the minimum pause (20 minutes at 10×: two real minutes).
// Alerts on Telegram (M8): token and chat set from Impostazioni ▸ Notifiche; the composition sends through the fake.
const string token = "4839574812:AAF-gettoneDelServerDiProva_123";
var events = """{"verification":true,"disconnection":true,"throttle":true,"backfillDone":true,"diskLow":true,"unknownFormat":true}""";
await bed.JsonAsync(HttpMethod.Put, "/api/settings/notifications", $$"""{"events":{{events}},"windows":false,"telegramChatId":"42","telegramToken":"{{token}}","removeTelegramToken":false}""");
var servedBefore = bed.Site.Control.CdnFilesServed;
bed.Site.Control.Set(StopMode.Challenge);
await ServerTestBed.WaitUntilAsync(
async () =>
{
var engine = await bed.JsonAsync(HttpMethod.Get, "/api/engine");
var snapshot = engine.GetProperty("snapshot");
return snapshot.GetProperty("mode").GetString() == "StoppedForVerification" && snapshot.GetProperty("banner").ValueKind == JsonValueKind.Object;
},
TimeSpan.FromSeconds(200),
"la striscia di arresto per la verifica in /api/engine");
Assert.AreEqual(ConnectionState.VerificationRequired, (await connection.GetStatusAsync(ct)).State);
TestContext.WriteLine($"CDN: {servedBefore} file prima del fermo, {bed.Site.Control.CdnFilesServed} adesso.");
// The same stop reached Telegram, with the Italian text, and the «Avvisi» list of Attività.
await ServerTestBed.WaitUntilAsync(
() => Task.FromResult(telegram.Requests.Any(r => r.Text.StartsWith("Febe · Instagram chiede una verifica\n", StringComparison.Ordinal))),
TimeSpan.FromSeconds(20),
"il messaggio Telegram della verifica");
var message = telegram.Requests.First(r => r.Text.StartsWith("Febe · Instagram chiede una verifica", StringComparison.Ordinal));
Assert.AreEqual($"/bot{token}/sendMessage", message.Uri.AbsolutePath);
Assert.AreEqual("42", message.ChatId);
StringAssert.Contains(message.Text, "premi «Riprendi»");
var alerts = await bed.JsonAsync(HttpMethod.Get, "/api/alerts");
Assert.IsTrue(alerts.GetProperty("items").EnumerateArray().Any(i => i.GetProperty("kind").GetString() == "verification"));
foreach (var file in Directory.EnumerateFiles(server.Data!.Logs))
{
var log = await ReadSharedAsync(file, ct);
Assert.DoesNotContain(token, log, file);
Assert.DoesNotContain("api.telegram.org", log, file);
}
// The remote view: meta first, then JPEG frames; Alberto is driving while it is open.
using (var socket = await bed.OpenRemoteViewAsync(ct))
{
var (metaType, meta) = await ReceiveAsync(socket, ct);
Assert.AreEqual(WebSocketMessageType.Text, metaType);
using var metaJson = JsonDocument.Parse(meta);
Assert.AreEqual("meta", metaJson.RootElement.GetProperty("type").GetString());
Assert.IsGreaterThan(0, metaJson.RootElement.GetProperty("width").GetInt32());
Assert.IsTrue(server.Browser.UserIsDriving, "the engine is still while the view is open");
byte[]? jpeg = null;
for (var i = 0; i < 20 && jpeg is null; i++)
{
// A gesture makes the page repaint, so a frame comes even on a still page.
await SendAsync(socket, """{"type":"mouse","action":"move","x":0.5,"y":0.5}""", ct);
var (type, data) = await ReceiveAsync(socket, ct);
if (type == WebSocketMessageType.Binary)
{
jpeg = data;
}
}
Assert.IsNotNull(jpeg, "a JPEG frame");
Assert.AreEqual(0xFF, jpeg[0]);
Assert.AreEqual(0xD8, jpeg[1]);
await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "fine", ct);
}
await ServerTestBed.WaitUntilAsync(() => Task.FromResult(!server.Browser.UserIsDriving), TimeSpan.FromSeconds(10), "la vista chiusa libera il browser");
}
[TestMethod]
[Timeout(360_000, CooperativeCancellation = true)]
public async Task FinishWithoutSessionSaysSoAndDisconnectDeletesTheProfile()
{
var ct = TestContext.CancellationToken;
await using var bed = await ServerTestBed.StartAsync(autoLogin: true);
var server = bed.Server;
await bed.WaitRunningAsync(ct);
var connection = server.Connection!;
Assert.AreEqual(ConnectionState.Connected, (await connection.GetStatusAsync(ct)).State, "the test site's automatic login");
// «Scollega»: engine stopped, profile deleted, browser restarted empty.
var marker = Path.Combine(server.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(server.Browser.IsAvailable, "the browser runs again, with an empty profile");
Assert.AreEqual(ConnectionState.NotConnected, (await connection.GetStatusAsync(ct)).State);
Assert.IsFalse(server.Browser.UserIsDriving);
await ServerTestBed.WaitUntilAsync(
() => Task.FromResult(server.Engine!.Services.Runtime.IsRunning),
TimeSpan.FromSeconds(60),
"il motore di nuovo in funzione, in attesa del collegamento");
// «Usa il browser» on the rest page, not connected (issue #52): never the white page, the login page. (The test
// site sets its session on any visit, so «Ho finito» without a login is proved with the fake browser of
// BrowserStartPageTests.)
await connection.BeginUserDrivingAsync(openLogin: false, ct);
Assert.IsTrue(server.Browser.UserIsDriving);
var opened = (await connection.GetStatusAsync(ct)).CurrentUrl;
Assert.IsNotNull(opened);
StringAssert.Contains(opened, "/benvenuto", "the login page of the test site");
await connection.CancelAsync(ct);
Assert.IsFalse(server.Browser.UserIsDriving);
// «Annulla» ends the driving and leaves the browser on the rest page.
await connection.BeginUserDrivingAsync(openLogin: true, ct);
await connection.CancelAsync(ct);
Assert.IsFalse(server.Browser.UserIsDriving);
Assert.AreEqual("about:blank", (await connection.GetStatusAsync(ct)).CurrentUrl);
// The debugging port listens on 127.0.0.1 only.
Assert.IsTrue(Febe.Browser.Hosting.LoopbackPortCheck.IsLoopbackOnly(bed.Options.DebugPort, out _));
}
[TestMethod]
[Timeout(420_000, CooperativeCancellation = true)]
public async Task TwoViewsOpenedAtTheSameMomentLeaveOneViewWithItsFramesAndOneLease()
{
var ct = TestContext.CancellationToken;
await using var bed = await ServerTestBed.StartAsync();
var server = bed.Server;
await bed.WaitRunningAsync(ct);
var host = server.RemoteView!;
var gate = server.Browser!.UserDriving;
var released = 0;
void OnChanged(object? sender, bool driving)
{
if (!driving)
{
Interlocked.Increment(ref released);
}
}
for (var round = 0; round < 5; round++)
{
// From the rest page: where the browser starts, and where the engine puts it once nobody is at the browser.
// Page.getFrameTree is answered by the page itself, which DevTools holds back while a navigation is pending:
// «about:blank» here means the engine's rest has committed.
await ServerTestBed.WaitUntilAsync(
async () => await Febe.Browser.Cdp.CdpPageExtensions.GetMainFrameUrlAsync(server.Browser.Cdp, ct) == "about:blank",
TimeSpan.FromSeconds(60),
"la pagina di riposo");
// Two tabs restored together, or the phone and the PC at once: the newest wins, the other is ended — and the
// engine never sees nobody at the browser in between (it would put the page to rest under the newer view).
Volatile.Write(ref released, 0);
gate.Changed += OnChanged;
IRemoteViewConnection[] views;
try
{
views = await Task.WhenAll(Task.Run(() => host.OpenAsync(ct), ct), Task.Run(() => host.OpenAsync(ct), ct));
}
finally
{
gate.Changed -= OnChanged;
}
var open = views.Where(v => v.Ended is null).ToList();
Assert.HasCount(1, open, $"Round {round}: one view stays open.");
Assert.AreEqual(RemoteViewEnd.Replaced, views.Single(v => v.Ended is not null).Ended);
Assert.AreEqual(1, gate.Leases, $"Round {round}: only the open view holds the browser.");
Assert.AreEqual(0, Volatile.Read(ref released), $"Round {round}: the browser was never left without a driver while one view replaced the other.");
// The page of the replaced view closes its socket: the open view still gets its frames.
await views.Single(v => v.Ended is not null).DisposeAsync();
await Febe.Browser.Cdp.CdpPageExtensions.NavigateAsync(server.Browser.Cdp, $"{bed.Site.SiteUrl}/?giro={round}", ct);
Assert.IsTrue(await FrameArrivesAsync(open[0], ct), $"Round {round}: frames go on after the other view closed.");
await open[0].DisposeAsync();
Assert.AreEqual(0, gate.Leases, $"Round {round}: no view, no lease.");
}
}
[TestMethod]
[Timeout(480_000, CooperativeCancellation = true)]
public async Task AViewOpenedWhileThePageIsNavigatingOpensAndShowsItsFrames()
{
var ct = TestContext.CancellationToken;
await using var bed = await ServerTestBed.StartAsync();
var server = bed.Server;
await bed.WaitRunningAsync(ct);
var host = server.RemoteView!;
var browser = server.Browser!;
for (var round = 0; round < 6; round++)
{
// From the rest page, as in TwoViews…: the previous round ends on a site page, so «about:blank» here means the
// engine's rest after the last view has committed and nothing else navigates.
await ServerTestBed.WaitUntilAsync(
async () => await Febe.Browser.Cdp.CdpPageExtensions.GetMainFrameUrlAsync(browser.Cdp, ct) == "about:blank",
TimeSpan.FromSeconds(60),
"la pagina di riposo");
// Even rounds: «Accedi a Instagram» on the Collegamento page navigates to the login page while the same page
// opens the view. Odd rounds: the rest page, as the engine puts it after a session or after the last view.
// A guard of these pairs, not of the retry: with the navigation already sent, the view's first look at the
// page (Page.getFrameTree) is answered only once it has committed, so the start comes after the commit (seen
// with Chrome 155). RemoteViewDuringNavigationTests makes the start meet the refusal on purpose.
var navigation = round % 2 == 0
? Febe.Browser.Cdp.CdpPageExtensions.NavigateAsync(browser.Cdp, $"{bed.Site.SiteUrl}/?vista={round}", ct)
: browser.RestAsync(ct);
var view = await host.OpenAsync(ct);
try
{
var navigated = await navigation;
Assert.IsTrue(navigated.Succeeded, $"Round {round}: {navigated.ErrorText}");
Assert.IsNull(view.Ended, $"Round {round}: the view is open.");
Assert.AreEqual(1, browser.UserDriving.Leases, $"Round {round}: only the view holds the browser.");
// A new page paints: its frames reach the view.
await Febe.Browser.Cdp.CdpPageExtensions.NavigateAsync(browser.Cdp, $"{bed.Site.SiteUrl}/?giro={round}", ct);
Assert.IsTrue(await FrameArrivesAsync(view, ct), $"Round {round}: frames of the page.");
}
finally
{
await view.DisposeAsync();
}
Assert.AreEqual(0, browser.UserDriving.Leases, $"Round {round}: no view, no lease.");
}
}
private static async Task<bool> FrameArrivesAsync(IRemoteViewConnection view, CancellationToken ct)
{
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(ct);
timeout.CancelAfter(TimeSpan.FromSeconds(60)); // only a hang guard: it returns at the first frame
try
{
await foreach (var output in view.ReadAsync(timeout.Token))
{
if (output is RemoteViewFrame)
{
return true;
}
}
}
catch (OperationCanceledException) when (!ct.IsCancellationRequested)
{
}
return false;
}
private static async Task<List<(Entry Entry, Media Media)>> StoredAsync(Febe.Server.FebeServer server, long accountId, CancellationToken ct)
{
var list = new List<(Entry, Media)>();
foreach (var entry in await server.Database.Entries.ListByAccountAsync(accountId, null, ct))
{
foreach (var media in await server.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((entry, media));
}
}
}
return list;
}
private static Task SendAsync(ClientWebSocket socket, string json, CancellationToken ct) =>
socket.SendAsync(Encoding.UTF8.GetBytes(json), WebSocketMessageType.Text, endOfMessage: true, ct);
private static async Task<(WebSocketMessageType Type, byte[] Data)> ReceiveAsync(ClientWebSocket socket, CancellationToken ct)
{
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(ct);
timeout.CancelAfter(TimeSpan.FromSeconds(10));
using var buffer = new MemoryStream();
var chunk = new byte[64 * 1024];
WebSocketReceiveResult result;
do
{
result = await socket.ReceiveAsync(chunk, timeout.Token);
buffer.Write(chunk, 0, result.Count);
}
while (!result.EndOfMessage);
return (result.MessageType, buffer.ToArray());
}
[GeneratedRegex(@"^(post|storie|evidenza/[^/]+|taggati)/(\d{4}/)?\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}_[^_/]+_\d+\.(jpg|mp4)$")]
private static partial Regex NamePattern();
private static async Task<string> ReadSharedAsync(string path, CancellationToken ct)
{
await using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using var reader = new StreamReader(stream);
return await reader.ReadToEndAsync(ct);
}
}