Raccolta (M0/M2): sito di prova completo, lettori per forma, ricette su browser vero, coda degli scaricamenti, sonda con ripulitura e verifica; documenti delle ipotesi e delle prove dal vivo
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
74 files changed
+12689
-13
No files matched your search
@@ -0,0 +1,121 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Febe.Core.Abstractions;
|
||||
using Febe.Core.Collection.Recipes;
|
||||
using Febe.Core.Engine;
|
||||
|
||||
namespace Febe.Tests.Collection.Support;
|
||||
|
||||
/// <summary>
|
||||
/// A clock that compresses time: <see cref="Delay"/> waits for real only a fraction of the requested time, and
|
||||
/// <see cref="Now"/> moves on by the whole of it. Recipes against a real browser keep their order of events, but a
|
||||
/// 2.2-second «wait after load» lasts a few hundred milliseconds.
|
||||
/// </summary>
|
||||
public sealed class CompressedClock : IClock
|
||||
{
|
||||
private readonly Lock _gate = new();
|
||||
private TimeSpan _offset;
|
||||
|
||||
public CompressedClock(double factor = 10, TimeZoneInfo? zone = null)
|
||||
{
|
||||
Factor = factor;
|
||||
LocalZone = zone ?? TimeZoneInfo.Utc;
|
||||
}
|
||||
|
||||
public double Factor { get; }
|
||||
|
||||
public TimeZoneInfo LocalZone { get; }
|
||||
|
||||
public DateTimeOffset Now
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
return DateTimeOffset.UtcNow + _offset;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task Delay(TimeSpan time, CancellationToken ct)
|
||||
{
|
||||
if (time <= TimeSpan.Zero)
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
return;
|
||||
}
|
||||
|
||||
var real = TimeSpan.FromTicks((long)(time.Ticks / Factor));
|
||||
if (real >= TimeSpan.FromMilliseconds(1))
|
||||
{
|
||||
await Task.Delay(real, ct).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
await Task.Yield();
|
||||
}
|
||||
|
||||
lock (_gate)
|
||||
{
|
||||
_offset += time - real;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A Governor that grants everything, records each request and can refuse a category after a number of grants
|
||||
/// (to test the orderly stop of the recipes). Never used outside tests.
|
||||
/// </summary>
|
||||
public sealed class PermissiveGovernor : IGovernor, IBudgetView
|
||||
{
|
||||
private readonly ConcurrentQueue<(ActionKind Kind, long? AccountId)> _requests = new();
|
||||
private readonly ConcurrentDictionary<ActionKind, int> _limits = new();
|
||||
private readonly ConcurrentDictionary<ActionKind, int> _granted = new();
|
||||
|
||||
public IReadOnlyList<(ActionKind Kind, long? AccountId)> Requests => [.. _requests];
|
||||
|
||||
/// <summary>Remaining budget reported to <see cref="IBudgetView"/> when no limit is set.</summary>
|
||||
public int ReportedRemaining { get; set; } = 1000;
|
||||
|
||||
public int Granted(ActionKind kind) => _granted.TryGetValue(kind, out var n) ? n : 0;
|
||||
|
||||
/// <summary>After <paramref name="grants"/> granted requests of <paramref name="kind"/>, the next ones are refused.</summary>
|
||||
public void Limit(ActionKind kind, int grants) => _limits[kind] = grants;
|
||||
|
||||
public Task<Permit> RequestAsync(ActionKind kind, long? accountId, CancellationToken ct)
|
||||
{
|
||||
_requests.Enqueue((kind, accountId));
|
||||
if (_limits.TryGetValue(kind, out var limit) && Granted(kind) >= limit)
|
||||
{
|
||||
return Task.FromResult(Permit.Deny(kind, accountId, DenialReason.HourlyBudgetReached, "Tetto orario raggiunto (prova)."));
|
||||
}
|
||||
|
||||
_granted.AddOrUpdate(kind, 1, (_, n) => n + 1);
|
||||
return Task.FromResult(Permit.Grant(kind, accountId));
|
||||
}
|
||||
|
||||
public Task<int> RemainingAsync(ActionKind kind, long? accountId, CancellationToken ct) =>
|
||||
Task.FromResult(_limits.TryGetValue(kind, out var limit) ? Math.Max(0, limit - Granted(kind)) : ReportedRemaining);
|
||||
}
|
||||
|
||||
/// <summary>A daily data budget kept in memory.</summary>
|
||||
public sealed class FakeDataBudget(long bytesPerDay = long.MaxValue / 4) : IDataBudget
|
||||
{
|
||||
private long _used;
|
||||
|
||||
public long Used => Interlocked.Read(ref _used);
|
||||
|
||||
public Task<long> RemainingTodayAsync(CancellationToken ct) => Task.FromResult(Math.Max(0, bytesPerDay - Used));
|
||||
|
||||
public Task RecordAsync(long bytes, long? accountId, CancellationToken ct)
|
||||
{
|
||||
Interlocked.Add(ref _used, bytes);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A session context for one test.</summary>
|
||||
public sealed record TestSession(long SessionId = 1, double Mood = 1.0) : ISessionContext
|
||||
{
|
||||
public DateTimeOffset PlannedEnd { get; init; } = DateTimeOffset.MaxValue;
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
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.Downloads;
|
||||
using Febe.Core.Collection.Readers;
|
||||
using Febe.Core.Collection.Recipes;
|
||||
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();
|
||||
|
||||
public static async Task<CollectionHarness> CreateAsync(SharedChromium chromium, RecipeOptions? options = null, double factor = 10)
|
||||
{
|
||||
var site = await TestSite.StartAsync(new TestSiteOptions());
|
||||
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);
|
||||
harness.Pipeline = await CollectionPipeline.StartAsync(new CollectionPipelineOptions
|
||||
{
|
||||
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(),
|
||||
});
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user