Files
Febe/tests/Febe.Tests/Collection/Support/CollectionHarness.cs
T
Alby96andClaude Opus 5.5 9941a2fa1f
verifica / controlli (push) Successful in 20s
verifica / prove (push) Failing after 19m40s
verifica / installatore (push) Skipped
verifica / immagine (push) Skipped
Ricerca dei profili con suggerimenti e foto (#37)
«Aggiungi account» ha una sola casella: mentre Alberto scrive compaiono
subito i suggerimenti locali, poi i profili di Instagram con la foto, come
chiesto in #37. Il browser di Febe scrive nella casella «Cerca» di Instagram
con tasti veri da una mappa chiusa (mai Invio, Tab o Esc), controlla prima
il fuoco, legge la risposta della pagina e non clicca mai un risultato.

La ricerca ha una categoria propria nel Governatore, con tetti larghi che
non consumano quelli della raccolta, e si dà il turno con il motore
sull'unica pagina; a fine sessione la pagina torna comunque al riposo. Le
foto passano dal CDN con un permesso ciascuna e restano solo in memoria.

Le risposte di Alberto del 15 novembre (controllo del fuoco, turni, evento
requestWillBeSent, niente foto dei seguiti per ora) sono in §6.1 e in
docs/decisioni.md. Su Instagram vero la ricerca parte quando i bersagli
search-open e search-box saranno raccolti dal vivo.

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

233 lines
8.6 KiB
C#

using System.Collections.Concurrent;
using Febe.Browser.Capture;
using Febe.Browser.Cdp;
using Febe.Browser.Diagnostics;
using Febe.Browser.Download;
using Febe.Browser.Hosting;
using Febe.Browser.Recipes;
using Febe.Browser.Targets;
using Febe.Core.Abstractions;
using Febe.Core.Collection;
using Febe.Core.Collection.Diagnostics;
using Febe.Core.Collection.Downloads;
using Febe.Core.Collection.Readers;
using Febe.Core.Collection.Recipes;
using Febe.Core.Collection.Search;
using Febe.Core.Domain;
using Febe.Core.Engine;
using Febe.SitoDiProva;
using Febe.Tests.Browser.TestSupport;
using Febe.Tests.Support;
namespace Febe.Tests.Collection.Support;
/// <summary>
/// One Chromium on a private Xvfb display, shared by a test class (the collection tests run one at a time).
/// </summary>
public sealed class SharedChromium : IAsyncDisposable
{
private SharedChromium(XvfbProcess xvfb, ChromiumProcessHost host, string profile, ConcurrentQueue<string> log)
{
Xvfb = xvfb;
Host = host;
Profile = profile;
Log = log;
}
public XvfbProcess Xvfb { get; }
public ChromiumProcessHost Host { get; }
public string Profile { get; }
public ConcurrentQueue<string> Log { get; }
public ICdpSession Cdp => Host.Cdp;
public static string? UnavailableReason() => RealBrowser.UnavailableReason();
public static async Task<SharedChromium> StartAsync()
{
var display = XvfbProcess.FindFreeDisplay(200, 299) ?? throw new InvalidOperationException("Nessun display libero.");
var log = new ConcurrentQueue<string>();
var browserLog = new DelegateBrowserLog((level, message, ex) => log.Enqueue($"{level}: {message} {ex?.Message}"));
var xvfb = new XvfbProcess(display, 1280, 900, log: browserLog);
await xvfb.StartAsync(CancellationToken.None);
var profile = Path.Combine(Path.GetTempPath(), "febe-raccolta-" + Guid.NewGuid().ToString("N"));
var host = new ChromiumProcessHost(
new ChromiumLaunchOptions { NoSandbox = RealBrowser.IsRoot, Environment = RealBrowser.BrowserEnvironment },
log: browserLog);
try
{
await host.StartAsync(new BrowserOptions(profile, 1280, 900, LocalTestSite.FreePort(), RealBrowser.ChromiumPath(), display), CancellationToken.None);
}
catch
{
await host.DisposeAsync();
await xvfb.DisposeAsync();
throw;
}
return new SharedChromium(xvfb, host, profile, log);
}
public async ValueTask DisposeAsync()
{
await Host.DisposeAsync();
await Xvfb.DisposeAsync();
RealBrowser.TryDelete(Profile);
}
}
/// <summary>
/// Everything one collection test needs, built fresh: the test site, the capture on the shared browser, the stop
/// signals, a library with its database, the page driver, the recipe runner and the download queue.
/// </summary>
internal sealed class CollectionHarness : IAsyncDisposable
{
private CollectionHarness(SharedChromium chromium, TestSite site, TempFolder temp, TestLibrary library, CompressedClock clock)
{
Chromium = chromium;
Site = site;
Temp = temp;
Library = library;
Clock = clock;
}
public SharedChromium Chromium { get; }
public TestSite Site { get; }
public TempFolder Temp { get; }
public TestLibrary Library { get; }
public CompressedClock Clock { get; }
public CollectionPipeline Pipeline { get; private set; } = null!;
public HostClassifier Hosts => new([Site.SiteHost], [Site.CdnHost]);
public RecordingBreaker Breaker { get; } = new();
public PermissiveGovernor Governor { get; } = new();
public FakeDataBudget DataBudget { get; } = new();
public SignalDetector Signals => Pipeline.Signals;
public CaptureCollector Capture => Pipeline.Capture;
public BrowserPageDriver Driver => Pipeline.Driver;
public RecipeRunner Runner => Pipeline.Runner;
public DownloadQueue Downloads => Pipeline.Downloads;
public Febe.Data.FebeDatabase Db => Library.Db;
/// <summary>The engine session the recipes run in (a real row: events refer to it).</summary>
public TestSession Session { get; private set; } = new();
/// <summary>The recorder of the diagnostic report, when asked for: what the page does goes to <see cref="Session"/>.</summary>
public CollectionDiagnostics? Diagnostics { get; private set; }
/// <summary>The turns of the page shared by the recipes and the profile search (issue #37).</summary>
public PageTurns Turns { get; } = new();
/// <summary>The photos of the profile search, in memory.</summary>
public SearchPhotoCache SearchPhotos { get; private set; } = null!;
/// <summary>The browser half of the profile search.</summary>
public ProfileSearchRunner Search => Pipeline.Search!;
public static async Task<CollectionHarness> CreateAsync(SharedChromium chromium, RecipeOptions? options = null, double factor = 10, bool diagnostics = false, ProfileSearchOptions? search = null)
{
var site = await SyntheticSite.StartAsync();
var temp = new TempFolder("raccolta");
var library = await TestLibrary.CreateAsync(temp);
var clock = new CompressedClock(factor, TestZones.Rome);
var harness = new CollectionHarness(chromium, site, temp, library, clock);
if (diagnostics)
{
harness.Diagnostics = new CollectionDiagnostics(clock, () => harness.Session.SessionId);
}
var searchOptions = search ?? new ProfileSearchOptions { AnswerTimeout = TimeSpan.FromSeconds(30) };
harness.SearchPhotos = new SearchPhotoCache(clock, searchOptions);
harness.Pipeline = await CollectionPipeline.StartAsync(new CollectionPipelineOptions
{
PageTurns = harness.Turns,
SearchOptions = searchOptions,
SearchPhotos = harness.SearchPhotos,
Cdp = chromium.Cdp,
Site = CollectionSite.TestSite(site.SiteHost, site.CdnHost),
Storage = library.Db,
Ingest = library.Ingest,
Governor = harness.Governor,
DataBudget = harness.DataBudget,
Breaker = harness.Breaker,
Clock = clock,
Random = new SeededRandom(20261013),
StagingFolder = temp.Combine("dati", "temporanei"),
Recipes = options ?? new RecipeOptions(),
Diagnostics = harness.Diagnostics,
});
harness.Session = new TestSession((await library.Db.Sessions.StartAsync(clock.Now)).Id);
await harness.SignInAsync();
return harness;
}
/// <summary>
/// What Alberto does by hand before the engine runs: opens the site once, so that its session cookie exists (the
/// test site has no login form: the first visit starts the session). Then the browser goes back to rest.
/// </summary>
public async Task SignInAsync()
{
await Chromium.Cdp.NavigateAsync(Site.Url("/benvenuto"));
await Chromium.Cdp.WaitForLoadAsync(new Febe.Core.Abstractions.SystemClock(), TimeSpan.FromSeconds(10));
await Chromium.Cdp.RestAsync();
}
/// <summary>Adds an account as Alberto would (by name, from the test site's world).</summary>
public async Task<Account> AddAccountAsync(string username, Func<Account, Account>? change = null, bool withId = true)
{
string? id;
bool followed;
lock (Site.World.Gate)
{
var site = Site.World.FindByUsername(username);
id = site?.Id;
followed = site?.FollowedByViewer ?? false;
}
var account = Account.Create(username, StartingPoint.EverythingAndKeepUpdated, Clock.Now, TestZones.Rome) with
{
InstagramId = withId ? id : null,
IsFollowed = followed,
};
return await Db.Accounts.AddAsync(change is null ? account : change(account));
}
public Task<RecipeOutcome> RunAsync(RecipeKind kind, long? accountId = null, long? entryId = null, string? highlightId = null, int? maxPages = null) =>
Runner.RunAsync(new RecipeRequest(kind, accountId, entryId, highlightId, maxPages), Session, CancellationToken.None);
public Task<string> CurrentPathAsync() => Driver.CurrentPathAsync(CancellationToken.None);
public async ValueTask DisposeAsync()
{
await Pipeline.DisposeAsync();
try
{
await Chromium.Cdp.RestAsync();
}
catch (Exception ex) when (ex is CdpCommandException or BrowserUnavailableException or CdpDisconnectedException)
{
}
await Site.DisposeAsync();
await Library.DisposeAsync();
Temp.Dispose();
}
}