Files
Febe/tests/Febe.Tests/Collection/Support/ScriptedRequester.cs
T
Alby96andClaude Opus 5.5 63acae4e73
verifica / controlli (push) Successful in 22s
verifica / prove (push) Successful in 21m4s
verifica / installatore (push) Successful in 5m21s
verifica / immagine (push) Successful in 19m0s
Ricette fatte di richieste, un permesso per richiesta (#36, S3)
Otto ricette (controllo del profilo, storico, taggati, reel, storie di un
account, raccolta in evidenza, post singolo, elenco dei seguiti) hanno una
strada per richieste dirette accanto a quella con il browser. Ci vanno solo
se ogni ruolo che servono è un'operazione configurata di richieste.<sito>.json:
con Instagram nessuna lo è e Febe non costruisce il client del sito, quindi il
comportamento con Instagram non cambia fino a S4.

Ogni richiesta prende il suo permesso dal Governatore nella categoria di ciò
che chiede (la prima pagina dei post conta come profilo); i segnali delle
risposte vanno all'interruttore e fermano la ricetta prima di ogni altra
richiesta. Se manca un valore o la pagina successiva, la ricetta torna alle
pagine per quella volta e il motore controlla tutti e due i tetti. I tetti non
cambiano: il dimezzamento delle storie viste è tolto perché toccava anche il
browser e i «Su misura» salvati, e resta una domanda per Alberto prima di S4.

Il simulatore ha --raccolta pagine|richieste con il costo delle operazioni
separate di Instagram: sei simulazioni di 30 giorni senza superamenti.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 12:10:54 +01:00

211 lines
9.0 KiB
C#

using System.Collections.Concurrent;
using System.Text;
using System.Text.Json.Nodes;
using Febe.Core.Abstractions;
using Febe.Core.Collection.Requests;
namespace Febe.Tests.Collection.Support;
/// <summary>What <see cref="ScriptedRequester"/> answers to the next request, instead of the sample.</summary>
public enum ScriptedReply
{
/// <summary>The cleaned sample of the operation (<see cref="SiteOperation.Sample"/>).</summary>
Sample,
/// <summary>A 429 without body (§7.6, Rallentamento).</summary>
TooManyRequests,
/// <summary>A 302 to the login page of the site, never followed.</summary>
RedirectToLogin,
/// <summary>A 302 to the challenge page of the site, never followed.</summary>
RedirectToChallenge,
/// <summary>A 200 with an HTML page where the catalog expects JSON.</summary>
HtmlInsteadOfJson,
/// <summary>The sample without its expected root field (the last one of <see cref="SiteOperation.RootFields"/>).</summary>
MissingRootField,
/// <summary>The sample, with the session cookie deleted by the site.</summary>
SessionCookieDeleted,
/// <summary>A 404 with the JSON of the test site («non-trovato»): the resource is not there (H19).</summary>
NotFoundJson,
/// <summary>A 500 with JSON: an error of the site.</summary>
ServerError500,
/// <summary>A 503 with JSON: the site is not available.</summary>
ServerError503,
/// <summary>No answer in time (<see cref="SiteUnreachableReason.Timeout"/>).</summary>
Timeout,
/// <summary>No connection (<see cref="SiteUnreachableReason.Unreachable"/>).</summary>
Unreachable,
/// <summary>An answer larger than allowed (<see cref="SiteUnreachableReason.TooLarge"/>).</summary>
TooLarge,
/// <summary>A body that cannot be decoded (<see cref="SiteUnreachableReason.Broken"/>).</summary>
Broken,
/// <summary>
/// A 200 with JSON that no reader recognizes (the body of the sample «forma-sconosciuta»), said to have the expected
/// fields: what a site that changed its format inside the same fields would send.
/// </summary>
UnknownShape,
}
/// <summary>
/// The site client of the tests (issue #36): answers the requests of the catalog from the cleaned samples of
/// <c>tests/Febe.Tests/campioni/&lt;sito&gt;</c>, by operation name, and can be scripted to answer the stop cases of
/// §7.6. It honours the <see cref="ISiteRequester"/> contract: <see cref="SiteRequestGuard"/> before anything (a permit
/// used once is spent, <see cref="SpentPermits"/>), and one request at a time. Nothing leaves the process.
/// </summary>
public sealed class ScriptedRequester : ISiteRequester
{
private readonly RequestCatalog _catalog;
private readonly string _samples;
private readonly ConcurrentQueue<ScriptedReply> _script = new();
private readonly ConcurrentQueue<SiteRequest> _sent = new();
private readonly SpentPermits _spent = new();
private int _busy;
private int _refused;
/// <param name="catalog">The catalog the requests are checked against.</param>
/// <param name="sampleFolder">Folder of the cleaned samples; by default <c>campioni/&lt;site of the catalog&gt;</c> next to the tests.</param>
public ScriptedRequester(RequestCatalog catalog, string? sampleFolder = null)
{
_catalog = catalog ?? throw new ArgumentNullException(nameof(catalog));
_samples = sampleFolder ?? Path.Combine(AppContext.BaseDirectory, "campioni", catalog.Site);
}
/// <summary>Path of the login page a redirect points to.</summary>
public string LoginLocation { get; init; } = "https://www.instagram.com/accounts/login/?next=%2F";
/// <summary>Path of the challenge page a redirect points to.</summary>
public string ChallengeLocation { get; init; } = "/challenge/AbCdEf123/?next=%2F";
/// <summary>Name of the session cookie (<see cref="ScriptedReply.SessionCookieDeleted"/>).</summary>
public string SessionCookie { get; init; } = "sessionid";
/// <summary>Requests that passed the guard, in order.</summary>
public IReadOnlyList<SiteRequest> Sent => [.. _sent];
/// <summary>Requests refused before anything was answered.</summary>
public int Refused => Volatile.Read(ref _refused);
/// <summary>The replies of the next requests, in order; after them, the samples again.</summary>
public ScriptedRequester Then(params ScriptedReply[] replies)
{
foreach (var reply in replies)
{
_script.Enqueue(reply);
}
return this;
}
public async Task<SiteAnswer> SendAsync(SiteRequest request, Permit permit, CancellationToken ct)
{
SiteOperation operation;
try
{
operation = SiteRequestGuard.Check(_catalog, request, permit, _spent);
}
catch (SiteRequestRefusedException)
{
Interlocked.Increment(ref _refused);
throw;
}
if (Interlocked.Exchange(ref _busy, 1) == 1)
{
Interlocked.Increment(ref _refused);
throw new SiteRequestRefusedException($"Richiesta «{request.Operation}» non inviata: un'altra richiesta è ancora in corso (una per volta).");
}
try
{
ct.ThrowIfCancellationRequested();
await Task.Yield();
_sent.Enqueue(request);
var reply = _script.TryDequeue(out var next) ? next : ScriptedReply.Sample;
return Answer(operation, reply);
}
finally
{
Volatile.Write(ref _busy, 0);
}
}
/// <summary>The descriptor and the body of the sample of <paramref name="operation"/>.</summary>
public (string MimeType, JsonNode Body) Sample(SiteOperation operation)
{
ArgumentNullException.ThrowIfNull(operation);
if (operation.Sample is not { } name)
{
throw new InvalidOperationException($"«{operation.Name}» non ha un campione.");
}
var file = Directory.EnumerateFiles(_samples, $"{name}.json").Single();
var sample = JsonNode.Parse(File.ReadAllText(file))!;
return (sample["descrittore"]!["tipo"]!.GetValue<string>(), sample["corpo"]!.DeepClone());
}
private SiteAnswer Answer(SiteOperation operation, ScriptedReply reply)
{
switch (reply)
{
case ScriptedReply.TooManyRequests:
return new SiteAnswer(operation, 429, "text/plain", ReadOnlyMemory<byte>.Empty);
case ScriptedReply.RedirectToLogin:
return new SiteAnswer(operation, 302, "text/html", ReadOnlyMemory<byte>.Empty, redirectTo: LoginLocation);
case ScriptedReply.RedirectToChallenge:
return new SiteAnswer(operation, 302, "text/html", ReadOnlyMemory<byte>.Empty, redirectTo: ChallengeLocation);
case ScriptedReply.HtmlInsteadOfJson:
return new SiteAnswer(operation, 200, "text/html; charset=utf-8", Encoding.UTF8.GetBytes("<!DOCTYPE html><html><head><title>Pagina</title></head><body></body></html>"));
case ScriptedReply.NotFoundJson:
return new SiteAnswer(operation, 404, "application/json", Encoding.UTF8.GetBytes("{\"errore\":\"non-trovato\"}"), rootFieldsPresent: false);
case ScriptedReply.ServerError500 or ScriptedReply.ServerError503:
return new SiteAnswer(operation, reply == ScriptedReply.ServerError500 ? 500 : 503, "application/json", Encoding.UTF8.GetBytes("{\"errore\":\"interno\"}"), rootFieldsPresent: false);
case ScriptedReply.Timeout or ScriptedReply.Unreachable or ScriptedReply.TooLarge or ScriptedReply.Broken:
var reason = reply switch
{
ScriptedReply.Timeout => SiteUnreachableReason.Timeout,
ScriptedReply.TooLarge => SiteUnreachableReason.TooLarge,
ScriptedReply.Broken => SiteUnreachableReason.Broken,
_ => SiteUnreachableReason.Unreachable,
};
throw new SiteUnreachableException($"«{operation.Name}» ({operation.PathShape}): nessuna risposta (prova).", reason);
case ScriptedReply.UnknownShape:
var unknown = Directory.EnumerateFiles(_samples, "*-forma-sconosciuta.json").Single();
var corpo = JsonNode.Parse(File.ReadAllText(unknown))!["corpo"]!;
return new SiteAnswer(operation, 200, "application/json", Encoding.UTF8.GetBytes(corpo.ToJsonString()), rootFieldsPresent: true);
}
var (mime, body) = Sample(operation);
if (reply == ScriptedReply.MissingRootField)
{
var parts = operation.RootFields[^1].Split('.');
var parent = body;
foreach (var part in parts[..^1])
{
parent = parent[part]!;
}
parent.AsObject().Remove(parts[^1]);
}
var bytes = Encoding.UTF8.GetBytes(body.ToJsonString());
return new SiteAnswer(
operation,
200,
mime,
bytes,
cookiesDeleted: reply == ScriptedReply.SessionCookieDeleted ? [SessionCookie] : null);
}
}