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; /// What answers to the next request, instead of the sample. public enum ScriptedReply { /// The cleaned sample of the operation (). Sample, /// A 429 without body (§7.6, Rallentamento). TooManyRequests, /// A 302 to the login page of the site, never followed. RedirectToLogin, /// A 302 to the challenge page of the site, never followed. RedirectToChallenge, /// A 200 with an HTML page where the catalog expects JSON. HtmlInsteadOfJson, /// The sample without its expected root field (the last one of ). MissingRootField, /// The sample, with the session cookie deleted by the site. SessionCookieDeleted, /// A 404 with the JSON of the test site («non-trovato»): the resource is not there (H19). NotFoundJson, /// A 500 with JSON: an error of the site. ServerError500, /// A 503 with JSON: the site is not available. ServerError503, /// No answer in time (). Timeout, /// No connection (). Unreachable, /// An answer larger than allowed (). TooLarge, /// A body that cannot be decoded (). Broken, /// /// 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. /// UnknownShape, } /// /// The site client of the tests (issue #36): answers the requests of the catalog from the cleaned samples of /// tests/Febe.Tests/campioni/<sito>, by operation name, and can be scripted to answer the stop cases of /// §7.6. It honours the contract: before anything (a permit /// used once is spent, ), and one request at a time. Nothing leaves the process. /// public sealed class ScriptedRequester : ISiteRequester { private readonly RequestCatalog _catalog; private readonly string _samples; private readonly ConcurrentQueue _script = new(); private readonly ConcurrentQueue _sent = new(); private readonly SpentPermits _spent = new(); private int _busy; private int _refused; /// The catalog the requests are checked against. /// Folder of the cleaned samples; by default campioni/<site of the catalog> next to the tests. public ScriptedRequester(RequestCatalog catalog, string? sampleFolder = null) { _catalog = catalog ?? throw new ArgumentNullException(nameof(catalog)); _samples = sampleFolder ?? Path.Combine(AppContext.BaseDirectory, "campioni", catalog.Site); } /// Path of the login page a redirect points to. public string LoginLocation { get; init; } = "https://www.instagram.com/accounts/login/?next=%2F"; /// Path of the challenge page a redirect points to. public string ChallengeLocation { get; init; } = "/challenge/AbCdEf123/?next=%2F"; /// Name of the session cookie (). public string SessionCookie { get; init; } = "sessionid"; /// Requests that passed the guard, in order. public IReadOnlyList Sent => [.. _sent]; /// Requests refused before anything was answered. public int Refused => Volatile.Read(ref _refused); /// The replies of the next requests, in order; after them, the samples again. public ScriptedRequester Then(params ScriptedReply[] replies) { foreach (var reply in replies) { _script.Enqueue(reply); } return this; } public async Task 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); } } /// The descriptor and the body of the sample of . 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(), sample["corpo"]!.DeepClone()); } private SiteAnswer Answer(SiteOperation operation, ScriptedReply reply) { switch (reply) { case ScriptedReply.TooManyRequests: return new SiteAnswer(operation, 429, "text/plain", ReadOnlyMemory.Empty); case ScriptedReply.RedirectToLogin: return new SiteAnswer(operation, 302, "text/html", ReadOnlyMemory.Empty, redirectTo: LoginLocation); case ScriptedReply.RedirectToChallenge: return new SiteAnswer(operation, 302, "text/html", ReadOnlyMemory.Empty, redirectTo: ChallengeLocation); case ScriptedReply.HtmlInsteadOfJson: return new SiteAnswer(operation, 200, "text/html; charset=utf-8", Encoding.UTF8.GetBytes("Pagina")); 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); } }