Files
Febe/tests/Febe.Tests/Web/Part2Support/Part2Helpers.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

141 lines
5.2 KiB
C#

using System.Net;
using System.Text.Json;
using Febe.Tests.Support;
using Febe.Tests.Web.Support;
using Febe.Web;
using Febe.Web.Connection;
using Microsoft.Extensions.DependencyInjection;
namespace Febe.Tests.Web.Part2Support;
/// <summary>Helpers of the tests of the second part of the interface (Attività, Impostazioni, Importa, Collegamento).</summary>
internal static class Part2
{
/// <summary>A web server for one test, imitating the given shell (Docker by default, like a real container).</summary>
public static Task<WebTestServer> StartAsync(FebeEdition edition = FebeEdition.Docker, Action<IServiceCollection>? services = null) =>
WebTestServer.StartAsync(new FebeWebOptions { Access = AccessMode.None, Edition = edition }, services);
/// <summary>Polls until the condition holds (background jobs: analysis, copy, rebuild).</summary>
public static async Task WaitUntilAsync(Func<Task<bool>> condition, string what, int seconds = 60)
{
var deadline = DateTime.UtcNow + TimeSpan.FromSeconds(seconds);
while (DateTime.UtcNow < deadline)
{
if (await condition())
{
return;
}
await Task.Delay(100);
}
Assert.Fail($"timeout: {what}");
}
/// <summary>GET that returns status and body without asserting.</summary>
public static async Task<(HttpStatusCode Status, string Text, JsonElement Json)> GetAsync(this WebTestServer server, string path)
{
using var response = await server.Client.GetAsync(path);
var text = await response.Content.ReadAsStringAsync();
var json = text.Length > 0 && (response.Content.Headers.ContentType?.MediaType?.Contains("json", StringComparison.Ordinal) ?? false)
? JsonDocument.Parse(text).RootElement.Clone()
: default;
return (response.StatusCode, text, json);
}
/// <summary>A command whose body is returned as text too (to check what it does not contain).</summary>
public static async Task<(HttpStatusCode Status, string Text)> SendTextAsync(this WebTestServer server, HttpMethod method, string path, object? body = null, string? origin = "")
{
using var request = server.Request(method, path, body, origin);
using var response = await server.Client.SendAsync(request);
return (response.StatusCode, await response.Content.ReadAsStringAsync());
}
/// <summary>Writes a JPEG of a generated scene.</summary>
public static void WritePhoto(string path, int scene, int width = 900, int height = 1100)
{
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
File.WriteAllBytes(path, TestFile.PhotoBytes(scene, width, height));
}
}
/// <summary>Configuration problems chosen by the test.</summary>
internal sealed class FixedProblems(params ConfigurationProblem[] problems) : IConfigurationProblems
{
public IReadOnlyList<ConfigurationProblem> Current { get; } = problems;
}
/// <summary>A connection service that records what the API asks.</summary>
internal sealed class RecordingConnection : IConnectionService
{
private readonly Lock _gate = new();
public List<string> Calls { get; } = [];
public ConnectionStatus Status { get; set; } = new(ConnectionState.NotConnected, null, null, false, "https://www.instagram.com/accounts/login/");
/// <summary>The status after «Ho finito» (connected by default; a verification still pending is the other case).</summary>
public ConnectionStatus StatusAfterFinish { get; set; } = new(ConnectionState.Connected, "tuo.profilo", "Il tuo profilo", false, null);
/// <summary>The calls so far, safe to read while the interface keeps sending.</summary>
public string[] Snapshot()
{
lock (_gate)
{
return [.. Calls];
}
}
public Task<ConnectionStatus> GetStatusAsync(CancellationToken ct) => Task.FromResult(Status);
public Task BeginUserDrivingAsync(bool openLogin, CancellationToken ct)
{
Record("begin:" + openLogin);
Status = Status with { UserIsDriving = true };
return Task.CompletedTask;
}
public Task<FinishResult> FinishAsync(CancellationToken ct)
{
Record("finish");
Status = StatusAfterFinish;
return Task.FromResult(new FinishResult(true, "Sei collegato come @tuo.profilo."));
}
public Task CancelAsync(CancellationToken ct)
{
Record("cancel");
Status = Status with { UserIsDriving = false };
return Task.CompletedTask;
}
public Task ShowBrowserAsync(bool openLogin, CancellationToken ct)
{
Record("show:" + openLogin);
Status = Status with { UserIsDriving = true };
return Task.CompletedTask;
}
public Task ReleaseAsync(CancellationToken ct)
{
Record("release");
Status = Status with { UserIsDriving = false };
return Task.CompletedTask;
}
public Task DisconnectAsync(CancellationToken ct)
{
Record("disconnect");
Status = new ConnectionStatus(ConnectionState.NotConnected, null, null, false, null);
return Task.CompletedTask;
}
private void Record(string call)
{
lock (_gate)
{
Calls.Add(call);
}
}
}