«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>
148 lines
5.0 KiB
C#
148 lines
5.0 KiB
C#
using System.Collections.Concurrent;
|
|
|
|
namespace Febe.SitoDiProva;
|
|
|
|
/// <summary>The stop cases the site can play (spec §14.2), switched by the tests through <c>/_controllo</c>.</summary>
|
|
public enum StopMode
|
|
{
|
|
/// <summary>Everything works.</summary>
|
|
Normal,
|
|
|
|
/// <summary>Data requests get HTTP 429 (Rallentamento).</summary>
|
|
TooManyRequests,
|
|
|
|
/// <summary>Pages are redirected to <c>/verifica</c>; data requests get 403 and the page moves there (Verifica).</summary>
|
|
Challenge,
|
|
|
|
/// <summary>The session cookie is removed and pages move to <c>/sessione-scaduta</c> (Disconnessione).</summary>
|
|
SessionEnded,
|
|
|
|
/// <summary>Data requests return empty pages.</summary>
|
|
EmptyPages,
|
|
|
|
/// <summary>Data requests return a shape no reader knows (the site «changed»).</summary>
|
|
UnknownFormat,
|
|
}
|
|
|
|
/// <summary>Switches and counters of the test site. Thread-safe.</summary>
|
|
public sealed class SiteControl
|
|
{
|
|
private readonly Lock _gate = new();
|
|
private readonly ConcurrentQueue<string> _dataRequests = new();
|
|
private StopMode _mode = StopMode.Normal;
|
|
private int _armAfter;
|
|
|
|
/// <summary>How long a photo story stays on screen before the viewer moves on by itself.</summary>
|
|
public TimeSpan StoryDuration { get; set; } = TimeSpan.FromSeconds(5);
|
|
|
|
/// <summary>Validity of the CDN addresses handed out from now on.</summary>
|
|
public TimeSpan AddressLifetime { get; set; } = TimeSpan.FromHours(6);
|
|
|
|
/// <summary>The CDN answers 429 to every file (Rallentamento from the CDN, spec §7.6).</summary>
|
|
public bool CdnTooManyRequests { get; set; }
|
|
|
|
/// <summary>Number of CDN requests answered with 429.</summary>
|
|
public int CdnThrottled => _cdnThrottled;
|
|
|
|
/// <summary>Paths with query of every data request, in arrival order (for the tests).</summary>
|
|
public IReadOnlyList<string> DataRequests => [.. _dataRequests];
|
|
|
|
/// <summary>Number of files served by the CDN.</summary>
|
|
public int CdnFilesServed => _cdnFiles;
|
|
|
|
/// <summary>Number of CDN requests refused because the address had expired.</summary>
|
|
public int CdnExpired => _cdnExpired;
|
|
|
|
private int _cdnFiles;
|
|
private int _cdnExpired;
|
|
private int _cdnThrottled;
|
|
private int _recentSearchWrites;
|
|
|
|
/// <summary>Extra wait before every answer of the search box (slow answers).</summary>
|
|
public TimeSpan SearchDelay { get; set; }
|
|
|
|
/// <summary>
|
|
/// The search box answers shorter texts later than longer ones, so an earlier prefix arrives last (answers out of
|
|
/// order, as on a slow network).
|
|
/// </summary>
|
|
public bool SearchDisorder { get; set; }
|
|
|
|
/// <summary>The page sends no request for a text it already searched, as a page with its own memory does.</summary>
|
|
public bool SearchMemory { get; set; }
|
|
|
|
/// <summary>
|
|
/// Number of «ricerche recenti» the page wrote: clicking a result or pressing Enter in the search box writes one,
|
|
/// as Instagram registers a search. Febe must never cause one.
|
|
/// </summary>
|
|
public int RecentSearchWrites => _recentSearchWrites;
|
|
|
|
public StopMode Mode
|
|
{
|
|
get
|
|
{
|
|
lock (_gate)
|
|
{
|
|
return _armAfter > 0 ? StopMode.Normal : _mode;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>Sets a stop case; with <paramref name="afterDataRequests"/> it starts after that many more data requests.</summary>
|
|
public void Set(StopMode mode, int afterDataRequests = 0)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
_mode = mode;
|
|
_armAfter = Math.Max(0, afterDataRequests);
|
|
}
|
|
}
|
|
|
|
/// <summary>Back to normal; counters cleared; default story duration and address lifetime.</summary>
|
|
public void Reset()
|
|
{
|
|
lock (_gate)
|
|
{
|
|
_mode = StopMode.Normal;
|
|
_armAfter = 0;
|
|
}
|
|
|
|
_dataRequests.Clear();
|
|
StoryDuration = TimeSpan.FromSeconds(5);
|
|
AddressLifetime = TimeSpan.FromHours(6);
|
|
CdnTooManyRequests = false;
|
|
SearchDelay = TimeSpan.Zero;
|
|
SearchDisorder = false;
|
|
SearchMemory = false;
|
|
Interlocked.Exchange(ref _recentSearchWrites, 0);
|
|
Interlocked.Exchange(ref _cdnFiles, 0);
|
|
Interlocked.Exchange(ref _cdnExpired, 0);
|
|
Interlocked.Exchange(ref _cdnThrottled, 0);
|
|
}
|
|
|
|
public void ClearRequests() => _dataRequests.Clear();
|
|
|
|
/// <summary>Records a data request and returns the mode that applies to it.</summary>
|
|
internal StopMode OnDataRequest(string pathAndQuery)
|
|
{
|
|
_dataRequests.Enqueue(pathAndQuery);
|
|
lock (_gate)
|
|
{
|
|
if (_armAfter > 0)
|
|
{
|
|
_armAfter--;
|
|
return StopMode.Normal;
|
|
}
|
|
|
|
return _mode;
|
|
}
|
|
}
|
|
|
|
internal void OnCdnServed() => Interlocked.Increment(ref _cdnFiles);
|
|
|
|
internal void OnRecentSearchWritten() => Interlocked.Increment(ref _recentSearchWrites);
|
|
|
|
internal void OnCdnExpired() => Interlocked.Increment(ref _cdnExpired);
|
|
|
|
internal void OnCdnThrottled() => Interlocked.Increment(ref _cdnThrottled);
|
|
}
|