using System.Net; using System.Text.Json; using Febe.Tests.Support; using Febe.Tests.Web.Support; using Febe.Web; using Febe.Web.Connection; using Microsoft.Extensions.DependencyInjection; namespace Febe.Tests.Web.Part2Support; /// Helpers of the tests of the second part of the interface (Attività, Impostazioni, Importa, Collegamento). internal static class Part2 { /// A web server for one test, imitating the given shell (Docker by default, like a real container). public static Task StartAsync(FebeEdition edition = FebeEdition.Docker, Action? services = null) => WebTestServer.StartAsync(new FebeWebOptions { Access = AccessMode.None, Edition = edition }, services); /// Polls until the condition holds (background jobs: analysis, copy, rebuild). public static async Task WaitUntilAsync(Func> condition, string what, int seconds = 60) { var deadline = DateTime.UtcNow + TimeSpan.FromSeconds(seconds); while (DateTime.UtcNow < deadline) { if (await condition()) { return; } await Task.Delay(100); } Assert.Fail($"timeout: {what}"); } /// GET that returns status and body without asserting. public static async Task<(HttpStatusCode Status, string Text, JsonElement Json)> GetAsync(this WebTestServer server, string path) { using var response = await server.Client.GetAsync(path); var text = await response.Content.ReadAsStringAsync(); var json = text.Length > 0 && (response.Content.Headers.ContentType?.MediaType?.Contains("json", StringComparison.Ordinal) ?? false) ? JsonDocument.Parse(text).RootElement.Clone() : default; return (response.StatusCode, text, json); } /// A command whose body is returned as text too (to check what it does not contain). public static async Task<(HttpStatusCode Status, string Text)> SendTextAsync(this WebTestServer server, HttpMethod method, string path, object? body = null, string? origin = "") { using var request = server.Request(method, path, body, origin); using var response = await server.Client.SendAsync(request); return (response.StatusCode, await response.Content.ReadAsStringAsync()); } /// Writes a JPEG of a generated scene. public static void WritePhoto(string path, int scene, int width = 900, int height = 1100) { Directory.CreateDirectory(Path.GetDirectoryName(path)!); File.WriteAllBytes(path, TestFile.PhotoBytes(scene, width, height)); } } /// Configuration problems chosen by the test. internal sealed class FixedProblems(params ConfigurationProblem[] problems) : IConfigurationProblems { public IReadOnlyList Current { get; } = problems; } /// A connection service that records what the API asks. internal sealed class RecordingConnection : IConnectionService { private readonly Lock _gate = new(); public List Calls { get; } = []; public ConnectionStatus Status { get; set; } = new(ConnectionState.NotConnected, null, null, false, "https://www.instagram.com/accounts/login/"); /// The status after «Ho finito» (connected by default; a verification still pending is the other case). public ConnectionStatus StatusAfterFinish { get; set; } = new(ConnectionState.Connected, "tuo.profilo", "Il tuo profilo", false, null); /// The calls so far, safe to read while the interface keeps sending. public string[] Snapshot() { lock (_gate) { return [.. Calls]; } } public Task GetStatusAsync(CancellationToken ct) => Task.FromResult(Status); public Task BeginUserDrivingAsync(bool openLogin, CancellationToken ct) { Record("begin:" + openLogin); Status = Status with { UserIsDriving = true }; return Task.CompletedTask; } public Task FinishAsync(CancellationToken ct) { Record("finish"); Status = StatusAfterFinish; return Task.FromResult(new FinishResult(true, "Sei collegato come @tuo.profilo.")); } public Task CancelAsync(CancellationToken ct) { Record("cancel"); Status = Status with { UserIsDriving = false }; return Task.CompletedTask; } public Task ShowBrowserAsync(bool openLogin, CancellationToken ct) { Record("show:" + openLogin); Status = Status with { UserIsDriving = true }; return Task.CompletedTask; } public Task ReleaseAsync(CancellationToken ct) { Record("release"); Status = Status with { UserIsDriving = false }; return Task.CompletedTask; } public Task DisconnectAsync(CancellationToken ct) { Record("disconnect"); Status = new ConnectionStatus(ConnectionState.NotConnected, null, null, false, null); return Task.CompletedTask; } private void Record(string call) { lock (_gate) { Calls.Add(call); } } }