using System.Collections.Concurrent; using Febe.Core.Abstractions; using Febe.Core.Collection; using Febe.Core.Collection.Diagnostics; using Febe.Core.Collection.Readers; using Febe.Core.Collection.Recipes; using Febe.Core.Collection.Requests; using Febe.Core.Domain; using Febe.Core.Engine; using Febe.Data; using Febe.Tests.Support; using RecordingBreaker = Febe.Tests.Browser.TestSupport.RecordingBreaker; using VirtualClock = Febe.Tests.Browser.TestSupport.VirtualClock; namespace Febe.Tests.Collection.Support; /// /// A page that must never be touched: the recipes by requests do not use it (issue #36, S3). Every call is counted /// and fails. /// public sealed class NoPageDriver : IPageDriver { private int _touched; /// Calls received (each one failed). public int Touched => Volatile.Read(ref _touched); public double WaitMultiplier => 1.0; /// Counted, not failed: it comes before the run, where a failure would not be the run's. public void BeginSession(double mood) => Interlocked.Increment(ref _touched); public Task OpenAsync(string path, CancellationToken ct) => Touch(); public Task CurrentPathAsync(CancellationToken ct) => Touch(); public Task IsOnSiteAsync(CancellationToken ct) => Touch(); public Task FindAsync(string target, IReadOnlyDictionary? parameters, CancellationToken ct) => Touch(); public Task ClickAsync(string target, IReadOnlyDictionary? parameters, CancellationToken ct) => Touch(); public Task ScrollBurstAsync(CancellationToken ct) => Touch(); public Task WaitAfterLoadAsync(CancellationToken ct) => Touch(); public Task WaitAsync(TimeSpan duration, CancellationToken ct) => Touch(); public Task MaybeDistractionAsync(CancellationToken ct) => Touch(); public Task HasSessionCookieAsync(CancellationToken ct) => Touch(); public Task RestAsync(CancellationToken ct) => Touch(); private void Touch() { Interlocked.Increment(ref _touched); throw new InvalidOperationException("La pagina è stata toccata da una ricetta per richieste."); } private Task Touch() { Touch(); return Task.FromResult(default!); } } /// /// The recipes by requests without a browser (issue #36, S3): the runner of the test site's readers and map, with a /// site client ( by default) and a page that must never be touched. /// internal sealed class RequestBed : IAsyncDisposable { private readonly ConcurrentQueue _warnings = new(); private RequestBed(TempFolder temp, FebeDatabase db, VirtualClock clock, NoPageDriver page, ISiteRequester requester, PermissiveGovernor governor, RecordingBreaker breaker, RecipeRunner runner, TestSession session) { Temp = temp; Db = db; Clock = clock; Page = page; Requester = requester; Governor = governor; Breaker = breaker; Runner = runner; Session = session; } public TempFolder Temp { get; } public FebeDatabase Db { get; } public VirtualClock Clock { get; } public NoPageDriver Page { get; } public ISiteRequester Requester { get; } /// The scripted site client (null when the bed runs on another one). public ScriptedRequester? Scripted => Requester as ScriptedRequester; public PermissiveGovernor Governor { get; } public RecordingBreaker Breaker { get; } public RecipeRunner Runner { get; } public TestSession Session { get; } public IReadOnlyList Warnings => [.. _warnings]; /// The map of the site; the test site's by default (every recipe could also go by the pages). /// The catalog; the test site's by default. /// The site client; a on the cleaned samples by default. /// The recorder of the diagnostic report, when the test reads it. /// False: no site client at all (every recipe by the page). public static async Task CreateAsync(SiteMap? map = null, RequestCatalog? catalog = null, ISiteRequester? requester = null, CollectionDiagnostics? diagnostics = null, bool requests = true, VirtualClock? clock = null) { var temp = new TempFolder("ricette-richieste"); var db = await TestDatabase.OpenAsync(temp); clock ??= new VirtualClock(new DateTimeOffset(2026, 10, 13, 16, 0, 0, TimeSpan.Zero)); catalog ??= RequestCatalog.LoadTestSite(); requester ??= new ScriptedRequester(catalog) { SessionCookie = "sessione_prova" }; var capture = new CaptureCollector(new ShapeReader(ReaderVocabulary.LoadTestSite()), clock) { Diagnostics = diagnostics }; var page = new NoPageDriver(); var breaker = new RecordingBreaker(); var governor = new PermissiveGovernor(); RequestBed? bed = null; var runner = new RecipeRunner(new RecipeEnvironment { Driver = page, Capture = capture, Governor = governor, Budget = governor, Signals = new SignalDetector(SignalRules.LoadTestSite(), breaker, clock), Storage = db, SiteMap = map ?? SiteMap.LoadTestSite(), Clock = clock, Random = new SeededRandom(7), Breaker = breaker, Diagnostics = diagnostics, Requests = requests ? new SiteRequestEnvironment(requester, catalog, new SiteAnswerSignals(SignalRules.LoadTestSite(), "sessione_prova"), Warn: m => bed?._warnings.Enqueue(m)) : null, }); var session = new TestSession((await db.Sessions.StartAsync(clock.Now)).Id); bed = new RequestBed(temp, db, clock, page, requester, governor, breaker, runner, session); return bed; } /// The account of the samples: «numero» 5555 (marta.prova on the site, a fake name in the samples). public Task AddSampleAccountAsync(Func? change = null) => AddAccountAsync("5555", "ndkwq.shzgd", change); public Task AddAccountAsync(string? instagramId, string username, Func? change = null, bool isMine = false) { var account = Account.Create(username, StartingPoint.EverythingAndKeepUpdated, Clock.Now, TestZones.Rome, isMine: isMine) with { InstagramId = instagramId, IsFollowed = !isMine, }; return Db.Accounts.AddAsync(change is null ? account : change(account)); } public Task RunAsync(RecipeKind kind, long? accountId = null, long? entryId = null, string? highlightId = null, int? maxPages = null, bool manual = false) => Runner.RunAsync(new RecipeRequest(kind, accountId, entryId, highlightId, maxPages, manual), Session, CancellationToken.None); public async Task> EventCodesAsync() => [.. (await Db.Events.ListAsync(new Febe.Core.Storage.EventQuery())).Select(e => e.Code)]; public async ValueTask DisposeAsync() { (Requester as IDisposable)?.Dispose(); await Db.DisposeAsync(); Temp.Dispose(); } }