«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>
126 lines
4.3 KiB
C#
126 lines
4.3 KiB
C#
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;
|
|
|
|
/// <summary>Runs at every request, before the answer (a test changes the page while the permit is asked).</summary>
|
|
public Action<ActionKind>? OnRequest { get; set; }
|
|
|
|
public Task<Permit> RequestAsync(ActionKind kind, long? accountId, CancellationToken ct)
|
|
{
|
|
_requests.Enqueue((kind, accountId));
|
|
OnRequest?.Invoke(kind);
|
|
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;
|
|
}
|