Un errore imprevisto durante la copia senza «Ho finito» (database, disco) non trattiene più per sempre il permesso di guida: la copia scrive solo il tipo d'errore e la guida finisce lo stesso, così il motore non resta in «Stai usando il browser». Un «Ho finito» tardivo dopo una copia fatta lasciando il browser dice che la sessione era già stata copiata invece di chiedere un nuovo accesso. Il registro distingue la finestra chiusa da quella ridotta a icona troppo a lungo. /api/engine e gli eventi prendono «Serve «Ho finito»» dall'istantanea del motore (EngineSnapshot.SessionCopyMissing) senza chiedere al browser a ogni evento. La prova di WebView2 aspetta la riga del registro, non solo la sessione in memoria. Documenti: nove operazioni GraphQL con __spin_*, H23 descritta in prove-dal-vivo (parte E), guida aggiornata. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
149 lines
5.5 KiB
C#
149 lines
5.5 KiB
C#
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;
|
|
|
|
/// <summary>Helpers of the tests of the second part of the interface (Attività, Impostazioni, Importa, Collegamento).</summary>
|
|
internal static class Part2
|
|
{
|
|
/// <summary>A web server for one test, imitating the given shell (Docker by default, like a real container).</summary>
|
|
public static Task<WebTestServer> StartAsync(FebeEdition edition = FebeEdition.Docker, Action<IServiceCollection>? services = null) =>
|
|
WebTestServer.StartAsync(new FebeWebOptions { Access = AccessMode.None, Edition = edition }, services);
|
|
|
|
/// <summary>Polls until the condition holds (background jobs: analysis, copy, rebuild).</summary>
|
|
public static async Task WaitUntilAsync(Func<Task<bool>> 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}");
|
|
}
|
|
|
|
/// <summary>GET that returns status and body without asserting.</summary>
|
|
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);
|
|
}
|
|
|
|
/// <summary>A command whose body is returned as text too (to check what it does not contain).</summary>
|
|
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());
|
|
}
|
|
|
|
/// <summary>Writes a JPEG of a generated scene.</summary>
|
|
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));
|
|
}
|
|
}
|
|
|
|
/// <summary>Configuration problems chosen by the test.</summary>
|
|
internal sealed class FixedProblems(params ConfigurationProblem[] problems) : IConfigurationProblems
|
|
{
|
|
public IReadOnlyList<ConfigurationProblem> Current { get; } = problems;
|
|
}
|
|
|
|
/// <summary>A connection service that records what the API asks.</summary>
|
|
internal sealed class RecordingConnection : IConnectionService
|
|
{
|
|
private readonly Lock _gate = new();
|
|
private int _statusReads;
|
|
|
|
public List<string> Calls { get; } = [];
|
|
|
|
public ConnectionStatus Status { get; set; } = new(ConnectionState.NotConnected, null, null, false, "https://www.instagram.com/accounts/login/");
|
|
|
|
/// <summary>The status after «Ho finito» (connected by default; a verification still pending is the other case).</summary>
|
|
public ConnectionStatus StatusAfterFinish { get; set; } = new(ConnectionState.Connected, "tuo.profilo", "Il tuo profilo", false, null);
|
|
|
|
/// <summary>The calls so far, safe to read while the interface keeps sending.</summary>
|
|
public string[] Snapshot()
|
|
{
|
|
lock (_gate)
|
|
{
|
|
return [.. Calls];
|
|
}
|
|
}
|
|
|
|
/// <summary>How many times the status was asked (each one costs the real connection a look at the browser).</summary>
|
|
public int StatusReads => Volatile.Read(ref _statusReads);
|
|
|
|
public Task<ConnectionStatus> GetStatusAsync(CancellationToken ct)
|
|
{
|
|
Interlocked.Increment(ref _statusReads);
|
|
return Task.FromResult(Status);
|
|
}
|
|
|
|
public Task BeginUserDrivingAsync(bool openLogin, CancellationToken ct)
|
|
{
|
|
Record("begin:" + openLogin);
|
|
Status = Status with { UserIsDriving = true };
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task<FinishResult> 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);
|
|
}
|
|
}
|
|
}
|